Use MiniMax API directly

This commit is contained in:
Manohar Gupta 2026-05-10 12:04:39 +05:30
parent 89ca48b420
commit bbb3953147

View file

@ -1,13 +1,8 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { sql } from "@/db"; import { sql } from "@/db";
interface Message { const MINIMAX_API_KEY = "289bf7d1cf0c0b12ff5ccf48d95ff3c3";
role: "user" | "assistant"; const MINIMAX_BASE_URL = "https://api.minimax.chat/v1";
content: string;
}
const LITELLM_BASE_URL = process.env.OPENAI_API_BASE_URL || "http://litellm-gateway:4000/v1";
const LITELLM_API_KEY = process.env.LITELLM_MASTER_KEY || "sk-tiger-gateway-289bf7d1cf0c0b12ff5ccf48d95ff3c3";
export async function POST(request: Request) { export async function POST(request: Request) {
try { try {
@ -40,42 +35,33 @@ export async function POST(request: Request) {
const allMessages = [systemMessage, ...messages]; const allMessages = [systemMessage, ...messages];
// Try different MiniMax model formats // Call MiniMax API directly
const models = [ const response = await fetch(`${MINIMAX_BASE_URL}/text/chatcompletion_v2`, {
"minimax/Abab7.5-chat", method: "POST",
"minimax/Abab7-Chat", headers: {
"azure/gpt-4o-mini", "Content-Type": "application/json",
"gpt-4o-mini", Authorization: `Bearer ${MINIMAX_API_KEY}`,
]; },
body: JSON.stringify({
model: "MiniMax-Text-01",
messages: allMessages,
max_tokens: 500,
}),
});
let lastError = ""; if (!response.ok) {
for (const model of models) { const error = await response.text();
try { return NextResponse.json({ error: "MiniMax error: " + error }, { status: response.status });
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);
}
} }
return NextResponse.json({ error: "No models available: " + lastError }, { status: 500 }); const data = await response.json();
// MiniMax response format
const reply = data.choices?.[0]?.message?.content
|| data.choices?.[0]?.delta?.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 });