Use LiteLLM gateway URL

This commit is contained in:
Manohar Gupta 2026-05-10 12:08:48 +05:30
parent c6ae0593f6
commit c466c8bad5

View file

@ -1,10 +1,8 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { sql } from "@/db"; import { sql } from "@/db";
// Try Ollama first (local), fallback to MiniMax const LITELLM_URL = process.env.LITELLM_URL || "https://litellm-gateway.manohargupta.com";
const OLLAMA_URL = process.env.OLLAMA_URL || "http://localhost:11434"; const LITELLM_KEY = process.env.LITELLM_KEY || "sk-tiger-gateway-289bf7d1cf0c0b12ff5ccf48d95ff3c3";
const MINIMAX_API_KEY = process.env.MINIMAX_API_KEY || "289bf7d1cf0c0b12ff5ccf48d95ff3c3";
const USE_MINIMAX = process.env.USE_MINIMAX === "true";
export async function POST(request: Request) { export async function POST(request: Request) {
try { try {
@ -15,7 +13,7 @@ export async function POST(request: Request) {
return NextResponse.json({ error: "messages array required" }, { status: 400 }); return NextResponse.json({ error: "messages array required" }, { status: 400 });
} }
// Get child's profile context // Get child's context
let context = ""; let context = "";
if (childId) { if (childId) {
const children = await sql.unsafe( const children = await sql.unsafe(
@ -29,56 +27,32 @@ export async function POST(request: Request) {
} }
} }
// System message
const systemMessage = { const systemMessage = {
role: "system", role: "system",
content: `You are Tia, a friendly baby care assistant. Give caring, practical advice for new parents. Keep responses brief and helpful.`, content: `You are Tia, a friendly baby care assistant. Give caring, practical advice for new parents. Keep responses brief and helpful.`,
}; };
// Try Ollama first // Call LiteLLM
if (!USE_MINIMAX) { const response = await fetch(`${LITELLM_URL}/v1/chat/completions`, {
try {
const response = await fetch(`${OLLAMA_URL}/api/chat`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "llama3",
messages: [systemMessage, ...messages],
stream: false,
}),
});
if (response.ok) {
const data = await response.json();
return NextResponse.json({ reply: data.message?.content || data.response });
}
} catch (e) {
console.log("Ollama not available:", e);
}
}
// Fallback to MiniMax
const mmResponse = await fetch("https://api.minimax.chat/v1/text/chatcompletion_v2", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${MINIMAX_API_KEY}`, Authorization: `Bearer ${LITELLM_KEY}`,
}, },
body: JSON.stringify({ body: JSON.stringify({
model: "abab7.5-chat", model: "tiger-minimax",
group_id: "minimax",
messages: [systemMessage, ...messages], messages: [systemMessage, ...messages],
max_tokens: 500, max_tokens: 500,
}), }),
}); });
if (!mmResponse.ok) { if (!response.ok) {
const error = await mmResponse.text(); const error = await response.text();
return NextResponse.json({ error: "MiniMax: " + error }, { status: mmResponse.status }); return NextResponse.json({ error: error }, { status: response.status });
} }
const data = await mmResponse.json(); const data = await response.json();
return NextResponse.json({ reply: data.choices?.[0]?.message?.content || "No response" }); return NextResponse.json({ reply: data.choices?.[0]?.message?.content });
} 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 });