Skip to content

Commit

Permalink
feat(docs): add langchain and cohere overview (#1067)
Browse files Browse the repository at this point in the history
Co-authored-by: Felix Nicolae Bucsa <[email protected]>
  • Loading branch information
gautamgambhir97 and FelixNicolaeBucsa authored Nov 26, 2024
1 parent e929acd commit 9e947da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/spelling/known_words_corpus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -747,4 +747,5 @@ xyz
ylabel
yml
yscale
zyx
zyx
nlp
48 changes: 35 additions & 13 deletions pages/guides/agents/intermediate/langchain-rag-agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import { CodeGroup, CodeSegment, DocsCode, GithubCodeSegment } from "../../../..

## 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 assist you in building a **LangChain Retrieval-Augmented Generation (RAG) Agent**!
In this guide, we'll walk through how to create [Agents ↗️](/guides/agents/getting-started/whats-an-agent) 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 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.
<Callout type="info" emoji="ℹ️">
Also, rememeber that you can have a look and eventually download the source code used within this guide from Github [here ↗️](https://github.com/fetchai/uAgent-Examples/tree/main/1-uagents/examples/intermediate/langchain-rag/src).
</Callout>

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.

<Callout type="info" emoji="ℹ️">
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!
Expand All @@ -17,9 +21,6 @@ The uAgents and AI Engine libraries offer a decentralized and modular framework
Current version of the uAgents package is <PackageVersion packageName="uagents" packageType="pypi" />.
</Callout>

<Callout type="info" emoji="ℹ️">
Also, rememeber that you can have a look and eventually download the source code used within this guide from Github [here ↗️](https://github.com/fetchai/uAgent-Examples/tree/main/1-uagents/examples/intermediate/langchain-rag/src).
</Callout>

**Let's dive into the LangChain RAG Agents development!**

Expand All @@ -39,6 +40,27 @@ Make sure you have read the following resources before going on with this guide:
- [Agentverse Functions ↗️](/guides/agents/intermediate/agent-functions)
- [Register an Agent Function on the Agentverse ↗️](/guides/agentverse/agentverse-functions/registering-agent-services)

### LangChain and Cohere Overview

#### LangChain

In the context of a RAG agent, LangChain helps by:

- **Document Loading and Parsing**: it processes and structures data from web pages.
- **Vector Storage**: LangChain indexes documents into vectors, making it easier to perform similarity searches.
- **Question Answering**: it generates answers by querying the indexed data, providing context-aware responses.

#### Cohere

Cohere offers **advanced NLP models** that assist in refining the answers generated by LangChain:

- **Contextual Compression**: it filters and prioritizes data based on relevance, ensuring that the Agent focuses on the most important information.
- **Enhanced Accuracy**: Cohere's NLP models improve the quality of the generated answers by understanding the context of the query more effectively.

#### How These Work Together in a RAG Agent

By combining **LangChain** for document retrieval and **Cohere** for NLP enhancement, this setup allows our Agents to process complex queries with context-aware responses, improving accuracy and user experience. LangChain handles the heavy lifting of document parsing and information retrieval, while Cohere fine-tunes the response to ensure high-quality, relevant answers.

## API KEYs

You will need **two API keys** to correctly go through this guide:
Expand Down Expand Up @@ -192,7 +214,7 @@ The script looks like the following:

- `question (str)`: it is the user's question that needs to be answered based on the provided website URL.
- `url (str)`: it is the URL of the website where the answer should be found.
- `deep_read (Optional[str], default="no")`: this optional field allows you to specify whether the RAG agent should follow and read nested pages (i.e., pages linked from the starting URL). Valid values are `"yes"` or `"no"`. By `default`, it is set to `"no"`, meaning that the agent only will focus on the initial page linked to the URL.
- `deep_read (Optional[str], default="no")`: this optional field allows you to specify whether the RAG Agent should follow and read nested pages (i.e., pages linked from the starting URL). Valid values are `"yes"` or `"no"`. By `default`, it is set to `"no"`, meaning that the Agent only will focus on the initial page linked to the URL.

## Agents

Expand Down Expand Up @@ -386,7 +408,7 @@ The script for this Agent looks as follows:

The Agent fetches and processes information from specified URLs to answer users' questions.

Let's explore the script above in more detail. We start by importing multiple modules, including tools for making HTTP requests, parsing HTML, and handling Natural Language Processing (NLP). The Agent uses **OpenAI's GPT-4o-mini model** to generate answers based on the retrieved documents. We then create an agent called `langchain_rag_agent()` with a unique `seed` and `mailbox` address. Check out the [Mailbox ↗️](/guides/agents/intermediate/mailbox) guide for additional information on how to set up Mailboxes for your Agents. **Make sure to have enough funds in the Agent's wallet for it to correctly register in the [Almanac contract ↗️](/references/contracts/uagents-almanac/almanac-overview). You can use the `fund_agent_if_low` function on the testnet to add funds to your Agent's wallet if needed.
Let's explore the script above in more detail. We start by importing multiple modules, including tools for making HTTP requests, parsing HTML, and handling Natural Language Processing (NLP). The Agent uses **OpenAI's GPT-4o-mini model** to generate answers based on the retrieved documents. We then create an Agent called `langchain_rag_agent()` with a unique `seed` and `mailbox` address. Check out the [Mailbox ↗️](/guides/agents/intermediate/mailbox) guide for additional information on how to set up Mailboxes for your Agents. **Make sure to have enough funds in the Agent's wallet for it to correctly register in the [Almanac contract ↗️](/references/contracts/uagents-almanac/almanac-overview). You can use the `fund_agent_if_low` function on the testnet to add funds to your Agent's wallet if needed.

Next, we define the `DocsBot` [protocol ↗️](/guides/agents/intermediate/protocols) using the `Protocol` class of the `uagents` library to handle message interactions. This protocol uses a predefined prompt template to structure the questions and context for the language model.

Expand All @@ -404,7 +426,7 @@ The script for this Agent looks as follows:

We are now ready to define the **LangChain User Agent** which interacts with the LangChain RAG Agent we defined above to ask a question based on documents found at specified URLs. The user Agent request the RAG Agent to retrieve and process information from the web page and then to handle the response. The Agent is defined as a [local Agent with an endpoint ↗️](https://fetch.ai/docs/guides/agents/intermediate/options-for-running-local-agents#run-a-local-agent-with-an-endpoint).

Create a file for this agent:
Create a file for this Agent:

<CodeGroup hasCopy isOSFile>
<DocsCode mac={true}>
Expand Down Expand Up @@ -494,17 +516,17 @@ The script 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.
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 Agent should only read the main page.

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.
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 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 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, 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.
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 included using the `.include()` method into the Agent which is then run by calling `rag_user.run()` in the main block.

Expand Down Expand Up @@ -571,11 +593,11 @@ 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.
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).

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.
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

Expand Down
4 changes: 2 additions & 2 deletions pages/guides/agentverse/avctl/avctl-ci-github.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ This tells git to update the permission on the executable script. Then push up t

## Running this locally:

Please follow the installation guide [here ↗️](guides/agentverse/avctl/avctl#installation)
Please follow the installation guide [here ↗️](/guides/agentverse/avctl/avctl#installation)

Update the permissions on `deploy-agent.sh` (You should only need to do this once):

Expand Down Expand Up @@ -207,4 +207,4 @@ For security reasons, the Agentverse defines your Agent's address and stores you

## Further steps

To get familiar with AVCTL, we recommend reading the other guides in this series: [AVCTL ↗️](guides/agentverse/avctl/avctl) and [AVCTL hosting ↗️](guides/agentverse/avctl/avctl-hosting).
To get familiar with AVCTL, we recommend reading the other guides in this series: [AVCTL ↗️](/guides/agentverse/avctl/avctl) and [AVCTL hosting ↗️](/guides/agentverse/avctl/avctl-hosting).

0 comments on commit 9e947da

Please sign in to comment.