共用方式為


將檢索增強生成(RAG)新增至語義核心代理程式

警告

語意核心代理程式 RAG 功能是實驗性的,可能會變更,而且只會根據意見反應和評估完成。

使用 TextSearchProvider 用於 RAG

Microsoft.SemanticKernel.Data.TextSearchProvider可讓代理程式根據使用者輸入擷取相關文件,並將其插入代理程序的內容中,以取得更明智的回應。 它會整合 Microsoft.SemanticKernel.Data.ITextSearch 實例與語意核心代理程式。 有多個 ITextSearch 實作存在,支援向量存放區和搜尋引擎整合的相似度搜尋。 您可以在這裡找到詳細資訊。

我們還提供一個Microsoft.SemanticKernel.Data.TextSearchStore,這是一個為特定需求設計的簡單文字數據向量儲存工具,用於增強式檢索生成。 TextSearchStore 具有內建架構,可用於在向量存放區中儲存和擷取文字數據。 如果您想要將自己的架構用於記憶體,請參閱 VectorStoreTextSearch

設定 TextSearchProvider

TextSearchProvider可與 和 VectorStore 搭配TextSearchStore使用,以儲存和搜尋文字檔。

下列範例示範如何設定和使用 TextSearchProvider 搭配 TextSearchStoreInMemoryVectorStore ,讓代理程式透過文字執行簡單的RAG。

// Create an embedding generator using Azure OpenAI.
var embeddingGenerator = new AzureOpenAIClient(new Uri("<Your_Azure_OpenAI_Endpoint>"), new AzureCliCredential())
    .GetEmbeddingClient("<Your_Deployment_Name>")
    .AsIEmbeddingGenerator(1536);

// Create a vector store to store documents.
var vectorStore = new InMemoryVectorStore(new() { EmbeddingGenerator = embeddingGenerator });

// Create a TextSearchStore for storing and searching text documents.
using var textSearchStore = new TextSearchStore<string>(vectorStore, collectionName: "FinancialData", vectorDimensions: 1536);

// Upsert documents into the store.
await textSearchStore.UpsertTextAsync(new[]
{
    "The financial results of Contoso Corp for 2024 is as follows:\nIncome EUR 154 000 000\nExpenses EUR 142 000 000",
    "The Contoso Corporation is a multinational business with its headquarters in Paris."
});

// Create an agent.
Kernel kernel = new Kernel();
ChatCompletionAgent agent = new()
{
    Name = "FriendlyAssistant",
    Instructions = "You are a friendly assistant",
    Kernel = kernel,
    // This setting must be set to true when using the on-demand RAG feature
    UseImmutableKernel = true
};

// Create an agent thread and add the TextSearchProvider.
ChatHistoryAgentThread agentThread = new();
var textSearchProvider = new TextSearchProvider(textSearchStore);
agentThread.AIContextProviders.Add(textSearchProvider);

// Use the agent with RAG capabilities.
ChatMessageContent response = await agent.InvokeAsync("Where is Contoso based?", agentThread).FirstAsync();
Console.WriteLine(response.Content);

進階功能:引文和篩選

支援 TextSearchStore 進階功能,例如依命名空間篩選結果,並在回應中包含引文。

包含引文

中的 TextSearchStore 檔可以包含元數據,例如來源名稱和連結,以在代理程式響應中產生引文。

await textSearchStore.UpsertDocumentsAsync(new[]
{
    new TextSearchDocument
    {
        Text = "The financial results of Contoso Corp for 2023 is as follows:\nIncome EUR 174 000 000\nExpenses EUR 152 000 000",
        SourceName = "Contoso 2023 Financial Report",
        SourceLink = "https://www.contoso.com/reports/2023.pdf",
        Namespaces = ["group/g2"]
    }
});

TextSearchProvider擷取此檔時,預設會在其回應中包含來源名稱和連結。

依命名空間篩選

在插入或更新檔案時,您可以選擇為每個檔案提供一個或多個命名空間。 命名空間可以是任何定義檔範圍的字串。 然後 TextSearchStore ,您可以將 設定為只將搜尋結果限製為符合所要求命名空間的記錄。

