Avec la génération d’incorporation de texte, vous pouvez utiliser un modèle IA pour générer des vecteurs (alias incorporations). Ces vecteurs encodent la signification sémantique du texte de telle sorte que les équations mathématiques peuvent être utilisées sur deux vecteurs pour comparer la similarité du texte d’origine.
Cela est utile pour les scénarios tels que la récupération d’une génération augmentée (RAG), où nous voulons rechercher une base de données d’informations pour du texte lié à une requête utilisateur.
Toutes les informations correspondantes peuvent ensuite être fournies en tant qu’entrée à la complétion de conversation, afin que le modèle IA ait plus de contexte lors de la réponse à une requête utilisateur.
Lorsque vous choisissez un modèle d’incorporation, vous devez prendre en compte les éléments suivants :
- Quelle est la taille des vecteurs générés par le modèle, et il est configurable, car cela affectera le coût de stockage vectoriel.
- Quel type d’éléments contient les vecteurs générés, par exemple float32, float16, etc. , car cela affectera le coût de stockage vectoriel.
- À quelle vitesse génère-t-il des vecteurs ?
- Combien coûte la génération ?
Configuration de votre environnement local
Certains des services IA peuvent être hébergés localement et peuvent nécessiter une configuration. Vous trouverez ci-dessous des instructions pour ceux qui le soutiennent.
Aucune configuration locale.
Aucune configuration locale.
Aucune configuration locale.
Aucune configuration locale.
Aucune configuration locale.
Pour exécuter Ollama localement à l’aide de Docker, utilisez la commande suivante pour démarrer un conteneur à l’aide du processeur.
docker run -d -v "c:\temp\ollama:/root/.ollama" -p 11434:11434 --name ollama ollama/ollama
Pour exécuter Ollama localement à l’aide de Docker, utilisez la commande suivante pour démarrer un conteneur à l’aide de GPU.
docker run -d --gpus=all -v "c:\temp\ollama:/root/.ollama" -p 11434:11434 --name ollama ollama/ollama
Une fois le conteneur démarré, lancez une fenêtre terminale pour le conteneur Docker, par exemple si vous utilisez docker Desktop, choisissez Open in Terminal à partir d’actions.
À partir de ce terminal, téléchargez les modèles requis, par exemple, nous téléchargeons le modèle d’incorporation de grande taille mxbai-embed.
ollama pull mxbai-embed-large
Clonez le référentiel contenant le modèle ONNX que vous souhaitez utiliser.
git clone https://huggingface.co/TaylorAI/bge-micro-v2
Installation des packages nécessaires
Avant d’ajouter la génération d’incorporation à votre noyau, vous devez installer les packages nécessaires. Voici les packages que vous devez installer pour chaque fournisseur de services IA.
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
Création de services de génération d’incorporation de texte
Maintenant que vous avez installé les packages nécessaires, vous pouvez créer un service de génération d’incorporation de texte. Vous trouverez ci-dessous plusieurs façons de créer des services de génération d'intégrations de texte à l'aide du noyau sémantique.
Ajout direct au noyau
Pour ajouter un service de génération d’incorporation de texte, vous pouvez utiliser le code suivant pour l’ajouter au fournisseur de services interne du noyau.
Important
Le connecteur de création d’intégration Azure OpenAI est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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();
Important
Le connecteur de génération d’intégration OpenAI est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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();
Important
Le connecteur de génération d’incorporation Mistral est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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();
Important
Le connecteur de génération d'intégration Google est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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();
Important
Le connecteur de génération d'incorporation de Hugging Face est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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();
Important
Le connecteur de génération incorporée Ollama est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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();
Important
Le connecteur de génération d’incorporation ONNX est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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();
Utilisation de l’injection de dépendances
Si vous utilisez l’injection de dépendances, vous souhaiterez probablement ajouter vos services de génération d’incorporation de texte directement au fournisseur de services. Cela est utile si vous souhaitez créer des singletons de vos services de génération d’incorporation et les réutiliser dans des noyaux temporaires.
Important
Le connecteur de génération d’incorporation Azure OpenAI est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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);
});
Important
Le connecteur de génération d'intégration OpenAI est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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);
});
Important
Le connecteur de génération d’incorporation Mistral est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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);
});
Important
Le connecteur de génération d’intégration Google est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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);
});
Important
Le connecteur de génération d'embarquement de Hugging Face est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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);
});
Important
Le connecteur de génération incorporée Ollama est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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);
});
Important
Le connecteur de génération d’incorporation ONNX est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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);
});
Création d’instances autonomes
Enfin, vous pouvez créer des instances du service directement afin de pouvoir les ajouter à un noyau ultérieurement ou les utiliser directement dans votre code sans jamais les injecter dans le noyau ou dans un fournisseur de services.
Important
Le connecteur de génération d'embeddings Azure OpenAI est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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.
);
Important
Le connecteur de génération d’intégration OpenAI est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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.
);
Important
Le connecteur de génération d’intégration Mistral est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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
);
Important
Le connecteur de génération d'intégration Google est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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
);
Important
Le connecteur de génération d’emplacements de Hugging Face est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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
);
Important
Le connecteur de création d'embeddings Ollama est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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();
Important
Le connecteur de génération d’incorporation ONNX est actuellement expérimental. Pour l’utiliser, vous devez ajouter #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
);
Utilisation des services de génération d’incorporation de texte
Tous les services de génération incorporés de texte implémentent le ITextEmbeddingGenerationService qui a une méthode unique GenerateEmbeddingsAsync qui peut générer des vecteurs ReadOnlyMemory<float> à partir de valeurs string fournies.
Une méthode d’extension GenerateEmbeddingAsync est également disponible pour les versions à valeur unique de la même action.
Voici un exemple d’appel du service avec plusieurs valeurs.
IList<ReadOnlyMemory<float>> embeddings =
await textEmbeddingGenerationService.GenerateEmbeddingsAsync(
[
"sample text 1",
"sample text 2"
]);
Voici un exemple d’appel du service avec une valeur unique.
using Microsoft.SemanticKernel.Embeddings;
ReadOnlyMemory<float> embedding =
await textEmbeddingGenerationService.GenerateEmbeddingAsync("sample text");
À venir
Plus d’informations seront bientôt disponibles.
À venir
Plus d’informations seront bientôt disponibles.