Skip to content

Instantly share code, notes, and snippets.

@wware
Last active October 26, 2024 16:13
Show Gist options
  • Save wware/4c9b2ea74f02b68c1ffa00b3a2d49b9a to your computer and use it in GitHub Desktop.
Save wware/4c9b2ea74f02b68c1ffa00b3a2d49b9a to your computer and use it in GitHub Desktop.

What is this document?

What follows is a conversation I had with Anthropic's https://claude.ai/ LLM, asking it questions about how Graph RAG works in as much detail as I could think of. When I think of graph databases, I normally think RDF, but the generally preferred graph DB for LLM apps seems to be Neo4J which uses a query language called Cypher rather than SPARQL, so there are some questions below about Neo4J and Cypher syntax. The thing I really wanted to know about was the sequence of queries and responses involved, and how the queries are constructed, and how the responses are interpreted.

Prompt:

During a typical graph rag query and response, what would a langsmith trace look like?

Response:

Let me create a visualization of what a typical LangSmith trace would look like for a RAG query using a graph database.

LangSmith Trace for Graph RAG Query

I've created a diagram showing a typical LangSmith trace for a graph RAG query. Here's what's happening in each major section:

  1. Query Construction Phase:
    • Takes the raw user input
    • Processes it into a structured format
    • Generates the appropriate graph query
  2. Graph Retrieval Phase:
    • Executes the query against the graph database
    • Returns relevant subgraphs
    • Processes the results
  3. Context Creation Phase:
    • Formats the graph data for the LLM
    • Creates an appropriate context window
  4. LLM Generation Phase:
    • Sends the combined query and context to the LLM
    • Processes and generates the final response

Each phase is tracked separately in LangSmith, allowing you to:

  • Monitor performance metrics
  • Debug bottlenecks
  • Track token usage
  • Analyze query patterns

Would you like me to expand on any particular phase or add more specific metrics to the trace visualization?

Prompt:

How is the first step accomplished? I assume there is some prompt engineering magic involved?

Response:

Yes, I'll break down the query construction phase and show how prompt engineering is typically used to transform a natural language query into a graph database query.

Graph Query Construction with Prompt Engineering

Let me explain the key components of the prompt engineering approach:

  1. System Prompt Design:
    • Defines the role and expertise (graph query expert)
    • Provides clear rules for query construction
    • Includes the complete schema definition
    • Sets expectations for output format
  2. Schema Definition:
    • Lists all node types with their properties
    • Defines relationship types and their attributes
    • Helps the LLM understand the graph structure
  3. Query Construction Rules:
    • Enforces consistent naming conventions
    • Requires descriptive variable names
    • Specifies when to use optional matches
    • Includes guidance on result limiting
  4. Implementation Features:
    • Uses zero temperature for deterministic outputs
    • Returns only the query without explanations
    • Handles various types of questions
    • Maintains consistent formatting

Some example transformations this enables:

Natural: "Who does Alice work with?"
↓
Cypher: MATCH (alice:Person {name: 'Alice'})-[:WORKS_AT]->(company:Company)
       MATCH (coworker:Person)-[:WORKS_AT]->(company)
       WHERE coworker <> alice
       RETURN DISTINCT coworker.name, company.name

Would you like me to:

  1. Add more example transformations?
  2. Show how to handle more complex graph patterns?
  3. Explain how to validate the generated queries?

Prompt:

I'm unfamiliar with the Cypher syntax. Where can I learn more about it?

Response:

Let me create a beginner-friendly guide to Cypher's core syntax.

Introduction to Cypher Query Language

I've created a beginner's guide to Cypher. The key thing to remember is that Cypher queries are essentially pattern matching using ASCII art-like syntax. The queries visually represent what you're looking for in the graph.

For example, if you want to find people who work at companies:

(person:Person)-[:WORKS_AT]->(company:Company)

This pattern literally looks like what it's matching: a person node, connected by a WORKS_AT relationship, pointing to a company node.

