Skip to main content

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_weightBehaviorUse When
0.0Pure vector searchTopical queries
0.3Default blendGeneral questions
0.5Equal weightRelationship questions
0.8Graph-heavy"Who knows who" queries
1.0Pure graph proximityNetwork analysis

What to Try Next