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