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