Try multiple model names for AI API

This commit is contained in:
Manohar Gupta 2026-05-10 12:01:11 +05:30
parent 4e9935cb74
commit 89ca48b420

View file

@ -40,7 +40,17 @@ export async function POST(request: Request) {
const allMessages = [systemMessage, ...messages]; const allMessages = [systemMessage, ...messages];
// Call LiteLLM // Try different MiniMax model formats
const models = [
"minimax/Abab7.5-chat",
"minimax/Abab7-Chat",
"azure/gpt-4o-mini",
"gpt-4o-mini",
];
let lastError = "";
for (const model of models) {
try {
const response = await fetch(`${LITELLM_BASE_URL}/chat/completions`, { const response = await fetch(`${LITELLM_BASE_URL}/chat/completions`, {
method: "POST", method: "POST",
headers: { headers: {
@ -48,21 +58,24 @@ export async function POST(request: Request) {
Authorization: `Bearer ${LITELLM_API_KEY}`, Authorization: `Bearer ${LITELLM_API_KEY}`,
}, },
body: JSON.stringify({ body: JSON.stringify({
model: "minimax/Abab7.5-chat", model,
messages: allMessages, messages: allMessages,
max_tokens: 500, max_tokens: 500,
}), }),
}); });
if (!response.ok) { if (response.ok) {
const error = await response.text();
return NextResponse.json({ error: error }, { status: response.status });
}
const data = await response.json(); const data = await response.json();
const reply = data.choices?.[0]?.message?.content || "Sorry, I couldn't get a response."; const reply = data.choices?.[0]?.message?.content || "Sorry, I couldn't get a response.";
return NextResponse.json({ reply }); return NextResponse.json({ reply });
}
lastError = await response.text();
} catch (e) {
lastError = String(e);
}
}
return NextResponse.json({ error: "No models available: " + lastError }, { status: 500 });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return NextResponse.json({ error: String(error) }, { status: 500 }); return NextResponse.json({ error: String(error) }, { status: 500 });