-
Notifications
You must be signed in to change notification settings - Fork 145
Adds sample documentation for two separate Neo4j context providers fo… #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
westey-m
merged 6 commits into
MicrosoftDocs:main
from
neo4j-partners:docs/neo4j-context-provider
Apr 1, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a7269f2
Python: Adds sample documentation for two separate Neo4j context prov…
retroryan a79eec4
adding pypi links
retroryan 96cdd2f
Move Neo4j integration docs from inline sections to dedicated integra…
retroryan 5ce444a
Add GraphRAG section and address PR review comments
retroryan 2a70c67
fixing dotnet docs
retroryan 3c22ca9
fixing dotnet docs
retroryan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| --- | ||
| title: Neo4j GraphRAG Context Provider for Agent Framework | ||
| description: Learn how to use the Neo4j GraphRAG Context Provider to add knowledge graph retrieval capabilities to your Agent Framework agents | ||
| zone_pivot_groups: programming-languages | ||
| author: retroryan | ||
| ms.topic: article | ||
| ms.author: westey | ||
| ms.date: 03/29/2026 | ||
| ms.service: agent-framework | ||
| --- | ||
|
|
||
| # Neo4j GraphRAG Context Provider | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| ## Why use Neo4j for GraphRAG? | ||
|
|
||
| - **Graph enhanced retrieval**: Standard vector search returns isolated chunks; graph traversal follows connections to surface related entities, giving agents richer context. | ||
| - **Flexible search modes**: Combine vector similarity, keyword/BM25, and graph traversal in a single query. | ||
| - **Custom retrieval queries**: Cypher queries let you control exactly which relationships to traverse and what context to return. | ||
|
|
||
| > [!NOTE] | ||
| > 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). | ||
|
|
||
| ::: zone pivot="programming-language-csharp" | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - A Neo4j instance (self-hosted or [Neo4j AuraDB](https://neo4j.com/cloud/aura/)) with a vector or fulltext index configured | ||
| - An Azure AI Foundry project with a deployed chat model and an embedding model (e.g. `text-embedding-3-small`) | ||
| - Environment variables set: `NEO4J_URI`, `NEO4J_USERNAME`, `NEO4J_PASSWORD`, `AZURE_AI_SERVICES_ENDPOINT`, `AZURE_AI_EMBEDDING_NAME` | ||
| - Azure CLI credentials configured (`az login`) | ||
| - .NET 8.0 or later | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| dotnet add package Neo4j.AgentFramework.GraphRAG | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```csharp | ||
| using Azure.AI.OpenAI; | ||
| using Azure.Identity; | ||
| using Microsoft.Agents.AI; | ||
| using Microsoft.Agents.AI.OpenAI; | ||
| using Microsoft.Extensions.AI; | ||
| using Neo4j.AgentFramework.GraphRAG; | ||
| using Neo4j.Driver; | ||
|
|
||
| // Read connection details from environment variables | ||
| var neo4jSettings = new Neo4jSettings(); | ||
| var azureEndpoint = Environment.GetEnvironmentVariable("AZURE_AI_SERVICES_ENDPOINT")!; | ||
|
|
||
| // Create embedding generator | ||
| var credential = new DefaultAzureCredential(); | ||
| var azureClient = new AzureOpenAIClient(new Uri(azureEndpoint), credential); | ||
|
|
||
| IEmbeddingGenerator<string, Embedding<float>> embedder = azureClient | ||
| .GetEmbeddingClient("text-embedding-3-small") | ||
| .AsIEmbeddingGenerator(); | ||
|
|
||
| // Create Neo4j driver | ||
| await using var driver = GraphDatabase.Driver( | ||
| neo4jSettings.Uri, AuthTokens.Basic(neo4jSettings.Username, neo4jSettings.Password!)); | ||
|
|
||
| // Create the Neo4j context provider | ||
| await using var provider = new Neo4jContextProvider(driver, new Neo4jContextProviderOptions | ||
| { | ||
| IndexName = "chunkEmbeddings", | ||
| IndexType = IndexType.Vector, | ||
| EmbeddingGenerator = embedder, | ||
| TopK = 5, | ||
| RetrievalQuery = """ | ||
| MATCH (node)-[:FROM_DOCUMENT]->(doc:Document) | ||
| OPTIONAL MATCH (doc)<-[:FILED]-(company:Company) | ||
| RETURN node.text AS text, score, doc.title AS title, company.name AS company | ||
| ORDER BY score DESC | ||
| """, | ||
| }); | ||
|
|
||
| // Create an agent with the provider | ||
| AIAgent agent = azureClient | ||
| .GetChatClient("gpt-4o") | ||
| .AsIChatClient() | ||
| .AsBuilder() | ||
| .UseAIContextProviders(provider) | ||
| .BuildAIAgent(new ChatClientAgentOptions | ||
| { | ||
| ChatOptions = new ChatOptions | ||
| { | ||
| Instructions = "You are a financial analyst assistant.", | ||
| }, | ||
| }); | ||
|
|
||
| var session = await agent.CreateSessionAsync(); | ||
| Console.WriteLine(await agent.RunAsync("What risks does Acme Corp face?", session)); | ||
| ``` | ||
|
|
||
| ## Key features | ||
|
|
||
| - **Index-driven**: Works with any Neo4j vector or fulltext index | ||
| - **Graph traversal**: Custom Cypher queries enrich search results with related entities | ||
| - **Search modes**: Vector (semantic similarity), fulltext (keyword/BM25), or hybrid (both combined) | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Neo4j Context Provider repository](https://github.com/neo4j-labs/neo4j-maf-provider) | ||
| - [NuGet package page](https://www.nuget.org/packages/Neo4j.AgentFramework.GraphRAG) | ||
| - [Workshop: Neo4j Context Providers for Agent Framework](https://github.com/neo4j-partners/maf-context-providers-lab) | ||
|
|
||
| ::: zone-end | ||
|
|
||
| ::: zone pivot="programming-language-python" | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - A Neo4j instance (self-hosted or [Neo4j AuraDB](https://neo4j.com/cloud/aura/)) with a vector or fulltext index configured | ||
| - An Azure AI Foundry project with a deployed chat model and an embedding model (e.g. `text-embedding-ada-002`) | ||
| - Environment variables set: `NEO4J_URI`, `NEO4J_USERNAME`, `NEO4J_PASSWORD`, `AZURE_AI_PROJECT_ENDPOINT`, `AZURE_AI_EMBEDDING_NAME` | ||
| - Azure CLI credentials configured (`az login`) | ||
| - Python 3.10 or later | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install agent-framework-neo4j | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```python | ||
| from agent_framework import Agent | ||
| from agent_framework.azure import AzureAIClient | ||
| from agent_framework_neo4j import Neo4jContextProvider, Neo4jSettings, AzureAISettings, AzureAIEmbedder | ||
| from azure.identity import DefaultAzureCredential | ||
| from azure.identity.aio import AzureCliCredential | ||
westey-m marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Reads NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD from environment variables | ||
| neo4j_settings = Neo4jSettings() | ||
|
|
||
| # Reads AZURE_AI_PROJECT_ENDPOINT, AZURE_AI_EMBEDDING_NAME from environment variables | ||
| azure_settings = AzureAISettings() | ||
|
|
||
| sync_credential = DefaultAzureCredential() | ||
| embedder = AzureAIEmbedder( | ||
| endpoint=azure_settings.inference_endpoint, | ||
| credential=sync_credential, | ||
| model=azure_settings.embedding_model, | ||
| ) | ||
|
|
||
| neo4j_provider = Neo4jContextProvider( | ||
| uri=neo4j_settings.uri, | ||
| username=neo4j_settings.username, | ||
| password=neo4j_settings.get_password(), | ||
| index_name=neo4j_settings.vector_index_name, | ||
| index_type="vector", | ||
| embedder=embedder, | ||
| top_k=5, | ||
| retrieval_query=""" | ||
| MATCH (node)-[:FROM_DOCUMENT]->(doc:Document) | ||
| OPTIONAL MATCH (doc)<-[:FILED]-(company:Company) | ||
| RETURN node.text AS text, score, doc.title AS title, company.name AS company | ||
| ORDER BY score DESC | ||
| """, | ||
| ) | ||
|
|
||
| async with ( | ||
| neo4j_provider, | ||
| AzureCliCredential() as credential, | ||
| AzureAIClient(credential=credential, project_endpoint=azure_settings.project_endpoint) as client, | ||
| Agent( | ||
| client=client, | ||
| instructions="You are a financial analyst assistant.", | ||
| context_providers=[neo4j_provider], | ||
| ) as agent, | ||
| ): | ||
| session = agent.create_session() | ||
| response = await agent.run("What risks does Acme Corp face?", session=session) | ||
| ``` | ||
|
|
||
| ## Key features | ||
|
|
||
| - **Index-driven**: Works with any Neo4j vector or fulltext index | ||
| - **Graph traversal**: Custom Cypher queries enrich search results with related entities | ||
| - **Search modes**: Vector (semantic similarity), fulltext (keyword/BM25), or hybrid (both combined) | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Neo4j Context Provider repository](https://github.com/neo4j-labs/neo4j-maf-provider) | ||
| - [PyPI package page](https://pypi.org/project/agent-framework-neo4j/) | ||
| - [Workshop: Neo4j Context Providers for Agent Framework](https://github.com/neo4j-partners/maf-context-providers-lab) | ||
|
|
||
| ::: zone-end | ||
|
|
||
| ## Next steps | ||
|
|
||
| > [!div class="nextstepaction"] | ||
| > [Neo4j Memory Provider](./neo4j-memory.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| --- | ||
| title: Neo4j Memory Provider for Agent Framework | ||
| description: Learn how to use the Neo4j Memory Provider to add persistent knowledge graph memory to your Agent Framework agents | ||
| zone_pivot_groups: programming-languages | ||
| author: retroryan | ||
| ms.topic: article | ||
| ms.author: westey | ||
| ms.date: 03/29/2026 | ||
| ms.service: agent-framework | ||
| --- | ||
|
|
||
| # Neo4j Memory Provider | ||
|
|
||
| The Neo4j Memory Provider gives Agent Framework agents persistent memory backed by a knowledge graph. Unlike RAG providers that retrieve from static knowledge bases, the memory provider stores and recalls agent interactions, automatically extracting entities and building a knowledge graph over time. | ||
|
|
||
| The provider manages three types of memory: | ||
|
|
||
| - **Short-term memory**: Conversation history and recent context | ||
| - **Long-term memory**: Entities, preferences, and facts extracted from interactions | ||
| - **Reasoning memory**: Past reasoning traces and tool usage patterns | ||
|
|
||
| ## Why use Neo4j for agent memory? | ||
|
|
||
| - **Knowledge graph persistence**: Memories are stored as connected entities, not flat records, so the agent can reason about relationships between things it remembers. | ||
| - **Automatic entity extraction**: Conversations are parsed into structured entities and relationships without manual schema design. | ||
| - **Cross-session recall**: Preferences, facts, and reasoning traces persist across sessions and surface automatically via context providers. | ||
|
|
||
| > [!NOTE] | ||
| > Neo4j offers two separate integrations for Agent Framework. This provider (`neo4j-agent-memory`) is for **persistent memory** — storing and recalling agent interactions, extracting entities, and building a knowledge graph over time. For **GraphRAG** from an existing knowledge graph using vector, fulltext, or hybrid search, see the [Neo4j GraphRAG Context Provider](./neo4j-graphrag.md). | ||
|
|
||
| ::: zone pivot="programming-language-csharp" | ||
|
|
||
| This provider is not yet available for C#. See the Python tab for usage examples. | ||
|
|
||
| ::: zone-end | ||
|
|
||
| ::: zone pivot="programming-language-python" | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - A Neo4j instance (self-hosted or [Neo4j AuraDB](https://neo4j.com/cloud/aura/)) | ||
| - An Azure AI Foundry project with a deployed chat model | ||
| - An OpenAI API key or Azure OpenAI deployment (for embeddings and entity extraction) | ||
| - Environment variables set: `NEO4J_URI`, `NEO4J_PASSWORD`, `AZURE_AI_PROJECT_ENDPOINT`, `OPENAI_API_KEY` | ||
| - Azure CLI credentials configured (`az login`) | ||
| - Python 3.10 or later | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install neo4j-agent-memory[microsoft-agent] | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```python | ||
| import os | ||
| from pydantic import SecretStr | ||
| from agent_framework import Agent | ||
| from agent_framework.azure import AzureAIClient | ||
| from azure.identity.aio import AzureCliCredential | ||
| from neo4j_agent_memory import MemoryClient, MemorySettings | ||
| from neo4j_agent_memory.integrations.microsoft_agent import ( | ||
| Neo4jMicrosoftMemory, | ||
| create_memory_tools, | ||
| ) | ||
|
|
||
| # Pass Neo4j and embedding configuration directly via constructor arguments. | ||
| # MemorySettings also supports loading from environment variables or .env files | ||
| # using the NAM_ prefix (e.g. NAM_NEO4J__URI, NAM_EMBEDDING__MODEL). | ||
| settings = MemorySettings( | ||
| neo4j={ | ||
| "uri": os.environ["NEO4J_URI"], | ||
| "username": os.environ.get("NEO4J_USERNAME", "neo4j"), | ||
| "password": SecretStr(os.environ["NEO4J_PASSWORD"]), | ||
| }, | ||
| embedding={ | ||
| "provider": "openai", | ||
| "model": "text-embedding-3-small", | ||
| }, | ||
| ) | ||
|
|
||
| memory_client = MemoryClient(settings) | ||
|
|
||
| async with memory_client: | ||
| memory = Neo4jMicrosoftMemory.from_memory_client( | ||
| memory_client=memory_client, | ||
| session_id="user-123", | ||
| ) | ||
| tools = create_memory_tools(memory) | ||
|
|
||
| async with ( | ||
| AzureCliCredential() as credential, | ||
| AzureAIClient( | ||
| credential=credential, | ||
| project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], | ||
| ) as client, | ||
| Agent( | ||
| client=client, | ||
| instructions="You are a helpful assistant with persistent memory.", | ||
| tools=tools, | ||
| context_providers=[memory.context_provider], | ||
| ) as agent, | ||
| ): | ||
| session = agent.create_session() | ||
| response = await agent.run("Remember that I prefer window seats on flights.", session=session) | ||
| ``` | ||
|
|
||
| ## Key features | ||
|
|
||
| - **Bidirectional**: Automatically retrieves relevant context before invocation and saves new memories after responses | ||
| - **Entity extraction**: Builds a knowledge graph from conversations using a multi-stage extraction pipeline | ||
| - **Preference learning**: Infers and stores user preferences across sessions | ||
| - **Memory tools**: Agents can explicitly search memory, remember preferences, and find entity connections | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Neo4j Agent Memory repository](https://github.com/neo4j-labs/agent-memory) | ||
| - [PyPI package page](https://pypi.org/project/neo4j-agent-memory/) | ||
| - [Sample: Retail Assistant with Neo4j Agent Memory](https://github.com/neo4j-labs/agent-memory/tree/main/examples/microsoft_agent_retail_assistant) | ||
| - [Workshop: Neo4j Context Providers for Agent Framework](https://github.com/neo4j-partners/maf-context-providers-lab) | ||
|
|
||
| ::: zone-end | ||
|
|
||
| ## Next steps | ||
|
|
||
| > [!div class="nextstepaction"] | ||
| > [Neo4j GraphRAG Context Provider](./neo4j-graphrag.md) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.