К основному содержанию
субагент CX Codex

rag — субагент для Claude Code: что делает и как установить

Помогает подключить RAG к агентам Microsoft Agent Framework: поиск добавляет контекст к ответам, а доступные сведения о документах можно оформить как цитаты.

MIT ★ 521 обновлён 2026-10-29

Установить в Claude Code · Вручную: положить файлы Все способы и платформы ↓
.claude/agents/rag.md
Открыть на GitHub

Что делает rag

Субагент объясняет, как добавить RAG-возможности к агентам через AI Context Providers. В примере для C# используется TextSearchProvider: его подключают к ChatClientAgent, а поиск выполняется перед вызовом модели или по запросу через функцию-инструмент.

Для TextSearchProvider нужно передать функцию, которая возвращает результаты поиска по запросу. Источником может быть Azure AI Search или веб-поиск. Результаты могут содержать имя и ссылку на документ, чтобы агент мог сослаться на источник; без функции поиска этот пример не заработает.

Параметры позволяют настроить момент поиска, формат контекста и окно недавних сообщений. В документации также упомянуты интеграции с Microsoft Foundry и Neo4j. Для установки в Claude Code поместите файл субагента по пути .claude/agents/rag.md.

Какие задачи решает

  • подключить поиск к агенту Microsoft Agent Framework
  • передавать результаты поиска в контекст перед вызовом модели
  • настроить поиск по запросу через функцию-инструмент
  • добавлять к ответам ссылки на исходные документы

Примеры запросов агенту

  • Объясни, как подключить RAG к агенту Microsoft Agent Framework на C# через TextSearchProvider.
  • Покажи, как настроить поиск перед каждым вызовом модели и учитывать недавние сообщения.
  • Как передать агенту результаты Azure AI Search и добавить ссылки на документы в ответы?

Как установить rag

Вручную: положить файлы

.claude/agents/rag.md

Для всех проектов — ~/.claude/agents/. Claude делегирует задачу сам по полю description или по @-упоминанию

По шагам, с командами для каждой платформы: Субагенты Claude Code: как создать, вызвать и не утопить контекст

Вопросы о rag

Как установить rag в Claude Code?

Добавьте файл субагента по пути .claude/agents/rag.md.

Что делает rag?

Помогает настраивать RAG для агентов Microsoft Agent Framework. В примере для C# TextSearchProvider передаёт результаты поиска агенту как контекст.

Что нужно для работы TextSearchProvider?

Нужна функция, которая принимает запрос и возвращает результаты поиска. Её можно связать, например, с Azure AI Search или веб-поиском.

Исходный текст

rag.md · MIT
---
title: RAG
description: Learn how to use Retrieval Augmented Generation (RAG) with Agent Framework
zone_pivot_groups: programming-languages
author: westey-m
ms.topic: reference
ms.author: westey
ms.date: 07/28/2026
ms.service: agent-framework
---

<!--
  Language parity table - keep in sync when adding/removing sections.

  | Section                  | C# | Python | Go | Notes                          |
  |--------------------------|:--:|:------:|:--:|--------------------------------|
  | RAG overview             | ✅ |   ✅   | ✅ | Shared                         |
  | TextSearchProvider       | ✅ |   ❌   | ❌ | .NET-specific                  |
  | VectorStore search tools | ❌ |   ✅   | ❌ | Python Semantic Kernel bridge  |
  | Go availability          | ✅ |   ✅   | ✅ | Go zone is status only         |
  | Service integrations     | ✅ |   ✅   | ✅ | Shared links                   |
-->

# RAG

Microsoft Agent Framework supports adding Retrieval Augmented Generation (RAG) capabilities to agents easily by adding AI Context Providers to the agent.

