|
| 1 | +import { gateway } from "ai" |
| 2 | +import { streamText } from "ai" |
1 | 3 | import { NextResponse } from "next/server" |
2 | 4 |
|
3 | | -export async function POST(request: Request) { |
| 5 | +export async function POST(req: Request) { |
4 | 6 | try { |
5 | | - const { message, history } = await request.json() |
6 | | - |
7 | | - const ollamaUrl = process.env.OLLAMA_URL || "http://localhost:11434" |
8 | | - const ollamaModel = process.env.OLLAMA_MODEL || "deepseek-r1:7b" |
9 | | - |
10 | | - const messages = history |
11 | | - .slice(-10) |
12 | | - .map((msg: { role: string; content: string }) => ({ |
13 | | - role: msg.role === "user" ? "user" : "assistant", |
14 | | - content: msg.content, |
15 | | - })) |
16 | | - |
17 | | - messages.push({ |
18 | | - role: "user", |
19 | | - content: message, |
20 | | - }) |
21 | | - |
22 | | - const ollamaResponse = await fetch(`${ollamaUrl}/api/chat`, { |
23 | | - method: "POST", |
24 | | - headers: { |
25 | | - "Content-Type": "application/json", |
26 | | - }, |
27 | | - body: JSON.stringify({ |
28 | | - model: ollamaModel, |
29 | | - messages, |
30 | | - stream: false, |
31 | | - options: { |
32 | | - temperature: 0.7, |
33 | | - top_p: 0.95, |
34 | | - }, |
35 | | - }), |
36 | | - }) |
37 | | - |
38 | | - if (!ollamaResponse.ok) { |
39 | | - throw new Error(`Ollama API error: ${ollamaResponse.status}`) |
| 7 | + const { messages, model } = await req.json() |
| 8 | + |
| 9 | + // Ensure API key is configured |
| 10 | + if (!process.env.AI_GATEWAY_API_KEY) { |
| 11 | + return NextResponse.json( |
| 12 | + { error: "AI Gateway API Key not found. Please set AI_GATEWAY_API_KEY in your env." }, |
| 13 | + { status: 500 } |
| 14 | + ) |
40 | 15 | } |
41 | 16 |
|
42 | | - const data = await ollamaResponse.json() |
43 | | - const aiResponse = data.message?.content || "Sorry, I couldn't generate a response." |
44 | | - |
45 | | - console.log(`✓ Using Ollama local model: ${ollamaModel}`) |
| 17 | + const result = streamText({ |
| 18 | + model: gateway(model || "google:gemini-1.5-pro"), |
| 19 | + messages, |
| 20 | + }) |
46 | 21 |
|
47 | | - return NextResponse.json({ response: aiResponse, provider: "Ollama (Local)" }) |
48 | | - } catch (error) { |
49 | | - console.error("Error in chat API:", error) |
| 22 | + return result.toTextStreamResponse() |
| 23 | + } catch (error: any) { |
| 24 | + console.error("Error in AI chat:", error) |
50 | 25 | return NextResponse.json( |
51 | | - { |
52 | | - error: "Failed to connect to Ollama. Please ensure Ollama is running.", |
53 | | - provider: "Error" |
54 | | - }, |
55 | | - { status: 500 }, |
| 26 | + { error: error.message || "Failed to process chat request" }, |
| 27 | + { status: 500 } |
56 | 28 | ) |
57 | 29 | } |
58 | 30 | } |
|
0 commit comments