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