Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/dotnet-build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ jobs:
AzureOpenAI__DeploymentName: ${{ vars.AZUREOPENAI__DEPLOYMENTNAME }}
AzureOpenAIEmbeddings__DeploymentName: ${{ vars.AZUREOPENAIEMBEDDING__DEPLOYMENTNAME }}
AzureOpenAI__Endpoint: ${{ secrets.AZUREOPENAI__ENDPOINT }}
AzureOpenAIEmbeddings__Endpoint: ${{ secrets.AZUREOPENAI__ENDPOINT }}
AzureOpenAIEmbeddings__Endpoint: ${{ secrets.AZUREOPENAI_EASTUS__ENDPOINT }}
AzureOpenAI__ApiKey: ${{ secrets.AZUREOPENAI__APIKEY }}
AzureOpenAIEmbeddings__ApiKey: ${{ secrets.AZUREOPENAI__APIKEY }}
AzureOpenAIEmbeddings__ApiKey: ${{ secrets.AZUREOPENAI_EASTUS__APIKEY }}
Planners__AzureOpenAI__ApiKey: ${{ secrets.PLANNERS__AZUREOPENAI__APIKEY }}
Planners__AzureOpenAI__Endpoint: ${{ secrets.PLANNERS__AZUREOPENAI__ENDPOINT }}
Planners__AzureOpenAI__DeploymentName: ${{ vars.PLANNERS__AZUREOPENAI__DEPLOYMENTNAME }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,25 @@ internal async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAs
/// </summary>
/// <param name="data">List of strings to generate embeddings for</param>
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use throughout the operation.</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>List of embeddings</returns>
internal async Task<IList<ReadOnlyMemory<float>>> GetEmbeddingsAsync(
IList<string> data,
Kernel? kernel,
int? dimensions,
CancellationToken cancellationToken)
{
var result = new List<ReadOnlyMemory<float>>(data.Count);

if (data.Count > 0)
{
var response = await RunRequestAsync(() => this.Client.GetEmbeddingsAsync(new(this.DeploymentOrModelName, data), cancellationToken)).ConfigureAwait(false);
var embeddingsOptions = new EmbeddingsOptions(this.DeploymentOrModelName, data)
{
Dimensions = dimensions
};

var response = await RunRequestAsync(() => this.Client.GetEmbeddingsAsync(embeddingsOptions, cancellationToken)).ConfigureAwait(false);
var embeddings = response.Value.Data;

if (embeddings.Count != data.Count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static class OpenAIMemoryBuilderExtensions
/// <param name="apiKey">Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param>
/// <param name="modelId">Model identifier</param>
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>Self instance</returns>
[Experimental("SKEXP0010")]
public static MemoryBuilder WithAzureOpenAITextEmbeddingGeneration(
Expand All @@ -31,7 +32,8 @@ public static MemoryBuilder WithAzureOpenAITextEmbeddingGeneration(
string endpoint,
string apiKey,
string? modelId = null,
HttpClient? httpClient = null)
HttpClient? httpClient = null,
int? dimensions = null)
{
return builder.WithTextEmbeddingGeneration((loggerFactory, builderHttpClient) =>
new AzureOpenAITextEmbeddingGenerationService(
Expand All @@ -40,7 +42,8 @@ public static MemoryBuilder WithAzureOpenAITextEmbeddingGeneration(
apiKey,
modelId,
HttpClientProvider.GetHttpClient(httpClient ?? builderHttpClient),
loggerFactory));
loggerFactory,
dimensions));
}

/// <summary>
Expand All @@ -53,6 +56,7 @@ public static MemoryBuilder WithAzureOpenAITextEmbeddingGeneration(
/// <param name="credential">Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc.</param>
/// <param name="modelId">Model identifier</param>
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>Self instance</returns>
[Experimental("SKEXP0010")]
public static MemoryBuilder WithAzureOpenAITextEmbeddingGeneration(
Expand All @@ -61,7 +65,8 @@ public static MemoryBuilder WithAzureOpenAITextEmbeddingGeneration(
string endpoint,
TokenCredential credential,
string? modelId = null,
HttpClient? httpClient = null)
HttpClient? httpClient = null,
int? dimensions = null)
{
return builder.WithTextEmbeddingGeneration((loggerFactory, builderHttpClient) =>
new AzureOpenAITextEmbeddingGenerationService(
Expand All @@ -70,7 +75,8 @@ public static MemoryBuilder WithAzureOpenAITextEmbeddingGeneration(
credential,
modelId,
HttpClientProvider.GetHttpClient(httpClient ?? builderHttpClient),
loggerFactory));
loggerFactory,
dimensions));
}

/// <summary>
Expand All @@ -82,21 +88,24 @@ public static MemoryBuilder WithAzureOpenAITextEmbeddingGeneration(
/// <param name="apiKey">OpenAI API key, see https://platform.openai.com/account/api-keys</param>
/// <param name="orgId">OpenAI organization id. This is usually optional unless your account belongs to multiple organizations.</param>
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>Self instance</returns>
[Experimental("SKEXP0010")]
public static MemoryBuilder WithOpenAITextEmbeddingGeneration(
this MemoryBuilder builder,
string modelId,
string apiKey,
string? orgId = null,
HttpClient? httpClient = null)
HttpClient? httpClient = null,
int? dimensions = null)
{
return builder.WithTextEmbeddingGeneration((loggerFactory, builderHttpClient) =>
new OpenAITextEmbeddingGenerationService(
modelId,
apiKey,
orgId,
HttpClientProvider.GetHttpClient(httpClient ?? builderHttpClient),
loggerFactory));
loggerFactory,
dimensions));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public static IServiceCollection AddOpenAITextGeneration(this IServiceCollection
/// <param name="serviceId">A local identifier for the given AI service</param>
/// <param name="modelId">Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param>
/// <param name="httpClient">The HttpClient to use with this service.</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
[Experimental("SKEXP0010")]
public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
Expand All @@ -347,7 +348,8 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
string apiKey,
string? serviceId = null,
string? modelId = null,
HttpClient? httpClient = null)
HttpClient? httpClient = null,
int? dimensions = null)
{
Verify.NotNull(builder);
Verify.NotNullOrWhiteSpace(deploymentName);
Expand All @@ -361,7 +363,8 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
apiKey,
modelId,
HttpClientProvider.GetHttpClient(httpClient, serviceProvider),
serviceProvider.GetService<ILoggerFactory>()));
serviceProvider.GetService<ILoggerFactory>(),
dimensions));

