Skip to content

Commit

Permalink
fix typos and folder location - agentic rag (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmer1 authored Oct 16, 2024
1 parent 94ab719 commit 70beb72
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Imagine a RAG system that can search over a diverse set of sources, such as a website, a database, and a set of documents.\n",
"Imagine a RAG system that can search over diverse sources, such as a website, a database, and a set of documents.\n",
"\n",
"In a standard RAG setting, the application would aggregate retrieved documents from all the different sources it is connected to. This may contribute to noise from less relevant documents.\n",
"\n",
"Additionally, it doesn’t take into consideration that, given a data source's nature, it might be less or more relevant to a query compared to the other data sources.\n",
"Additionally, it doesn’t take into consideration that, given a data source's nature, it might be less or more relevant to a query than the other data sources.\n",
"\n",
"An agentic RAG system can solve this problem by routing queries to the most relevant tools based on the query's nature. This is done by leveraging the tool use capabilities of the Chat endpoint.\n",
"\n",
"In this tutorial, you'll learn about:\n",
"In this tutorial, we'll cover:\n",
"- Setting up the tools\n",
"- Running an agentic RAG workflow\n",
"- Routing queries to tools\n",
"\n",
"You'll learn these by building an agent that answers questions about using Cohere."
"We'll build an agent that can answer questions about using Cohere, equipped with a number of different tools."
]
},
{
Expand Down Expand Up @@ -78,23 +78,17 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In an agentic RAG system, each data source is represented as a \"tool\". A tool is broadly any function or service that can receive and send objects to the LLM. But in the case of RAG, this becomes a more specific case of a tool that takes a query as input and return a set of documents.\n",
"In an agentic RAG system, each data source is represented as a tool. A tool is broadly any function or service that can receive and send objects to the LLM. But in the case of RAG, this becomes a more specific case of a tool that takes a query as input and returns a set of documents.\n",
"\n",
"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.\n",
"- `search_developer_docs`: Searches Cohere developer documentation. Here we are creating a small list of sample documents for simplicity and will return the same list for every query. In practice, you will want to implement a search function as such those that use semantic search.\n",
"- `search_code_examples`: Searches for Cohere code examples and tutorials. Here we are also creating a small list of sample documents for simplicity.\n",
"- `search_developer_docs`: Searches Cohere developer documentation. Here we are creating a small list of sample documents for simplicity and will return the same list for every query. In practice, you will want to implement a search function such as those that use semantic search.\n",
"- `search_internet`: Performs an internet search using Tavily search, which we take from LangChain's ready implementation.\n",
"- `search_code_examples`: Searches for Cohere code examples and tutorials. Here we are also creating a small list of sample documents for simplicity.\n",
"\n",
"These functions are mapped to a dictionary called `functions_map` for easy access.\n",
"\n",
"These functions are mapped to a dictionary called functions_map for easy access.\n",
"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.\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Further reading:\n",
"- [Documentation on parameter types in tool use](https://docs.cohere.com/v2/docs/parameter-types-in-tool-use)"
]
Expand All @@ -116,9 +110,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"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. \n",
"The second and final setup step is to define the tool schemas in a format that can be passed to the Chat endpoint. A tool schema must contain the following fields: `name`, `description`, and `parameters` in the format shown below. \n",
"\n",
"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."
"This schema informs the LLM about what the tool does, which enables an LLM to decide whether to use a particular tool. Therefore, the more descriptive and specific the schema, the more likely the LLM will make the right tool call decisions."
]
},
{
Expand All @@ -141,7 +135,7 @@
"At its most basic, these four components interact in a workflow through four steps:\n",
"- **Step 1: Get user message** – The LLM gets the user message (via the application)\n",
"- **Step 2: Tool planning and calling** – The LLM makes a decision on the tools to call (if any) and generates - the tool calls\n",
"- **Step 3: Tool execution** - The application executes the tools and the results are sent to the LLM\n",
"- **Step 3: Tool execution** - The application executes the tools and the sends the results to the LLM\n",
"- **Step 4: Response and citation generation** – The LLM generates the response and citations to back to the user\n",
"\n",
"We wrap all these steps in a function called `run_agent`."
Expand Down Expand Up @@ -265,9 +259,9 @@
"\n",
"Because of question asks about a specific feature, the agent decides to use the `search_developer_docs` tool (instead of retrieving from all the data sources it's connected to).\n",
"\n",
"It first generates a tool plan that describes how it will handle the query. Then it generates tool calls to the `search_developer_docs` tool with the associated `query` parameter.\n",
"It first generates a tool plan that describes how it will handle the query. Then, it generates tool calls to the `search_developer_docs` tool with the associated `query` parameter.\n",
"\n",
"And the tool does indeed contain the information asked by the user, which the agent then uses to generate its response."
"The tool does indeed contain the information asked by the user, which the agent then uses to generate its response."
]
},
{
Expand Down Expand Up @@ -309,7 +303,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's now ask the agent a question about Cohere's co-founders. This information is not likely to be found in the developer documentation or code examples, so we can expect the agent to use the internet search tool.\n",
"Let's now ask the agent a question about the authors of the sentence BERT paper. This information is not likely to be found in the developer documentation or code examples because it is not Cohere-specific, so we can expect the agent to use the internet search tool.\n",
"\n",
"And this is exactly what the agent does. This time, it decides to use the `search_internet` tool, triggers the search through Tavily search, and uses the results to generate its response."
]
Expand Down Expand Up @@ -372,9 +366,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's ask a final question to the agent, this time about tutorials that are relevant for enterprises.\n",
"Let's ask the agent a final question, this time about tutorials that are relevant for enterprises.\n",
"\n",
"An again, the agent uses the context of the query to decide on the most relevant tool to use. In this case, it selects the `search_code_examples` tool and provides a response based on the information found."
"Again, the agent uses the context of the query to decide on the most relevant tool. In this case, it selects the `search_code_examples` tool and provides a response based on the information found."
]
},
{
Expand Down Expand Up @@ -418,14 +412,14 @@
"source": [
"## Summary\n",
"\n",
"In this tutorial, you learned about:\n",
"In this tutorial, we learned about:\n",
"- How to set up tools in an agentic RAG system\n",
"- How to run an agentic RAG workflow\n",
"- How to automatically route queries to the most relevant data sources\n",
"\n",
"However, so far we have only seen rather simple queries. In practice, we may run into a multi-faceted query that needs to simplified, optimized, or split (etc.) before we can perform the retrieval.\n",
"However, so far we have only seen rather simple queries. In practice, we may run into a complex query that needs to simplified, optimized, or split (etc.) before we can perform the retrieval.\n",
"\n",
"In Part 2, you will learn how the agentic RAG system can handle multi-faceted queries."
"In Part 2, we'll learn how to build an agentic RAG system that can expand user queries into parallel queries."
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@
"source": [
"Compare two user queries to a RAG chatbot, \"What was Apple's revenue in 2023?\" and \"What were Apple's and Google's revenue in 2023?\".\n",
"\n",
"The first query is straightforward as you can perform retrieval using pretty much the same query you get.\n",
"The first query is straightforward as we can perform retrieval using pretty much the same query we get.\n",
"\n",
"But the second query is more complex. We need to break it down into two separate queries, one for Apple and one for Google.\n",
"\n",
"This is an example that requires parallel query execution. Here, the agentic RAG will need to turn or expand the query it gets from the user into a more optimized set of queries it should use to perform the retrieval.\n",
"This is an example that requires query expansion. Here, the agentic RAG will need to turn or expand the query into a more optimized set of queries it should use to perform the retrieval.\n",
"\n",
"In this part, you will learn how agentic RAG with Cohere handles parallel query execution.\n",
"In this part, we'll learn how to create an agentic RAG system that can perform query expansion and then run those queries in parallel:\n",
"- Query expansion\n",
"- Query expansion over multiple data sources\n",
"- Query expansion in multi-turn conversations\n",
"\n",
"\n",
"You'll learn these by building an agent that answers questions about using Cohere."
"We'll learn these by building an agent that answers questions about using Cohere."
]
},
{
Expand Down Expand Up @@ -227,14 +226,13 @@
"\n",
"Firstly, the agent rightly chooses the `search_developer_docs` tool to retrieve the information it needs.\n",
"\n",
"Additionally, because the question asks about two different things, retrieving information using the same query as the user's may not be the most optimal. Instead, the query needs to be expanded, or split into multiple parts, each retrieving its own set of documents.\n",
"Additionally, because the question asks about two different things, retrieving information using the same query as the user's may not be the most optimal approach. Instead, the query needs to be expanded or split into multiple parts, each retrieving its own set of documents.\n",
"\n",
"Thus, the agent expands the original query into two queries.\n",
"\n",
"This is enabled by the parallel tool calling feature that comes with the Chat endpoint.\n",
"\n",
"This results in a richer and more representative list of documents retrieved, and therefore a more accurate and comprehensive answer.\n",
"\n"
"This results in a richer and more representative list of documents retrieved, and therefore a more accurate and comprehensive answer."
]
},
{
Expand Down Expand Up @@ -428,9 +426,9 @@
"\n",
"For example, in the first turn, a user might ask \"What is A\" and in the second turn, they might ask \"Compare that with B and C\". So, the agent needs to be able to infer that the user's intent is to compare A with B and C.\n",
"\n",
"Let's see an example of this. First, note that the `run_agent` function is already enabled to handle multi-turn conversations. It can take messages from the previous conversation turns and append them to the `messages` list.\n",
"Let's see an example of this. First, note that the `run_agent` function is already set up to handle multi-turn conversations. It can take messages from the previous conversation turns and append them to the `messages` list.\n",
"\n",
"So in the first turn, the user asks about the Chat endpoint, which the agent duly provides a response to."
"In the first turn, the user asks about the Chat endpoint, to which the agent duly responds."
]
},
{
Expand Down Expand Up @@ -472,13 +470,13 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In the second turn, the user asks a question that contains two parts: first asking how it's different from RAG and then asking for code examples.\n",
"In the second turn, the user asks a question that has two parts: first, how it's different from RAG, and then, for code examples.\n",
"\n",
"We pass the messages from the previous conversation turn to the `run_agent` function.\n",
"\n",
"Because of this, the agent is able to infer that the question is referring to the Chat endpoint even though the user didn't explicitly mention it.\n",
"\n",
"And the agent goes on to expand the query into two separate queries, one for the `search_code_examples` tool and one for the `search_developer_docs` tool."
"The agent then expands the query into two separate queries, one for the `search_code_examples` tool and one for the `search_developer_docs` tool."
]
},
{
Expand Down Expand Up @@ -572,22 +570,15 @@
"source": [
"## Summary\n",
"\n",
"In this tutorial, you learned about:\n",
"In this tutorial, we learned about:\n",
"- How query expansion works in an agentic RAG system\n",
"- How query expansion works over multiple data sources\n",
"- How query expansion works in multi-turn conversations\n",
"\n",
"Having said that, we may encounter even more complex queries that what we've seen so far. In particular, there are queries that require sequential reasoning where the retrieval needs to happen over multiple steps.\n",
"Having said that, we may encounter even more complex queries that what we've seen so far. In particular, some queries require sequential reasoning where the retrieval needs to happen over multiple steps.\n",
"\n",
"In Part 3, you will learn how the agentic RAG system can perform sequential reasoning."
"In Part 3, we'll learn how the agentic RAG system can perform sequential reasoning."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 70beb72

Please sign in to comment.