using var textSearchStore = new TextSearchStore<string>(
    vectorStore,
    collectionName: "FinancialData",
    vectorDimensions: 1536,
    new() { SearchNamespace = "group/g2" }
);

自動與隨選RAG比較

TextSearchProvider可以在每個代理程式調用期間自動執行搜尋,或在代理程式需要其他資訊時,允許透過工具呼叫進行隨選搜尋。

默認設定為 BeforeAIInvoke,這表示會使用傳遞至代理程式的訊息,在每個代理程式調用之前執行搜尋。 這可以變更為 OnDemandFunctionCalling,這可讓代理程式使用代理程式的搜尋字串進行工具呼叫,以執行搜尋。

var options = new TextSearchProviderOptions
{
    SearchTime = TextSearchProviderOptions.RagBehavior.OnDemandFunctionCalling,
};

var provider = new TextSearchProvider(mockTextSearch.Object, options: options);

警告

使用 TextSearchProvider 搭配 OnDemandFunctionCalling 時,代理程式上的UseImmutableKernel 設定必須設為 true,因為調用代理程式時需要複製核心。 請注意,設定 UseImmutableKerneltrue 表示在代理程式調用期間完成的任何核心數據修改,例如外掛程式,將不會在調用完成之後保留。

TextSearchProvider 選項

TextSearchProvider可以使用各種選項來設定 ,以自定義其行為。 選項會使用 TextSearchProviderOptions 類 提供給 TextSearchProvider 建構函式。

頁首

指定要從相似度搜尋傳回的結果數目上限。

  • 默認值:3

搜尋時間

控制文字搜尋的執行時機。 這些選項包括:

  • BeforeAIInvoke:每次叫用模型/代理程式時,都會執行搜尋,而結果會透過叫用內容提供給模型/代理程式。
  • OnDemandFunctionCalling:模型/代理程式可以視需要透過函式呼叫來執行搜尋。

PluginFunctionName

指定外掛程式的名稱,此插件方法在SearchTime設為OnDemandFunctionCalling時可供搜尋。

  • 默認值:“搜尋”

外掛功能描述

提供外掛程式方法的描述,當SearchTime設為OnDemandFunctionCalling時,該方法將可供搜尋。

  • 默認值:「允許搜尋其他資訊以協助回答用戶問題」。

ContextPrompt

在叫用時將文字區塊提供給 AI 模型時,需要提示向 AI 模型指出文字區塊的用途,以及其使用方式。 此設定允許覆寫 內建的預設 TextSearchProvider傳訊。

包含引文提示

在叫用時將文字區塊提供給 AI 模型時,需要提示告訴 AI 模型是否以及如何進行引文。 此設定允許覆寫 內建的預設 TextSearchProvider傳訊。

ContextFormatter

這個選擇性回呼可用來完全自定義 所產生的 TextSearchProvider文字。 根據預設,TextSearchProvider 會生成包含某些內容的文字。

  1. 提示,告知 AI 模型文字區塊的用途。
  2. 具有來源連結和名稱的文字區塊清單。
  3. 用於指導 AI 模型納入引文的提示。

您可以通過實現並提供這個回調函數來生成自己的輸出。

注意:如果提供此委派,則不會使用 ContextPromptIncludeCitationsPrompt 設定。

結合RAG與其他服務提供者

TextSearchProvider可以與其他提供者結合,例如 mem0WhiteboardProvider,以建立同時具有記憶體和擷取功能的代理程式。

// Add both mem0 and TextSearchProvider to the agent thread.
agentThread.AIContextProviders.Add(mem0Provider);
agentThread.AIContextProviders.Add(textSearchProvider);

// Use the agent with combined capabilities.
ChatMessageContent response = await agent.InvokeAsync("What was Contoso's income for 2023?", agentThread).FirstAsync();
Console.WriteLine(response.Content);

藉由結合這些功能,代理程式可以提供更個人化和內容感知的體驗。

後續步驟

即將推出

更多信息即將推出。

即將推出

更多信息即將推出。