Comments (2)
OK, I successfully implemented function composition using streamUI
by recursively calling sendMessage
server action from inside the tools execute function.
This actually is the same pattern as nested streamable UIs because sendMessage
server function returns a streamable that when returned from a tool's execute function would would be wrapped inside the parent streamable.
I had to patch the streamUI
logic to call streamableUI.done() with latest as it looks like nested streamable UIs won't work if the parent streamable does not call done with the child streamable.
My final code is something like this:
export async function sendUserMessage(content, mutableAIState) {
const aiState = mutableAIState ?? getMutableAIState<typeof AI>()
const result = streamUI({
tools: {
getStocks: {
execute: async function *(_, toolCallInfo) {
yield {
<LoadingStocks />
}
const { stocks } = await getStocks()
aiState.update({
...aiState
{
id: nanoid(),
role: 'assistant',
content: [{
...toolCallInfo,
type: 'tool-call',
args,
}]
},
{
id: nanoid(),
role: 'tool',
content: [{
...toolCallInfo,
type: 'tool-result',
result: stocks,
}],
}
})
return (await submitUserMessage('', aiState)).display
}
},
logStocks: {
execute: async function*({ stocks }) {
// use the previous tool's call
}
}
}
})
return {
display: result.value
}
}
from ai.
Based on what I've seen, there are two different ways in the examples, both embedded into the components rather than the functions:
- Gemini chatbot submits messages on behalf of the user e.g. in list-flights.tsx:
<div
key={flight.id}
className="flex cursor-pointer flex-row items-start sm:items-center gap-4 rounded-xl p-2 hover:bg-zinc-50"
onClick={async () => {
const response = await submitUserMessage(
`The user has selected flight ${flight.airlines}, departing at ${flight.departureTime} and arriving at
${flight.arrivalTime} for $${flight.price}. Now proceeding to select seats.`
)
setMessages((currentMessages: any[]) => [
...currentMessages,
response
])
}}
>
- Vercel chatbot creates a system message and uses
useAction
to call a custom action specified in to chain a flow (e.g. stock-purchase.tsx callsconfirmPurchase
)
For me, the current setup is a bit confusing. There are different implementations/examples, some buggy ("cannot destructure role...") and it's not clearly documented what are the benefits of each approach (e.g. in Gemini why is is the flow to get a confirmation code split into 2 separate actions whereas everything else is part of tool calls in the default submitUserMessage?).
I understand they spin some of this together for events like the Google presentation and things are moving quickly = but at least the main chatbot could get some more consistent love. Maybe Ship24 will bring a new version?
In general, agree that it would be cool to have a simpler way to chain a few AI tool calls together e.g. in my case to (1) fetch data and display in a component & (2) get a text response describing the data w/o submitting extra user messages.
from ai.
Related Issues (20)
- Ability to set defaultHeaders with OpenRouter HOT 1
- Support for batch embed HOT 4
- chunkToText in OpenAIStream is mishandling tool_calls HOT 2
- Error: Maximum update depth exceeded. When using useCompletion hook in nextjs, on long response on gpt-4o HOT 8
- Add AI Vercel SDK to React
- `Error: failed to pipe response` when trying to respond with `fullStream` HOT 1
- feat: Anthropic streaming tool calls support
- Error when using model 'gpt-4o' with multiple tools in chat-with-tools example HOT 3
- feat: define tool parameters with JSON schema instead of Zod HOT 4
- Client-side exception upon recursive append to streamable UI in production HOT 2
- Add `stopSequences` parameter to AI SDK functions HOT 1
- Timeout error while using `Stream Text generation` from ai-sdk (Free plan) HOT 4
- Azure OpenAI support HOT 2
- Jest: cannot find module 'ai/rsc' HOT 1
- Documentation and examples how to use Vercel AI in React HOT 3
- Groq models don't return the usage HOT 3
- Missing links in OpenAI provider README HOT 2
- Erratic behavior of useChat - Could SWR be the issue?
- `onError` hook not called when an error occurs during streaming in `useAssistant`
- the example of bedrock/anthropic-v3 show error HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ai.