-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Align the chat request options for useChat across all frameworks (#859)
Co-authored-by: Lars Grammel <lgrammel@Larss-MBP.fritz.box>
- Loading branch information
Showing
11 changed files
with
221 additions
and
28 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
ai/solid: add chat request options to useChat |
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,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
ai/vue: add chat request options to useChat |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<script setup lang="ts"> | ||
import { useChat } from 'ai/vue'; | ||
const { messages, input, handleSubmit } = useChat({ | ||
api: '/api/chat-with-vision', | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div class="flex flex-col w-full max-w-md py-24 mx-auto stretch"> | ||
<div v-for="m in messages" key="m.id" class="whitespace-pre-wrap"> | ||
{{ m.role === 'user' ? 'User: ' : 'AI: ' }} | ||
{{ m.content }} | ||
</div> | ||
|
||
<form | ||
@submit=" | ||
e => | ||
handleSubmit(e, { | ||
data: { | ||
imageUrl: | ||
'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Field_sparrow_in_CP_%2841484%29_%28cropped%29.jpg/733px-Field_sparrow_in_CP_%2841484%29_%28cropped%29.jpg', | ||
}, | ||
}) | ||
" | ||
> | ||
<input | ||
class="fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl" | ||
v-model="input" | ||
placeholder="Say something..." | ||
/> | ||
</form> | ||
</div> | ||
</template> |
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,46 @@ | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import OpenAI from 'openai'; | ||
|
||
export default defineLazyEventHandler(async () => { | ||
const apiKey = useRuntimeConfig().openaiApiKey; | ||
if (!apiKey) throw new Error('Missing OpenAI API key'); | ||
const openai = new OpenAI({ | ||
apiKey: apiKey, | ||
}); | ||
|
||
return defineEventHandler(async (event: any) => { | ||
// Extract the `prompt` from the body of the request | ||
const { messages, data } = await readBody(event); | ||
|
||
const initialMessages = messages.slice(0, -1); | ||
const currentMessage = messages[messages.length - 1]; | ||
|
||
// Ask OpenAI for a streaming chat completion given the prompt | ||
const response = await openai.chat.completions.create({ | ||
model: 'gpt-4-vision-preview', | ||
stream: true, | ||
max_tokens: 150, | ||
messages: [ | ||
...initialMessages, | ||
{ | ||
...currentMessage, | ||
content: [ | ||
{ type: 'text', text: currentMessage.content }, | ||
|
||
// forward the image information to OpenAI: | ||
{ | ||
type: 'image_url', | ||
image_url: data.imageUrl, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
// Convert the response into a friendly text-stream | ||
const stream = OpenAIStream(response); | ||
|
||
// Respond with the stream | ||
return new StreamingTextResponse(stream); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
examples/solidstart-openai/src/routes/api/chat-with-vision/index.ts
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,43 @@ | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import OpenAI from 'openai'; | ||
import { APIEvent } from 'solid-start/api'; | ||
|
||
// Create an OpenAI API client | ||
const openai = new OpenAI({ | ||
apiKey: process.env['OPENAI_API_KEY'] || '', | ||
}); | ||
|
||
export const POST = async (event: APIEvent) => { | ||
// 'data' contains the additional data that you have sent: | ||
const { messages, data } = await event.request.json(); | ||
|
||
const initialMessages = messages.slice(0, -1); | ||
const currentMessage = messages[messages.length - 1]; | ||
|
||
// Ask OpenAI for a streaming chat completion given the prompt | ||
const response = await openai.chat.completions.create({ | ||
model: 'gpt-4-vision-preview', | ||
stream: true, | ||
max_tokens: 150, | ||
messages: [ | ||
...initialMessages, | ||
{ | ||
...currentMessage, | ||
content: [ | ||
{ type: 'text', text: currentMessage.content }, | ||
|
||
// forward the image information to OpenAI: | ||
{ | ||
type: 'image_url', | ||
image_url: data.imageUrl, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
// Convert the response into a friendly text-stream | ||
const stream = OpenAIStream(response); | ||
// Respond with the stream | ||
return new StreamingTextResponse(stream); | ||
}; |
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,46 @@ | ||
import { For, JSX } from 'solid-js'; | ||
import { useChat } from 'ai/solid'; | ||
|
||
export default function Chat() { | ||
const { messages, input, setInput, handleSubmit } = useChat({ | ||
api: '/api/chat-with-vision', | ||
}); | ||
|
||
const handleInputChange: JSX.ChangeEventHandlerUnion< | ||
HTMLInputElement, | ||
Event | ||
> = e => { | ||
setInput(e.target.value); | ||
}; | ||
|
||
return ( | ||
<div class="flex flex-col w-full max-w-md py-24 mx-auto stretch"> | ||
<For each={messages()}> | ||
{m => ( | ||
<div class="whitespace-pre-wrap"> | ||
{m.role === 'user' ? 'User: ' : 'AI: '} | ||
{m.content} | ||
</div> | ||
)} | ||
</For> | ||
|
||
<form | ||
onSubmit={e => | ||
handleSubmit(e, { | ||
data: { | ||
imageUrl: | ||
'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Field_sparrow_in_CP_%2841484%29_%28cropped%29.jpg/733px-Field_sparrow_in_CP_%2841484%29_%28cropped%29.jpg', | ||
}, | ||
}) | ||
} | ||
> | ||
<input | ||
class="fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl" | ||
value={input()} | ||
placeholder="Say something..." | ||
onChange={handleInputChange} | ||
/> | ||
</form> | ||
</div> | ||
); | ||
} |
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
Oops, something went wrong.