LLM Tool calls
getting it to talk back
- tags
- ollama
- ai
- vercel
Contents
Continuing on the exploration of vercels ai package, lets look at how to add tool calling. I'm mainly going to be testing this against ollama, but it's easy enough to point it to a different model provider without changing any of the code. (This is a big reason why I'm using vercel's npm package rather than hitting the APIs directly.)
Make sure that you get an ollama model that supports tools, such as
llama3.1
:
|
|
Install
|
|
Model Management
First we build something that abstracts over the model selection, so
we can do things like ollama/llama3.1
or openai/gpt-4o
.
|
|
Agent and tools
I'm going to wrap everything into a Context
object, which contains the
model name, the system prompt, and running list of messages that have
been exchanged with the LLM. We'll also put tools on here, so that we
can have a clean way to add things together.
The makeGenerator
function returns them all put together which will be
passed to either generatorResponse
(sync) or streamResponse
(async).
maxToolRoundtrips
is honored by generateText
but we will need to
implement this manually for streamText
.
|
|
Generate response
Fairly straight forward.
|
|
Stream response
Here we get more complicated. When you call streamText
, it will
return chunks which we print out from the onChunk
handler. We will
also get a few different chunks – tool-call
which is when the LLM
requests to call our tool, and tool-result
which is the result.
This is fine, but at the end it calls onFinish
with the results. This
isn't really what we want, we want it to take the results and do
something with it. So we check to see if the finish reason is
tool-results
and if so we call streamResponse
again and hope for an
actual text output.
|
|
Weather Tool
Lets try it out! We'll define a simple weatherTool like so, which just returns something random.
|
|
And then put it together:
|
|
|
|
Calling tool weather { location: 'Tokyo' } tool call for Tokyo Tool result weather { location: 'Tokyo', temperature: 77 } The current temperature in Tokyo is 77 degrees Fahrenheit.
Meta Tool
How about a tool that the llm can call when it wishes it had a tool? We could then use that to add that tool and rerun the chat so maybe it'll be smarter in the future!
When the meta tool is invoked, it creates a typescript file that defines the tool that it wants to use. You can then implement that tool.
|
|
Chat
Here's a chat interface for going dynamic with it.
|
|
Previously
Next