Skip to content

Commit

Permalink
.Net: Copy AzureOpenAITextEmbeddingGenerationService to Connectors.Az…
Browse files Browse the repository at this point in the history
…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
SergeyMenshykh committed Jul 1, 2024
1 parent 6af09e2 commit 05374c8
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace SemanticKernel.Connectors.AzureOpenAI.UnitTests.Extensions;

/// <summary>
/// Unit tests for <see cref="AzureOpenAIServiceCollectionExtensions"/> class.
/// Unit tests for the service collection extensions in the <see cref="AzureOpenAIServiceCollectionExtensions"/> class.
/// </summary>
public sealed class AzureOpenAIServiceCollectionExtensionsTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace SemanticKernel.Connectors.AzureOpenAI.UnitTests.Extensions;

/// <summary>
/// Unit tests for <see cref="AzureOpenAIServiceKernelBuilderExtensions"/> class.
/// Unit tests for the kernel builder extensions in the <see cref="AzureOpenAIServiceCollectionExtensions"/> class.
/// </summary>
public sealed class AzureOpenAIServiceKernelBuilderExtensionsTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@
<Description>Semantic Kernel connectors for Azure OpenAI. Contains clients for text generation, chat completion, embedding and DALL-E text to image.</Description>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Services\AzureOpenAITextEmbeddingGenerationService.cs" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="SemanticKernel.Connectors.AzureOpenAI.UnitTests" />
</ItemGroup>

<ItemGroup>
<None Include="Services\AzureOpenAITextEmbeddingGenerationService.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" VersionOverride="2.0.0-beta.2" />
</ItemGroup>
Expand Down
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);
}
}

0 comments on commit 05374c8

Please sign in to comment.