Would you like me to:

  1. Show more specific examples for your use case?
  2. Explain any particular pattern in more detail?
  3. Demonstrate how to build more complex queries step by step?

Prompt:

Is neo4j a triple store?

Response:

No, Neo4j is not a triple store, though there are some important similarities and differences worth exploring.

Triple Store vs Property Graph Model

Click to open diagram

Key differences between Neo4j (a property graph database) and triple stores:

  1. Data Model:
    • Triple Store: Data is stored as subject-predicate-object triples
    • Neo4j: Uses labeled property graphs with:
      • Nodes that can have multiple properties
      • Relationships that can have properties
      • Both nodes and relationships can have labels/types
  2. Property Storage:
    • Triple Store: Properties are stored as additional triples
    • Neo4j: Properties are stored directly on nodes and relationships
  3. Query Language:
    • Triple Store: Usually SPARQL
    • Neo4j: Cypher
  4. Schema:
    • Triple Store: Often uses RDF Schema or OWL ontologies
    • Neo4j: Schema-optional with optional constraints and indexes
  5. Use Cases:
    • Triple Stores: Better for:
      • Semantic web applications
      • Linked data
      • Ontology-based integration
    • Neo4j: Better for:
      • Complex relationship patterns
      • Path finding
      • Graph algorithms

To illustrate, here's how the same data might be represented:

# Triple Store (RDF)
:person/123 :name "John" .
:person/123 :works_at :company/456 .
:company/456 :name "Acme Corp" .

# Neo4j (Cypher)
CREATE (p:Person {id: 123, name: 'John'})
CREATE (c:Company {id: 456, name: 'Acme Corp'})
CREATE (p)-[:WORKS_AT {since: 2020}]->(c)

Would you like me to elaborate on any of these differences or explore specific use cases where one might be preferred over the other?

Prompt:

OWL can express set theoretic ideas and other useful info about your entities, so you can structure your domain knowledge nicely. Is there anything equivalent for property graphs?

Response:

Yes, property graphs can capture semantic information through Property Graph Schema (PGS) and Graph Data Science (GDS) capabilities, though it's structured differently from OWL.

Property Graph Schema and Constraints

Click to open diagram

Here are the key ways property graphs can express domain knowledge:

  1. Node Label Hierarchies:
CREATE CONSTRAINT person_age IF NOT EXISTS
FOR (p:Person) REQUIRE p.age IS NOT NULL
    AND p.age >= 18;

CREATE CONSTRAINT employee_inherits IF NOT EXISTS
FOR (e:Employee) REQUIRE e:Person;
  1. Property Constraints:
// Define required properties
CREATE CONSTRAINT person_name IF NOT EXISTS
FOR (p:Person) REQUIRE p.name IS NOT NULL;

// Define property types
CREATE CONSTRAINT person_age_type IF NOT EXISTS
FOR (p:Person) REQUIRE p.age IS INTEGER;

// Define value constraints
CREATE CONSTRAINT company_industry IF NOT EXISTS
FOR (c:Company) REQUIRE c.industry IN ['Tech', 'Finance', 'Healthcare'];
  1. Relationship Cardinality:
// Ensure each Employee works at exactly one Company
CREATE CONSTRAINT employee_company IF NOT EXISTS
FOR ()-[r:WORKS_AT]->() 
REQUIRE r.since IS NOT NULL;

MATCH (e:Employee)
WITH e, count(()-[:WORKS_AT]->()) as company_count
WHERE company_count <> 1
RETURN e.name;
  1. Semantic Rules via Procedures:
@procedure("domain.validateEmployeeHierarchy")
def validate_hierarchy(tx):
    # Check that no employee manages their manager
    query = """
    MATCH (e1:Employee)-[:MANAGES]->(e2:Employee)
    MATCH (e2)-[:MANAGES*]->(e1)
    RETURN e1.name, e2.name
    """
    return tx.run(query)
  1. Graph Data Science for Inference:
