透過文字內嵌產生,您可以使用 AI 模型來產生向量(也稱為內嵌)。 這些向量將文本的語義編碼,以便可以使用數學方程式在兩個向量上比較原始文本的相似性。
這對於捕獲擴增生成(RAG)等情境非常有用,我們可以搜尋資料庫中的文字信息,以滿足使用者的查詢需求。
接著可以將任何比對資訊作為聊天完成模型的輸入,讓 AI 模型在回答用戶查詢時能有更多的情境參考。
選擇內嵌模型時,您必須考慮下列事項:
- 模型所產生的向量大小為何,而且可加以設定,因為這會影響您的向量儲存成本。
- 產生的向量包含何種類型的元素,例如 float32、float16 等,因為這將影響您的向量儲存成本。
- 產生向量的速度有多快?
- 產生成本是多少?
設定本機環境
某些 AI 服務可以裝載在本機,而且可能需要一些設定。 以下是支援此專案的指示。
若要使用 docker 在本機執行 Ollama,請使用下列命令來使用 CPU 啟動容器。
docker run -d -v "c:\temp\ollama:/root/.ollama" -p 11434:11434 --name ollama ollama/ollama
若要使用 docker 在本機執行 Ollama,請使用下列命令來使用 GPU 啟動容器。
docker run -d --gpus=all -v "c:\temp\ollama:/root/.ollama" -p 11434:11434 --name ollama ollama/ollama
容器啟動之後,請啟動 docker 容器的終端機視窗,例如,如果使用 Docker 桌面,請從動作中選擇 [Open in Terminal]。
在此終端機下載所需的模型,例如,我們在這裡下載 mxbai-embed-large 嵌入模型。
ollama pull mxbai-embed-large
複製包含您想要使用之 ONNX 模型的存放庫。
git clone https://huggingface.co/TaylorAI/bge-micro-v2
安裝必要的套件
在將嵌入生成新增至 kernel 之前,您必須安裝必要的套件。 以下是您需要為每個 AI 服務提供者安裝的套件。
dotnet add package Microsoft.SemanticKernel.Connectors.AzureOpenAI
dotnet add package Microsoft.SemanticKernel.Connectors.OpenAI
dotnet add package Microsoft.SemanticKernel.Connectors.MistralAI --prerelease
dotnet add package Microsoft.SemanticKernel.Connectors.Google --prerelease
dotnet add package Microsoft.SemanticKernel.Connectors.HuggingFace --prerelease
dotnet add package Microsoft.SemanticKernel.Connectors.Ollama --prerelease
dotnet add package Microsoft.SemanticKernel.Connectors.Onnx --prerelease
創建文本嵌入生成服務
現在您已安裝必要的套件,您可以建立文字內嵌產生服務。 以下是您可以使用語意核心建立內嵌產生服務的數種方式。
直接新增至核心
若要新增文字內嵌產生服務,您可以使用下列程式代碼將它新增至核心的內部服務提供者。
重要
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();
重要
OpenAI 內嵌產生連接器目前為實驗性。 若要使用它,您必須新增 #pragma warning disable SKEXP0010。
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0010
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAITextEmbeddingGeneration(
modelId: "MODEL_ID", // Name of the embedding model, e.g. "text-embedding-ada-002".
apiKey: "YOUR_API_KEY",
orgId: "YOUR_ORG_ID", // Optional organization id.
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();
重要
Mistral 嵌入生成連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0070
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddMistralTextEmbeddingGeneration(
modelId: "NAME_OF_MODEL", // Name of the embedding model, e.g. "mistral-embed".
apiKey: "API_KEY",
endpoint: new Uri("YOUR_ENDPOINT"), // Optional uri endpoint including the port where MistralAI server is hosted. Default is https://api.mistral.ai.
serviceId: "SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
httpClient: new HttpClient() // Optional; for customizing HTTP client
);
Kernel kernel = kernelBuilder.Build();
重要
Google 嵌入生成連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Google;
#pragma warning disable SKEXP0070
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddGoogleAIEmbeddingGeneration(
modelId: "NAME_OF_MODEL", // Name of the embedding model, e.g. "models/text-embedding-004".
apiKey: "API_KEY",
apiVersion: GoogleAIVersion.V1, // Optional
serviceId: "SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
httpClient: new HttpClient() // Optional; for customizing HTTP client
);
Kernel kernel = kernelBuilder.Build();
重要
Hugging Face 嵌入生成連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0070
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddHuggingFaceTextEmbeddingGeneration(
model: "NAME_OF_MODEL", // Name of the embedding model.
apiKey: "API_KEY",
endpoint: new Uri("YOUR_ENDPOINT"), // Optional
serviceId: "SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
httpClient: new HttpClient() // Optional; for customizing HTTP client
);
Kernel kernel = kernelBuilder.Build();
重要
Ollama 嵌入生成連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0070
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOllamaTextEmbeddingGeneration(
modelId: "NAME_OF_MODEL", // E.g. "mxbai-embed-large" if mxbai-embed-large was downloaded as described above.
endpoint: new Uri("YOUR_ENDPOINT"), // E.g. "http://localhost:11434" if Ollama has been started in docker as described above.
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
Kernel kernel = kernelBuilder.Build();
重要
ONNX 內嵌產生連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0070
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddBertOnnxTextEmbeddingGeneration(
onnxModelPath: "PATH_ON_DISK", // Path to the model on disk e.g. C:\Repos\huggingface\microsoft\TaylorAI\bge-micro-v2\onnx\model.onnx
vocabPath: "VOCABULARY_PATH_ON_DISK",// Path to the vocabulary file on disk, e.g. C:\Repos\huggingface\TailorAI\bge-micro-v2\vocab.txt
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
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);
});
重要
OpenAI 內嵌產生連接器目前為實驗性。 若要使用它,您必須新增 #pragma warning disable SKEXP0010。
using Microsoft.SemanticKernel;
#pragma warning disable SKEXP0010
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddOpenAITextEmbeddingGeneration(
modelId: "MODEL_ID", // Name of the embedding model, e.g. "text-embedding-ada-002".
apiKey: "YOUR_API_KEY",
orgId: "YOUR_ORG_ID", // Optional organization id.
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);
});
重要
Mistral 嵌入式產生連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0070
builder.Services.AddMistralTextEmbeddingGeneration(
modelId: "NAME_OF_MODEL", // Name of the embedding model, e.g. "mistral-embed".
apiKey: "API_KEY",
endpoint: new Uri("YOUR_ENDPOINT"), // Optional uri endpoint including the port where MistralAI server is hosted. Default is https://api.mistral.ai.
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
重要
Google 嵌入式生成連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Google;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0070
builder.Services.AddGoogleAIEmbeddingGeneration(
modelId: "NAME_OF_MODEL", // Name of the embedding model, e.g. "models/text-embedding-004".
apiKey: "API_KEY",
apiVersion: GoogleAIVersion.V1, // Optional
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
重要
Hugging Face 嵌入生成連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0070
builder.Services.AddHuggingFaceTextEmbeddingGeneration(
model: "NAME_OF_MODEL", // Name of the embedding model.
apiKey: "API_KEY",
endpoint: new Uri("YOUR_ENDPOINT"), // Optional
serviceId: "SERVICE_ID", // Optional; for targeting specific services within Semantic Kernel
httpClient: new HttpClient() // Optional; for customizing HTTP client
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
重要
Ollama 嵌入生成連結器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0070
builder.Services.AddOllamaTextEmbeddingGeneration(
modelId: "NAME_OF_MODEL", // E.g. "mxbai-embed-large" if mxbai-embed-large was downloaded as described above.
endpoint: new Uri("YOUR_ENDPOINT"), // E.g. "http://localhost:11434" if Ollama has been started in docker as described above.
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
builder.Services.AddTransient((serviceProvider)=> {
return new Kernel(serviceProvider);
});
重要
ONNX 內嵌產生連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel;
var builder = Host.CreateApplicationBuilder(args);
#pragma warning disable SKEXP0070
builder.Services.AddBertOnnxTextEmbeddingGeneration(
onnxModelPath: "PATH_ON_DISK", // Path to the model on disk e.g. C:\Repos\huggingface\microsoft\TaylorAI\bge-micro-v2\onnx\model.onnx
vocabPath: "VOCABULARY_PATH_ON_DISK",// Path to the vocabulary file on disk, e.g. C:\Repos\huggingface\TailorAI\bge-micro-v2\vocab.txt
serviceId: "SERVICE_ID" // Optional; for targeting specific services within Semantic Kernel
);
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.
);
重要
OpenAI 內嵌產生連接器目前為實驗性。 若要使用它,您必須新增 #pragma warning disable SKEXP0010。
#pragma warning disable SKEXP0010
using Microsoft.SemanticKernel.Connectors.OpenAI;
OpenAITextEmbeddingGenerationService textEmbeddingGenerationService = new (
modelId: "MODEL_ID", // Name of the embedding model, e.g. "text-embedding-ada-002".
apiKey: "YOUR_API_KEY",
organization: "YOUR_ORG_ID", // Optional organization id.
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.
);
重要
Mistral 嵌入生成連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel.Connectors.MistralAI;
#pragma warning disable SKEXP0070
MistralAITextEmbeddingGenerationService textEmbeddingGenerationService = new (
modelId: "NAME_OF_MODEL", // Name of the embedding model, e.g. "mistral-embed".
apiKey: "API_KEY",
endpoint: new Uri("YOUR_ENDPOINT"), // Optional uri endpoint including the port where MistralAI server is hosted. Default is https://api.mistral.ai.
httpClient: new HttpClient() // Optional; for customizing HTTP client
);
重要
Google 嵌入式生成連接器目前處於實驗階段。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel.Connectors.Google;
#pragma warning disable SKEXP0070
GoogleAITextEmbeddingGenerationService textEmbeddingGenerationService = new (
modelId: "NAME_OF_MODEL", // Name of the embedding model, e.g. "models/text-embedding-004".
apiKey: "API_KEY",
apiVersion: GoogleAIVersion.V1, // Optional
httpClient: new HttpClient() // Optional; for customizing HTTP client
);
重要
Hugging Face 嵌入生成連接器目前仍處於實驗階段。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel.Connectors.HuggingFace;
#pragma warning disable SKEXP0070
HuggingFaceTextEmbeddingGenerationService textEmbeddingGenerationService = new (
model: "NAME_OF_MODEL", // Name of the embedding model.
apiKey: "API_KEY",
endpoint: new Uri("YOUR_ENDPOINT"), // Optional
httpClient: new HttpClient() // Optional; for customizing HTTP client
);
重要
Ollama 內嵌產生連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel.Embeddings;
using OllamaSharp;
#pragma warning disable SKEXP0070
using var ollamaClient = new OllamaApiClient(
uriString: "YOUR_ENDPOINT" // E.g. "http://localhost:11434" if Ollama has been started in docker as described above.
defaultModel: "NAME_OF_MODEL" // E.g. "mxbai-embed-large" if mxbai-embed-large was downloaded as described above.
);
ITextEmbeddingGenerationService textEmbeddingGenerationService = ollamaClient.AsTextEmbeddingGenerationService();
重要
ONNX 內嵌產生連接器目前是實驗性的。 若要使用它,您必須新增 #pragma warning disable SKEXP0070。
using Microsoft.SemanticKernel.Connectors.Onnx;
#pragma warning disable SKEXP0070
BertOnnxTextEmbeddingGenerationService textEmbeddingGenerationService = await BertOnnxTextEmbeddingGenerationService.CreateAsync(
onnxModelPath: "PATH_ON_DISK", // Path to the model on disk e.g. C:\Repos\huggingface\microsoft\TaylorAI\bge-micro-v2\onnx\model.onnx
vocabPath: "VOCABULARY_PATH_ON_DISK" // Path to the vocabulary file on disk, e.g. C:\Repos\huggingface\TailorAI\bge-micro-v2\vocab.txt
);
使用文本嵌入生成服務
所有文字內嵌生成服務都會實作 ITextEmbeddingGenerationService,它包含單一方法 GenerateEmbeddingsAsync,能從提供的 string 值生成 ReadOnlyMemory<float> 向量。
擴充方法 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");