Back to Blog
engineeringknowledge-graph

Building Knowledge Graphs for AI Applications

How to extract entities and relationships from conversations to build powerful knowledge graphs.

Memoid Team · January 5, 2024 · 10 min read

Traditional memory systems store facts as isolated pieces of information. But real knowledge is connected — people work at companies, companies operate in industries, industries face challenges.

Knowledge graphs capture these connections, enabling more intelligent AI applications.

What is a Knowledge Graph?

A knowledge graph represents information as a network of entities and relationships:

[John] --works_at--> [Acme Corp]
[Acme Corp] --industry--> [Technology]
[John] --knows--> [Sarah]
[Sarah] --founded--> [Acme Corp]

From this graph, we can answer complex questions:

  • “Who works at companies Sarah founded?”
  • “What industry is John’s employer in?”
  • “How are John and Sarah connected?”

Extracting Knowledge Automatically

Memoid extracts entities and relationships from natural conversation:

from memoid import MemoryClient

client = MemoryClient("your-api-key")

# Add with graph extraction
result = client.add(
    messages=[{
        "role": "user",
        "content": "My colleague Sarah just got promoted to VP of Engineering at Acme Corp"
    }],
    user_id="user_123",
    extract_graph=True
)

# Extracted automatically:
# Entities: Sarah (person), VP of Engineering (role), Acme Corp (organization)
# Relationships: Sarah -> has_role -> VP of Engineering
#               Sarah -> works_at -> Acme Corp

Querying the Graph

Once you have a knowledge graph, you can traverse relationships:

# Find everything connected to "Sarah"
result = client.graph_query(
    entity="Sarah",
    depth=2  # Two relationship hops
)

# Returns:
# - Sarah works_at Acme Corp
# - Acme Corp is_in Technology industry
# - Sarah has_role VP of Engineering

Use Cases

1. Rich User Profiles

Build complete pictures of users by connecting:

  • Their preferences
  • Their relationships
  • Their activities
  • Their context

2. Intelligent Recommendations

Recommend based on graph traversal:

  • “Users who like X also connected to Y”
  • “Similar to people in your network”

3. Question Answering

Answer complex, multi-hop questions:

  • “What projects is my manager’s team working on?”
  • “Who should I talk to about the budget?”

4. Compliance and Audit

Track relationships for:

  • Conflict of interest detection
  • Access control decisions
  • Audit trails

Best Practices

1. Define Your Schema

Decide on entity types and relationship types upfront:

Entity Types: person, organization, project, skill, location
Relationships: works_at, knows, manages, located_in, has_skill

2. Handle Ambiguity

The same name can refer to different entities. Use context to disambiguate:

# "Apple" could be fruit or company
# Context helps: "I work at Apple" -> company

3. Merge Carefully

When the same entity appears multiple times, merge intelligently:

# "Sarah" + "Sarah Chen" + "sarah.chen@email.com"
# Should merge into one entity with multiple aliases

4. Prune Regularly

Knowledge graphs can grow large. Periodically:

  • Remove outdated relationships
  • Consolidate duplicate entities
  • Archive inactive nodes

Getting Started

Enable knowledge graphs in your Memoid integration:

client = MemoryClient(
    api_key="your-api-key",
    enable_graph=True
)

Then use extract_graph=True when adding memories, and query with the graph API.

Read the full documentation to learn more.