// Find implicit relationships through pattern analysis
CALL gds.nodeSimilarity.stream({
    nodeProjection: ['Person'],
    relationshipProjection: {
        KNOWS: {
            type: 'KNOWS',
            orientation: 'UNDIRECTED'
        }
    }
})

Key differences from OWL:

  1. Expressiveness:
    • OWL: Formal logic, supports complex reasoning
    • PGS: More practical, focused on data integrity
  2. Reasoning:
    • OWL: Built-in inference engine
    • PGS: Custom procedures + Graph algorithms
  3. Validation:
    • OWL: Logical consistency checking
    • PGS: Constraint checking + Custom validation
  4. Use Cases:
    • OWL: Knowledge representation, inference
    • PGS: Data integrity, pattern matching

Would you like me to:

  1. Show more examples of how to implement specific OWL-like features in property graphs?
  2. Explain how to combine Graph Data Science with schema constraints?
  3. Demonstrate how to build custom inference rules?

Prompt:

What does the asterisk in [:MANAGES*] mean?

Response:

The asterisk * in Cypher indicates a variable-length path - it means "follow this relationship type zero or more times" (or in other words, traverse this relationship transitively). This is incredibly useful for hierarchical or recursive relationships.

Let me show you how it works:

Variable-Length Path Matching in Cypher

Here are the key variations of path matching:

// Direct management (depth = 1)
MATCH (manager)-[:MANAGES]->(employee)
RETURN manager.name, employee.name;

// Exactly two levels down (depth = 2)
MATCH (manager)-[:MANAGES*2]->(employee)
RETURN manager.name, employee.name;

// Between 1 and 3 levels (1 ≤ depth ≤ 3)
MATCH (manager)-[:MANAGES*1..3]->(employee)
RETURN manager.name, employee.name;

// All levels down (any depth)
MATCH (manager)-[:MANAGES*]->(employee)
RETURN manager.name, employee.name;

// Zero or more levels (includes self)
MATCH (person)-[:MANAGES*0..]->(employee)
RETURN person.name, employee.name;

Common use cases include:

  1. Organizational Hierarchies:
// Find all employees under a VP
MATCH (vp:Employee {title: 'VP'})-[:MANAGES*]->(employee)
RETURN employee.name;
  1. Cycle Detection:
// Find circular management chains
MATCH (e:Employee)-[:MANAGES*]->(e)
RETURN e.name;
  1. Level-specific Queries:
// Find skip-level reports (reports of reports)
MATCH (manager)-[:MANAGES*2]->(skip_level_report)
RETURN manager.name, skip_level_report.name;
  1. Path Analysis:
// Find the management chain between two people
MATCH path = (e1:Employee)-[:MANAGES*]->(e2:Employee)
WHERE e1.name = 'Alice' AND e2.name = 'Eve'
RETURN [node in nodes(path) | node.name] as chain;

Would you like me to:

  1. Show more examples of variable-length patterns?
  2. Explain how to optimize queries with variable-length paths?
  3. Demonstrate how to combine this with other Cypher features?

Prompt:

It sounds like neo4j is the de facto standard graph database for graph rag applications, is that right? Are any other graph Dbs in common usage?

Response:

Neo4j is very popular for graph RAG applications, but there are several other significant players in this space. Let me break this down.

Graph Databases in RAG Applications

Click to open document

Some key observations about the current landscape:

  1. Neo4j's Popularity comes from:
    • Large existing userbase
    • Strong integration with common RAG frameworks
    • Good documentation for AI/ML use cases
    • Vector search capabilities
  2. Emerging Trends:
    • Many databases are adding native vector support
    • Hybrid architectures becoming more common
    • Focus on scalability and real-time updates
  3. Selection Factors:
    • Scale of data
    • Query patterns
    • Infrastructure requirements
    • Team expertise
    • Cost considerations

