Tool Use
Tool use is a technique which allows developers to connect Cohere’s Command R family of models to external tools like search engines, APIs, functions, databases, etc.
This opens up a richer set of behaviors by leveraging data stored in tools, taking actions through APIs, interacting with a vector database, querying a search engine, etc., and is particularly valuable for enterprise developers, since a lot of enterprise data lives in external sources.
Check out this notebook for worked-out examples.
What Is Possible with Tool Use?
Tool use (or “function calling”) enables a wide range of new use cases; it allows your chatbot to interact with your CRM to change the status of a deal, for example, to engage with a Python interpreter to conduct data science analysis, or to have an assistant to automatically search across different databases to retrieve information.
More broadly, it’s now possible to reliably ask the model to recommend a tool (or set of tools) along with advice on how to use them, which can be passed back to the model for more flexible workflows.
A popular application is to transform a user message into a search query for a vector database or any search engine. Because the user message can be transformed into one or many search queries, it’s possible to do multiple subtasks based on the content of the message.
The Four Steps of Tool Use (Theory)
Tool use allows developers to tell Command R/R+ which tools it can interact with and how to structure interactions (e.g. API requests, or anything that can be formatted in JSON). The model will then dynamically select the right tools and the right parameters to complete these interactions. Developers can then execute these tool calls, and receive tool results in return. Finally, to generate the final response from the model, developers submit these tool results to the Command R/R+ model.
We want to stress that it’s the developers executing tool calls and submitting final results to Command R/R+.
Here’s a graphic that represents the four steps discussed below:
Feel free to refer back to it as you read on.
Step 1 - Configure the Request to the Model
Before being able to run a tool use workflow, a developer must set up a few things:
- A list of tools to the model
- (Optionally) a system message containing instructions about the task and the desired style for the output.
Developers can provide one or many tools to the model. Every tool is described with a schema, indicating the tool name, description, and parameters (code snippets below).
Step 2 - The Model Dynamically Chooses the Right Tool
Once you’ve completed step one, the model will intelligently select the right tool(s) to call — and the right parameters for each tool call — based on the content of the user message.
Given a list of tool definitions, the model will generate a plan of action and decide which tools to use, in which order, and with what parameters.
Step 3 - The Developer Can Then Execute The Tool Calls
With the list of tool(s), the developer can then execute the appropriate calls (e.g. by pinging an API) using the tool parameters generated by the model. These tool calls will return tool results that will be fed to the model in Step 4.
As things stand, the developer is responsible for executing these tool calls, as the tool call executes on the developer’s side.
Step 4 - Command R/R+ Generates an Answer Based on the Tool Results
Finally, the developer calls the Cohere model, providing the tool results, in order to generate the model’s final answer, which includes the response and a list of citations.
The Four Steps of Tool Use (Step-by-Step Example)
For the sake of this illustration, we’ll assume a developer is building a chatbot to assist with sales-related questions. The chatbot has access to two tools to answer user questions: a daily sales report tool which holds data on sales volumes, and a product catalog which contains information about each product being sold.
Here is a walkthrough of what a relevant tool use workflow would look like.
Step 1
The developer provides the sales database and the products database to the model using the tools
parameter.
Observe that, for each tool, the developer describes the tool name, description, and inputs. Each input can have a type and can be marked as required.
Step 2
The model’s response contains the tool plan, a list of appropriate tools to call in order to answer the user’s question, as well as the appropriate inputs for each tool call.
Step 3
Now, the developer will query the appropriate tools and receive a tool result in return.
Step 4
Call the chat endpoint again with the tool results for the model to generate the response with citations.
This step comes with a unique differentiator: the language model cites which tool results were used to generate the final model answer! These citations make it easy to check where the model’s generated response claims are coming from.
More on this in the next section.
Built-In Citations in Tool Use
At Cohere, we care about building responsible, useful, and factually-accurate models.
For this reason, Cohere’s tool use comes with a unique differentiator; as part of its generation, the underlying model cites which tool results were used to generate the final model answer. These citations make it easy to check where the model’s generated response claims are coming from.
In other words, the model only generates claims that are verifiable through fine-grained citations.
These citations are optional — you can decide to ignore them. Having said that, citations tend to be valuable in tool use; they help users gain visibility into the model reasoning, as well as sanity check the final model generation.
Structured Outputs (Tools)
Setting the strict_tools
parameter to True will enforce each tool call to follow the specified tool schema. To learn more about this feature, visit the Structured Outputs documentation.
Note that strict_tools
is currently an experimental feature.
How to Get Good Answers With Tool Use
To get good answers with tool use, make sure that the tool name and description as well as the names and descriptions for each parameter are descriptive. If you’re not getting the model to recommend your tool correctly, iterate on those descriptions and names to help the model understand the tool better.
When you pass the tool results back to the model make sure that they are structured in a comprehensive way. For example, if you are passing the results of a add_numbers
function:
What’s Next?
Here, we’ll preview some of the functionality we plan on adding in the coming months.
Cohere-hosted Tools
The model can currently handle any tool provided by the developer. That having been said, Cohere has implemented some pre-defined tools that users can leverage out-of-the-box.
Specifically we’re going to roll out a Python interpreter tool and a Web search tool.
Please reach out to join the beta.
Getting started
Check out this notebook for a worked-out examples.
FAQ
What is the difference between tool use and Retrieval Augmented Generation (RAG)?
Tool use is a natural extension of retrieval augmented generation (RAG). RAG is about enabling the model to interact with an information retrieval system (like a vector database). Our models are trained to be excellent at RAG use cases.
Tool use pushes this further, allowing Cohere models to go far beyond information retrieval, interact with search engines, APIs, functions, databases, and many other tools.
If I provide many tools to the model, will the model ignore the tools that aren’t useful for the user message?
- Yes. The model has the ability of assessing the value of a given tool in answering a given query, and will ignore any (and all) tools that don’t serve that purpose.
If I provide many tools to the model, can the model call each tool multiple times?
- Yes, the model may call each tool 0-to-many times.
If I provide tools to the model, can the model decide to not call any tool?
- Yes, the model may return an empty list of
tool_calls
which indicates that no tool call is required. This is common for user queries like greetings, chitchat, out-of-scope request, or safety violations, which do not require calling tools. - The model has a tendency to provide tool suggestions even if they might not be directly relevant to the question. To encourage direct answers to irrelevant questions, we recommend including a sentence in the system message such as: “When a question is irrelevant or unrelated to the available tools, please choose to directly answer it.”
Why is the output of a tool a list of objects?
- Some tools (such as search for example) might produce many different documents (eg: search results). In order for the model to cite the documents individually when generating the response, the output has to be a list of objects. If your tool returns a single object, wrap it in a list. For example:
Are there any other caveats I should be aware of?
- Yes. An important one is that the model may return tool parameters that are invalid, so be sure to give everything a thorough once-over.