Skip to main content
Diffbot is a suite of ML-based products that make it easy to structure web data. Diffbot’s Natural Language Processing API allows for the extraction of entities, relationships, and semantic meaning from unstructured text data. Open In Colab

Use case

Text data often contain rich relationships and insights used for various analytics, recommendation engines, or knowledge management applications. By coupling Diffbot's NLP API with Neo4j, a graph database, you can create powerful, dynamic graph structures based on the information extracted from text. These graph structures are fully queryable and can be integrated into various applications. This combination allows for use cases such as:
  • Building knowledge graphs (like Diffbot’s Knowledge Graph) from textual documents, websites, or social media feeds.
  • Generating recommendations based on semantic relationships in the data.
  • Creating advanced search features that understand the relationships between entities.
  • Building analytics dashboards that allow users to explore the hidden relationships in data.

Overview

LangChain provides tools to interact with Graph Databases:
  1. Construct knowledge graphs from text using graph transformer and store integrations
  2. Query a graph database using chains for query creation and execution
  3. Interact with a graph database using agents for robust and flexible querying

Setting up

First, get required packages and set environment variables:

Diffbot NLP API

Diffbot's NLP API is a tool for extracting entities, relationships, and semantic context from unstructured text data. This extracted information can be used to construct a knowledge graph. To use the API, you’ll need to obtain a free API token from Diffbot.
This code fetches Wikipedia articles about “Warren Buffett” and then uses DiffbotGraphTransformer to extract entities and relationships. The DiffbotGraphTransformer outputs a structured data GraphDocument, which can be used to populate a graph database. Note that text chunking is avoided due to Diffbot’s character limit per API request.

Loading the data into a knowledge graph

You will need to have a running Neo4j instance. One option is to create a free Neo4j database instance in their Aura cloud service. You can also run the database locally using the Neo4j Desktop application, or running a docker container. You can run a local docker container by running the executing the following script:
If you are using the docker container, you need to wait a couple of second for the database to start.
The GraphDocuments can be loaded into a knowledge graph using the add_graph_documents method.

Refresh graph schema information

If the schema of database changes, you can refresh the schema information needed to generate Cypher statements

Querying the graph

We can now use the graph cypher QA chain to ask question of the graph. It is advisable to use gpt-4 to construct Cypher queries to get the best experience.