Tutorial Pt7 V2
Agents with Tool Use
Tool use extends the ideas from RAG, where external systems are used to guide the response of an LLM, but by leveraging a much bigger set of tools than what’s possible with RAG. The concept of tool use leverages LLMs' useful feature of being able to act as a reasoning and decision-making engine.
While RAG enables applications that can answer questions, tool use enables those that can automate tasks.
Tool use also enables developers to build agentic applications that can take actions, that is, doing both read and write operations on an external system.
In this tutorial, you'll learn about:
- Creating tools
- Tool planning and calling
- Tool execution
- Response and citation generation
- Multi-step tool use
You'll learn these by building an onboarding assistant for new hires.
Setup
To get started, first we need to install the cohere library and create a Cohere client.
Creating 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. For simplicity, we'll not implement any retrieval logic, but we'll simply pass a list of pre-defined documents, which are the FAQ documents we had used in the Text Embeddings section.search_emails: A tool for searching the emails. Same as above, we'll simply pass a list of pre-defined emails from the Reranking section.create_calendar_event: A tool for creating new calendar events. Again, for simplicity, we'll not implement actual event bookings, but will return a mock success event. 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.
The second and final setup step is to define the tool schemas in a format that can be passed to the Chat endpoint. The schema must contain the following fields: name, description, and parameters in the format shown below.
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.
Further reading:
Tool planning and calling
We can now run the tool use workflow. 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: Tool planning and calling – The LLM makes a decision on the tools to call (if any) and generates - the tool calls
- Step 3: Tool execution - The application executes the tools and the results are sent to the LLM
- Step 4: Response and citation generation – The LLM generates the response and citations to back to the user
Tool plan:
I will search the emails for 'IT setup' to find out if there is any message about getting set up with IT.
Tool calls:
Tool name: search_emails | Parameters: {"query":"IT setup"}
Given three tools to choose from, the model is able to pick the right tool (in this case, 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 do ...") before actually generating the tool call(s).
Tool execution
Tool results:
{'type': 'document', 'document': {'data': '{"from": "it@co1t.com", "to": "david@co1t.com", "date": "2024-06-24", "subject": "Setting Up Your IT Needs", "text": "Greetings! To ensure a seamless start, please refer to the attached comprehensive guide, which will assist you in setting up all your work accounts."}'}}
{'type': 'document', 'document': {'data': '{"from": "john@co1t.com", "to": "david@co1t.com", "date": "2024-06-24", "subject": "First Week Check-In", "text": "Hello! I hope you\'re settling in well. Let\'s connect briefly tomorrow to discuss how your first week has been going. Also, make sure to join us for a welcoming lunch this Thursday at noon\\u2014it\'s a great opportunity to get to know your colleagues!"}'}}
Response and citation generation
Response:
Yes, I found an email from the IT department with the subject 'Setting Up Your IT Needs'. It contains a comprehensive guide to help you set up all your work accounts.
==================================================
CITATIONS:
start=13 end=44 text='an email from the IT department' sources=[ToolSource(type='tool', id='search_emails_j10wmwt8pbrz:0', tool_output={'date': '2024-06-24', 'from': 'it@co1t.com', 'subject': 'Setting Up Your IT Needs', 'text': 'Greetings! To ensure a seamless start, please refer to the attached comprehensive guide, which will assist you in setting up all your work accounts.', 'to': 'david@co1t.com'})] content_index=None type='TEXT_CONTENT'
start=62 end=88 text="'Setting Up Your IT Needs'" sources=[ToolSource(type='tool', id='search_emails_j10wmwt8pbrz:0', tool_output={'date': '2024-06-24', 'from': 'it@co1t.com', 'subject': 'Setting Up Your IT Needs', 'text': 'Greetings! To ensure a seamless start, please refer to the attached comprehensive guide, which will assist you in setting up all your work accounts.', 'to': 'david@co1t.com'})] content_index=None type='TEXT_CONTENT'
start=102 end=166 text='a comprehensive guide to help you set up all your work accounts.' sources=[ToolSource(type='tool', id='search_emails_j10wmwt8pbrz:0', tool_output={'date': '2024-06-24', 'from': 'it@co1t.com', 'subject': 'Setting Up Your IT Needs', 'text': 'Greetings! To ensure a seamless start, please refer to the attached comprehensive guide, which will assist you in setting up all your work accounts.', 'to': 'david@co1t.com'})] content_index=None type='TEXT_CONTENT'
Agents
The model can execute more complex tasks in tool use – tasks that require tool calls to happen in a sequence. Agents typically run in this manner.
Let's create a function to called run_assistant to implement these steps, and along the way, print out the key events and messages. Optionally, this function also accepts the chat history as an argument to keep the state in a multi-turn conversation.
To illustrate the concept of multi-step tool user, let's ask the assistant to block time for any lunch invites received in the email.
This requires tasks to happen over multiple steps in a sequence. Here, we see the assistant running these steps:
- First, it calls the
search_emailstool to find any lunch invites, which it found one. - Next, it calls the
create_calendar_eventtool to create an event to block the person's calendar on the day mentioned by the email.
This is also an example of tool use enabling a write operation instead of just a read operation that we saw with RAG.
QUESTION:
Can you check if there are any lunch invites, and for those days, create a one-hour event on my calendar at 12PM.
==================================================
TOOL PLAN:
I will search for lunch invites in the user's emails. Then, I will create a one-hour event on their calendar at 12PM for each day that has a lunch invite.
TOOL CALLS:
Tool name: search_emails | Parameters: {"query":"lunch invites"}
==================================================
TOOL PLAN:
I found a lunch invite for Thursday. I will now create a one-hour event on the user's calendar at 12PM for Thursday.
TOOL CALLS:
Tool name: create_calendar_event | Parameters: {"date":"06/27/24","time":"12:00","duration":1.0}
==================================================
RESPONSE:
I found a lunch invite for Thursday, and have created a one-hour event on your calendar at 12PM for that day.
CITATIONS:
start=27 end=35 text='Thursday' sources=[ToolSource(type='tool', id='search_emails_9rgb5xp8qqcy:1', tool_output={'date': '2024-06-24', 'from': 'john@co1t.com', 'subject': 'First Week Check-In', 'text': "Hello! I hope you're settling in well. Let's connect briefly tomorrow to discuss how your first week has been going. Also, make sure to join us for a welcoming lunch this Thursday at noon—it's a great opportunity to get to know your colleagues!", 'to': 'david@co1t.com'})] content_index=None type='TEXT_CONTENT'
In this tutorial, you learned about:
- How to create tools
- How tool planning and calling happens
- How tool execution happens
- How to generate the response and citations
- How to run tool use in a multi-step scenario
And that concludes our 7-part Cohere tutorial. We hope that they have provided you with a foundational understanding of the Cohere API, the available models and endpoints, and the types of use cases that you can build with them.
To continue your learning, check out: