語意核心中的文字內嵌產生

透過文字內嵌產生,您可以使用 AI 模型來產生向量(也稱為內嵌)。 這些向量將文本的語義編碼,以便可以使用數學方程式在兩個向量上比較原始文本的相似性。 這對於捕獲擴增生成(RAG)等情境非常有用,我們可以搜尋資料庫中的文字信息,以滿足使用者的查詢需求。 接著可以將任何比對資訊作為聊天完成模型的輸入,讓 AI 模型在回答用戶查詢時能有更多的情境參考。

選擇內嵌模型時,您必須考慮下列事項:

  • 模型所產生的向量大小為何,而且可加以設定,因為這會影響您的向量儲存成本。
  • 產生的向量包含何種類型的元素,例如 float32、float16 等,因為這將影響您的向量儲存成本。
  • 產生向量的速度有多快?
  • 產生成本是多少?

提示

如需儲存和搜尋向量的詳細資訊,請參閱 什麼是語意核心向量存放區連接器?

提示

如需在語意核心中搭配向量存放區使用RAG的詳細資訊,請參閱 如何使用向量存放區搭配語意核心文字搜尋 和 什麼是語意核心文字搜尋外掛程式?

設定本機環境

部分 AI 服務可在本地託管,可能需要一些設定。 以下是支援此專案的指示。

無本地設置。

安裝必要的套件

在將嵌入生成新增至 kernel 之前,您必須安裝必要的套件。 以下是您需要為每個 AI 服務提供者安裝的套件。

dotnet add package Microsoft.SemanticKernel.Connectors.AzureOpenAI

創建文本嵌入生成服務

現在您已安裝必要的套件,您可以建立文字內嵌產生服務。 以下是您可以使用語意核心建立內嵌產生服務的數種方式。

直接新增至核心

若要新增文字內嵌產生服務,您可以使用下列程式代碼將它新增至核心的內部服務提供者。

重要

Azure OpenAI 內嵌產生連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0010。

using Microsoft.SemanticKernel;

#pragma warning disable SKEXP0010
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddAzureOpenAITextEmbeddingGeneration(
    deploymentName: "NAME_OF_YOUR_DEPLOYMENT", // Name of deployment, e.g. "text-embedding-ada-002".
    endpoint: "YOUR_AZURE_ENDPOINT",           // Name of Azure OpenAI service endpoint, e.g. https://myaiservice.openai.azure.com.
    apiKey: "YOUR_API_KEY",
    modelId: "MODEL_ID",          // Optional name of the underlying model if the deployment name doesn't match the model name, e.g. text-embedding-ada-002.
    serviceId: "YOUR_SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel.
    httpClient: new HttpClient(), // Optional; if not provided, the HttpClient from the kernel will be used.
    dimensions: 1536              // Optional number of dimensions to generate embeddings with.
);
Kernel kernel = kernelBuilder.Build();

使用相依性注入

如果您使用相依性注入,您可能會想將產生文字內嵌的服務直接新增到服務提供者裡。 如果您希望創建嵌入生成服務的單例,並在短暫性核心中重複使用它們,這將非常有幫助。

重要

Azure OpenAI 內嵌產生連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0010。

using Microsoft.SemanticKernel;

var builder = Host.CreateApplicationBuilder(args);

#pragma warning disable SKEXP0010
builder.Services.AddAzureOpenAITextEmbeddingGeneration(
    deploymentName: "NAME_OF_YOUR_DEPLOYMENT", // Name of deployment, e.g. "text-embedding-ada-002".
    endpoint: "YOUR_AZURE_ENDPOINT",           // Name of Azure OpenAI service endpoint, e.g. https://myaiservice.openai.azure.com.
    apiKey: "YOUR_API_KEY",
    modelId: "MODEL_ID",          // Optional name of the underlying model if the deployment name doesn't match the model name, e.g. text-embedding-ada-002.
    serviceId: "YOUR_SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel.
    dimensions: 1536              // Optional number of dimensions to generate embeddings with.
);

builder.Services.AddTransient((serviceProvider)=> {
    return new Kernel(serviceProvider);
});

建立獨立實例

最後,您可以直接建立服務的實例,以便稍後將它們新增至核心,或直接在程式代碼中使用這些實例,而不需要將它們插入核心或服務提供者中。

重要

Azure OpenAI 內嵌產生連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0010。

using Microsoft.SemanticKernel.Connectors.AzureOpenAI;

#pragma warning disable SKEXP0010
AzureOpenAITextEmbeddingGenerationService textEmbeddingGenerationService = new (
    deploymentName: "NAME_OF_YOUR_DEPLOYMENT", // Name of deployment, e.g. "text-embedding-ada-002".
    endpoint: "YOUR_AZURE_ENDPOINT",           // Name of Azure OpenAI service endpoint, e.g. https://myaiservice.openai.azure.com.
    apiKey: "YOUR_API_KEY",
    modelId: "MODEL_ID",          // Optional name of the underlying model if the deployment name doesn't match the model name, e.g. text-embedding-ada-002.
    httpClient: new HttpClient(), // Optional; if not provided, the HttpClient from the kernel will be used.
    dimensions: 1536              // Optional number of dimensions to generate embeddings with.
);

使用文本嵌入生成服務

所有文字內嵌生成服務都會實作 ITextEmbeddingGenerationService,它包含單一方法 GenerateEmbeddingsAsync,能從提供的 ReadOnlyMemory<float> 值生成 string 向量。 擴充方法 GenerateEmbeddingAsync 也適用於相同動作的單一值版本。

以下是如何使用多個值叫用服務的範例。

IList<ReadOnlyMemory<float>> embeddings =
    await textEmbeddingGenerationService.GenerateEmbeddingsAsync(
    [
        "sample text 1",
        "sample text 2"
    ]);

以下是如何使用單一值叫用服務的範例。

using Microsoft.SemanticKernel.Embeddings;

ReadOnlyMemory<float> embedding =
    await textEmbeddingGenerationService.GenerateEmbeddingAsync("sample text");

即將推出

更多信息即將推出。

即將推出

更多信息即將推出。