Skip to content

Commit 929a437

Browse files
feat(hub): Add Groq as primary LLM provider in hub sophia API
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 34782e4 commit 929a437

1 file changed

Lines changed: 41 additions & 3 deletions

File tree

  • apps/hub/src/app/api/sophia/chat

apps/hub/src/app/api/sophia/chat/route.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,46 @@ async function callLLM(
337337
maxTokens: number = 2048,
338338
temperature: number = 0.7
339339
): Promise<{ text?: string; toolCalls?: any[]; error?: string }> {
340+
const groqKey = process.env.GROQ_API_KEY;
340341
const grokKey = process.env.XAI_API_KEY || process.env.GROK_API_KEY;
341342
const anthropicKey = process.env.ANTHROPIC_API_KEY;
342343
const openaiKey = process.env.OPENAI_API_KEY;
343344

344-
// Try Grok first (supports tools)
345+
// Groq (fastest inference — OpenAI-compatible)
346+
if (groqKey) {
347+
try {
348+
const response = await fetch('https://api.groq.com/openai/v1/chat/completions', {
349+
method: 'POST',
350+
headers: {
351+
'Content-Type': 'application/json',
352+
'Authorization': `Bearer ${groqKey}`,
353+
},
354+
body: JSON.stringify({
355+
model: 'llama-3.3-70b-versatile',
356+
max_tokens: maxTokens,
357+
temperature,
358+
messages: [{ role: 'system', content: systemPrompt }, ...messages],
359+
tools: enableTools ? TOOL_DEFINITIONS : undefined,
360+
tool_choice: enableTools ? 'auto' : undefined,
361+
}),
362+
});
363+
364+
if (response.ok) {
365+
const data = await response.json();
366+
const choice = data.choices[0];
367+
368+
if (choice.message.tool_calls) {
369+
return { toolCalls: choice.message.tool_calls };
370+
}
371+
372+
return { text: choice.message.content };
373+
}
374+
} catch (e) {
375+
console.warn('Groq API failed:', e);
376+
}
377+
}
378+
379+
// Grok / xAI (OpenAI-compatible)
345380
if (grokKey) {
346381
try {
347382
const response = await fetch('https://api.x.ai/v1/chat/completions', {
@@ -464,7 +499,7 @@ async function callLLM(
464499
}
465500
}
466501

467-
return { error: 'No LLM API available' };
502+
return { error: 'No LLM API key configured. Set GROQ_API_KEY, XAI_API_KEY, ANTHROPIC_API_KEY, or OPENAI_API_KEY.' };
468503
}
469504

470505
export async function POST(request: NextRequest) {
@@ -561,17 +596,20 @@ The user is interacting through the wallet extension.`;
561596

562597
// Health check endpoint
563598
export async function GET() {
599+
const hasGroq = !!process.env.GROQ_API_KEY;
564600
const hasGrok = !!(process.env.XAI_API_KEY || process.env.GROK_API_KEY);
565601
const hasAnthropic = !!process.env.ANTHROPIC_API_KEY;
566602
const hasOpenAI = !!process.env.OPENAI_API_KEY;
567603

568604
return NextResponse.json({
569-
status: hasGrok || hasAnthropic || hasOpenAI ? 'ready' : 'no-api-key',
605+
status: hasGroq || hasGrok || hasAnthropic || hasOpenAI ? 'ready' : 'no-api-key',
570606
providers: {
607+
groq: hasGroq,
571608
grok: hasGrok,
572609
anthropic: hasAnthropic,
573610
openai: hasOpenAI,
574611
},
575612
tools: TOOL_DEFINITIONS.map(t => t.function.name),
613+
toolCount: TOOL_DEFINITIONS.length,
576614
});
577615
}

0 commit comments

Comments
 (0)