Skip to main content
You can also check this cookbook in colab here Star us on Github, join our Discord or follow our X

Overview

In this notebook, we show the usage of CAMEL Retrieve Module in both customized way and auto way. We will also show how to combine AutoRetriever with ChatAgent, and further combine AutoRetriever with RolePlaying by using Function Calling. 4 main parts included:
  • Customized RAG
  • Auto RAG
  • Single Agent with Auto RAG
  • Role-playing with Auto RAG

Installation

Ensure you have CAMEL AI installed in your Python environment:

Load Data

Let’s first load the CAMEL paper from https://arxiv.org/pdf/2303.17760.pdf. This will be our local example data.

1. Customized RAG

In this section we will set our customized RAG pipeline, we will take VectorRetriever as an example. Set embedding model, we will use OpenAIEmbedding as the embedding model, so we need to set the OPENAI_API_KEY in below.
Alternatively, if running on Colab, you could save your API keys and tokens as Colab Secrets, and use them across notebooks. To do so, comment out the above manual API key prompt code block(s), and uncomment the following codeblock. ⚠️ Don’t forget granting access to the API key you would be using to the current notebook.
Import and set the embedding instance:
Import and set the vector storage instance:
Import and set the retriever instance:
We use integrated Unstructured Module to splite the content into small chunks, the content will be splited automacitlly with its chunk_by_title function, the max character for each chunk is 500 characters, which is a suitable length for OpenAIEmbedding. All the text in the chunks will be embed and stored to the vector storage instance, it will take some time, please wait..
Now we can retrieve information from the vector storage by giving a query. By default it will give you back the text content from top 1 chunk with highest Cosine similarity score, and the similarity score should be higher than 0.75 to ensure the retrieved content is relevant to the query. You can also change the top_k value and similarity_threshold value with your needs. The returned dictionary list includes:
  • similarity score
  • content path
  • metadata
  • text
Let’s try an irrelevant query:

2. Auto RAG

In this section we will run the AutoRetriever with default settings. It uses OpenAIEmbedding as default embedding model and Qdrant as default vector storage. What you need to do is:
  • Set content input paths, which can be local paths or remote urls
  • Give a query
The Auto RAG pipeline would create collections for given content input paths, the collection name will be set automatically based on the content input path name, if the collection exists, it will do the retrieve directly.

3. Single Agent with Auto RAG

In this section we will show how to combine the AutoRetriever with one ChatAgent. Let’s set an agent function, in this function we can get the response by providing a query to this agent.

4. Role-playing with Auto RAG

In this section we will show how to combine the RETRIEVAL_FUNCS with RolePlaying by applying Function Calling.
Run the role-playing with defined retriever function: