-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Copy AzureOpenAITextEmbeddingGenerationService to Connectors.Az…
…ureOpenAI project (#7022) ### Motivation and Context This PR prepares the AzureOpenAITextEmbeddingGenerationService for migration to the new Azure AI SDK v2. The AzureOpenAITextEmbeddingGenerationService is copied to the Connectors.AzureOpenAI project as is and excluded from compilation to simplify code review of the next PR, which will refactor it to use the new Azure SDK. The next PR will also add unit/integration tests, along with service collection and kernel builder extension methods. ### Description The AzureOpenAITextEmbeddingGenerationService class was copied as is to the Connectors.AzureOpenAI project. The class build action was set to none to exclude it temporarily from the compilation process until it gets refactored to the new SDK.
- Loading branch information
1 parent
6af09e2
commit 05374c8
Showing
4 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...c/Connectors/Connectors.AzureOpenAI/Services/AzureOpenAITextEmbeddingGenerationService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.AI.OpenAI; | ||
using Azure.Core; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel.Embeddings; | ||
using Microsoft.SemanticKernel.Services; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.AzureOpenAI; | ||
|
||
/// <summary> | ||
/// Azure OpenAI text embedding service. | ||
/// </summary> | ||
[Experimental("SKEXP0010")] | ||
public sealed class AzureOpenAITextEmbeddingGenerationService : ITextEmbeddingGenerationService | ||
{ | ||
private readonly AzureOpenAIClientCore _core; | ||
private readonly int? _dimensions; | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="AzureOpenAITextEmbeddingGenerationService"/> client instance using API Key auth. | ||
/// </summary> | ||
/// <param name="deploymentName">Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource</param> | ||
/// <param name="endpoint">Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param> | ||
/// <param name="apiKey">Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param> | ||
/// <param name="modelId">Azure OpenAI model id, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource</param> | ||
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param> | ||
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.</param> | ||
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param> | ||
public AzureOpenAITextEmbeddingGenerationService( | ||
string deploymentName, | ||
string endpoint, | ||
string apiKey, | ||
string? modelId = null, | ||
HttpClient? httpClient = null, | ||
ILoggerFactory? loggerFactory = null, | ||
int? dimensions = null) | ||
{ | ||
this._core = new(deploymentName, endpoint, apiKey, httpClient, loggerFactory?.CreateLogger(typeof(AzureOpenAITextEmbeddingGenerationService))); | ||
|
||
this._core.AddAttribute(AIServiceExtensions.ModelIdKey, modelId); | ||
|
||
this._dimensions = dimensions; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="AzureOpenAITextEmbeddingGenerationService"/> client instance supporting AAD auth. | ||
/// </summary> | ||
/// <param name="deploymentName">Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource</param> | ||
/// <param name="endpoint">Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param> | ||
/// <param name="credential">Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc.</param> | ||
/// <param name="modelId">Azure OpenAI model id, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource</param> | ||
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param> | ||
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.</param> | ||
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param> | ||
public AzureOpenAITextEmbeddingGenerationService( | ||
string deploymentName, | ||
string endpoint, | ||
TokenCredential credential, | ||
string? modelId = null, | ||
HttpClient? httpClient = null, | ||
ILoggerFactory? loggerFactory = null, | ||
int? dimensions = null) | ||
{ | ||
this._core = new(deploymentName, endpoint, credential, httpClient, loggerFactory?.CreateLogger(typeof(AzureOpenAITextEmbeddingGenerationService))); | ||
|
||
this._core.AddAttribute(AIServiceExtensions.ModelIdKey, modelId); | ||
|
||
this._dimensions = dimensions; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new <see cref="AzureOpenAITextEmbeddingGenerationService"/> client. | ||
/// </summary> | ||
/// <param name="deploymentName">Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource</param> | ||
/// <param name="openAIClient">Custom <see cref="OpenAIClient"/> for HTTP requests.</param> | ||
/// <param name="modelId">Azure OpenAI model id, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource</param> | ||
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.</param> | ||
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param> | ||
public AzureOpenAITextEmbeddingGenerationService( | ||
string deploymentName, | ||
OpenAIClient openAIClient, | ||
string? modelId = null, | ||
ILoggerFactory? loggerFactory = null, | ||
int? dimensions = null) | ||
{ | ||
this._core = new(deploymentName, openAIClient, loggerFactory?.CreateLogger(typeof(AzureOpenAITextEmbeddingGenerationService))); | ||
|
||
this._core.AddAttribute(AIServiceExtensions.ModelIdKey, modelId); | ||
|
||
this._dimensions = dimensions; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IReadOnlyDictionary<string, object?> Attributes => this._core.Attributes; | ||
|
||
/// <inheritdoc/> | ||
public Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync( | ||
IList<string> data, | ||
Kernel? kernel = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return this._core.GetEmbeddingsAsync(data, kernel, this._dimensions, cancellationToken); | ||
} | ||
} |