Tool Use & Agents
Tool use enhances retrieval-augmented generation (RAG) capabilities by enabling applications to both answer questions and automate tasks.
Tools provide a broader access to external systems compared to traditional RAG. This approach leverages LLMs’ inherent ability to reason and make decisions. By incorporating tools, developers can create agent-like applications that interact with external systems through both read and write operations.
In this chapter, we’ll explore how to build an agentic application by building an agent that can answer questions and automate tasks, enabled by a number of tools.
Setup
First, you will need to deploy the Command model on Azure via Azure AI Foundry. The deployment will create a serverless API with pay-as-you-go token based billing. You can find more information on how to deploy models in the Azure documentation.
In the example below, we are deploying the Command R+ (August 2024) model.
Once the model is deployed, you can access it via Cohere’s Python SDK. Let’s now install the Cohere SDK and set up our client.
To create a client, you need to provide the API key and the model’s base URL for the Azure endpoint. You can get these information from the Azure AI Foundry platform where you deployed the model.
Create tools
The pre-requisite, before we can run a tool use workflow, is to set up the tools. Let’s create three tools:
search_faqs
: A tool for searching the FAQs of a company. For simplicity, we’ll not implement any retrieval logic, but we’ll simply pass a list of three predefined documents. In practice, we would set up a retrieval system as we did in Chapters 4, 5, and 6.search_emails
: A tool for searching the emails. Same as above, we’ll simply pass a list of predefined emails.create_calendar_event
: A tool for creating new calendar events. Again, for simplicity, we’ll only return mock successful event creations without actual implementation. In practice, we can connect to a calendar service API and implement all the necessary logic here.
Here, we are defining a Python function for each tool, but more broadly, the tool can be any function or service that can receive and send objects.
Define tool schemas
The next step is to define the tool schemas in a format that can be accepted by the Chat endpoint. The schema must contain the following fields: name
, description
, and parameter_definitions
.
This schema informs the LLM about what the tool does, and the LLM decides whether to use a particular tool based on it. Therefore, the more descriptive and specific the schema, the more likely the LLM will make the right tool call decisions.
Run agent
Now, let’s set up the agent using Cohere’s tool use feature. We can think of a tool use system as consisting of four components:
- The user
- The application
- The LLM
- The tools
At its most basic, these four components interact in a workflow through four steps:
- Step 1: Get user message. The LLM gets the user message (via the application).
- Step 2: Generate tool calls. The LLM makes a decision on the tools to call (if any) and generates the tool calls.
- Step 3: Get tool results. The application executes the tools and sends the tool results to the LLM.
- Step 4: Generate response and citations. The LLM generates the response and citations and sends them back to the user.
Let’s create a function called run_assistant
to implement these steps and print out the key events and messages along the way. This function also optionally accepts the chat history as an argument to keep the state in a multi-turn conversation.
Let’s now run the agent. We’ll use an example of a new hire asking about IT access and the travel expense process.
Given three tools to choose from, the model is able to pick the right tools (in this case, search_faqs
and search_emails
) based on what the user is asking for.
Also, notice that the model first generates a plan about what it should do (“I will …”) before actually generating the tool call(s).
Additionally, the model also generates fine-grained citations in tool use mode based on the tool results it receives, the same way we saw with RAG.
Conclusion
In this tutorial, we learned about:
- How to set up tools with parameter definitions for the Cohere chat API
- How to define tools for building agentic applications
- How to set up the agent
- How to run a tool use workflow involving the user, the application, the LLM, and the tools