-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from jakobhoeg/development
feat: add vision support (dev env. support only for now)
- Loading branch information
Showing
9 changed files
with
3,105 additions
and
2,669 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,31 +1,35 @@ | ||
import { StreamingTextResponse, Message } from "ai"; | ||
import { ChatOllama } from "@langchain/community/chat_models/ollama"; | ||
import { AIMessage, HumanMessage } from "@langchain/core/messages"; | ||
import { BytesOutputParser } from "@langchain/core/output_parsers"; | ||
import { createOllama } from 'ollama-ai-provider'; | ||
import { streamText, convertToCoreMessages, CoreMessage, UserContent } from 'ai'; | ||
|
||
export const runtime = "edge"; | ||
export const dynamic = "force-dynamic"; | ||
|
||
export async function POST(req: Request) { | ||
const { messages, selectedModel } = await req.json(); | ||
// Destructure request data | ||
const { messages, selectedModel, data } = await req.json(); | ||
|
||
const model = new ChatOllama({ | ||
baseUrl: process.env.NEXT_PUBLIC_OLLAMA_URL || "http://localhost:11434", | ||
model: selectedModel, | ||
}); | ||
const initialMessages = messages.slice(0, -1); | ||
const currentMessage = messages[messages.length - 1]; | ||
|
||
const ollama = createOllama({}); | ||
|
||
const parser = new BytesOutputParser(); | ||
// Build message content array directly | ||
const messageContent: UserContent = [{ type: 'text', text: currentMessage.content }]; | ||
|
||
const stream = await model | ||
.pipe(parser) | ||
.stream( | ||
(messages as Message[]).map((m) => | ||
m.role == "user" | ||
? new HumanMessage(m.content) | ||
: new AIMessage(m.content) | ||
) | ||
); | ||
// Add images if they exist | ||
data?.images?.forEach((imageUrl: string) => { | ||
const image = new URL(imageUrl); | ||
messageContent.push({ type: 'image', image }); | ||
}); | ||
|
||
// Stream text using the ollama model | ||
const result = await streamText({ | ||
model: ollama(selectedModel), | ||
messages: [ | ||
...convertToCoreMessages(initialMessages), | ||
{ role: 'user', content: messageContent }, | ||
], | ||
}); | ||
|
||
return new StreamingTextResponse(stream); | ||
return result.toDataStreamResponse(); | ||
} |
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,29 @@ | ||
|
||
import { CoreMessage } from "ai"; | ||
import { create } from "zustand"; | ||
|
||
interface State { | ||
base64Images: string[] | null; | ||
messages: CoreMessage[]; | ||
} | ||
|
||
interface Actions { | ||
setBase64Images: (base64Images: string[] | null) => void; | ||
setMessages: ( | ||
fn: ( | ||
messages: CoreMessage[] | ||
) => CoreMessage[] | ||
) => void; | ||
} | ||
|
||
const useChatStore = create<State & Actions>()( | ||
(set) => ({ | ||
base64Images: null, | ||
setBase64Images: (base64Images) => set({ base64Images }), | ||
|
||
messages: [], | ||
setMessages: (fn) => set((state) => ({ messages: fn(state.messages) })), | ||
}) | ||
) | ||
|
||
export default useChatStore; |
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.