-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (ui/solid): add support for prepareRequestBody (#4711)
Co-authored-by: Jai <[email protected]>
- Loading branch information
Showing
8 changed files
with
317 additions
and
119 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-sdk/solid': patch | ||
--- | ||
|
||
feat (ui/solid): add support for prepareRequestBody |
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
24 changes: 24 additions & 0 deletions
24
examples/solidstart-openai/src/routes/api/use-chat-request/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,24 @@ | ||
import { openai } from '@ai-sdk/openai'; | ||
import { streamText, Message } from 'ai'; | ||
import { APIEvent } from '@solidjs/start/server'; | ||
|
||
export const POST = async (event: APIEvent) => { | ||
// Extract the `messages` from the body of the request | ||
const { message } = await event.request.json(); | ||
|
||
// Implement your own logic here to add message history | ||
const previousMessages: Message[] = []; | ||
const messages = [...previousMessages, message]; | ||
|
||
// Call the language model | ||
const result = streamText({ | ||
model: openai('gpt-4o-mini'), | ||
messages, | ||
async onFinish({ text, toolCalls, toolResults, usage, finishReason }) { | ||
// Implement your own logic here, e.g. for storing messages | ||
}, | ||
}); | ||
|
||
// Respond with the stream | ||
return result.toDataStreamResponse(); | ||
}; |
78 changes: 78 additions & 0 deletions
78
examples/solidstart-openai/src/routes/use-chat-request/index.tsx
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,78 @@ | ||
/* eslint-disable @next/next/no-img-element */ | ||
import { For } from 'solid-js'; | ||
import { useChat } from '@ai-sdk/solid'; | ||
import { createIdGenerator } from 'ai'; | ||
|
||
export default function Chat() { | ||
const { | ||
input, | ||
messages, | ||
handleInputChange, | ||
handleSubmit, | ||
isLoading, | ||
error, | ||
stop, | ||
reload, | ||
} = useChat({ | ||
api: '/api/use-chat-request', | ||
sendExtraMessageFields: true, | ||
generateId: createIdGenerator({ prefix: 'msgc', size: 16 }), | ||
|
||
experimental_prepareRequestBody({ messages }) { | ||
return { | ||
message: messages[messages.length - 1], | ||
}; | ||
}, | ||
}); | ||
|
||
return ( | ||
<div class="flex flex-col w-full max-w-md py-24 mx-auto stretch"> | ||
<div class="flex flex-col gap-2 p-2"> | ||
<For each={messages()}> | ||
{message => ( | ||
<div class="whitespace-pre-wrap"> | ||
{message.role === 'user' ? 'User: ' : 'AI: '} | ||
{message.content} | ||
</div> | ||
)} | ||
</For> | ||
</div> | ||
|
||
{isLoading() && ( | ||
<div class="mt-4 text-gray-500"> | ||
<div>Loading...</div> | ||
<button | ||
type="button" | ||
class="px-4 py-2 mt-4 text-blue-500 border border-blue-500 rounded-md" | ||
onClick={stop} | ||
> | ||
Stop | ||
</button> | ||
</div> | ||
)} | ||
|
||
{error() && ( | ||
<div class="mt-4"> | ||
<div class="text-red-500">An error occurred.</div> | ||
<button | ||
type="button" | ||
class="px-4 py-2 mt-4 text-blue-500 border border-blue-500 rounded-md" | ||
onClick={() => reload()} | ||
> | ||
Retry | ||
</button> | ||
</div> | ||
)} | ||
|
||
<form onSubmit={handleSubmit} class="fixed bottom-0 w-full max-w-md p-2"> | ||
<input | ||
class="w-full p-2 mb-8 border border-gray-300 rounded shadow-xl" | ||
value={input()} | ||
placeholder="Say something..." | ||
onInput={handleInputChange} | ||
disabled={isLoading()} | ||
/> | ||
</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
Oops, something went wrong.