Spaces:
Sleeping
Sleeping
lakshmisravya123 commited on
Commit ·
a3da232
1
Parent(s): 273bb97
Upgrade: security analysis, performance review, clean code scoring, test suggestions
Browse files- backend/services/ai.js.bak +59 -0
backend/services/ai.js.bak
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const GROQ_API_KEY = process.env.GROQ_API_KEY;
|
| 2 |
+
const OLLAMA_URL = process.env.OLLAMA_URL || 'http://localhost:11434';
|
| 3 |
+
const GROQ_MODEL = process.env.GROQ_MODEL || 'llama-3.3-70b-versatile';
|
| 4 |
+
const OLLAMA_MODEL = process.env.OLLAMA_MODEL || 'llama3.2:3b';
|
| 5 |
+
|
| 6 |
+
async function callAI(prompt) {
|
| 7 |
+
if (GROQ_API_KEY) {
|
| 8 |
+
const res = await fetch('https://api.groq.com/openai/v1/chat/completions', {
|
| 9 |
+
method: 'POST',
|
| 10 |
+
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${GROQ_API_KEY}` },
|
| 11 |
+
body: JSON.stringify({ model: GROQ_MODEL, messages: [{ role: 'user', content: prompt }], temperature: 0.8 }),
|
| 12 |
+
});
|
| 13 |
+
if (res.ok) { const data = await res.json(); return data.choices[0].message.content; }
|
| 14 |
+
console.warn('Groq failed, falling back to Ollama...');
|
| 15 |
+
}
|
| 16 |
+
const res = await fetch(`${OLLAMA_URL}/api/generate`, {
|
| 17 |
+
method: 'POST',
|
| 18 |
+
headers: { 'Content-Type': 'application/json' },
|
| 19 |
+
body: JSON.stringify({ model: OLLAMA_MODEL, prompt, stream: false }),
|
| 20 |
+
});
|
| 21 |
+
if (!res.ok) throw new Error('Both Groq and Ollama failed.');
|
| 22 |
+
return (await res.json()).response;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
function parseJSON(text) {
|
| 26 |
+
try { return JSON.parse(text.trim()); }
|
| 27 |
+
catch { const m = text.match(/\{[\s\S]*\}/); if (m) return JSON.parse(m[0]); throw new Error('Failed to parse AI response'); }
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
async function roastCode(code, language) {
|
| 31 |
+
const text = await callAI(`You are "Chef CodeRamsay" - a Gordon Ramsay-style code reviewer. You roast bad code with sharp wit, creative insults, and dramatic reactions. But you ALSO provide genuinely helpful advice.
|
| 32 |
+
|
| 33 |
+
LANGUAGE: ${language || 'auto-detect'}
|
| 34 |
+
CODE:
|
| 35 |
+
\`\`\`
|
| 36 |
+
${code}
|
| 37 |
+
\`\`\`
|
| 38 |
+
|
| 39 |
+
Return ONLY valid JSON:
|
| 40 |
+
{
|
| 41 |
+
"language": "<detected language>",
|
| 42 |
+
"overallScore": <1-100>,
|
| 43 |
+
"roastLevel": "<raw|medium-rare|well-done|burnt-to-a-crisp>",
|
| 44 |
+
"openingRoast": "<A 2-3 sentence Gordon Ramsay style opening roast of this code. Be dramatic and funny.>",
|
| 45 |
+
"issues": [
|
| 46 |
+
{"line": "<line or section>", "roast": "<funny roast of this issue>", "fix": "<actual helpful fix>", "severity": "<mild|spicy|nuclear>"}
|
| 47 |
+
],
|
| 48 |
+
"codeSmells": ["<smell 1>", "<smell 2>", "<smell 3>"],
|
| 49 |
+
"bestPracticeViolations": ["<violation 1>", "<violation 2>"],
|
| 50 |
+
"rewrittenCode": "<the entire code rewritten properly with best practices>",
|
| 51 |
+
"closingRoast": "<A final dramatic one-liner, Gordon Ramsay style>",
|
| 52 |
+
"wouldHire": "<YES|MAYBE|GET OUT OF MY KITCHEN>"
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
Return ONLY JSON, no markdown.`);
|
| 56 |
+
return parseJSON(text);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
module.exports = { roastCode };
|