From bbb39531474d82b9c7a3f1c906ca2ceba6dce6b4 Mon Sep 17 00:00:00 2001 From: Mannu Date: Sun, 10 May 2026 12:04:39 +0530 Subject: [PATCH] Use MiniMax API directly --- src/app/api/ai/route.ts | 66 ++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/src/app/api/ai/route.ts b/src/app/api/ai/route.ts index 08b270f..119f02b 100644 --- a/src/app/api/ai/route.ts +++ b/src/app/api/ai/route.ts @@ -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", - ]; + // Call MiniMax API directly + const response = await fetch(`${MINIMAX_BASE_URL}/text/chatcompletion_v2`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${MINIMAX_API_KEY}`, + }, + body: JSON.stringify({ + model: "MiniMax-Text-01", + messages: allMessages, + max_tokens: 500, + }), + }); - 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); - } + 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 });