For conversation/session patterns alongside retrieval, see [Conversations & Memory overview](../concepts/agents/conversations/index.md).
For service-specific setup, see [Azure AI Search](../integrations/by-component/context-providers/azure-ai-search.md), [Microsoft Foundry](../integrations/by-component/context-providers/microsoft-foundry.md#use-file-search-rag), and [Neo4j](../integrations/by-component/context-providers/neo4j.md#graphrag-from-an-existing-knowledge-graph).

::: zone pivot="programming-language-csharp"

## Using TextSearchProvider

The `TextSearchProvider` class is an out-of-the-box implementation of a RAG context provider.
It supports different modes of operation, e.g. doing a search for each agent run with chat history, or advertising function tools for doing searches.

It can easily be attached to a `ChatClientAgent` using the `AIContextProviders` option.

```csharp
// Configure the options for the TextSearchProvider.
TextSearchProviderOptions textSearchOptions = new()
{
    SearchTime = TextSearchProviderOptions.TextSearchBehavior.BeforeAIInvoke,
};

// Create the AI agent with the TextSearchProvider.
AIAgent agent = azureOpenAIClient
    .GetChatClient(deploymentName)
    .AsAIAgent(new ChatClientAgentOptions
    {
        ChatOptions = new() { Instructions = "You are a helpful support specialist. Answer questions using the provided context and cite the source document when available." },
        AIContextProviders = [new TextSearchProvider(SearchAdapter, textSearchOptions)]
    });
```

The `TextSearchProvider` requires a function that provides the search results given a query. This can be implemented using any search technology, e.g. Azure AI Search, or a web search engine.

> [!TIP]
> See the [Vector Stores integration](../integrations/index.md#vector-stores) documentation for more information on how to use a vector store for search results.

Here is an example of a mock search function that returns pre-defined results based on the query.
`SourceName` and `SourceLink` are optional, but if provided will be used by the agent to cite the source of the information when answering the user's question.

```csharp
static Task<IEnumerable<TextSearchProvider.TextSearchResult>> SearchAdapter(string query, CancellationToken cancellationToken)
{
    // The mock search inspects the user's question and returns pre-defined snippets
    // that resemble documents stored in an external knowledge source.
    List<TextSearchProvider.TextSearchResult> results = new();

    if (query.Contains("return", StringComparison.OrdinalIgnoreCase) || query.Contains("refund", StringComparison.OrdinalIgnoreCase))
    {
        results.Add(new()
        {
            SourceName = "Contoso Outdoors Return Policy",
            SourceLink = "https://contoso.com/policies/returns",
            Text = "Customers may return any item within 30 days of delivery. Items should be unused and include original packaging. Refunds are issued to the original payment method within 5 business days of inspection."
        });
    }

    return Task.FromResult<IEnumerable<TextSearchProvider.TextSearchResult>>(results);
}
```

### TextSearchProvider Options

The `TextSearchProvider` can be customized via the `TextSearchProviderOptions` class. Here is an example of creating options to run the search prior to every model invocation and keep a short rolling window of chat history for searches.

```csharp
TextSearchProviderOptions textSearchOptions = new()
{
    // Run the search prior to every model invocation and keep a short rolling window of chat history for searches.
    SearchTime = TextSearchProviderOptions.TextSearchBehavior.BeforeAIInvoke,
    RecentMessageMemoryLimit = 6,
};
```

The `TextSearchProvider` class supports the following options via the `TextSearchProviderOptions` class.

| Option | Type | Description | Default |
|--------|------|-------------|---------|
| SearchTime | `TextSearchProviderOptions.TextSearchBehavior` | Indicates when the search should be executed. There are two options, each time the agent is run, or on-demand via function calling. | `TextSearchProviderOptions.TextSearchBehavior.BeforeAIInvoke` |
| FunctionToolName | `string` | The name of the exposed search tool when operating in on-demand mode. | "Search" |
| FunctionToolDescription | `string` | The description of the exposed search tool when operating in on-demand mode. | "Allows searching for additional information to help answer the user question." |
| ContextPrompt | `string` | The context prompt prefixed to results. | "## Additional Context\nConsider the following information from source documents when responding to the user:" |
| CitationsPrompt | `string` | The instruction appended after results to request citations. | "Include citations to the source document with document name and link if document name and link is available." |
| ContextFormatter | `Func<IList<TextSearchProvider.TextSearchResult>, string>` | Optional delegate to fully customize formatting of the result list. If provided, `ContextPrompt` and `CitationsPrompt` are ignored. | `null` |
| RecentMessageMemoryLimit | `int` | The number of recent conversation messages (both user and assistant) to keep in memory and include when constructing the search input for `BeforeAIInvoke` searches. | `0` (disabled) |
| RecentMessageRolesIncluded | `List<ChatRole>` | The list of `ChatRole` types to filter recent messages to when deciding which recent messages to include when constructing the search input. | `ChatRole.User` |

> [!TIP]
> See the [.NET samples](https://github.com/microsoft/agent-framework/tree/main/dotnet/samples/02-agents/AgentWithRAG) for complete runnable examples.

::: zone-end
::: zone pivot="programming-language-python"

Agent Framework supports using Semantic Kernel's VectorStore collections to provide RAG capabilities to agents. This is achieved through the bridge functionality that converts Semantic Kernel search functions into Agent Framework tools.

### Creating a Search Tool from VectorStore

The `create_search_function` method from a Semantic Kernel VectorStore collection returns a `KernelFunction` that can be converted to an Agent Framework tool using `.as_agent_framework_tool()`.
Use [the vector store connectors documentation](/semantic-kernel/concepts/vector-store-connectors) to learn how to set up different vector store collections.

```python
from semantic_kernel.connectors.ai.open_ai import OpenAITextEmbedding
from semantic_kernel.connectors.azure_ai_search import AzureAISearchCollection
from semantic_kernel.functions import KernelParameterMetadata
from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient

# Define your data model
class SupportArticle:
    article_id: str
    title: str
    content: str
    category: str
    # ... other fields

# Create an Azure AI Search collection
collection = AzureAISearchCollection[str, SupportArticle](
    record_type=SupportArticle,
    embedding_generator=OpenAITextEmbedding()
)

async with collection:
    await collection.ensure_collection_exists()
    # Load your knowledge base articles into the collection
    # await collection.upsert(articles)

    # Create a search function from the collection
    search_function = collection.create_search_function(
        function_name="search_knowledge_base",
        description="Search the knowledge base for support articles and product information.",
        search_type="keyword_hybrid",
        parameters=[
            KernelParameterMetadata(
                name="query",
                description="The search query to find relevant information.",
                type="str",
                is_required=True,
                type_object=str,
            ),
            KernelParameterMetadata(
                name="top",
                description="Number of results to return.",
                type="int",
                default_value=3,
                type_object=int,
            ),
        ],
        string_mapper=lambda x: f"[{x.record.category}] {x.record.title}: {x.record.content}",
    )

    # Convert the search function to an Agent Framework tool
    search_tool = search_function.as_agent_framework_tool()

    # Create an agent with the search tool
    agent = Agent(
        client=OpenAIChatClient(model="gpt-4o"),
        instructions="You are a helpful support specialist. Use the search tool to find relevant information before answering questions. Always cite your sources.",
        tools=search_tool
    )

    # Use the agent with RAG capabilities
    response = await agent.run("How do I return a product?")
    print(response.text)
```

> [!IMPORTANT]
> This feature requires `semantic-kernel` version 1.38 or higher.

### Customizing Search Behavior

You can customize the search function with various options:

```python
# Create a search function with filtering and custom formatting
search_function = collection.create_search_function(
    function_name="search_support_articles",
    description="Search for support articles in specific categories.",
    search_type="keyword_hybrid",
    # Apply filters to restrict search scope
    filter=lambda x: x.is_published == True,
    parameters=[
        KernelParameterMetadata(
            name="query",
            description="What to search for in the knowledge base.",
            type="str",
            is_required=True,
            type_object=str,
        ),
        KernelParameterMetadata(
            name="category",
            description="Filter by category: returns, shipping, products, or billing.",
            type="str",
            type_object=str,
        ),
        KernelParameterMetadata(
            name="top",
            description="Maximum number of results to return.",
            type="int",
            default_value=5,
            type_object=int,
        ),
    ],
    # Customize how results are formatted for the agent
    string_mapper=lambda x: f"Article: {x.record.title}\nCategory: {x.record.category}\nContent: {x.record.content}\nSource: {x.record.article_id}",
)
```

For the full details on the parameters available for `create_search_function`, see the [Semantic Kernel documentation](/semantic-kernel/concepts/vector-store-connectors/).

### Using Multiple Search Functions

You can provide multiple search tools to an agent for different knowledge domains:

```python
# Create search functions for different knowledge bases
product_search = product_collection.create_search_function(
    function_name="search_products",
    description="Search for product information and specifications.",
    search_type="semantic_hybrid",
    string_mapper=lambda x: f"{x.record.name}: {x.record.description}",
).as_agent_framework_tool()

policy_search = policy_collection.create_search_function(
    function_name="search_policies",
    description="Search for company policies and procedures.",
    search_type="keyword_hybrid",
    string_mapper=lambda x: f"Policy: {x.record.title}\n{x.record.content}",
).as_agent_framework_tool()

# Create an agent with multiple search tools
agent = Agent(
    client=chat_client,
    instructions="You are a support agent. Use the appropriate search tool to find information before answering. Cite your sources.",
    tools=[product_search, policy_search]
)
```

You can also create multiple search functions from the same collection with different descriptions and parameters to provide specialized search capabilities:

```python
# Create multiple search functions from the same collection
# Generic search for broad queries
general_search = support_collection.create_search_function(
    function_name="search_all_articles",
    description="Search all support articles for general information.",
    search_type="semantic_hybrid",
    parameters=[
        KernelParameterMetadata(
            name="query",
            description="The search query.",
            type="str",
            is_required=True,
            type_object=str,
        ),
    ],
    string_mapper=lambda x: f"{x.record.title}: {x.record.content}",
).as_agent_framework_tool()

# Detailed lookup for specific article IDs
detail_lookup = support_collection.create_search_function(
    function_name="get_article_details",
    description="Get detailed information for a specific article by its ID.",
    search_type="keyword",
    top=1,
    parameters=[
        KernelParameterMetadata(
            name="article_id",
            description="The specific article ID to retrieve.",
            type="str",
            is_required=True,
            type_object=str,
        ),
    ],
    string_mapper=lambda x: f"Title: {x.record.title}\nFull Content: {x.record.content}\nLast Updated: {x.record.updated_date}",
).as_agent_framework_tool()

# Create an agent with both search functions
agent = Agent(
    client=chat_client,
    instructions="You are a support agent. Use search_all_articles for general queries and get_article_details when you need full details about a specific article.",
    tools=[general_search, detail_lookup]
)
```

This approach allows the agent to choose the most appropriate search strategy based on the user's query.

### Supported VectorStore Connectors

This pattern works with any Semantic Kernel VectorStore connector, including:

- Azure AI Search (`AzureAISearchCollection`)
- Qdrant (`QdrantCollection`)
- Pinecone (`PineconeCollection`)
- Redis (`RedisCollection`)
- Weaviate (`WeaviateCollection`)
- In-Memory (`InMemoryVectorStoreCollection`)
- And more

Each connector provides the same `create_search_function` method that can be bridged to Agent Framework tools, allowing you to choose the vector database that best fits your needs. See [the full list here](/semantic-kernel/concepts/vector-store-connectors/out-of-the-box-connectors).

::: zone-end

::: zone pivot="programming-language-go"

> [!NOTE]
> Go support for this feature is coming soon. See the [Agent Framework Go repository](https://github.com/microsoft/agent-framework-go) for the latest status.

::: zone-end
## Graph RAG

For GraphRAG using graph traversal enriched search with Cypher queries, see the [Neo4j GraphRAG Provider](../integrations/by-component/context-providers/neo4j.md#graphrag-from-an-existing-knowledge-graph).

## Next steps

> [!div class="nextstepaction"]
> [Declarative Agents](./declarative.md)

© westey-m · MIT · managedcode/dotnet-skills

Похожие субагенты

Все субагенты по теме «Настройка агента: память, контекст, субагенты» →
foundry-iq-skills Microsoft Создаёт базы знаний Foundry IQ из файлов, Azure Blob или ADLS Gen2 и подключает их к агентам. Запускается по запросам создания, запроса или диагностики базы знаний Azure AI Search. ★ 3 232звёзд репо microsoft-agent-framework-dotnet davila7 Субагент microsoft-agent-framework-dotnet генерирует, обновляет и переносит код автономных агентов и графовых сценариев на C# с пакетом Microsoft.Agents.AI. CX Codex 256установок langchain-rag langchain-ai Скилл langchain-rag даёт ИИ-агенту готовые шаблоны и код для сборки RAG-пайплайнов на LangChain в Python и TypeScript: от загрузки документов до векторного поиска. CX Codex +1 18 600установок agent-platform-rag-engine-management официальный google Скилл agent-platform-rag-engine-management управляет корпусами Agent Platform RAG Engine и ищет в них контекст через Google GenAI SDK. Он также помогает генерировать ответы с опорой на найденные данные. CX Codex +1 8 626установок rag-blueprint NVIDIA RAG Скилл помогает разворачивать, настраивать и диагностировать NVIDIA RAG Blueprint. Он пригодится для управления сервисами RAG в Docker, Kubernetes или библиотечном режиме. CX Codex +1 3 500установок rag-retrieval OrchestKit Скилл rag-retrieval для Claude Code, Codex, Cursor, Copilot, Gemini CLI и OpenCode с паттернами RAG-пайплайнов. Загружается автоматически при работе с векторным поиском, эмбеддингами, реранкингом или pgvector. CX Codex +1 1 500установок

Из того же набора38

Весь набор →
worker-services Скилл worker-services помогает создавать фоновые службы .NET на базе BackgroundService и Generic Host с корректным graceful shutdown и скоупами зависимостей. CX Codex +1 ★ 521звёзд репо aspire Скилл помогает создавать и обновлять AppHost для распределённых приложений на C# или TypeScript. Он пригодится для подключения сервисов, интеграций и тестов Aspire. CX Codex +1 ★ 521звёзд репо improve-skill-quality Скилл improve-skill-quality для Claude Code, диагностирующий и исправляющий скиллы dotnet/skills репозитория, которые теряют к собственному базелайну, не активируются, зависают или возвращают «нет улучшения». CX Codex +1 ★ 521звёзд репо orleans Помогает проектировать архитектуру на Microsoft Orleans: выбирать примитивы акторов, организовывать хранилище и диагностировать тесты. CX Codex +1 ★ 521звёзд репо hosted-mcp-tools Субагент hosted-mcp-tools помогает собрать агента Microsoft Foundry, который вызывает инструменты удалённого MCP-сервера, на C# или Python. CX Codex ★ 521звёзд репо agent-pipeline Субагент agent-pipeline помогает настроить конвейер агентов в Microsoft Agent Framework: подключить middleware, провайдеры контекста и чат-клиенты в C#, Python или Go. CX Codex ★ 521звёзд репо microsoft-agent-framework Скилл microsoft-agent-framework для Claude Code помогает создавать и проверять .NET-приложения с агентами и рабочими процессами Microsoft Agent Framework. Он включается при выборе типа агента, сессии, инструментов или способа размещения. CX Codex +1 ★ 521звёзд репо function-tools Субагент function-tools для Claude Code позволяет агенту вызывать функции вашего кода как инструменты. Агент сам выбирает, когда запросить погоду или получить данные. CX Codex ★ 521звёзд репо

Спонсоры проекта

Компании, которые помогают держать аналитику открытой