Would you like me to:

  1. Dive deeper into any particular database?
  2. Explain specific RAG integration patterns?
  3. Compare query languages across these databases?

Prompt:

Is neo4j available as a docker image? If so, what docker-run options are typically used with it?

Response:

Yes, Neo4j is available as a Docker image. Let me show you the typical Docker setup.

Neo4j Docker Configuration

Click to open code

Let me explain the key components:

  1. Essential Ports:
    • 7474: HTTP for browser interface
    • 7687: Bolt protocol for database connections
    • 7473: HTTPS (optional)
  2. Important Volumes:
    • /data: Database files
    • /logs: Log files
    • /import: For importing data files
    • /plugins: Custom plugins/extensions
    • /conf: Configuration files
  3. Common Environment Variables:
# Basic Setup
NEO4J_AUTH=neo4j/yourpassword  # Set initial password
NEO4J_ACCEPT_LICENSE_AGREEMENT=yes  # Required for enterprise

# Memory Configuration
NEO4J_server_memory_heap_initial__size=2G
NEO4J_server_memory_heap_max__size=2G
NEO4J_server_memory_pagecache_size=1G

# APOC and GDS (Graph Data Science)
NEO4J_dbms_security_procedures_unrestricted=apoc.*,gds.*
  1. Quick Start Command:
docker run --name neo4j \
  -p 7474:7474 -p 7687:7687 \
  -v $HOME/neo4j/data:/data \
  -e NEO4J_AUTH=neo4j/password123 \
  neo4j:latest
  1. Development vs Production:
# Development
docker run --name neo4j \
  -p 7474:7474 -p 7687:7687 \
  -v $HOME/neo4j/data:/data \
  -e NEO4J_AUTH=neo4j/password123 \
  -e NEO4J_ACCEPT_LICENSE_AGREEMENT=yes \
  -e NEO4J_server_memory_heap_max__size=512M \
  neo4j:latest

# Production
docker run --name neo4j \
  -p 7474:7474 -p 7687:7687 \
  -v /path/to/data:/data \
  -v /path/to/logs:/logs \
  -v /path/to/import:/var/lib/neo4j/import \
  -v /path/to/plugins:/plugins \
  -e NEO4J_AUTH=neo4j/strongpassword \
  -e NEO4J_ACCEPT_LICENSE_AGREEMENT=yes \
  -e NEO4J_server_memory_heap_max__size=16G \
  -e NEO4J_server_memory_pagecache_size=4G \
  --ulimit nofile=40000:40000 \
  --restart unless-stopped \
  neo4j:enterprise

Would you like me to:

  1. Explain any specific configuration options in more detail?
  2. Show how to set up clustering with Docker?
  3. Demonstrate how to integrate with other services (like vector databases)?

Prompt:

Let's assume I want to build a Neo4J-based graph RAG system using the earlier Python code for constructing prompts and I want to do this in docker-compose using the official Neo4J docker image for one of my containers. And for the LLM, I want to use the public Groq API, accessed by either LangChain or LlamaIndex. Please show me the docker-compose.yml file I would need to do this.

Response:

I'll help you create a docker-compose file that sets up a Neo4j container and a Python service that can interact with both Neo4j and Groq's API.

Docker Compose Configuration for Neo4j Graph RAG System

version: '3.8'

services:
  neo4j:
    image: neo4j:5.13.0
    container_name: neo4j-graph-rag
    ports:
      - "7474:7474"  # HTTP
      - "7687:7687"  # Bolt
    environment:
      - NEO4J_AUTH=neo4j/your_password_here
      - NEO4J_PLUGINS=["graph-data-science", "apoc"]  # Optional but recommended plugins
      - NEO4J_dbms_memory_heap_initial__size=512M
      - NEO4J_dbms_memory_heap_max__size=2G
      - NEO4J_dbms_memory_pagecache_size=512M
    volumes:
      - neo4j_data:/data
      - neo4j_logs:/logs
      - neo4j_import:/import
      - neo4j_plugins:/plugins
    networks:
      - graph-rag-network

  rag-service:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: rag-service
    depends_on:
      - neo4j
    environment:
      - NEO4J_URI=bolt://neo4j:7687
      - NEO4J_USER=neo4j
      - NEO4J_PASSWORD=your_password_here
      - GROQ_API_KEY=your_groq_api_key_here
    volumes:
      - ./app:/app
    networks:
      - graph-rag-network
    command: python main.py  # Adjust based on your entry point