return builder;
}
Expand All @@ -375,6 +378,7 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
/// <param name="apiKey">Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param>
/// <param name="serviceId">A local identifier for the given AI service</param>
/// <param name="modelId">Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>The same instance as <paramref name="services"/>.</returns>
[Experimental("SKEXP0010")]
public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
Expand All @@ -383,7 +387,8 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
string endpoint,
string apiKey,
string? serviceId = null,
string? modelId = null)
string? modelId = null,
int? dimensions = null)
{
Verify.NotNull(services);
Verify.NotNullOrWhiteSpace(deploymentName);
Expand All @@ -397,7 +402,8 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
apiKey,
modelId,
HttpClientProvider.GetHttpClient(serviceProvider),
serviceProvider.GetService<ILoggerFactory>()));
serviceProvider.GetService<ILoggerFactory>(),
dimensions));
}

/// <summary>
Expand All @@ -410,6 +416,7 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
/// <param name="serviceId">A local identifier for the given AI service</param>
/// <param name="modelId">Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param>
/// <param name="httpClient">The HttpClient to use with this service.</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
[Experimental("SKEXP0010")]
public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
Expand All @@ -419,7 +426,8 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
TokenCredential credential,
string? serviceId = null,
string? modelId = null,
HttpClient? httpClient = null)
HttpClient? httpClient = null,
int? dimensions = null)
{
Verify.NotNull(builder);
Verify.NotNullOrWhiteSpace(deploymentName);
Expand All @@ -433,7 +441,8 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
credential,
modelId,
HttpClientProvider.GetHttpClient(httpClient, serviceProvider),
serviceProvider.GetService<ILoggerFactory>()));
serviceProvider.GetService<ILoggerFactory>(),
dimensions));

return builder;
}
Expand All @@ -447,6 +456,7 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
/// <param name="credential">Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc.</param>
/// <param name="serviceId">A local identifier for the given AI service</param>
/// <param name="modelId">Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>The same instance as <paramref name="services"/>.</returns>
[Experimental("SKEXP0010")]
public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
Expand All @@ -455,7 +465,8 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
string endpoint,
TokenCredential credential,
string? serviceId = null,
string? modelId = null)
string? modelId = null,
int? dimensions = null)
{
Verify.NotNull(services);
Verify.NotNullOrWhiteSpace(deploymentName);
Expand All @@ -469,7 +480,8 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
credential,
modelId,
HttpClientProvider.GetHttpClient(serviceProvider),
serviceProvider.GetService<ILoggerFactory>()));
serviceProvider.GetService<ILoggerFactory>(),
dimensions));
}

