Switch gmail-agent from Ollama to Gemini

This commit is contained in:
Deep Koluguri 2026-06-19 13:53:55 -04:00
parent 5efc41af3d
commit db1dfdaa95
1 changed files with 27 additions and 27 deletions

View File

@ -14,10 +14,9 @@ function getCredentials() {
return JSON.parse(fs.readFileSync(CREDENTIALS_PATH)); 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_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 WHATSAPP_TO = process.env.WHATSAPP_TO;
const OLLAMA_MODEL = process.env.OLLAMA_MODEL || 'qwen2.5:0.5b';
let baseOAuth2Client; let baseOAuth2Client;
let accounts = []; let accounts = [];
@ -120,46 +119,47 @@ async function markAsAgentRead(email, labelId) {
}); });
} }
async function analyzeWithOllama(email) { async function analyzeWithGemini(email) {
const prompt = `You are an AI assistant that determines if an email is important enough to notify the user immediately on WhatsApp. const prompt = `You are a strict AI assistant. Your ONLY job is to filter emails to send to WhatsApp.
CRITICAL INSTRUCTION: You MUST strictly IGNORE all promotional emails, newsletters, marketing, discounts, product updates, or any emails of low significance. CRITICAL RULES:
For these, you MUST output IMPORTANT: NO. 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.
Important things: Bills due, flight/travel updates, urgent requests from people, security alerts, direct personal messages.
Email Subject: ${email.subject} Email Subject: ${email.subject}
Email From: ${email.from} Email From: ${email.from}
Email Body Preview: Email Body Preview:
${email.body} ${email.body}
First, determine if this is important (YES or NO). Determine if this is an important email. Return a strict JSON response. Do not include markdown formatting.
If YES, provide a 1-sentence summary of what the user needs to know. Format:
Output format exactly like this: {
IMPORTANT: YES/NO "isImportant": true or false,
SUMMARY: <your summary if yes, otherwise leave blank> "summary": "1-sentence summary of what the user needs to know if true, otherwise leave empty"
`; }`;
try { 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', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
model: OLLAMA_MODEL, contents: [{ parts: [{ text: prompt }] }],
prompt: prompt, generationConfig: {
stream: false temperature: 0.1,
responseMimeType: "application/json"
}
}) })
}); });
const data = await res.json(); const data = await res.json();
const responseText = data.response || ''; if (data.candidates && data.candidates.length > 0) {
const isImportant = responseText.includes('IMPORTANT: YES'); const text = data.candidates[0].content.parts[0].text;
const summaryMatch = responseText.match(/SUMMARY:\s*(.*)/); const parsed = JSON.parse(text);
const summary = summaryMatch ? summaryMatch[1].trim() : ''; return { isImportant: parsed.isImportant, summary: parsed.summary || '' };
}
return { isImportant, summary }; return { isImportant: false, summary: '' };
} catch (err) { } catch (err) {
console.error('Ollama Error:', err.message); console.error('Gemini Error:', err.message);
return { isImportant: false, summary: '' }; return { isImportant: false, summary: '' };
} }
} }
@ -203,7 +203,7 @@ async function processEmails() {
console.log(`Found ${emails.length} unread emails for ${account.name}.`); console.log(`Found ${emails.length} unread emails for ${account.name}.`);
for (const email of emails) { for (const email of emails) {
const analysis = await analyzeWithOllama(email); const analysis = await analyzeWithGemini(email);
if (analysis.isImportant) { if (analysis.isImportant) {
await sendWhatsAppNotification(email, analysis.summary); await sendWhatsAppNotification(email, analysis.summary);
} else { } else {