使用矢量存储文本搜索(预览版)

警告

语义内核文本搜索功能处于预览状态,需要重大更改的改进可能在发布前的有限情况下发生。

概述

矢量存储文本搜索实现使用 矢量存储连接器 来检索搜索结果。 这意味着,可以将矢量存储文本搜索与语义内核支持的任何向量存储以及 Microsoft.Extensions.VectorData.Abstractions 的任何实现一起使用。

限制

请参阅你正在使用的 Vector Store 连接器 列出的限制。

入门

下面的示例演示如何使用内存中向量存储来创建 VectorStoreTextSearch 和使用它来执行文本搜索。

using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.InMemory;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Embeddings;

// Create an embedding generation service.
var textEmbeddingGeneration = new OpenAITextEmbeddingGenerationService(
        modelId: TestConfiguration.OpenAI.EmbeddingModelId,
        apiKey: TestConfiguration.OpenAI.ApiKey);

// Construct an InMemory vector store.
var vectorStore = new InMemoryVectorStore();
var collectionName = "records";

// Get and create collection if it doesn't exist.
var recordCollection = vectorStore.GetCollection<TKey, TRecord>(collectionName);
await recordCollection.EnsureCollectionExistsAsync().ConfigureAwait(false);

// TODO populate the record collection with your test data
// Example https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Search/VectorStore_TextSearch.cs

// Create a text search instance using the InMemory vector store.
var textSearch = new VectorStoreTextSearch<DataModel>(recordCollection, textEmbeddingGeneration);

// Search and return results as TextSearchResult items
var query = "What is the Semantic Kernel?";
KernelSearchResults<TextSearchResult> textResults = await textSearch.GetTextSearchResultsAsync(query, new() { Top = 2, Skip = 0 });
Console.WriteLine("\n--- Text Search Results ---\n");
await foreach (TextSearchResult result in textResults.Results)
{
    Console.WriteLine($"Name:  {result.Name}");
    Console.WriteLine($"Value: {result.Value}");
    Console.WriteLine($"Link:  {result.Link}");
}

即将推出

即将推出更多内容。

即将推出

即将推出更多内容。

后续步骤

文档的以下部分介绍了如何:

  1. 创建插件并将其用于检索扩充生成(RAG)。
  2. 将文本搜索与函数调用结合使用
  3. 详细了解如何使用 矢量存储 进行文本搜索。

文本搜索抽象文本搜索插件文本搜索函数使用矢量存储调用文本搜索