You can also check this cookbook in colab here
โญ Star us on Github, join our Discord or follow our X
This cookbook walks you through the process of performing graph-based Retrieval-Augmented Generation (RAG) using CAMEL, powered by the advanced Mistral models. Specifically, weโll utilize the Mistral Large 2 model to extract and structure knowledge from a given content source, and store this information in a Neo4j graph database. Subsequently, we can leverage a hybrid approach, combining vector retrieval and knowledge graph retrieval, to query and explore the stored knowledge.
๐ฆ Installation
First, install the CAMEL package with all its dependencies:
pip install "camel-ai[all]==0.2.16"
๐ง Setup
Import the required modules from CAMEL-AI:
from camel.models import ModelFactory
from camel.types import ModelPlatformType, ModelType
from camel.configs import MistralConfig, OllamaConfig
from camel.loaders import UnstructuredIO
from camel.storages import Neo4jGraph
from camel.retrievers import AutoRetriever
from camel.embeddings import MistralEmbedding
from camel.types import StorageType
from camel.agents import ChatAgent, KnowledgeGraphAgent
from camel.messages import BaseMessage
๐ Setting Up API Keys
For secure access to Mistral AIโs services, weโll prompt for the API key.
import os
from getpass import getpass
# Prompt for the API key securely
mistral_api_key = getpass('Enter your API key: ')
os.environ["MISTRAL_API_KEY"] = mistral_api_key
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 os
# from google.colab import userdata
# os.environ["MISTRAL_API_KEY"] = userdata.get("MISTRAL_API_KEY")
๐๏ธ Configuring Neo4j Graph Database
Set up your Neo4j instance by providing the URL, username, and password, here is the guidance, check your credentials in the downloaded .txt file. Note that you may need to wait up to 60 seconds if the instance has just been set up.
# Set Neo4j instance
n4j = Neo4jGraph(
url="Your_URL",
username="Your_USERNAME",
password="Your_PASSWORD",
)
๐ง Creating the Model
Set up Mistral Large 2 model using the CAMEL ModelFactory:
# Set up model
mistral_large_2 = ModelFactory.create(
model_platform=ModelPlatformType.MISTRAL,
model_type=ModelType.MISTRAL_LARGE,
model_config_dict=MistralConfig(temperature=0.2).as_dict(),
)
# You can also set up model locally by using ollama
mistral_large_2_local = ModelFactory.create(
model_platform=ModelPlatformType.OLLAMA,
model_type="mistral-large",
model_config_dict=OllamaConfig(temperature=0.2).as_dict(),
)
๐ค Generate a Knowledge Graph Using CAMELโs Agent
Set up instances for knowledge graph agent:
# Set instance
uio = UnstructuredIO()
kg_agent = KnowledgeGraphAgent(model=mistral_large_2)
Provide an example text input that the knowledge graph agent will process:
# Set example text input
text_example = """
CAMEL has developed a knowledge graph agent can run with Mistral AI's most
advanced model, the Mistral Large 2. This knowledge graph agent is capable
of extracting entities and relationships from given content and create knowledge
graphs automatically.
"""
Create an element from the text and use the knowledge graph agent to extract node and relationship information:
# Create an element from given text
element_example = uio.create_element_from_text(
text=text_example, element_id="0"
)
# Let Knowledge Graph Agent extract node and relationship information
ans_element = kg_agent.run(element_example, parse_graph_elements=False)
print(ans_element)
# Check graph element
graph_elements = kg_agent.run(element_example, parse_graph_elements=True)
print(graph_elements)
Add the extracted graph elements to the Neo4j database:
# Add the element to neo4j database
n4j.add_graph_elements(graph_elements=[graph_elements])
๐ Now you can go to here to check the knowledge graph built with CAMELโs Knowledge Graph Agent and Mistral AIโs Mistral Large 2 model!
๐๏ธ Running Graph RAG with CAMEL
Next we will showcase how to run RAG in a hybrid approach, combining vector retrieval and knowledge graph retrieval, to query and explore the stored knowledge.
Set up a vector retriever with local storage and embedding model from Mistral AI:
# Set retriever
camel_retriever = AutoRetriever(
vector_storage_local_path="local_data/embedding_storage",
storage_type=StorageType.QDRANT,
embedding_model=MistralEmbedding(),
)
Provide an example user query:
# Set one user query
query="what's the relationship between Mistral Large 2 and Mistral AI? What kind of feature does Mistral Large 2 has?"
Retrieve related content using the vector retriever, here we take Mistral AIโs news in the website as example content, you can also set the local file path here:
# Get related content by using vector retriever
vector_result = camel_retriever.run_vector_retriever(
query=query,
contents="https://mistral.ai/news/mistral-large-2407/",
)
# Show the result from vector search
print(vector_result)
Parse content from the specified URL and create knowledge graph data:
# Parse content from mistral website and create knowledge graph data by using
# the Knowledge Graph Agent, store the information into graph database.
elements = uio.parse_file_or_url(
input_path="https://mistral.ai/news/mistral-large-2407/"
)
chunk_elements = uio.chunk_elements(
chunk_type="chunk_by_title", elements=elements
)
graph_elements = []
for chunk in chunk_elements:
graph_element = kg_agent.run(chunk, parse_graph_elements=True)
n4j.add_graph_elements(graph_elements=[graph_element])
graph_elements.append(graph_element)
Create an element from the user query:
# Create an element from user query
query_element = uio.create_element_from_text(
text=query, element_id="1"
)
# Let Knowledge Graph Agent extract node and relationship information from the qyery
ans_element = kg_agent.run(query_element, parse_graph_elements=True)
Match entities from the query in the knowledge graph storage content:
# Match the entity got from query in the knowledge graph storage content
kg_result = []
for node in ans_element.nodes:
n4j_query = f"""
MATCH (n {{id: '{node.id}'}})-[r]->(m)
RETURN 'Node ' + n.id + ' (label: ' + labels(n)[0] + ') has relationship ' + type(r) + ' with Node ' + m.id + ' (label: ' + labels(m)[0] + ')' AS Description
UNION
MATCH (n)<-[r]-(m {{id: '{node.id}'}})
RETURN 'Node ' + m.id + ' (label: ' + labels(m)[0] + ') has relationship ' + type(r) + ' with Node ' + n.id + ' (label: ' + labels(n)[0] + ')' AS Description
"""
result = n4j.query(query=n4j_query)
kg_result.extend(result)
kg_result = [item['Description'] for item in kg_result]
# Show the result from knowledge graph database
print(kg_result)
Combine results from the vector search and knowledge graph entity search:
# combine result from vector search and knowledge graph entity search
comined_results = str(vector_result) + "\n".join(kg_result)
Set up an assistant agent to answer questions based on the retrieved context:
# Set agent
sys_msg = BaseMessage.make_assistant_message(
role_name="CAMEL Agent",
content="""You are a helpful assistant to answer question,
I will give you the Original Query and Retrieved Context,
answer the Original Query based on the Retrieved Context.""",
)
camel_agent = ChatAgent(system_message=sys_msg,
model=mistral_large_2)
# Pass the retrieved information to agent
user_prompt=f"""
The Original Query is {query}
The Retrieved Context is {comined_results}
"""
user_msg = BaseMessage.make_user_message(
role_name="CAMEL User", content=user_prompt
)
# Get response
agent_response = camel_agent.step(user_msg)
print(agent_response.msg.content)
๐ Highlights
-
Automated Knowledge Extraction: The Knowledge Graph Agent automates the extraction of entities and relationships, making the process efficient and effective.
-
Mistral AI Integration: This cookbook showcases the integration of Mistral AIโs advanced models, particularly the Mistral Large 2, with CAMEL-AI to create a powerful knowledge graph system.
-
Secure and Scalable: Using CAMEL-AIโs robust architecture and Neo4j for graph storage ensures that the solution is both secure and scalable.
By following this cookbook, you can leverage the cutting-edge capabilities of CAMEL AI and Mistral AI to build sophisticated knowledge graphs, facilitating advanced data analysis and retrieval tasks.