警告
語意核心向量存放區功能處於預覽狀態,且需要重大變更的改善可能仍會在發行前有限的情況下發生。
警告
語意核心向量存放區功能處於預覽狀態,且需要重大變更的改善可能仍會在發行前有限的情況下發生。
語意核心向量存放區連接器支援多種產生內嵌方式。
內嵌可由開發人員產生,並在使用 VectorStoreCollection 時傳遞為記錄的一部分,或可在內部產生至 VectorStoreCollection。
讓向量存放區產生內嵌
您可以在向量存放區上設定內嵌產生器,允許在 upsert 和搜尋作業期間自動產生內嵌,而不需要手動前置處理。
若要在 upsert 上自動啟用產生向量,數據模型的向量屬性會定義為來源類型,例如字串,但仍以 VectorStoreVectorPropertyAttribute裝飾。
[VectorStoreVector(1536)]
public string Embedding { get; set; }
在 upsert 之前, Embedding 屬性應該包含應該從中產生向量的字串。 儲存在資料庫中的向量類型(例如 float32、float16 等)會衍生自已設定的內嵌產生器。
這很重要
這些向量屬性不支援擷取產生的向量或從中產生向量的原始文字。 它們也不會儲存原始文字。 如果需要儲存原始文字,應該新增個別的 Data 屬性來儲存它。
支持實作抽象概念的 Microsoft.Extensions.AI 內嵌產生器,而且可在各種層級設定:
在向量存放區上:您可以設定整個向量存放區的預設內嵌產生器。 除非覆寫,否則此產生器將用於所有集合和屬性。
using Microsoft.Extensions.AI; using Microsoft.SemanticKernel.Connectors.Qdrant; using OpenAI; using Qdrant.Client; var embeddingGenerator = new OpenAIClient("your key") .GetEmbeddingClient("your chosen model") .AsIEmbeddingGenerator(); var vectorStore = new QdrantVectorStore( new QdrantClient("localhost"), ownsClient: true, new QdrantVectorStoreOptions { EmbeddingGenerator = embeddingGenerator });在集合上:您可以為特定集合設定嵌入生成器,覆蓋存放區層級的生成器。
using Microsoft.Extensions.AI; using Microsoft.SemanticKernel.Connectors.Qdrant; using OpenAI; using Qdrant.Client; var embeddingGenerator = new OpenAIClient("your key") .GetEmbeddingClient("your chosen model") .AsIEmbeddingGenerator(); var collectionOptions = new QdrantCollectionOptions { EmbeddingGenerator = embeddingGenerator }; var collection = new QdrantCollection<ulong, MyRecord>( new QdrantClient("localhost"), "myCollection", ownsClient: true, collectionOptions);在記錄定義:使用
VectorStoreCollectionDefinition以程式設計方式定義屬性時,您可以為所有屬性指定內嵌產生器。using Microsoft.Extensions.AI; using Microsoft.Extensions.VectorData; using Microsoft.SemanticKernel.Connectors.Qdrant; using OpenAI; using Qdrant.Client; var embeddingGenerator = new OpenAIClient("your key") .GetEmbeddingClient("your chosen model") .AsIEmbeddingGenerator(); var definition = new VectorStoreCollectionDefinition { EmbeddingGenerator = embeddingGenerator, Properties = new List<VectorStoreProperty> { new VectorStoreKeyProperty("Key", typeof(ulong)), new VectorStoreVectorProperty("DescriptionEmbedding", typeof(string), dimensions: 1536) } }; var collectionOptions = new QdrantCollectionOptions { Definition = definition }; var collection = new QdrantCollection<ulong, MyRecord>( new QdrantClient("localhost"), "myCollection", ownsClient: true, collectionOptions);在向量屬性定義:以程序設計方式定義屬性時,您可以直接在 屬性上設定內嵌產生器。
using Microsoft.Extensions.AI; using Microsoft.Extensions.VectorData; using OpenAI; var embeddingGenerator = new OpenAIClient("your key") .GetEmbeddingClient("your chosen model") .AsIEmbeddingGenerator(); var vectorProperty = new VectorStoreVectorProperty("DescriptionEmbedding", typeof(string), dimensions: 1536) { EmbeddingGenerator = embeddingGenerator };
範例使用方式
下列範例示範如何使用內嵌產生器,在 upsert 和搜尋作業期間自動產生向量。 這種方法可藉由不需要手動預先計算內嵌,來簡化工作流程。
// The data model
internal class FinanceInfo
{
[VectorStoreKey]
public string Key { get; set; } = string.Empty;
[VectorStoreData]
public string Text { get; set; } = string.Empty;
// Note that the vector property is typed as a string, and
// its value is derived from the Text property. The string
// value will however be converted to a vector on upsert and
// stored in the database as a vector.
[VectorStoreVector(1536)]
public string Embedding => this.Text;
}
// Create an OpenAI embedding generator.
var embeddingGenerator = new OpenAIClient("your key")
.GetEmbeddingClient("your chosen model")
.AsIEmbeddingGenerator();
// Use the embedding generator with the vector store.
var vectorStore = new InMemoryVectorStore(new() { EmbeddingGenerator = embeddingGenerator });
var collection = vectorStore.GetCollection<string, FinanceInfo>("finances");
await collection.EnsureCollectionExistsAsync();
// Create some test data.
string[] budgetInfo =
{
"The budget for 2020 is EUR 100 000",
"The budget for 2021 is EUR 120 000",
"The budget for 2022 is EUR 150 000",
"The budget for 2023 is EUR 200 000",
"The budget for 2024 is EUR 364 000"
};
// Embeddings are generated automatically on upsert.
var records = budgetInfo.Select((input, index) => new FinanceInfo { Key = index.ToString(), Text = input });
await collection.UpsertAsync(records);
// Embeddings for the search is automatically generated on search.
var searchResult = collection.SearchAsync(
"What is my budget for 2024?",
top: 1);
// Output the matching result.
await foreach (var result in searchResult)
{
Console.WriteLine($"Key: {result.Record.Key}, Text: {result.Record.Text}");
}
自行產生內嵌
建構內嵌產生器
如需如何建構語意核心實例的範例,請參閱ITextEmbeddingGenerationService。
如需如何建構內嵌產生服務的資訊,請參閱 Microsoft.Extensions.AI。
使用語意核心在 upsert 上產生內嵌 ITextEmbeddingGenerationService
public async Task GenerateEmbeddingsAndUpsertAsync(
ITextEmbeddingGenerationService textEmbeddingGenerationService,
VectorStoreCollection<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" }
});
}
使用語意核心在搜尋時產生內嵌 ITextEmbeddingGenerationService
public async Task GenerateEmbeddingsAndSearchAsync(
ITextEmbeddingGenerationService textEmbeddingGenerationService,
VectorStoreCollection<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.
IAsyncEnumerable<VectorSearchResult<Hotel>> searchResult = collection.SearchAsync(searchEmbedding, top: 1);
List<VectorSearchResult<Hotel>> resultItems = await searchResult.ToListAsync();
// Print the first search result.
Console.WriteLine("Score for first result: " + resultItems.FirstOrDefault()?.Score);
Console.WriteLine("Hotel description for first result: " + resultItems.FirstOrDefault()?.Record.Description);
}
提示
如需產生內嵌的詳細資訊,請參閱 在語意核心中內嵌產生。
內嵌維度
向量資料庫通常需要您在建立集合時指定每個向量擁有的維度數目。
不同的內嵌模型通常支援產生具有各種維度大小的向量。 例如,OpenAI text-embedding-ada-002 會產生具有1536維度的向量。 有些模型也允許開發人員選擇輸出向量中想要的維度數目。 例如,Google text-embedding-004 預設會產生具有 768 個維度的向量,但可讓開發人員選擇介於 1 到 768 之間的任意數目維度。
請務必確保內嵌模型所產生的向量具有與資料庫中相符向量相同的維度數目。
如果使用語意核心向量存放區抽象概念建立集合,您必須透過註釋或記錄定義指定每個向量屬性所需的維度數目。 以下是將維度數目設定為1536的範例。
[VectorStoreVector(Dimensions: 1536)]
public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; }
new VectorStoreVectorProperty("DescriptionEmbedding", typeof(float), dimensions: 1536);
提示
如需如何標註數據模型的詳細資訊,請參閱 定義您的數據模型。
提示
如需建立記錄定義的詳細資訊,請參閱 使用記錄定義來定義架構。
即將推出
更多信息即將推出。
即將推出
更多信息即將推出。