|
| 1 | +--- |
| 2 | +title: Neo4j GraphRAG Context Provider for Agent Framework |
| 3 | +description: Learn how to use the Neo4j GraphRAG Context Provider to add knowledge graph retrieval capabilities to your Agent Framework agents |
| 4 | +zone_pivot_groups: programming-languages |
| 5 | +author: retroryan |
| 6 | +ms.topic: article |
| 7 | +ms.author: westey |
| 8 | +ms.date: 03/29/2026 |
| 9 | +ms.service: agent-framework |
| 10 | +--- |
| 11 | + |
| 12 | +# Neo4j GraphRAG Context Provider |
| 13 | + |
| 14 | +The Neo4j GraphRAG Context Provider adds Retrieval Augmented Generation (RAG) capabilities to Agent Framework agents using a Neo4j knowledge graph. It supports vector, fulltext, and hybrid search modes, with optional graph traversal to enrich results with related entities via custom Cypher queries. |
| 15 | + |
| 16 | +For knowledge graph scenarios where relationships between entities matter, this provider retrieves relevant subgraphs rather than isolated text chunks, giving agents richer context for generating responses. |
| 17 | + |
| 18 | +## Why use Neo4j for GraphRAG? |
| 19 | + |
| 20 | +- **Graph enhanced retrieval**: Standard vector search returns isolated chunks; graph traversal follows connections to surface related entities, giving agents richer context. |
| 21 | +- **Flexible search modes**: Combine vector similarity, keyword/BM25, and graph traversal in a single query. |
| 22 | +- **Custom retrieval queries**: Cypher queries let you control exactly which relationships to traverse and what context to return. |
| 23 | + |
| 24 | +> [!NOTE] |
| 25 | +> Neo4j offers two separate integrations for Agent Framework. This provider is for **GraphRAG** — searching an existing knowledge graph to ground agent responses. For **persistent memory** that learns from conversations and builds a knowledge graph over time, see the [Neo4j Memory Provider](./neo4j-memory.md). |
| 26 | +
|
| 27 | +::: zone pivot="programming-language-csharp" |
| 28 | + |
| 29 | +## Prerequisites |
| 30 | + |
| 31 | +- A Neo4j instance (self-hosted or [Neo4j AuraDB](https://neo4j.com/cloud/aura/)) with a vector or fulltext index configured |
| 32 | +- An Azure AI Foundry project with a deployed chat model and an embedding model (e.g. `text-embedding-3-small`) |
| 33 | +- Environment variables set: `NEO4J_URI`, `NEO4J_USERNAME`, `NEO4J_PASSWORD`, `AZURE_AI_SERVICES_ENDPOINT`, `AZURE_AI_EMBEDDING_NAME` |
| 34 | +- Azure CLI credentials configured (`az login`) |
| 35 | +- .NET 8.0 or later |
| 36 | + |
| 37 | +## Installation |
| 38 | + |
| 39 | +```bash |
| 40 | +dotnet add package Neo4j.AgentFramework.GraphRAG |
| 41 | +``` |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +```csharp |
| 46 | +using Azure.AI.OpenAI; |
| 47 | +using Azure.Identity; |
| 48 | +using Microsoft.Agents.AI; |
| 49 | +using Microsoft.Agents.AI.OpenAI; |
| 50 | +using Microsoft.Extensions.AI; |
| 51 | +using Neo4j.AgentFramework.GraphRAG; |
| 52 | +using Neo4j.Driver; |
| 53 | + |
| 54 | +// Read connection details from environment variables |
| 55 | +var neo4jSettings = new Neo4jSettings(); |
| 56 | +var azureEndpoint = Environment.GetEnvironmentVariable("AZURE_AI_SERVICES_ENDPOINT")!; |
| 57 | + |
| 58 | +// Create embedding generator |
| 59 | +var credential = new DefaultAzureCredential(); |
| 60 | +var azureClient = new AzureOpenAIClient(new Uri(azureEndpoint), credential); |
| 61 | + |
| 62 | +IEmbeddingGenerator<string, Embedding<float>> embedder = azureClient |
| 63 | + .GetEmbeddingClient("text-embedding-3-small") |
| 64 | + .AsIEmbeddingGenerator(); |
| 65 | + |
| 66 | +// Create Neo4j driver |
| 67 | +await using var driver = GraphDatabase.Driver( |
| 68 | + neo4jSettings.Uri, AuthTokens.Basic(neo4jSettings.Username, neo4jSettings.Password!)); |
| 69 | + |
| 70 | +// Create the Neo4j context provider |
| 71 | +await using var provider = new Neo4jContextProvider(driver, new Neo4jContextProviderOptions |
| 72 | +{ |
| 73 | + IndexName = "chunkEmbeddings", |
| 74 | + IndexType = IndexType.Vector, |
| 75 | + EmbeddingGenerator = embedder, |
| 76 | + TopK = 5, |
| 77 | + RetrievalQuery = """ |
| 78 | + MATCH (node)-[:FROM_DOCUMENT]->(doc:Document) |
| 79 | + OPTIONAL MATCH (doc)<-[:FILED]-(company:Company) |
| 80 | + RETURN node.text AS text, score, doc.title AS title, company.name AS company |
| 81 | + ORDER BY score DESC |
| 82 | + """, |
| 83 | +}); |
| 84 | + |
| 85 | +// Create an agent with the provider |
| 86 | +AIAgent agent = azureClient |
| 87 | + .GetChatClient("gpt-4o") |
| 88 | + .AsIChatClient() |
| 89 | + .AsBuilder() |
| 90 | + .UseAIContextProviders(provider) |
| 91 | + .BuildAIAgent(new ChatClientAgentOptions |
| 92 | + { |
| 93 | + ChatOptions = new ChatOptions |
| 94 | + { |
| 95 | + Instructions = "You are a financial analyst assistant.", |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | +var session = await agent.CreateSessionAsync(); |
| 100 | +Console.WriteLine(await agent.RunAsync("What risks does Acme Corp face?", session)); |
| 101 | +``` |
| 102 | + |
| 103 | +## Key features |
| 104 | + |
| 105 | +- **Index-driven**: Works with any Neo4j vector or fulltext index |
| 106 | +- **Graph traversal**: Custom Cypher queries enrich search results with related entities |
| 107 | +- **Search modes**: Vector (semantic similarity), fulltext (keyword/BM25), or hybrid (both combined) |
| 108 | + |
| 109 | +## Resources |
| 110 | + |
| 111 | +- [Neo4j Context Provider repository](https://github.com/neo4j-labs/neo4j-maf-provider) |
| 112 | +- [NuGet package page](https://www.nuget.org/packages/Neo4j.AgentFramework.GraphRAG) |
| 113 | +- [Workshop: Neo4j Context Providers for Agent Framework](https://github.com/neo4j-partners/maf-context-providers-lab) |
| 114 | + |
| 115 | +::: zone-end |
| 116 | + |
| 117 | +::: zone pivot="programming-language-python" |
| 118 | + |
| 119 | +## Prerequisites |
| 120 | + |
| 121 | +- A Neo4j instance (self-hosted or [Neo4j AuraDB](https://neo4j.com/cloud/aura/)) with a vector or fulltext index configured |
| 122 | +- An Azure AI Foundry project with a deployed chat model and an embedding model (e.g. `text-embedding-ada-002`) |
| 123 | +- Environment variables set: `NEO4J_URI`, `NEO4J_USERNAME`, `NEO4J_PASSWORD`, `AZURE_AI_PROJECT_ENDPOINT`, `AZURE_AI_EMBEDDING_NAME` |
| 124 | +- Azure CLI credentials configured (`az login`) |
| 125 | +- Python 3.10 or later |
| 126 | + |
| 127 | +## Installation |
| 128 | + |
| 129 | +```bash |
| 130 | +pip install agent-framework-neo4j |
| 131 | +``` |
| 132 | + |
| 133 | +## Usage |
| 134 | + |
| 135 | +```python |
| 136 | +from agent_framework import Agent |
| 137 | +from agent_framework.azure import AzureAIClient |
| 138 | +from agent_framework_neo4j import Neo4jContextProvider, Neo4jSettings, AzureAISettings, AzureAIEmbedder |
| 139 | +from azure.identity import DefaultAzureCredential |
| 140 | +from azure.identity.aio import AzureCliCredential |
| 141 | + |
| 142 | +# Reads NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD from environment variables |
| 143 | +neo4j_settings = Neo4jSettings() |
| 144 | + |
| 145 | +# Reads AZURE_AI_PROJECT_ENDPOINT, AZURE_AI_EMBEDDING_NAME from environment variables |
| 146 | +azure_settings = AzureAISettings() |
| 147 | + |
| 148 | +sync_credential = DefaultAzureCredential() |
| 149 | +embedder = AzureAIEmbedder( |
| 150 | + endpoint=azure_settings.inference_endpoint, |
| 151 | + credential=sync_credential, |
| 152 | + model=azure_settings.embedding_model, |
| 153 | +) |
| 154 | + |
| 155 | +neo4j_provider = Neo4jContextProvider( |
| 156 | + uri=neo4j_settings.uri, |
| 157 | + username=neo4j_settings.username, |
| 158 | + password=neo4j_settings.get_password(), |
| 159 | + index_name=neo4j_settings.vector_index_name, |
| 160 | + index_type="vector", |
| 161 | + embedder=embedder, |
| 162 | + top_k=5, |
| 163 | + retrieval_query=""" |
| 164 | + MATCH (node)-[:FROM_DOCUMENT]->(doc:Document) |
| 165 | + OPTIONAL MATCH (doc)<-[:FILED]-(company:Company) |
| 166 | + RETURN node.text AS text, score, doc.title AS title, company.name AS company |
| 167 | + ORDER BY score DESC |
| 168 | + """, |
| 169 | +) |
| 170 | + |
| 171 | +async with ( |
| 172 | + neo4j_provider, |
| 173 | + AzureCliCredential() as credential, |
| 174 | + AzureAIClient(credential=credential, project_endpoint=azure_settings.project_endpoint) as client, |
| 175 | + Agent( |
| 176 | + client=client, |
| 177 | + instructions="You are a financial analyst assistant.", |
| 178 | + context_providers=[neo4j_provider], |
| 179 | + ) as agent, |
| 180 | +): |
| 181 | + session = agent.create_session() |
| 182 | + response = await agent.run("What risks does Acme Corp face?", session=session) |
| 183 | +``` |
| 184 | + |
| 185 | +## Key features |
| 186 | + |
| 187 | +- **Index-driven**: Works with any Neo4j vector or fulltext index |
| 188 | +- **Graph traversal**: Custom Cypher queries enrich search results with related entities |
| 189 | +- **Search modes**: Vector (semantic similarity), fulltext (keyword/BM25), or hybrid (both combined) |
| 190 | + |
| 191 | +## Resources |
| 192 | + |
| 193 | +- [Neo4j Context Provider repository](https://github.com/neo4j-labs/neo4j-maf-provider) |
| 194 | +- [PyPI package page](https://pypi.org/project/agent-framework-neo4j/) |
| 195 | +- [Workshop: Neo4j Context Providers for Agent Framework](https://github.com/neo4j-partners/maf-context-providers-lab) |
| 196 | + |
| 197 | +::: zone-end |
| 198 | + |
| 199 | +## Next steps |
| 200 | + |
| 201 | +> [!div class="nextstepaction"] |
| 202 | +> [Neo4j Memory Provider](./neo4j-memory.md) |
0 commit comments