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 { sql } from "@/db";
interface Message {
role: "user" | "assistant";
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";
const MINIMAX_API_KEY = "289bf7d1cf0c0b12ff5ccf48d95ff3c3";
const MINIMAX_BASE_URL = "https://api.minimax.chat/v1";
export async function POST(request: Request) {
try {
@ -40,42 +35,33 @@ export async function POST(request: Request) {
const allMessages = [systemMessage, ...messages];
// 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`, {
// Call MiniMax API directly
const response = await fetch(`${MINIMAX_BASE_URL}/text/chatcompletion_v2`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${LITELLM_API_KEY}`,
Authorization: `Bearer ${MINIMAX_API_KEY}`,
},
body: JSON.stringify({
model,
model: "MiniMax-Text-01",
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);
}
if (!response.ok) {
const error = await response.text();
return NextResponse.json({ error: "MiniMax error: " + error }, { status: response.status });
}
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) {
console.error(error);
return NextResponse.json({ error: String(error) }, { status: 500 });