Automatically chain another function call if needed

26 Views Asked by At

I am working with the new generative AI from Vercel. I have two functions: search_for_person and search_for_species. These functions call SWAPI. Their functions are below:

tools: {
    search_for_person: {
        description: 'Search for a person in the Star Wars universe',
        parameters: z.object({ personName: z.string().describe('the name of the person') }).required(),
        render: async function* ({ personName }) {
            yield <Spinner />
            const people = await searchForPerson(personName)
            aiState.done([...aiState.get(), { role: 'function', name: 'search_for_person', content: JSON.stringify(people) }])
            return <pre>{JSON.stringify(people, null, 2)}</pre>
        },
    },

    search_for_species: {
        description: 'Search for a species in the Star Wars universe by a url',
        parameters: z.object({ speciesUrl: z.string().describe('the url of the species') }).required(),
        render: async function* ({ speciesUrl }) {
            yield <Spinner />
            const species = await searchForSpecies(speciesUrl)
            aiState.done([...aiState.get(), { role: 'function', name: 'search_for_species', content: JSON.stringify(species) }])
            return <pre>{JSON.stringify(species, null, 2)}</pre>
        },
    },
}

When I ask the AI assistant to find me luke it returns an array with 1 entry: Luke Skywalker. When I give it Luke's specie's URL it will give me the species information of Luke.

If I ask the assistant to find me the species of Luke Skywalker, it just returns the person object with the URL.

Is there a way that I can automatically chain another function call but only if it's relevant to the prompt i.e. just go ahead and use the species URL found in the response for Luke to find the species information?

https://github.com/vercel/ai/discussions/1150

0

There are 0 best solutions below