Git Product home page Git Product logo

Comments (2)

HosseinAgha avatar HosseinAgha commented on June 25, 2024 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.

nckre avatar nckre commented on June 25, 2024

Based on what I've seen, there are two different ways in the examples, both embedded into the components rather than the functions:

  1. 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
         ])
     }}
 >
  1. 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 calls confirmPurchase)

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.