Switch gmail-agent from Ollama to Gemini
This commit is contained in:
parent
5efc41af3d
commit
db1dfdaa95
|
|
@ -14,10 +14,9 @@ function getCredentials() {
|
|||
return JSON.parse(fs.readFileSync(CREDENTIALS_PATH));
|
||||
}
|
||||
|
||||
const OLLAMA_URL = process.env.OLLAMA_URL || 'http://ollama.ai-agents.svc.cluster.local:11434/api/generate';
|
||||
const GEMINI_API_KEY = process.env.GEMINI_API_KEY || 'AIzaSyDKYcgVPN2oJirwzf_td3sBYMnXHWfVphU';
|
||||
const WHATSAPP_GATEWAY_URL = process.env.WHATSAPP_GATEWAY_URL || 'http://whatsapp-gateway.ai-agents.svc.cluster.local:5001/api/send-message';
|
||||
const WHATSAPP_TO = process.env.WHATSAPP_TO;
|
||||
const OLLAMA_MODEL = process.env.OLLAMA_MODEL || 'qwen2.5:0.5b';
|
||||
|
||||
let baseOAuth2Client;
|
||||
let accounts = [];
|
||||
|
|
@ -120,46 +119,47 @@ async function markAsAgentRead(email, labelId) {
|
|||
});
|
||||
}
|
||||
|
||||
async function analyzeWithOllama(email) {
|
||||
const prompt = `You are an AI assistant that determines if an email is important enough to notify the user immediately on WhatsApp.
|
||||
|
||||
CRITICAL INSTRUCTION: You MUST strictly IGNORE all promotional emails, newsletters, marketing, discounts, product updates, or any emails of low significance.
|
||||
For these, you MUST output IMPORTANT: NO.
|
||||
|
||||
Important things: Bills due, flight/travel updates, urgent requests from people, security alerts, direct personal messages.
|
||||
async function analyzeWithGemini(email) {
|
||||
const prompt = `You are a strict AI assistant. Your ONLY job is to filter emails to send to WhatsApp.
|
||||
|
||||
CRITICAL RULES:
|
||||
1. STRICTLY IGNORE ALL promotional emails, newsletters, daily updates, market recaps, deals, marketing, product updates, surveys, or general spam. If it looks like a mass email or newsletter, it is NOT important.
|
||||
2. ONLY flag truly personal messages, urgent requests from real people, travel updates, direct security alerts, or specific receipts.
|
||||
|
||||
Email Subject: ${email.subject}
|
||||
Email From: ${email.from}
|
||||
Email Body Preview:
|
||||
${email.body}
|
||||
|
||||
First, determine if this is important (YES or NO).
|
||||
If YES, provide a 1-sentence summary of what the user needs to know.
|
||||
Output format exactly like this:
|
||||
IMPORTANT: YES/NO
|
||||
SUMMARY: <your summary if yes, otherwise leave blank>
|
||||
`;
|
||||
Determine if this is an important email. Return a strict JSON response. Do not include markdown formatting.
|
||||
Format:
|
||||
{
|
||||
"isImportant": true or false,
|
||||
"summary": "1-sentence summary of what the user needs to know if true, otherwise leave empty"
|
||||
}`;
|
||||
|
||||
try {
|
||||
const res = await fetch(OLLAMA_URL, {
|
||||
const res = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${GEMINI_API_KEY}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
model: OLLAMA_MODEL,
|
||||
prompt: prompt,
|
||||
stream: false
|
||||
contents: [{ parts: [{ text: prompt }] }],
|
||||
generationConfig: {
|
||||
temperature: 0.1,
|
||||
responseMimeType: "application/json"
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
const responseText = data.response || '';
|
||||
const isImportant = responseText.includes('IMPORTANT: YES');
|
||||
const summaryMatch = responseText.match(/SUMMARY:\s*(.*)/);
|
||||
const summary = summaryMatch ? summaryMatch[1].trim() : '';
|
||||
|
||||
return { isImportant, summary };
|
||||
if (data.candidates && data.candidates.length > 0) {
|
||||
const text = data.candidates[0].content.parts[0].text;
|
||||
const parsed = JSON.parse(text);
|
||||
return { isImportant: parsed.isImportant, summary: parsed.summary || '' };
|
||||
}
|
||||
return { isImportant: false, summary: '' };
|
||||
} catch (err) {
|
||||
console.error('Ollama Error:', err.message);
|
||||
console.error('Gemini Error:', err.message);
|
||||
return { isImportant: false, summary: '' };
|
||||
}
|
||||
}
|
||||
|
|
@ -203,7 +203,7 @@ async function processEmails() {
|
|||
console.log(`Found ${emails.length} unread emails for ${account.name}.`);
|
||||
|
||||
for (const email of emails) {
|
||||
const analysis = await analyzeWithOllama(email);
|
||||
const analysis = await analyzeWithGemini(email);
|
||||
if (analysis.isImportant) {
|
||||
await sendWhatsAppNotification(email, analysis.summary);
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue