Knowledge Graph for RAG
5/12/2025
✨ Inspired by “Knowledge Graphs for RAG” by DeepLearningAI. ✨
✨ Collaborate with NTU’s Civil Engineering AI Ph.D. Colleague. ✨
What are knowledge graphs?Knowledge Graphs are comprised of nodes and edges, which respectively represent entities or concepts, as well as the relationships, facts, attributes, or categories between them.This graph describes data in the form of nodes, as well as the associations (relationships) between the nodes.
✨ Collaborate with NTU’s Civil Engineering AI Ph.D. Colleague. ✨
What are knowledge graphs?Knowledge Graphs are comprised of nodes and edges, which respectively represent entities or concepts, as well as the relationships, facts, attributes, or categories between them.This graph describes data in the form of nodes, as well as the associations (relationships) between the nodes.
- Nodes are data records
- Both nodes and relationships can have properties.
- Nodes can be given labels to group them together.
- Relationships always have a type and a direction.
- A knowledge graph is a database that stores information in nodes and relationships.

Knowledge Graphs’ main components
A knowledge graph is a directed labeled graph that comprises three elements:
A knowledge graph is a directed labeled graph that comprises three elements:

- Nodes — real-world entities that can be both material things and abstract concepts
- Edges — links that connect the nodes
- Labels — attributes that define the relationships between the nodes and reasoning rules on edges
Creating a Knowledge Graph
Extract: identify interesting information

Enhance: supercharge the data

Expand: connect information to expand context

How Do Knowledge Graphs Work in Neo4j
Basic syntactic patterns
Basic syntactic patterns

() indicates nodes; — represents connections/undirected; -> indicates directed connections
For instance
For instance

Refer to the basic code example
Creating a graph model based on bridge design specifications in civil engineering can be quite illustrative. Here, we’ll define a small example of such a graph using Neo4j and Cypher. This example will include two types of each node: Material, Beam, Pier, DesignRequirement, and Section. Creating a graph model based on bridge design specifications in civil engineering. This example will include two types of each node: Material, Beam, Pier, DesignRequirement, and Section. Also define relationships: MADE_OF, SUPPORTS, MEETS, USES, APPLIES_TO.
Step 1: Define Nodes
Creating a graph model based on bridge design specifications in civil engineering can be quite illustrative. Here, we’ll define a small example of such a graph using Neo4j and Cypher. This example will include two types of each node: Material, Beam, Pier, DesignRequirement, and Section. Creating a graph model based on bridge design specifications in civil engineering. This example will include two types of each node: Material, Beam, Pier, DesignRequirement, and Section. Also define relationships: MADE_OF, SUPPORTS, MEETS, USES, APPLIES_TO.
Step 1: Define Nodes


Step 2: Define Relationships


Visualizing the Graph
Once you have created the nodes and relationships in Neo4j, you can visualize the graph using the following query:
Once you have created the nodes and relationships in Neo4j, you can visualize the graph using the following query:

Vector Databases

Vector databases are collections of high-dimensional vectors that represent entities or concepts, such as words, phrases, or documents. These can be used to measure the similarity or relevance between different entities or concepts.
Vector databases are adept at storing high-dimensional vectors and performing semantic searches with blistering speed. In situations that require immediate data retrieval, such as powering a customer service chatbot, vector databases excel. They quickly find the nearest vector match to a query, ensuring relevancy and accuracy.
- Vector databases consist of high-dimensional vectors representing various entities or concepts.
- It can be used to measure the similarity or relatedness of different entities or concepts based on their vector representations.
Vector databases are adept at storing high-dimensional vectors and performing semantic searches with blistering speed. In situations that require immediate data retrieval, such as powering a customer service chatbot, vector databases excel. They quickly find the nearest vector match to a query, ensuring relevancy and accuracy.

Vector Databases vs Knowledge Graphs

Knowledge graphs have a significant advantage over vector databases in supporting language model text generation. Knowledge graphs can provide more precise, specific information, support more complex and diverse queries, and enable deeper levels of reasoning and inference.
- Knowledge graphs provide precise and specific information, capable of clearly demonstrating the types and directions of relationships between entities or concepts, which helps in generating more accurate and relevant text.
- Knowledge graphs support more complex and varied queries, capable of answering questions using logical operators (for example, questions are based on all entities with specific attributes or the common categories of multiple entities), making the generated text more diverse and interesting.
- Knowledge graphs enable more advanced reasoning and inference, which allows indirect information to be derived from the database, thereby helping to generate text that is more logical and consistent.