Retrieval-augmented generation (RAG)
These examples follow the five .NET AgentWithRAG samples
at revision 0c9944c, preserving their source documents, questions, retrieval
settings, and context/history behavior.
| Example |
Retrieval |
Prerequisites |
| Step 01: Basic text RAG |
In-memory cosine search; one result per query |
Foundry chat and embedding deployments |
| Step 02: Custom vector store RAG |
Qdrant; five results; five recent messages in the query |
Foundry deployments and Qdrant at localhost:6334 |
| Step 03: Custom RAG data source |
Mock keyword search; six recent messages in the query |
Foundry chat deployment |
| Step 04: Foundry service RAG |
Server-side file search over an uploaded knowledge base |
Foundry chat deployment and permission to create files, vector stores, and agents |
| Step 05: Neo4j graph RAG |
Full-text search followed by graph enrichment; five results |
Foundry chat deployment and a populated Neo4j graph/full-text index |
Configuration
Set the Foundry project endpoint and sign in using a credential supported by
DefaultAzureCredential, such as az login:
$env:FOUNDRY_PROJECT_ENDPOINT = "https://<resource>.services.ai.azure.com/api/projects/<project>"
$env:FOUNDRY_MODEL = "gpt-5.4-mini"
$env:FOUNDRY_EMBEDDING_MODEL = "text-embedding-3-large"
The examples use the shared demo.FoundryModel, which defaults to gpt-4o-mini
when FOUNDRY_MODEL is unset or blank. Set it as above to match the upstream
sample's gpt-5.4-mini choice. Steps 01 and 02 also use
FOUNDRY_EMBEDDING_MODEL, which defaults to text-embedding-3-large for their
3072-dimensional vector schemas. Deployment names must match those in your resource.
For production, choose a specific credential appropriate to the deployment,
rather than the development-oriented DefaultAzureCredential chain.
Run an example from the repository root:
go run .\examples\02-agents\rag\step01_basic_text_rag
go run .\examples\02-agents\rag\step02_custom_vector_store_rag
go run .\examples\02-agents\rag\step03_custom_rag_data_source
go run .\examples\02-agents\rag\step04_foundry_service_rag
go run .\examples\02-agents\rag\step05_neo4j_graph_rag
These commands invoke live services and may incur charges.
Qdrant
Start a disposable local Qdrant instance with its gRPC port exposed, for example:
docker run --rm -p 6333:6333 -p 6334:6334 qdrant/qdrant
Step 02 deletes and recreates the documentation collection on every run,
matching the upstream sample. Do not point it at a collection containing data
you want to retain. It downloads the same two Agent Framework documentation
pages, splits them into 2000 UTF-16 code units with 200 units of overlap, and
stores the text, source metadata, and an unnamed cosine vector in Qdrant.
Foundry service RAG
Step 04 embeds the upstream
knowledge-base file,
so it works independently of the current working directory.
It creates a file and a vector store, waits up to five minutes for the file to
finish indexing, and then creates a version of the agent named AskContoso.
Failed or cancelled ingestion stops provisioning and triggers cleanup.
It invokes that exact version through the project Responses API using an
agent_reference, without overriding server-owned instructions or tools.
The example deletes AskContoso during cleanup, including its versions.
Run it only in a project where this name is disposable. Cleanup also removes
the uploaded file and vector store. Go defers additionally attempt cleanup
after partial failures and cancellation; cleanup failures remain visible.
Neo4j
$env:NEO4J_URI = "neo4j://localhost:7687"
$env:NEO4J_USERNAME = "neo4j"
$env:NEO4J_PASSWORD = "<password>"
$env:NEO4J_FULLTEXT_INDEX_NAME = "search_chunks"
The username and index name default to neo4j and search_chunks. The graph
must already contain the same document/company/product/risk schema used by
the upstream sample, with a full-text index over the chunk text. The example
does not create the graph or seed data. See the
upstream Neo4j integration
for graph setup guidance.
The provider uses the ten most recent nonblank user/assistant messages,
the upstream stop-word list, read-routed Cypher, limits before and after graph
enrichment, and the same score/metadata formatting. It does not generate
embeddings for this full-text sample. Retrieved graph context is excluded from
retained history so later searches use conversation turns rather than prior
search results. The driver is closed before reporting operation or cleanup errors.
Go implementation notes
- Each example contains its own instructions, questions, and conversation loop;
there is no shared RAG support package. Step 01 keeps its document schema and
seed data alongside its entrypoint.
- Examples use the shared
demo.NewLogger middleware and demo.Response output.
demo.FoundryEmbeddingModel holds the embedding deployment resolved at startup.
- Model-backed agents use
foundryprovider.NewAgent. Local conversation history
is enabled explicitly with DisableStoreOutput; Steps 01, 02, and 05 exclude
retrieved context from retained history.
- Steps 01 and 02 use
foundryprovider.NewEmbeddingGenerator for embeddings.
It derives the resource OpenAI endpoint from the project URL, configures
Foundry authentication, and delegates to the OpenAI embedding adapter.
- The existing project client does not expose file/vector-store operations or
agent-version administration. Step 04 uses
foundryprovider.NewOpenAIClient
for those operations, retaining Azure credential refresh and origin protections.
- Step 04 uses the native SDK's generic HTTP methods for agent administration
and an explicit version reference, rather than attaching to a potentially
different version selected by an agent endpoint.
- Step 01 uses the SDK's in-memory connector
for storage, batch embedding generation, and cosine top-1 search. Its
document schema declares a
3072-dimensional vector generated from the text.
embedding.Bind connects the
Foundry generator to the collection options. UUID keys are generated for each
upload; source IDs remain metadata, so repeated uploads create separate records.
- Qdrant and Neo4j use native Go connectors, not mock substitutes.
- Neo4j retrieval helpers follow
Neo4j.AgentFramework.GraphRAG
0.1.0-preview.2; its MIT notice is included in
LICENSE.neo4j.
Local validation
go build .\examples\02-agents\rag\...
go vet .\examples\02-agents\rag\...
These checks do not require credentials or running Qdrant, Neo4j, or Foundry
services.