How Does Single-Step Tool Use Work?
How Does Single-Step Tool Use Work?
How Does Single-Step Tool Use Work?
Tool use is a technique which allows developers to connect Cohere’s Command family of models to external tools like search engines, APIs, functions, databases, etc. It comes in two variants, single-step and multi-step, and this doc will focus on single-step tool use.
Single-step tool use enables 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. To use it, set force_single_step=True when creating the model.
This is particularly valuable for enterprise developers, since a lot of enterprise data lives in external sources.
Check out this notebook for worked-out examples.
Single-step tool use (or “function calling”) opens up a wide range of new use cases. Below, we walk through a few examples.
It’s now possible to reliably ask the model to recommend a tool (or set of tools) to use and offer advice on how to use them, which you can pass back to the model for more flexible workflows. Tool use allows your chatbot to interact with your CRM to change the status of a deal, for example, or to engage with a Python interpreter to conduct data science analysis.
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.
For instance, this enables your work assistant to automatically search across different databases and platforms to retrieve relevant information or to conduct comparative analysis.
Single-step tool use allows developers to tell Command R which tools it can interact with and how to structure interactions (e.g. API requests, or anything that can be formatted in JSON). Command R then dynamically selects the right tools and the right parameters for 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 model.
We want to stress that it’s the developers executing tool calls and submitting final results to Command R.
Here’s a graphic that represents the four steps discussed below:
Feel free to refer back to it as you read on.
In order to begin a single-step tool use workflow, a developer must provide a few things:
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).
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.
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.
Finally, the developer calls the Cohere model, providing the tool results, in order to generate the model’s final answer. Cohere makes it easy to provide tool results back to the model through the tool_results parameter (demonstrated in code snippets below).
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 single-step tool use workflow would look like.
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.
The model’s response contains the list of appropriate tools to call in order to answer the user’s question, as well as the appropriate inputs for each tool call.
Now, the developer will query the appropriate tools and receive a tool result in return.
Call the chat endpoint again with the tool results to get the final model answer. Note that this is done through the tool_results parameter, with the other parameters operating as expected.
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.
At Cohere, we care about building responsible, useful, and factually-accurate models.
For this reason, Cohere single-step 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 single-step tool use; they help users gain visibility into the model reasoning, as well as sanity check the final model generation.
Developers can control the granularity of these citations. Simply split tool results into multiple tool result objects (tool_results accepts lists). The language model will then cite tool results at the specified level of granularity.
There may be times when a single-step tool use query doesn’t turn up a tool that will help answer the question. In those scenarios the model will return an empty list of tool_calls, along with an empty string in the text field. In that case, call the Chat API again with an empty list of tool_results
During the tool calling step, the model may decide to either:
You can force the model to make tool call(s), i.e. to not respond directly, by setting the force_single_step=True and providing some tool definitions through the tools parameter.
This is equivalent to setting the tool_choice as REQUIRED in the v2 API.
Besides, you can force the model to respond directly, by setting force_single_step=True and by providing some tool results through the tool_results parameter. This is equivalent to specifying tool_choice as NONE in the v2 API.
Single-step tool use functions as part of a two-part conversational process. Here’s how that works:
chat_history parameter.chat_history.This is valuable for scenarios where the user and chatbot need multiple messages to achieve a task, as those are situations in which the model needs to remember and reference previous messages. This continuity in interactions between a user and a conversational algorithm results in nuanced and helpful responses. For example, it allows users to make edit requests, make follow up requests to the chatbot, and to disambiguate co-references across multiple messages.
Suppose a user asks for how much of a given product is in stock in a warehouse, and then asks, “When was the last purchase of this product?” The chatbot needs to understand what “this product” refers to, which it can puzzle out based on the chat history. In short, it would identify the product that was discussed previously to make the relevant tool calls before providing a response.
To get good answers with single-step 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:
Check out this notebook for a worked-out examples.
You should operate in single-step mode when you want the model to answer a question after one step. With a query like “Is there a reservation available at Johnny’s Pizza or Alfredo’s Pizza at 9PM,” for example, the model would query the APIs for Johny’s Pizza and Alfredo’s Pizza, in parallel.
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.Are there any other caveats I should be aware of?