Rag

Grounding a Microsoft Agent Framework agent in your own docs with a search tool.

Part 19 of 72 Microsoft Agent Framework Python — Every Lesson

What this lesson demonstrates

RAG (retrieval-augmented generation) grounds an agent’s answers in your own knowledge base instead of the model’s parametric memory. The pattern is simple: expose a search tool over your documents, then instruct the agent to call it before answering and to cite the source it used. The model decides when and what to retrieve, the tool returns matching snippets, and the model composes a grounded, cited reply.

The core shape

The search tool is just a Python function the agent can call:

def search_knowledge_base(
    query: Annotated[str, "The search query to find relevant support articles."],
) -> str:
    """Search the knowledge base and return matching snippets with citations."""
    q = query.lower()
    hits = [d for d in KNOWLEDGE_BASE if any(kw in q for kw in d["keywords"])]
    if not hits:
        return "No relevant articles found in the knowledge base."
    return "\n\n".join(f"[{d['title']}]({d['link']})\n{d['content']}" for d in hits)

You then pass tools=search_knowledge_base and instruct the agent to retrieve first, answer only from the snippets, cite the source, and decline when the base has nothing relevant.

The gotcha

To stay runnable on Foundry alone, this lesson uses keyword matching over an in-memory document list. That is the exact RAG-as-a-tool shape the doc teaches; only the retrieval mechanism is simplified. For production, swap search_knowledge_base for a VectorStore-backed collection.create_search_function(...).as_agent_framework_tool(), which requires semantic-kernel >= 1.38 and a live vector store (Azure AI Search, Qdrant, Redis, in-memory).

The Azure / MAF mapping

The agent is a FoundryChatClient-backed Agent with AzureCliCredential. Each document carries a source name and link so the agent can cite where a fact came from — the reason for the “always cite your sources” instruction.

Run it

uv run tutorial/02-agents/11_rag.py — needs Foundry credentials via az login.


Next: Declarative Agents

Sources & References

The Python SDK this lesson exercises
Retrieval-augmented generation