GraphRAG Search
GraphRAG blends vector similarity with knowledge graph traversal for multi-hop retrieval.
Setup: Create Entities and Edges
import requests
TOKEN = "your-api-key"
BASE = "https://api.ainative.studio/api/v1/public/memory/v2/graph"
HEADERS = {"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json"}
# Create entities
for entity in [
{"canonical_name": "Alice", "entity_type": "person", "properties": {"role": "CTO"}},
{"canonical_name": "AINative", "entity_type": "org"},
{"canonical_name": "Python", "entity_type": "tech"},
{"canonical_name": "ZeroDB", "entity_type": "tech"},
]:
requests.post(f"{BASE}/entity", headers=HEADERS, json=entity)
# Create relationships
for edge in [
{"source_name": "Alice", "target_name": "AINative", "predicate": "works_at", "confidence": 0.95},
{"source_name": "Alice", "target_name": "Python", "predicate": "uses", "confidence": 0.9},
{"source_name": "AINative", "target_name": "ZeroDB", "predicate": "builds", "confidence": 1.0},
]:
requests.post(f"{BASE}/edge", headers=HEADERS, json=edge)
Run GraphRAG Query
# Hybrid search: vector similarity + graph traversal
response = requests.post(f"{BASE}/graphrag", headers=HEADERS, json={
"query": "What technologies does Alice's company build?",
"graph_weight": 0.4,
"max_hops": 2,
"limit": 5,
})
for result in response.json()["results"]:
print(f"Score: {result['final_score']:.3f} — {result['content']}")
The query traverses: Alice → works_at → AINative → builds → ZeroDB, finding information that pure vector search would miss.
Traverse the Graph
# Multi-hop traversal from Alice
response = requests.post(f"{BASE}/traverse", headers=HEADERS, json={
"entity": "Alice",
"max_hops": 3,
"predicates": ["works_at", "uses", "builds"],
})
for node in response.json()["nodes"]:
print(f" {node['canonical_name']} ({node['entity_type']})")
Tuning graph_weight
| graph_weight | Behavior | Use When |
|---|---|---|
| 0.0 | Pure vector search | Topical queries |
| 0.3 | Default blend | General questions |
| 0.5 | Equal weight | Relationship questions |
| 0.8 | Graph-heavy | "Who knows who" queries |
| 1.0 | Pure graph proximity | Network analysis |
What to Try Next
- Apply an ontology template to structure your graph
- Use contradiction detection for fact versioning
- Read the GraphRAG guide for architecture deep-dive