What is Knowledge Graph and How it's used in Gen AI?

What is Knowledge Graph and How it's used in Gen AI?

For past 1 year I have been working as a component designer in telecom and have extensively used Graph Database with Kafka for proactive assurance.

During last residency at ISB we were introduced to Knowledge graph and Neo4j's use case in building it. In this blog we will understand the basics of Graph DB and how it's used in Gen AI.

What is Knowledge Graph ?

Traditional database are tabular in nature with foreign key used to create relationships and to infer relations using aggregate or group by function. Graph db established relationship in the structure itself.

A- worksat-> XYZ.         

A and XYZ are called Nodes and worksat is the relationship built between them. Each Node can hold multiple properties which are unique/common to itself.

For instance:

  • A could have properties like Age and Height.
  • XYZ could have properties like Location


Example

https://graphacademy.neo4j.com/courses/llm-fundamentals/

Nodes

  • Michael: A person node.
  • Sarah: Another person node.
  • Hans: Another person node.
  • Graph Inc: A company node.

Relationships

  • WORKS_AT: This relationship indicates that a person works at a specific company.Michael -> WORKS_AT -> Graph IncHans -> WORKS_AT -> Graph Inc
  • MARRIED_TO: This relationship indicates a marriage between two person nodes.Michael -> MARRIED_TO -> Sarah

Explanation

  • Nodes: In this graph, Michael, Sarah, Hans, and Graph Inc are represented as nodes. Each node can hold properties (e.g., name, age, etc.), but these properties are not shown in the diagram.
  • Relationships: The arrows represent the relationships between nodes.The solid arrow from Michael to Graph Inc labeled "WORKS_AT" signifies that Michael works at Graph Inc.The solid arrow from Hans to Graph Inc labeled "WORKS_AT" signifies that Hans also works at Graph Inc.The dotted arrow from Michael to Sarah labeled "MARRIED_TO" signifies that Michael is married to Sarah.

Cypher Query

from py2neo import Graph, Node, Relationship

# Connect to the Neo4j database
graph = Graph("bolt://localhost:7687", auth=("neo4j", "password"))

# Create Nodes
person = Node("Person", name="A", age=30, height=175)
company = Node("Company", name="XYZ", location="New York")

# Create Relationship
works_at = Relationship(person, "WORKS_AT", company)

# Add Nodes and Relationship to the graph
graph.create(works_at)
        

Query in Graph DB

# Query to find all people who work at a specific company
query = """
MATCH (p:Person)-[:WORKS_AT]->(c:Company {name: 'XYZ'})
RETURN p.name, p.age, p.height
"""

results = graph.run(query)

for record in results:
    print(f"Name: {record['p.name']}, Age: {record['p.age']}, Height: {record['p.height']}")
        

When to use Graph DB?

Graph DB should be used when you are not bound by ACID properties ie (Atomicity, Consistency, Isolation and Durability). They are widely used where relationship and connections are of paramount importance and are generally avoided for transactional dataset.

  • Social Media Platforms: Understanding and analyzing connections between users.
  • Cybersecurity: Identifying and tracking relationships between different entities in a network for threat detection.


Knowledge Graph in LLM

Unstructured data is used to finetune and pretrain LLM by incorporating Knowledge graph we can give more contextual information. Knowledge Graphs enable LLMs to provide more comprehensive answers by contextualizing the retrieved information. This is done through vector searches that locate relevant nodes within the graph, identifying the shortest paths or neighborhoods around these nodes.

Conclusion

By incorporating Knowledge Graph we can increase the understanding and uncover new relations for our existing LLM use case.

Special thanks to Prof. Sunila Gollapudi for introducing the Knowledge graph and it's practical use case in the last residency class.

Godwin Josh

Co-Founder of Altrosyn and DIrector at CDTECH | Inventor | Manufacturer

7 个月

A Knowledge Graph is a structured representation of knowledge as a network of entities and their relationships. In Gen AI, KGs provide factual grounding and context for text generation, enabling models to produce more accurate, relevant, and coherent outputs. They enhance tasks like question answering, summarization, and dialogue systems by leveraging semantic understanding. Think of it like this: while Transformer models excel at capturing linguistic patterns, KGs offer the world knowledge base they need to make sense of those patterns. This symbiotic relationship empowers Gen AI to move beyond simple pattern matching towards true comprehension and reasoning. How does the schema design in a Knowledge Graph, particularly the use of RDF triples, compare to the concept of embeddings in word2vec or BERT, considering their respective strengths and limitations in representing semantic relationships?

回复

要查看或添加评论,请登录

Rahula Raj的更多文章

社区洞察

其他会员也浏览了