From 2a3370f8f06ee57801131a4fda6a1a69e7613f6b Mon Sep 17 00:00:00 2001 From: "felix.bucsa" <72919584+FelixNicolaeBucsa@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:24:25 +0100 Subject: [PATCH] fix(docs): reviewed rag agents guide and made minor changes (#1056) Co-authored-by: Joshua Croft <32483134+devjsc@users.noreply.github.com> --- .../intermediate/langchain-rag-agent.mdx | 170 +++++++++--------- 1 file changed, 86 insertions(+), 84 deletions(-) diff --git a/pages/guides/agents/intermediate/langchain-rag-agent.mdx b/pages/guides/agents/intermediate/langchain-rag-agent.mdx index b0f2dea1..026d80ae 100644 --- a/pages/guides/agents/intermediate/langchain-rag-agent.mdx +++ b/pages/guides/agents/intermediate/langchain-rag-agent.mdx @@ -2,13 +2,13 @@ import {Callout} from 'nextra/components' import PackageVersion from 'components/package-version' import { CodeGroup, CodeSegment, DocsCode, GithubCodeSegment } from "../../../../components/code"; -# Build a LangChain RAG Agent +# Build a RAG Agent with uAgents and Langchain ## Introduction -In this guide, we'll walk through how to create agents capable of answering questions based on any provided document using the Fetch.ai uAgents and ai_engine python libraries as well as openai and cohere. The aim is to assists you in building a **LangChain Retrieval-Augmented Generation (RAG) Agent**! +In this guide, we'll walk through how to create agents capable of answering questions based on any provided document using the Fetch.ai uAgents and AI Engine Python libraries as well as OpenAI and Cohere. The aim is to assist you in building a **LangChain Retrieval-Augmented Generation (RAG) Agent**! -The process of creating RAG agents has been improved by offering a decentralized and modular framework through the uAgents and AI Engine libraries. These tools streamline the integration of AI models like OpenAI and Cohere, enabling more efficient and scalable development of intelligent agents. With enhanced interoperability, security, and resource management, this framework allows developers to quickly build and deploy sophisticated agents that can effectively answer questions based on any provided document, making the entire process faster and more robust. +The uAgents and AI Engine libraries offer a decentralized and modular framework for creating RAG agents, improving the process of creating them. These tools streamline the integration of AI models like OpenAI and Cohere, enabling more efficient and scalable development of intelligent agents. With enhanced interoperability, security, and resource management, this framework allows developers to quickly build and deploy sophisticated agents that can effectively answer questions based on any provided document, making the entire process faster and more robust. Check out the [AI Engine package ↗️](https://pypi.org/project/uagents-ai-engine/) and [uAgents ↗️](https://pypi.org/project/uagents/) packages to download them and start integrating this tools within your Agents project! @@ -23,12 +23,12 @@ Let's dive into the LangChain RAG Agents development! Make sure you have read the following resources before going on with this guide: - - [Creating an agent ↗️](/guides/agents/create-a-uagent) + - [Creating an Agent ↗️](/guides/agents/create-a-uagent) - [Creating an interval task ↗️](/guides/agents/interval-task) - - [Communicating with other agents ↗️](/guides/agents/communicating-with-other-agents) - - [Agent Handlers ↗️](/guides/agents/intermediate/handlers) - - [Register in Almanac ↗️](/guides/agents/register-in-almanac) + - [Communicating with other Agents ↗️](/guides/agents/communicating-with-other-agents) + - [Agent handlers ↗️](/guides/agents/intermediate/handlers) - [Almanac Contract ↗️](/references/contracts/uagents-almanac/almanac-overview) + - [Register in Almanac ↗️](/guides/agents/register-in-almanac) - [Mailbox ↗️](/guides/agents/intermediate/mailbox) - [Utilising the Agentverse Mailroom service ↗️](/guides/agentverse/utilising-the-mailbox) - [Protocols ↗️](/references/uagents/uagents-protocols/agent-protocols) @@ -37,7 +37,12 @@ Make sure you have read the following resources before going on with this guide: ## API KEYs -Importantly, you will need two API keys to correctly go through this guide: one from **OpenAI** and one from **Cohere**. Follow the steps provided below to obtain these keys: +You will need **two API keys** to correctly go through this guide: + + 1. One from **OpenAI** + 2. One from **Cohere**. + +Follow the steps provided below to obtain them: ### OpenAI API Key @@ -51,7 +56,7 @@ Importantly, you will need two API keys to correctly go through this guide: one 1. Visit the [Cohere website ↗️](https://dashboard.cohere.com/). 2. Sign up or log in to your account. 3. Go to the **API Keys** section. -4. Copy an existing key or create a new one.: one from OpenAI and one from Cohere. Follow the steps below to obtain these keys. +4. Copy an existing key or create a new one. ## Project structure and overview @@ -75,9 +80,9 @@ The project is structured as follows: The source code directory (`src`) contains the following directories and files: - - `agents`: Contains scripts for the LangChain agents. - - `langchain_rag_agent.py`: Script for the RAG agent (retrieves info, finds documents, generates answers). - - langchain_rag_user.py: Script for the User agent (asks questions, handles responses). + - `agents`: contains the scripts for the LangChain Agents. + - `langchain_rag_agent.py`: it is the script for the RAG agent (retrieves info, finds documents, generates answers). + - `langchain_rag_user.py`: Script for the User agent (asks questions, handles responses). - `main.py`: Starts both the RAG and user agents. - `messages`: Defines custom message models. - `requests.py`: Defines the RagRequest message model (question, URL, optional deep read). @@ -97,7 +102,7 @@ To do this, navigate to the `src` directory. Here, create and source the `.env` ### Project dependencies -You'll need several Python packages for the project. These can be managed efficiently using [Poetry ↗️](https://python-poetry.org/). The following dependencies are needed for the correct development of the Langchain RAG Agent project: +You'll need several Python packages for the project. These can be managed efficiently using [Poetry ↗️](https://python-poetry.org/). The following dependencies are required for the correct development of the Langchain RAG Agent project: ``` [tool.poetry.dependencies] @@ -119,7 +124,7 @@ You'll need several Python packages for the project. These can be managed effici ### RagRequest Model -We now need to define the `requests.py` file under the `messages` folder in the project. +We now need to define the `requests.py` file under the `messages` folder in the project: @@ -141,7 +146,7 @@ We now need to define the `requests.py` file under the `messages` folder in the -The script look like the following: +The script looks like the following: - -```py copy filename="mac" -touch langchain_rag_agent.py -``` - +Let's start by creating the file within the `agents` directory, under `src` directory of our project, and name it `langchain_rag_agent.py` by using the following command: - - ```py copy filename="windows" - echo. > langchain_rag_agent.py - ``` + + + ```py copy filename="mac" + touch langchain_rag_agent.py + ``` - - - ```py copy filename="ubuntu" - touch langchain_rag_agent.py - ``` + + + ```py copy filename="windows" + echo. > langchain_rag_agent.py + ``` - + + + ```py copy filename="ubuntu" + touch langchain_rag_agent.py + ``` + + -The agent is defined as a [local agent with a Mailbox ↗️](https://fetch.ai/docs/guides/agents/intermediate/options-for-running-local-agents#run-a-local-agent-using-a-mailbox) and it is able to answer questions by fetching and summarizing information from a given website URL. +The Agent is defined as a [local agent with a Mailbox ↗️](https://fetch.ai/docs/guides/agents/intermediate/options-for-running-local-agents#run-a-local-agent-using-a-mailbox) and it can answer questions by fetching and summarizing information from a given website URL. -The script for this agent looks as follows: +The script for this Agent looks as follows: + Here, we create the User Agent which interacts with the LangChain RAG Agent we previously defined. The User Agent periodically asks the RAG Agent a predefined question and then handles the response. + After importing all required modules and classes, a specific `QUESTION` is provided: `"How to install uagents using pip"`. We also provide the `URL` for the web page where the relevant information can be found to answer the question. The `DEEP_READ` variable is set to `no`; this indicates that the gent should only read the main page. - Remember that you need to provide the `QUESTION`, `URL`, `DEEP_READ`, `RAG_AGENT_ADDRESS`, `name`, `seed` and `endpoint` parameters to correctly run this code. - - Here, we have created the User Agent which interacts with the LangChain RAG Agent we previously defined. The User Agent periodically asks the RAG Agent a predefined question and then handles the response. - - After importing all required modules and classes, a specific `QUESTION` is provided: `"How to install uagents using pip"`. We also provide the `URL` for the webpage where the relevant information can be found to answer the question. The `DEEP_READ` variable is set to `no` thus indicating that the agent should only read the main page and not follow and read any nested or linked pages. - - The `RAG_AGENT_ADDRESS` variable holds the address of the RAG agent, which is responsible for retrieving and processing the webpage content to answer the user's question. + The `RAG_AGENT_ADDRESS` variable holds the address of the RAG agent responsible of retrieving and processing the web page content to answer the user's question. - We are now ready to define the User Agent. We create the `langchain_rag_user` is created with a specific `port` and `endpoint`, which is where it will communicate with the RAG Agent. The agent's wallet is funded if needed using the `fund_agent_if_low()` function. + We can then define the User Agent. We create the `langchain_rag_user` with a specific `port` and `endpoint`; this last one is essential to allow for communication with the RAG Agent. **Make sure your Agent has enough funds to register in the Almanac contract**. On the Testnet, you can use the `fund_agent_if_low()` function to fund your Agent's wallet if needed. - We then proceed and define the [protocol ↗️](/references/uagents/uagents-protocols/agent-protocols). A protocol named `LangChain RAG user` is defined using the `Protocol` class of the `uagents` library to handle the agent's interactions. This protocol contains two important functions: + We then proceed and define the [protocol ↗️](/references/uagents/uagents-protocols/agent-protocols). A protocol named `LangChain RAG user` is created using the `Protocol` class from the `uagents` library to handle the Agent's interactions. This protocol contains two important functions: - 1. The `ask_question()` function is decorated with the `.on_interval()` [handler ↗️](/guides/agents/intermediate/handlers) and it is set to run at 60-second intervals. This function sends a `RagRequest` message to the LangChain RAG agent, asking it to answer the `QUESTION` based on the specified `URL`. It then logs the `QUESTION` and `URL` details for debugging purposes. + 1. The `ask_question()` function, which is decorated with the `.on_interval()` [handler ↗️](/guides/agents/intermediate/handlers) and which is set to run at 60-second intervals. This function sends a `RagRequest` message to the LangChain RAG Agent, asking it to answer the `QUESTION` based on the specified `URL`. It then logs the `QUESTION` and `URL` details for debugging purposes. 2. The `handle_data()` function is decorated with the `.on_message()` handler and it handles incoming messages from the LangChain RAG agent. When a response is received, it logs the response message. -Finally, the LangChain RAG user protocol is then included using the `.include()` method into the agent which is then run by calling `rag_user.run()` in the main block. + Finally, the LangChain RAG user protocol is included using the `.include()` method into the Agent which is then run by calling `rag_user.run()` in the main block. -## Main script + **Remember to provide the `QUESTION`, `URL`, `DEEP_READ`, `RAG_AGENT_ADDRESS`, `name`, `seed` and `endpoint` parameters to correctly run this code.** -We are now ready to define the main script for our project. In the `src` folder of our project we create a Python script named `main.py` using the following command: +## Main script - - -```py copy filename="mac" -touch main.py -``` - +We are now ready to define the main script for our project. In the `src` folder of the project, we create a Python script named `main.py` using the following command: - - ```py copy filename="windows" - echo. > main.py - ``` + + + ```py copy filename="mac" + touch main.py + ``` - - - ```py copy filename="ubuntu" - touch main.py - ``` + + + ```py copy filename="windows" + echo. > main.py + ``` - + + + ```py copy filename="ubuntu" + touch main.py + ``` + + + +We can now write the code for it. -We then define the code within this one which looks like the one provided here below: +It will look as follows: +Cool! All scripts are now set up! You will need to [connect the local Agent to the Agentverse using a Mailbox ↗️](/guides/agents/intermediate/options-for-running-local-agents#run-a-local-agent-using-a-mailbox). The Mailbox enables your Agents to be retrievable for communication and interaction with any other registered Agent on the Almanac contract and Fetch Network. In this example, the `langchain_rag_agent` will be connected to the Agentverse using a [Mailbox ↗️](/guides/agents/intermediate/mailbox). The `langchain_rag_user` Agent is used as a testing agent for the RAG Agent being registered on the Agentverse and made subsequently available on DeltaV for queries. +By creating an [Agent Function ↗️](https://fetch.ai/docs/guides/agents/intermediate/agent-functions) and [registering it ↗️](https://fetch.ai/docs/guides/agentverse/agentverse-functions/registering-agent-services) via the Agentverse, you will make it retrievable to the [AI Engine ↗️](/concepts/ai-engine/ai-engine-intro) for interaction with users via natural language queries made on [DeltaV ↗️](/concepts/ai-engine/deltav). -Now, both of agents are set up, and you will need to **connect your local agents to the Agentverse** so for them to be retrievable for communication and interaction with any other registered agent on the Fetch Network. This way, the `langchain_rag_agent` will be connected to the Agentverse using a [Mailbox ↗️](/guides/agents/intermediate/mailbox) and afterward a [Agent Function ↗️](https://fetch.ai/docs/guides/agents/intermediate/agent-functions) will be created and [registered ↗️](https://fetch.ai/docs/guides/agentverse/agentverse-functions/registering-agent-services) for this agent. - -The `langchain_rag_user` agent is used as a testing agent for the RAG agent being registered on the Agentverse and made subsequently available on DeltaV for queries. - - - Head over to the following [guide ↗️](/guides/agents/intermediate/options-for-running-local-agents#run-a-local-agent-using-a-mailbox) for a better understanding of local agent registration on the Agentverse. - +The `langchain_rag_user` Agent is used as a testing agent for the RAG Agent being registered on the Agentverse and made subsequently available on DeltaV for queries. ### Expected output -The expected output for this example should be similar to the following one where we questioned the **RAG Agent** with the question `How to install uagents using pip` from the **RAG user** agent: +The expected output for this example should be similar to the following one. Here, we questioned the **RAG Agent** with the question `How to install uagents using pip` from the **RAG User** Agent: ![](src/images/examples/rag_deltav_response.png) -