Skip to content

Commit e39303b

Browse files
Merge pull request #27 from lab68dev/feat/AI-SDK-Vercel
feat: AI SDK from Vercel AI Gateway
2 parents f7e29dd + d2d2603 commit e39303b

4 files changed

Lines changed: 240 additions & 174 deletions

File tree

app/api/chat/route.ts

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,30 @@
1+
import { gateway } from "ai"
2+
import { streamText } from "ai"
13
import { NextResponse } from "next/server"
24

3-
export async function POST(request: Request) {
5+
export async function POST(req: Request) {
46
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+
)
4015
}
4116

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+
})
4621

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)
5025
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 }
5628
)
5729
}
5830
}

0 commit comments

Comments
 (0)