/// <summary>
Expand All @@ -480,14 +492,16 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
/// <param name="openAIClient"><see cref="OpenAIClient"/> to use for the service. If null, one must be available in the service provider when this service is resolved.</param>
/// <param name="serviceId">A local identifier for the given AI service</param>
/// <param name="modelId">Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
[Experimental("SKEXP0010")]
public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
this IKernelBuilder builder,
string deploymentName,
OpenAIClient? openAIClient = null,
string? serviceId = null,
string? modelId = null)
string? modelId = null,
int? dimensions = null)
{
Verify.NotNull(builder);
Verify.NotNullOrWhiteSpace(deploymentName);
Expand All @@ -497,7 +511,8 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
deploymentName,
openAIClient ?? serviceProvider.GetRequiredService<OpenAIClient>(),
modelId,
serviceProvider.GetService<ILoggerFactory>()));
serviceProvider.GetService<ILoggerFactory>(),
dimensions));

return builder;
}
Expand All @@ -510,14 +525,16 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration(
/// <param name="openAIClient"><see cref="OpenAIClient"/> to use for the service. If null, one must be available in the service provider when this service is resolved.</param>
/// <param name="serviceId">A local identifier for the given AI service</param>
/// <param name="modelId">Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>The same instance as <paramref name="services"/>.</returns>
[Experimental("SKEXP0010")]
public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
this IServiceCollection services,
string deploymentName,
OpenAIClient? openAIClient = null,
string? serviceId = null,
string? modelId = null)
string? modelId = null,
int? dimensions = null)
{
Verify.NotNull(services);
Verify.NotNullOrWhiteSpace(deploymentName);
Expand All @@ -527,7 +544,8 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
deploymentName,
openAIClient ?? serviceProvider.GetRequiredService<OpenAIClient>(),
modelId,
serviceProvider.GetService<ILoggerFactory>()));
serviceProvider.GetService<ILoggerFactory>(),
dimensions));
}

/// <summary>
Expand All @@ -539,6 +557,7 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration(
/// <param name="orgId">OpenAI organization id. This is usually optional unless your account belongs to multiple organizations.</param>
/// <param name="serviceId">A local identifier for the given AI service</param>
/// <param name="httpClient">The HttpClient to use with this service.</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>The same instance as <paramref name="builder"/>.</returns>
[Experimental("SKEXP0010")]
public static IKernelBuilder AddOpenAITextEmbeddingGeneration(
Expand All @@ -547,7 +566,8 @@ public static IKernelBuilder AddOpenAITextEmbeddingGeneration(
string apiKey,
string? orgId = null,
string? serviceId = null,
HttpClient? httpClient = null)
HttpClient? httpClient = null,
int? dimensions = null)
{
Verify.NotNull(builder);
Verify.NotNullOrWhiteSpace(modelId);
Expand All @@ -559,7 +579,8 @@ public static IKernelBuilder AddOpenAITextEmbeddingGeneration(
apiKey,
orgId,
HttpClientProvider.GetHttpClient(httpClient, serviceProvider),
serviceProvider.GetService<ILoggerFactory>()));
serviceProvider.GetService<ILoggerFactory>(),
dimensions));

return builder;
}
Expand All @@ -572,14 +593,16 @@ public static IKernelBuilder AddOpenAITextEmbeddingGeneration(
/// <param name="apiKey">OpenAI API key, see https://platform.openai.com/account/api-keys</param>
/// <param name="orgId">OpenAI organization id. This is usually optional unless your account belongs to multiple organizations.</param>
/// <param name="serviceId">A local identifier for the given AI service</param>
/// <param name="dimensions">The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models.</param>
/// <returns>The same instance as <paramref name="services"/>.</returns>
[Experimental("SKEXP0010")]
public static IServiceCollection AddOpenAITextEmbeddingGeneration(
this IServiceCollection services,
string modelId,
string apiKey,
string? orgId = null,
string? serviceId = null)
string? serviceId = null,
int? dimensions = null)
{
Verify.NotNull(services);
Verify.NotNullOrWhiteSpace(modelId);
Expand All @@ -591,7 +614,8 @@ public static IServiceCollection AddOpenAITextEmbeddingGeneration(
apiKey,
orgId,
HttpClientProvider.GetHttpClient(serviceProvider),
serviceProvider.GetService<ILoggerFactory>()));
serviceProvider.GetService<ILoggerFactory>(),
dimensions));
}

/// <summary>
Expand Down
Loading