networks:
  graph-rag-network:
    driver: bridge

volumes:
  neo4j_data:
  neo4j_logs:
  neo4j_import:
  neo4j_plugins:

You'll also need a Dockerfile for the rag-service. Here it is:

Dockerfile for RAG Service

FROM python:3.11-slim

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements file
COPY requirements.txt .

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY ./app .

CMD ["python", "main.py"]

And here's a minimal requirements.txt file you'll need:

Python Requirements

langchain
langchain-groq
neo4j
python-dotenv
llama-index
llama-index-vector-stores-neo4j

To use this setup:

  1. Create a project directory with the following structure:
project/
├── docker-compose.yml
├── Dockerfile
├── requirements.txt
└── app/
    └── main.py  # Your application code
  1. Important notes:
    • Replace your_password_here with a secure password for Neo4j
    • Replace your_groq_api_key_here with your actual Groq API key
    • The Neo4j container includes optional but recommended plugins (APOC and Graph Data Science)
    • The Neo4j data, logs, and plugins are persisted using named volumes
    • The services are connected via a dedicated network named graph-rag-network
    • The Python service waits for Neo4j to be ready before starting
    • Memory settings for Neo4j are conservative and can be adjusted based on your needs
  2. To start the services:
docker-compose up -d
  1. Access points:
    • Neo4j Browser: http://localhost:7474
    • Neo4j Bolt: bolt://localhost:7687
    • Your RAG service will run inside its container and can communicate with Neo4j using the internal network

Would you like me to show you how to modify the Python code you provided to work with this Docker setup, or would you like to see how to configure environment variables for better security?

Cypher Query Language - A Beginner's Guide

Core Concept: ASCII Art Pattern Matching

Cypher queries look like ASCII art! The syntax visually represents the patterns you're trying to match in your graph. (node1)-[relationship]->(node2)

Basic Building Blocks

  1. Nodes

    • Represented with parentheses: ()
    • Labels use a colon: (:Person)
    • Properties use curly braces: (:Person {name: "John"})

    MATCH (p:Person {name: "John"}) RETURN p

  2. Relationships

    • Represented with square brackets: []
    • Arrows show direction: -> or <-
    • Types use colon: [:WORKS_AT]
    • Properties in curly braces: [:WORKS_AT {since: 2020}]

    MATCH (:Person)-[r:WORKS_AT]->(c:Company) RETURN r, c

  3. Common Commands

    • MATCH: Find patterns in your graph
    • WHERE: Filter results
    • RETURN: Specify what to output
    • CREATE: Make new nodes/relationships
    • MERGE: Create if doesn't exist
    • SET: Update properties
    • DELETE: Remove nodes/relationships

Common Patterns

Finding Connected Nodes

// Find John's friends
MATCH (john:Person {name: "John"})-[:FRIENDS_WITH]->(friend:Person)
RETURN friend.name

Creating Relationships

// Make John work at Acme Corp
MATCH (p:Person {name: "John"})
MATCH (c:Company {name: "Acme Corp"})
CREATE (p)-[:WORKS_AT {since: 2024}]->(c)

Optional Matching

// Find all people and their managers (if they have one)
MATCH (p:Person)
OPTIONAL MATCH (p)-[:MANAGED_BY]->(manager:Person)
RETURN p.name, manager.name

Aggregation

