为语义内核矢量存储连接器生成嵌入内容
警告
语义内核向量存储功能处于预览状态,需要中断性变更的改进可能仍发生在发布前的有限情况下。
语义内核支持使用许多常用 AI 服务现装生成嵌入。
可以直接构造这些服务,也可以将其添加到依赖项注入容器,并从该容器解析。
构造嵌入生成器
可以直接构造语义内核提供的文本嵌入服务的实例。
它们都实现 ITextEmbeddingGenerationService
接口。
// Constructing an Azure Open AI embedding generation service directly.
ITextEmbeddingGenerationService azureOpenAITES = new AzureOpenAITextEmbeddingGenerationService(
"text-embedding-ada-002",
"https://{myservice}.openai.azure.com/",
"apikey");
// Constructing an Olama embedding generation service directly.
ITextEmbeddingGenerationService olamaTES = new OllamaTextEmbeddingGenerationService(
"mxbai-embed-large",
new Uri("http://localhost:11434"));
还可以使用帮助程序向依赖项注入容器注册它们。
// Registering Google AI embedding generation service with a service collection.
var services = new ServiceCollection();
services.AddGoogleAIEmbeddingGeneration("text-embedding-004", "apiKey");
// Registering Mistral AI embedding generation service with the dependency injection container on
// the kernel builder.
var kernelBuilder = Kernel
.CreateBuilder()
.AddMistralTextEmbeddingGeneration("mistral-embed", "apiKey");
生成嵌入
若要使用所创建的方法 ITextEmbeddingGenerationService
,只需调用 GenerateEmbeddingAsync
该方法即可。
下面是在上传记录时生成嵌入的示例。
public async Task GenerateEmbeddingsAndUpsertAsync(
ITextEmbeddingGenerationService textEmbeddingGenerationService,
IVectorStoreRecordCollection<ulong, Hotel> collection)
{
// Upsert a record.
string descriptionText = "A place where everyone can be happy.";
ulong hotelId = 1;
// Generate the embedding.
ReadOnlyMemory<float> embedding =
await textEmbeddingGenerationService.GenerateEmbeddingAsync(descriptionText);
// Create a record and upsert with the already generated embedding.
await collection.UpsertAsync(new Hotel
{
HotelId = hotelId,
HotelName = "Hotel Happy",
Description = descriptionText,
DescriptionEmbedding = embedding,
Tags = new[] { "luxury", "pool" }
});
}
下面是在搜索时生成嵌入的示例。
public async Task GenerateEmbeddingsAndSearchAsync(
ITextEmbeddingGenerationService textEmbeddingGenerationService,
IVectorStoreRecordCollection<ulong, Hotel> collection)
{
// Upsert a record.
string descriptionText = "Find me a hotel with happiness in mind.";
// Generate the embedding.
ReadOnlyMemory<float> searchEmbedding =
await textEmbeddingGenerationService.GenerateEmbeddingAsync(descriptionText);
// Search using the already generated embedding.
List<VectorSearchResult<Hotel>> searchResult = await collection.VectorizedSearchAsync(searchEmbedding).ToListAsync();
// Print the first search result.
Console.WriteLine("Score for first result: " + searchResult.FirstOrDefault()?.Score);
Console.WriteLine("Hotel description for first result: " + searchResult.FirstOrDefault()?.Record.Description);
}
嵌入维度
矢量数据库通常需要指定每个向量在创建集合时具有的维度数。
不同的嵌入模型通常支持生成具有不同维度大小的矢量。 例如,Open AI text-embedding-ada-002
生成具有 1536 个维度的矢量。 某些模型还允许开发人员选择输出向量中所需的维度数,例如 Google text-embedding-004
默认生成维度为 768 的矢量,但允许开发人员选择介于 1 到 768 之间的任意数量的维度。
请务必确保嵌入模型生成的向量具有与数据库中匹配向量相同的维度数。
如果使用语义内核向量存储抽象创建集合,则需要通过批注或通过记录定义指定每个向量属性所需的维度数。 下面是将维度数设置为 1536 的两个示例。
[VectorStoreRecordVector(Dimensions: 1536)]
public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; }
new VectorStoreRecordVectorProperty("DescriptionEmbedding", typeof(float)) { Dimensions = 1536 }
提示
有关如何批注数据模型的详细信息,请参阅 定义数据模型。
提示
有关创建记录定义的详细信息,请参阅 使用记录定义定义架构。
即将推出
更多信息即将推出。
即将推出
更多信息即将推出。