lakshmisravya123
Major upgrade: comprehensive debate analysis
efceb0f
const GROQ_API_KEY = process.env.GROQ_API_KEY;
const OLLAMA_URL = process.env.OLLAMA_URL || 'http://localhost:11434';
const GROQ_MODEL = process.env.GROQ_MODEL || 'llama-3.3-70b-versatile';
const OLLAMA_MODEL = process.env.OLLAMA_MODEL || 'llama3.2:3b';
async function callAI(prompt) {
if (GROQ_API_KEY) {
const res = await fetch('https://api.groq.com/openai/v1/chat/completions', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${GROQ_API_KEY}` },
body: JSON.stringify({ model: GROQ_MODEL, messages: [{ role: 'user', content: prompt }], temperature: 0.7, max_tokens: 4096 }),
});
if (res.ok) { const data = await res.json(); return data.choices[0].message.content; }
console.warn('Groq failed, falling back to Ollama...');
}
const res = await fetch(`${OLLAMA_URL}/api/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ model: OLLAMA_MODEL, prompt, stream: false }),
});
if (!res.ok) throw new Error('Both Groq and Ollama failed.');
return (await res.json()).response;
}
function parseJSON(text) {
try { return JSON.parse(text.trim()); }
catch { const m = text.match(/\{[\s\S]*\}/); if (m) return JSON.parse(m[0]); throw new Error('Failed to parse AI response'); }
}
async function settleArgument(topic, side1Name, side1Argument, side2Name, side2Argument) {
const text = await callAI(`You are "The Settler" - a world-class debate judge with expertise in logic, rhetoric, and critical thinking. You combine the analytical rigor of a philosophy professor with the wit of a late-night show host.
TOPIC: ${topic}
${side1Name}'s ARGUMENT:
${side1Argument}
${side2Name}'s ARGUMENT:
${side2Argument}
Perform a DEEP analysis of both arguments. Check for:
- Logical fallacies (ad hominem, straw man, false dichotomy, appeal to authority, slippery slope, etc.)
- Evidence quality (anecdotal, statistical, expert opinion, common sense)
- Emotional vs rational reasoning balance
- Bias and assumptions
- Argument structure and coherence
Return ONLY valid JSON:
{
"topic": "<topic summary>",
"side1": {
"name": "${side1Name}",
"score": <1-100>,
"persuasivenessScore": <1-100>,
"factualScore": <1-100>,
"emotionalVsRational": { "emotional": <0-100>, "rational": <0-100> },
"strengths": ["<specific strength 1>", "<specific strength 2>", "<strength 3>"],
"weaknesses": ["<specific weakness 1>", "<weakness 2>"],
"fallacies": [{ "name": "<fallacy name>", "quote": "<the part that commits it>", "explanation": "<why it's a fallacy>" }],
"evidenceQuality": "<anecdotal/statistical/expert/mixed - with explanation>",
"devilsAdvocate": "<3 sentences arguing AGAINST this side>",
"steelMan": "<The strongest possible version of this argument rewritten in 2-3 sentences>",
"whatWouldChangeTheirMind": "<What evidence or argument would make this person reconsider?>"
},
"side2": {
"name": "${side2Name}",
"score": <1-100>,
"persuasivenessScore": <1-100>,
"factualScore": <1-100>,
"emotionalVsRational": { "emotional": <0-100>, "rational": <0-100> },
"strengths": ["<specific strength 1>", "<specific strength 2>", "<strength 3>"],
"weaknesses": ["<specific weakness 1>", "<weakness 2>"],
"fallacies": [{ "name": "<fallacy name>", "quote": "<the part that commits it>", "explanation": "<why it's a fallacy>" }],
"evidenceQuality": "<anecdotal/statistical/expert/mixed - with explanation>",
"devilsAdvocate": "<3 sentences arguing AGAINST this side>",
"steelMan": "<The strongest possible version of this argument rewritten in 2-3 sentences>",
"whatWouldChangeTheirMind": "<What evidence or argument would make this person reconsider?>"
},
"winner": "<name of winner or 'Draw'>",
"verdict": "<4-5 sentence witty final verdict with specific reasoning>",
"commonGround": "<2-3 sentences on where both sides actually agree>",
"plotTwist": "<A surprising perspective neither side considered>",
"biasCheck": "<Any shared biases or blind spots both arguers have>",
"compromise": "<A detailed middle-ground solution that could satisfy both sides>",
"debateTips": {
"side1Tip": "<Specific advice for ${side1Name} on how to argue better>",
"side2Tip": "<Specific advice for ${side2Name} on how to argue better>"
}
}
Return ONLY JSON, no markdown.`);
return parseJSON(text);
}
module.exports = { settleArgument };