diff --git a/__LOCAL_LLMs/dashboard/src/app/api/ollama/stream/route.ts b/__LOCAL_LLMs/dashboard/src/app/api/ollama/stream/route.ts new file mode 100644 index 00000000..6ffc50e4 --- /dev/null +++ b/__LOCAL_LLMs/dashboard/src/app/api/ollama/stream/route.ts @@ -0,0 +1,37 @@ +import { NextRequest } from 'next/server'; + +const OLLAMA_URL = process.env.OLLAMA_URL || 'http://localhost:11434'; + +export async function POST(request: NextRequest) { + try { + const body = await request.json(); + const { model, prompt } = body; + + const response = await fetch(`${OLLAMA_URL}/api/generate`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ model, prompt, stream: true }), + }); + + if (!response.ok || !response.body) { + return new Response(JSON.stringify({ error: `Ollama error: ${response.status}` }), { + status: 500, + headers: { 'Content-Type': 'application/json' }, + }); + } + + // Pipe the Ollama stream directly to the client + return new Response(response.body, { + headers: { + 'Content-Type': 'application/x-ndjson', + 'Transfer-Encoding': 'chunked', + 'Cache-Control': 'no-cache', + }, + }); + } catch (err) { + return new Response(JSON.stringify({ error: String(err) }), { + status: 500, + headers: { 'Content-Type': 'application/json' }, + }); + } +}