forked from chainable-dev/the-front
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
217 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,43 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
// Remove the Edge runtime specification | ||
// export const runtime = 'edge'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
const { message } = await req.json(); | ||
const { message, model } = await req.json(); | ||
|
||
// Process the chat message here | ||
// For now, let's just echo the message back | ||
const response = await fetch('http://localhost:11434/api/chat', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
model, | ||
messages: [{ role: 'user', content: message }], | ||
stream: true, | ||
}), | ||
}); | ||
|
||
return NextResponse.json({ reply: `You said: ${message}` }); | ||
} catch (error) { | ||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); | ||
if (!response.ok) { | ||
throw new Error('Failed to fetch from Ollama'); | ||
} | ||
|
||
const stream = new ReadableStream({ | ||
async start(controller) { | ||
const reader = response.body?.getReader(); | ||
while (true) { | ||
const { done, value } = await reader!.read(); | ||
if (done) break; | ||
const chunk = new TextDecoder().decode(value); | ||
try { | ||
const parsed = JSON.parse(chunk); | ||
if (parsed.message?.content) { | ||
controller.enqueue(parsed.message.content); | ||
} | ||
} catch (e) { | ||
console.error('Error parsing JSON:', e); | ||
} | ||
} | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
return new Response(stream, { | ||
headers: { 'Content-Type': 'text/plain' }, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
const response = await fetch('http://localhost:11434/api/tags'); | ||
if (!response.ok) { | ||
throw new Error('Failed to fetch models from Ollama'); | ||
} | ||
const data = await response.json(); | ||
const models = data.models.map((model: { name: string }) => model.name); | ||
return NextResponse.json({ models }); | ||
} catch (error) { | ||
console.error('Error fetching models:', error); | ||
return NextResponse.json({ error: 'Failed to fetch models' }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.