Add Ollama fallback + better error handling
This commit is contained in:
parent
d070dd8d21
commit
c6ae0593f6
1 changed files with 35 additions and 19 deletions
|
|
@ -1,8 +1,10 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { sql } from "@/db";
|
||||
|
||||
const MINIMAX_API_KEY = "289bf7d1cf0c0b12ff5ccf48d95ff3c3";
|
||||
const MINIMAX_BASE_URL = "https://api.minimax.chat/v1";
|
||||
// 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";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
|
|
@ -27,16 +29,36 @@ export async function POST(request: Request) {
|
|||
}
|
||||
}
|
||||
|
||||
// Build messages with system prompt
|
||||
// System message
|
||||
const systemMessage = {
|
||||
role: "system",
|
||||
content: `You are Tia, a helpful baby care assistant. ${context} 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.`,
|
||||
};
|
||||
|
||||
const allMessages = [systemMessage, ...messages];
|
||||
// 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,
|
||||
}),
|
||||
});
|
||||
|
||||
// Call MiniMax API directly
|
||||
const response = await fetch(`${MINIMAX_BASE_URL}/text/chatcompletion_v2`, {
|
||||
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",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
|
|
@ -45,24 +67,18 @@ export async function POST(request: Request) {
|
|||
body: JSON.stringify({
|
||||
model: "abab7.5-chat",
|
||||
group_id: "minimax",
|
||||
messages: allMessages,
|
||||
messages: [systemMessage, ...messages],
|
||||
max_tokens: 500,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.text();
|
||||
return NextResponse.json({ error: "MiniMax error: " + error }, { status: response.status });
|
||||
if (!mmResponse.ok) {
|
||||
const error = await mmResponse.text();
|
||||
return NextResponse.json({ error: "MiniMax: " + error }, { status: mmResponse.status });
|
||||
}
|
||||
|
||||
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 });
|
||||
const data = await mmResponse.json();
|
||||
return NextResponse.json({ reply: data.choices?.[0]?.message?.content || "No response" });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue