You can also check this cookbook in colab hereβ Star us on Github, join our Discord or follow our XThis 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.
For secure access to Mistral AIβs services, weβll prompt for the API key.
Copy
import osfrom getpass import getpass# Prompt for the API key securelymistral_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.
Copy
# import os# from google.colab import userdata# os.environ["MISTRAL_API_KEY"] = userdata.get("MISTRAL_API_KEY")
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.
Copy
# Set Neo4j instancen4j = Neo4jGraph( url="Your_URL", username="Your_USERNAME", password="Your_PASSWORD",)
Set up Mistral Large 2 model using the CAMEL ModelFactory:
Copy
# Set up modelmistral_large_2 = ModelFactory.create( model_platform=ModelPlatformType.MISTRAL, model_type=ModelType.MISTRAL_LARGE, model_config_dict=MistralConfig(temperature=0.2).as_dict(),)
Copy
# You can also set up model locally by using ollamamistral_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:
Copy
# Set instanceuio = UnstructuredIO()kg_agent = KnowledgeGraphAgent(model=mistral_large_2)
Provide an example text input that the knowledge graph agent will process:
Copy
# Set example text inputtext_example = """CAMEL has developed a knowledge graph agent can run with Mistral AI's mostadvanced model, the Mistral Large 2. This knowledge graph agent is capableof extracting entities and relationships from given content and create knowledgegraphs automatically."""
Create an element from the text and use the knowledge graph agent to extract node and relationship information:
Copy
# Create an element from given textelement_example = uio.create_element_from_text( text=text_example, element_id="0")
Copy
# Let Knowledge Graph Agent extract node and relationship informationans_element = kg_agent.run(element_example, parse_graph_elements=False)print(ans_element)
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:
Copy
# Set retrievercamel_retriever = AutoRetriever( vector_storage_local_path="local_data/embedding_storage", storage_type=StorageType.QDRANT, embedding_model=MistralEmbedding(),)
Provide an example user query:
Copy
# Set one user queryquery="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:
Copy
# Get related content by using vector retrievervector_result = camel_retriever.run_vector_retriever( query=query, contents="https://mistral.ai/news/mistral-large-2407/",)# Show the result from vector searchprint(vector_result)
Parse content from the specified URL and create knowledge graph data:
Copy
# 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:
Copy
# Create an element from user queryquery_element = uio.create_element_from_text( text=query, element_id="1")# Let Knowledge Graph Agent extract node and relationship information from the qyeryans_element = kg_agent.run(query_element, parse_graph_elements=True)
Match entities from the query in the knowledge graph storage content:
Copy
# Match the entity got from query in the knowledge graph storage contentkg_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 DescriptionUNIONMATCH (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 databaseprint(kg_result)
Combine results from the vector search and knowledge graph entity search:
Copy
# combine result from vector search and knowledge graph entity searchcomined_results = str(vector_result) + "\n".join(kg_result)
Set up an assistant agent to answer questions based on the retrieved context:
Copy
# Set agentsys_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 agentuser_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 responseagent_response = camel_agent.step(user_msg)print(agent_response.msg.content)
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.