경고
의미 체계 커널 에이전트 RAG 기능은 실험적이며 변경될 수 있으며 피드백 및 평가에 따라 종료됩니다.
RAG에 TextSearchProvider 사용
이를 통해 에이전트는 Microsoft.SemanticKernel.Data.TextSearchProvider 사용자 입력에 따라 관련 문서를 검색하고 에이전트의 컨텍스트에 삽입하여 보다 정보에 입각한 응답을 할 수 있습니다.
인스턴스를 Microsoft.SemanticKernel.Data.ITextSearch 의미 체계 커널 에이전트와 통합합니다.
벡터 저장소 및 검색 엔진 통합에서 유사성 검색을 지원하는 여러 ITextSearch 구현이 있습니다.
자세한 내용은 여기에서 확인할 수 있습니다.
또한 검색 증강 생성을 위해 텍스트 데이터를 간단하게 저장할 수 있는 벡터 스토리지 기능인 Microsoft.SemanticKernel.Data.TextSearchStore을 제공합니다.
TextSearchStore 에는 벡터 저장소에 텍스트 데이터를 저장하고 검색하기 위한 기본 제공 스키마가 있습니다. 스토리지에 고유한 스키마를 사용하려면 VectorStoreTextSearch를 확인하세요.
TextSearchProvider 설정
TextSearchProvider는 VectorStore 및 TextSearchStore와 함께 텍스트 문서를 저장하고 검색하는 데 사용할 수 있습니다.
다음 예제에서는 에이전트가 텍스트에 대해 간단한 RAG를 수행하도록 TextSearchProvider를 TextSearchStore 및 InMemoryVectorStore와 함께 설정하고 사용하는 방법을 보여 줍니다.
// 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로 설정되어야 합니다.
UseImmutableKernel 설정을 true로 변경하면 플러그인 등을 통해 에이전트 호출 중에 수행된 커널 데이터 수정이 호출이 완료된 후에는 유지되지 않음을 의미합니다.
TextSearchProvider 옵션
동작 TextSearchProvider 을 사용자 지정하는 다양한 옵션을 사용하여 구성할 수 있습니다.
TextSearchProviderOptions 클래스를 사용하여 TextSearchProvider 생성자에 옵션이 제공됩니다.
최상위
유사성 검색에서 반환할 최대 결과 수를 지정합니다.
- 기본값: 3
검색시간
텍스트 검색이 수행되는 시기를 제어합니다. 옵션은 다음과 같습니다.
- BeforeAIInvoke: 호출 직전에 모델/에이전트가 호출될 때마다 검색이 수행되고 호출 컨텍스트를 통해 모델/에이전트에 결과가 제공됩니다.
- OnDemandFunctionCalling: 모델/에이전트가 함수 호출을 통해 요청 시 검색을 수행할 수 있습니다.
PluginFunctionName
으로 설정된 경우 SearchTime 검색에 사용할 수 있는 플러그 인 메서드의 이름을 지정합니다 OnDemandFunctionCalling.
- 기본값: "검색"
플러그인 기능 설명
로 설정된 SearchTime경우 OnDemandFunctionCalling 검색에 사용할 수 있는 플러그 인 메서드에 대한 설명을 제공합니다.
- 기본값: "사용자 질문에 대답하는 데 도움이 되는 추가 정보를 검색할 수 있습니다."
[문맥 프롬프트]
호출 시 AI 모델에 텍스트 청크를 제공할 때 텍스트 청크의 용도와 사용 방법을 AI 모델에 나타내려면 프롬프트가 필요합니다.
이 설정을 사용하면 TextSearchProvider에 기본 제공된 기본 메시지를 재정의할 수 있습니다.
인용 포함 요청
호출 시 AI 모델에 텍스트 청크를 제공할 때는 AI 모델에 인용 수행 여부와 방법을 알려야 합니다.
이 설정을 사용하면 TextSearchProvider에 기본 제공된 기본 메시지를 재정의할 수 있습니다.
ContextFormatter
이 선택적 콜백은 TextSearchProvider에 의해 생성된 텍스트를 완전히 사용자 지정하는 데 사용할 수 있습니다.
기본적으로 TextSearchProvider는 포함된 텍스트를 생성합니다.
- 텍스트 청크의 용도를 AI 모델에 알리는 프롬프트입니다.
- 원본 링크와 이름이 있는 텍스트 청크 목록입니다.
- 인용을 포함하는 방법에 대해 AI 모델에 지시하는 프롬프트입니다.
이 콜백을 구현하고 제공하여 고유한 출력을 작성할 수 있습니다.
참고: 이 대리자가 제공되면 ContextPrompt 설정 및 IncludeCitationsPrompt 설정이 사용되지 않습니다.
RAG을 다른 공급자와 결합
TextSearchProvider는 메모리 및 검색 기능을 갖춘 에이전트를 만들기 위해 mem0 또는 WhiteboardProvider 같은 다른 공급자와 결합할 수 있습니다.
// 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);
에이전트는 이러한 기능을 결합하여 보다 개인화된 컨텍스트 인식 환경을 제공할 수 있습니다.
다음 단계
곧 출시
자세한 내용은 곧 제공될 예정입니다.
곧 출시
자세한 내용은 곧 제공될 예정입니다.