Skip to main content
You can also check this cookbook in colab here
CAMEL HomepageJoin Discord
⭐ Star us on GitHub, join our Discord, or follow us on X
This notebook demonstrates how to build a dynamic knowledge graph using CAMEL’s Knowledge Graph Agent and Neo4j. The knowledge graph is constructed by parsing PDF documents, extracting entities and relationships, and storing them in a Neo4j database. The graph is then queried to retrieve time-based relationships. In this notebook, you’ll explore:
  • CAMEL: A powerful multi-agent framework that enables the construction of knowledge graphs from unstructured data.
  • Neo4j: A graph database used to store and query the knowledge graph.
  • Together and SambaVerse Models: Large language models used to generate the knowledge graph from parsed documents.
  • Deduplication: Techniques to ensure the uniqueness of nodes and relationships in the graph.
This setup not only demonstrates a practical application of AI-driven knowledge graph construction but also provides a flexible framework that can be adapted to other real-world scenarios requiring dynamic graph generation and querying.

πŸ“¦ Installation

First, install the CAMEL package with all its dependencies: Second, make sure that Neo4j is running and accessible from your local machine.

πŸš€ Launch Service

Start Neo4j service in the background((using Ubuntu as an example))

πŸ”‘ Setting Up API Keys

You’ll need to set up your API keys for Together and SambaVerse. This ensures that the tools can interact with external services securely.
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.

πŸ› οΈ Setting Up Neo4j

To store and query the knowledge graph, you’ll need a Neo4j instance. If you don’t have one, you can set up a local instance or use a cloud service like Neo4j Aura.
  1. Local Setup: Download and install Neo4j Desktop from here.
  2. Cloud Setup: Sign up for Neo4j Aura here.
Once you have your Neo4j instance running, set up the connection details:

🧠 Setting Up the Knowledge Graph Agent

We will use CAMEL’s Knowledge Graph Agent to parse PDF documents, extract entities and relationships, and store them in the Neo4j database. The agent uses Together and SambaVerse models for graph generation. Replace the file path in the code below with your own data directory path example_file_dir = Path(β€œ/home/mi/daily/fin-camel/pdf_tmp”)

πŸ—οΈ Building the Knowledge Graph

The ID normalization process ensures compliant Neo4j identifiers by sanitizing input strings (replacing non-alphanumeric characters with underscores), ensuring no numeric prefixes, splitting/cleaning components, and applying SHA-1 hashing truncation to enforce a maximum 64-character limit while preserving uniqueness and readability.
Prompt we use for dynamic knowledge graph generation, which has timestamp in element.
Here we iterate over each PDF file in the example_pdf_files list. For each file, it parses the content and chunks the elements based on titles, ensuring that each chunk does not exceed 2048 characters.Within the first loop, we process each chunked element. We run it through the openai_kg_agent to generate graph elements. Then, we iterate over each node in the graph element to adjust its type and normalize its ID.After processing the nodes, we prepare a list of node IDs to be used for embedding. This list will be used in the next step for deduplication.
Perform internal deduplication on the node texts using the deduplicate_internally function. We set a threshold of 0.65 and specify the embedding strategy as β€œtop1”. The result provides unique IDs of the nodes.
Filter relationships to include only those where both the subject and object nodes are unique, as determined by the deduplication step.
After running the program above, access results by navigating to https://console-preview.neo4j.io/tools/query in your web browser. Sign in using your Neo4j credentials (as specified in the configuration file), and you’ll see the knowledge graph with timestamps displayed as shown below. Peek 2025-02-25 10-20.gif 2025-02-24 17-37-17屏幕ζˆͺε›Ύ.png 2025-02-24 17-36-39屏幕ζˆͺε›Ύ.png Add the unique relationships to a Neo4j graph. For each relationship, we generate a timestamp and add the triplet to the graph.

πŸ” Querying the Knowledge Graph

Now that the knowledge graph is built, we can query it to retrieve time-based relationships.

Parameters Investigation.

  • max_characters: The maximum number of characters in a chunk.
  • model: The model to use for the knowledge graph agent. (TogetherAI or Samba Verse)

🌟 Highlights

This notebook has guided you through setting up and running a dynamic knowledge graph construction workflow using CAMEL’s Knowledge Graph Agent and Neo4j. You can adapt and expand this example for various other scenarios requiring dynamic graph generation and querying. Key tools utilized in this notebook include:
  • CAMEL: A powerful multi-agent framework that enables the construction of knowledge graphs from unstructured data.
  • Neo4j: A graph database used to store and query the knowledge graph.
  • Together and SambaVerse Models: Large language models used to generate the knowledge graph from parsed documents.
  • Deduplication: Techniques to ensure the uniqueness of nodes and relationships in the graph.
This comprehensive setup allows you to adapt and expand the example for various scenarios requiring dynamic graph generation and querying.
CAMEL HomepageJoin Discord
⭐ Star us on GitHub, join our Discord, or follow us on X