-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add use-chat-tool-call-ui example. (#1523)
- Loading branch information
Showing
5 changed files
with
442 additions
and
11 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
48 changes: 48 additions & 0 deletions
48
examples/next-openai/app/api/use-chat-tool-call-ui/route.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,48 @@ | ||
import { openai } from '@ai-sdk/openai'; | ||
import { StreamData, StreamingTextResponse, streamText } from 'ai'; | ||
import { z } from 'zod'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export async function POST(req: Request) { | ||
// Extract the `messages` from the body of the request | ||
const { messages } = await req.json(); | ||
|
||
// Call the language model | ||
const result = await streamText({ | ||
model: openai('gpt-4-turbo'), | ||
messages, | ||
tools: { | ||
weather: { | ||
description: 'show the weather in a given city to the user', | ||
parameters: z.object({ city: z.string() }), | ||
execute: async ({ city }: { city: string }) => { | ||
// return fake weather data: | ||
return { weather: 'sunny' }; | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const streamData = new StreamData(); | ||
|
||
// async: process tool calls and append them to the stream data | ||
(async () => { | ||
try { | ||
for await (const part of result.fullStream) { | ||
if (part.type === 'tool-result') { | ||
// only weather tool, so it's fully typed: | ||
streamData.append({ | ||
city: part.args.city, | ||
weather: part.result.weather, | ||
}); | ||
} | ||
} | ||
} finally { | ||
streamData.close(); | ||
} | ||
})().catch(console.error); | ||
|
||
// Respond with the stream | ||
return new StreamingTextResponse(result.toAIStream(), undefined, streamData); | ||
} |
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,35 @@ | ||
'use client'; | ||
|
||
import { useChat } from 'ai/react'; | ||
|
||
export default function Chat() { | ||
const { messages, input, handleInputChange, handleSubmit, data } = useChat({ | ||
api: '/api/use-chat-tool-call-ui', | ||
}); | ||
|
||
return ( | ||
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch"> | ||
{data?.map((d: any, i) => ( | ||
<div key={i} className="whitespace-pre-wrap"> | ||
{d.city ? `Weather in ${d.city}: ${d.weather}` : ''} | ||
</div> | ||
))} | ||
|
||
{messages.map(m => ( | ||
<div key={m.id} className="whitespace-pre-wrap"> | ||
{m.role === 'user' ? 'User: ' : 'AI: '} | ||
{m.content} | ||
</div> | ||
))} | ||
|
||
<form onSubmit={handleSubmit}> | ||
<input | ||
className="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
Oops, something went wrong.