// Count employees per company
MATCH (c:Company)<-[:WORKS_AT]-(p:Person)
RETURN c.name, COUNT(p) as employee_count

Best Practices

Start Small

  • Begin with simple patterns
  • Add complexity gradually
  • Test each part separately

Use Parameters

  • Instead of: {name: "John"}
  • Use: {name: $personName}
  • Prevents injection, improves caching

Name Your Patterns

  • Use meaningful variable names
  • Makes queries more readable
  • Helps with maintenance

Limit Results

  • Use LIMIT for large datasets
  • Add ORDER BY for consistency
  • Consider pagination

Interactive Learning Resources

  • Neo4j's Official Sandbox: https://neo4j.com/sandbox/

    • Free interactive environment
    • Comes with sample datasets
    • Guided tutorials
  • Neo4j Browser Tutorial:

    • Type :play cypher in Neo4j Browser
    • Interactive, step-by-step lessons
    • Real-time feedback
  • Cypher Manual: https://neo4j.com/docs/cypher-manual/

    • Comprehensive reference
    • Detailed examples
    • Best practices
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
# Define the system prompt that teaches the LLM how to construct graph queries
SYSTEM_TEMPLATE = """You are a graph query expert that converts natural language questions into Cypher queries.
Follow these rules when constructing queries:
1. Always use lowercase for relationship types and property names
2. Use descriptive variable names (e.g., 'person' instead of 'p')
3. Include relevant properties in the RETURN clause
4. Add appropriate WHERE clauses for filtering
5. Use OPTIONAL MATCH when the relationship might not exist
6. Limit results when appropriate
The graph schema is:
- Nodes:
* Person(id, name, age, title)
* Company(id, name, industry)
* Project(id, name, status)
- Relationships:
* WORKS_AT(since)
* MANAGES(role)
* WORKS_ON(role, hours)
Convert the user's question into a Cypher query."""
# Define the human prompt template
HUMAN_TEMPLATE = """Question: {question}
Create a Cypher query that answers this question.
Return only the Cypher query with no explanations."""
# Create the full prompt template
prompt = ChatPromptTemplate.from_messages([
("system", SYSTEM_TEMPLATE),
("human", HUMAN_TEMPLATE)
])
def construct_graph_query(question: str, llm: ChatOpenAI) -> str:
"""
Constructs a Cypher query from a natural language question.
Args:
question: Natural language question
llm: Language model instance
Returns:
str: Generated Cypher query
"""
# Create the chain
chain = LLMChain(llm=llm, prompt=prompt)
# Generate the query
result = chain.invoke({"question": question})
return result["text"].strip()
# Example usage
if __name__ == "__main__":
example_questions = [
"Who are all the senior managers at tech companies?",
"Which projects have no one working on them?",
"Find people who work at multiple companies"
]
llm = ChatOpenAI(temperature=0, model="gpt-4")
for question in example_questions:
query = construct_graph_query(question, llm)
print(f"\nQuestion: {question}")
print(f"Generated Query: {query}")
# Example outputs:
"""
Question: Who are all the senior managers at tech companies?
Generated Query:
MATCH (person:Person)-[m:MANAGES]->(company:Company)
WHERE company.industry = 'tech' AND m.role CONTAINS 'senior'
RETURN person.name, company.name, m.role
ORDER BY company.name
Question: Which projects have no one working on them?
Generated Query:
MATCH (project:Project)
WHERE NOT EXISTS ((person:Person)-[:WORKS_ON]->(project))
RETURN project.name, project.status
Question: Find people who work at multiple companies
Generated Query:
MATCH (person:Person)-[w:WORKS_AT]->(company:Company)
WITH person, COUNT(company) as company_count
WHERE company_count > 1
MATCH (person)-[w:WORKS_AT]->(company:Company)
RETURN person.name, COLLECT(company.name) as companies, COUNT(company) as num_companies
ORDER BY num_companies DESC
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment