Notebooks
C
Cohere
Agentic Multi Stage Rag Native

Agentic Multi Stage Rag Native

agentsagentic-RAGnotebookscohere-developer-experience

Agentic Multi-Stage RAG with Cohere Tools API

Motivation

Retrieval augmented generation (RAG) has been a go-to use case that enterprises have been adopting with large language models (LLMs). Even though it works well in general, there are edge cases where this can fail. Most commonly, when the retrieved document mentions the query but actually refers to another document, the model will fail to generate the correct answer.

We propose an agentic RAG system that leverages tool use to continue to retrieve documents if correct ones were not retrieved at first try. This is ideal for use cases where accuracy is a top priority and latency is not. For example, lawyers trying to find the most accurate answer from their contracts are willing to wait a few more seconds to get the answer instead of getting wrong answers fast.

Objective

This notebook, we will explore how we can build a simple agentic RAG using Cohere's native API. We have prepared a fake dataset to demonstrate the use case. We ask three questions that require different depths of retrieval. We will see how the model answers the question between simple and agentic RAG.

Disclaimer

One of the challenges in building a RAG system is that it has many moving pieces: vector database, type of embedding model, use of reranker, number of retrieved documents, chunking strategy, and more. These components can make debugging and evaluating RAG systems difficult. Since this notebook focuses on the concept of agentic RAG, it will simplify other parts of the RAG system. For example, we will only retrieve top 1 docuemnt to demonstrate what happens when retrieved document does not contain the answer needed.

Result

TypeQuestionSimple RagAgentic Rag
Single-stage retrievalIs there a state level law for wearing helmets?There is currently no state law requiring the use of helmets when riding a bicycle. However, some cities and counties do require helmet use.There is currently no state law requiring helmet use. However, some cities and counties do require helmet use with bicycles.
Multi-stage retrievalI live in orting, do I need to wear a helmet with a bike?In the state of Washington, there is no law requiring you to wear a helmet when riding a bike. However, some cities and counties do require helmet use, so it is worth checking your local laws.Yes, you do need to wear a helmet with a bike in Orting if you are under 17.

As you will see more below, the multi-stage retrieval is achieved by adding a new function reference_extractor() that extracts other references in the documents and updating the instruction so the agent continues to retrieve more documents.

[56]
[161]
cohere version: 5.5.1

Setup

[29]

Data

We leveraged data from Washington Department of Transportation and modified to fit the need of this demo.

[279]
[329]

Tools

Following functions and tools will be used in the subsequent tasks.

[220]

RAG function

[237]

Agentic RAG - cohere_agent()

[325]

Question 1 - single-stage retrieval

Here we are asking a question that can be answered easily with single-stage retrieval. Both regular and agentic RAG should be able to answer this question easily. Below is the comparsion of the response.

QuestionSimple RagAgentic Rag
Is there a state level law for wearing helmets?There is currently no state law requiring the use of helmets when riding a bicycle. However, some cities and counties do require helmet use.There is currently no state law requiring helmet use. However, some cities and counties do require helmet use with bicycles.
[179]

Simple RAG

[180]
top_matched_document 1    Title: Bicycle helmet requirement\nBody: Curre...
Name: combined, dtype: object
There is currently no state law requiring the use of helmets when riding a bicycle. However, some cities and counties do require helmet use.

Agentic RAG

[183]

running 0th step.
I will search for 'state level law for wearing helmets' in the documents provided and write an answer based on what I find.

running 1th step.
= running tool retrieve_documents, with parameters: 
{'query': 'state level law for wearing helmets'}
== tool results:
{'top_matched_document': 1    Title: Bicycle helmet requirement\nBody: Curre...
Name: combined, dtype: object}
There is currently no state law requiring helmet use. However, some cities and counties do require helmet use with bicycles.

Question 2 - double-stage retrieval

The second question requires a double-stage retrieval because top matched document references another document. You will see below that the agentic RAG is unable to produce the correct answer initially. But when given proper tools and instructions, it finds the correct answer.

QuestionSimple RagAgentic Rag
I live in orting, do I need to wear a helmet with a bike?In the state of Washington, there is no law requiring you to wear a helmet when riding a bike. However, some cities and counties do require helmet use, so it is worth checking your local laws.Yes, you do need to wear a helmet with a bike in Orting if you are under 17.
[188]

Simple RAG

[189]
top_matched_document 1    Title: Bicycle helmet requirement\nBody: Curre...
Name: combined, dtype: object
In the state of Washington, there is no law requiring you to wear a helmet when riding a bike. However, some cities and counties do require helmet use, so it is worth checking your local laws.

Agentic RAG

Produces same quality answer as the simple rag.

[190]

running 0th step.
I will search for 'helmet with a bike' and then write an answer.

running 1th step.
= running tool retrieve_documents, with parameters: 
{'query': 'helmet with a bike'}
== tool results:
{'top_matched_document': 1    Title: Bicycle helmet requirement\nBody: Curre...
Name: combined, dtype: object}
There is no state law requiring helmet use, however, some cities and counties do require helmet use with bicycles. I cannot find any information about Orting specifically, but you should check with your local authority.

Agentic RAG - New Tools

In order for the model to retrieve correct documents, we do two things:

  1. New reference_extractor() function is added. This function finds the references to other documents when given query and documents.
  2. We update the instruction that directs the agent to keep retrieving relevant documents.
[247]
[249]

running 0th step.
I will search for 'Orting' and 'bike helmet' to find the relevant information.

running 1th step.
= running tool retrieve_documents, with parameters: 
{'query': 'Orting bike helmet'}
== tool results:
{'other_references_to_query': 'Section 21a, Section 3b',
 'top_matched_document': 0    Title: Bicycle law\nBody: \n        Riding on ...
Name: combined, dtype: object}
I have found that there is no state law requiring helmet use, but some cities and counties do require helmets. I will now search for 'Section 21a' to find out if Orting is one of these cities or counties.

running 2th step.
= running tool retrieve_documents, with parameters: 
{'query': 'Section 21a'}
== tool results:
{'other_references_to_query': '- Section 3b',
 'top_matched_document': 2    Title: Section 21a\nBody: helmet rules by loca...
Name: combined, dtype: object}
Yes, you do need to wear a helmet when riding a bike in Orting if you are under 17.