Skip to content

Commit de7ef3d

Browse files
authored
.Net: Update Microsoft.Extensions.AI to 9.4.0-preview (#11491)
### Motivation and Context Upgrade for latest version of MEAI 9.4.0
1 parent cc679c0 commit de7ef3d

File tree

17 files changed

+38
-74
lines changed

17 files changed

+38
-74
lines changed

dotnet/Directory.Packages.props

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<PackageVersion Include="MSTest.TestFramework" Version="3.8.0" />
6666
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
6767
<PackageVersion Include="Npgsql" Version="8.0.6" />
68-
<PackageVersion Include="OllamaSharp" Version="5.1.7" />
68+
<PackageVersion Include="OllamaSharp" Version="5.1.12" />
6969
<PackageVersion Include="OpenAI" Version="[2.2.0-beta.4]" />
7070
<PackageVersion Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
7171
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
@@ -91,10 +91,10 @@
9191
<PackageVersion Include="Microsoft.DeepDev.TokenizerLib" Version="1.3.3" />
9292
<PackageVersion Include="SharpToken" Version="2.0.3" />
9393
<!-- Microsoft.Extensions.* -->
94-
<PackageVersion Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25161.3" />
95-
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="9.3.0-preview.1.25161.3" />
96-
<PackageVersion Include="Microsoft.Extensions.AI.AzureAIInference" Version="9.3.0-preview.1.25161.3" />
97-
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="9.3.0-preview.1.25161.3" />
94+
<PackageVersion Include="Microsoft.Extensions.AI" Version="9.4.0-preview.1.25207.5" />
95+
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="9.4.0-preview.1.25207.5" />
96+
<PackageVersion Include="Microsoft.Extensions.AI.AzureAIInference" Version="9.4.0-preview.1.25207.5" />
97+
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="9.4.0-preview.1.25207.5" />
9898
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
9999
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
100100
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />

dotnet/samples/Concepts/ChatCompletion/AzureAIInference_ChatCompletion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task ServicePromptAsync()
2525
var chatService = new ChatCompletionsClient(
2626
endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint),
2727
credential: new Azure.AzureKeyCredential(TestConfiguration.AzureAIInference.ApiKey))
28-
.AsChatClient(TestConfiguration.AzureAIInference.ChatModelId)
28+
.AsIChatClient(TestConfiguration.AzureAIInference.ChatModelId)
2929
.AsChatCompletionService();
3030

3131
Console.WriteLine("Chat content:");

dotnet/samples/Concepts/ChatCompletion/AzureAIInference_ChatCompletionStreaming.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Task StreamChatAsync()
2626
var chatService = new ChatCompletionsClient(
2727
endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint),
2828
credential: new Azure.AzureKeyCredential(TestConfiguration.AzureAIInference.ApiKey!))
29-
.AsChatClient(TestConfiguration.AzureAIInference.ChatModelId)
29+
.AsIChatClient(TestConfiguration.AzureAIInference.ChatModelId)
3030
.AsChatCompletionService();
3131

3232
return this.StartStreamingChatAsync(chatService);
@@ -76,7 +76,7 @@ public async Task StreamTextFromChatAsync()
7676
var chatService = new ChatCompletionsClient(
7777
endpoint: new Uri(TestConfiguration.AzureAIInference.Endpoint),
7878
credential: new Azure.AzureKeyCredential(TestConfiguration.AzureAIInference.ApiKey!))
79-
.AsChatClient(TestConfiguration.AzureAIInference.ChatModelId)
79+
.AsIChatClient(TestConfiguration.AzureAIInference.ChatModelId)
8080
.AsChatCompletionService();
8181

8282
// Create chat history with initial system and user messages

dotnet/samples/Concepts/ChatCompletion/HybridCompletion_Fallback.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private static IChatClient CreateUnavailableOpenAIChatClient()
104104
)
105105
};
106106

107-
IChatClient openAiClient = new AzureOpenAIClient(new Uri(TestConfiguration.AzureOpenAI.Endpoint), new AzureCliCredential(), options).AsChatClient(TestConfiguration.AzureOpenAI.ChatDeploymentName);
107+
IChatClient openAiClient = new AzureOpenAIClient(new Uri(TestConfiguration.AzureOpenAI.Endpoint), new AzureCliCredential(), options).GetChatClient(TestConfiguration.AzureOpenAI.ChatDeploymentName).AsIChatClient();
108108

109109
return new ChatClientBuilder(openAiClient)
110110
.UseFunctionInvocation()
@@ -113,7 +113,7 @@ private static IChatClient CreateUnavailableOpenAIChatClient()
113113

114114
private static IChatClient CreateAzureOpenAIChatClient()
115115
{
116-
IChatClient chatClient = new AzureOpenAIClient(new Uri(TestConfiguration.AzureOpenAI.Endpoint), new AzureCliCredential()).AsChatClient(TestConfiguration.AzureOpenAI.ChatDeploymentName);
116+
IChatClient chatClient = new AzureOpenAIClient(new Uri(TestConfiguration.AzureOpenAI.Endpoint), new AzureCliCredential()).GetChatClient(TestConfiguration.AzureOpenAI.ChatDeploymentName).AsIChatClient();
117117

118118
return new ChatClientBuilder(chatClient)
119119
.UseFunctionInvocation()

dotnet/src/Connectors/Connectors.AzureAIInference.UnitTests/Extensions/AzureAIInferenceServiceCollectionExtensionsTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using Azure;
55
using Azure.AI.Inference;
6-
using Microsoft.Extensions.AI;
76
using Microsoft.Extensions.DependencyInjection;
87
using Microsoft.SemanticKernel;
98
using Microsoft.SemanticKernel.ChatCompletion;
@@ -18,13 +17,11 @@ public sealed class AzureAIInferenceServiceCollectionExtensionsTests
1817
[Theory]
1918
[InlineData(InitializationType.ApiKey)]
2019
[InlineData(InitializationType.ClientInline)]
21-
[InlineData(InitializationType.ChatClientInline)]
2220
[InlineData(InitializationType.ClientInServiceProvider)]
2321
public void ItCanAddChatCompletionService(InitializationType type)
2422
{
2523
// Arrange
2624
var client = new ChatCompletionsClient(this._endpoint, new AzureKeyCredential("key"));
27-
using var chatClient = new AzureAIInferenceChatClient(client, "model-id");
2825
var builder = Kernel.CreateBuilder();
2926

3027
builder.Services.AddSingleton(client);
@@ -34,7 +31,6 @@ public void ItCanAddChatCompletionService(InitializationType type)
3431
{
3532
InitializationType.ApiKey => builder.Services.AddAzureAIInferenceChatCompletion("modelId", "api-key", this._endpoint),
3633
InitializationType.ClientInline => builder.Services.AddAzureAIInferenceChatCompletion("modelId", client),
37-
InitializationType.ChatClientInline => builder.Services.AddAzureAIInferenceChatCompletion(chatClient),
3834
InitializationType.ClientInServiceProvider => builder.Services.AddAzureAIInferenceChatCompletion("modelId", chatClient: null),
3935
_ => builder.Services
4036
};

dotnet/src/Connectors/Connectors.AzureAIInference/Extensions/AzureAIInferenceServiceCollectionExtensions.cs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static IServiceCollection AddAzureAIInferenceChatCompletion(
5050
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
5151

5252
var builder = new Azure.AI.Inference.ChatCompletionsClient(endpoint, new Azure.AzureKeyCredential(apiKey ?? SingleSpace), options)
53-
.AsChatClient(modelId)
53+
.AsIChatClient(modelId)
5454
.AsBuilder()
5555
.UseFunctionInvocation(loggerFactory, f => f.MaximumIterationsPerRequest = MaxInflightAutoInvokes);
5656

@@ -96,7 +96,7 @@ public static IServiceCollection AddAzureAIInferenceChatCompletion(
9696
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
9797

9898
var builder = new Azure.AI.Inference.ChatCompletionsClient(endpoint, credential, options)
99-
.AsChatClient(modelId)
99+
.AsIChatClient(modelId)
100100
.AsBuilder()
101101
.UseFunctionInvocation(loggerFactory, f => f.MaximumIterationsPerRequest = MaxInflightAutoInvokes);
102102

@@ -131,39 +131,7 @@ public static IServiceCollection AddAzureAIInferenceChatCompletion(this IService
131131
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
132132

133133
var builder = chatClient
134-
.AsChatClient(modelId)
135-
.AsBuilder()
136-
.UseFunctionInvocation(loggerFactory, f => f.MaximumIterationsPerRequest = MaxInflightAutoInvokes);
137-
138-
if (loggerFactory is not null)
139-
{
140-
builder.UseLogging(loggerFactory);
141-
}
142-
143-
return builder.Build(serviceProvider).AsChatCompletionService(serviceProvider);
144-
});
145-
}
146-
147-
/// <summary>
148-
/// Adds an Azure AI Inference <see cref="IChatCompletionService"/> to the <see cref="IServiceCollection"/>.
149-
/// </summary>
150-
/// <param name="services">The <see cref="IServiceCollection"/> instance to augment.</param>
151-
/// <param name="chatClient"><see cref="AzureAIInferenceChatClient"/> to use for the service. If null, one must be available in the service provider when this service is resolved.</param>
152-
/// <param name="serviceId">A local identifier for the given AI service</param>
153-
/// <returns>The same instance as <paramref name="services"/>.</returns>
154-
public static IServiceCollection AddAzureAIInferenceChatCompletion(this IServiceCollection services,
155-
AzureAIInferenceChatClient? chatClient = null,
156-
string? serviceId = null)
157-
{
158-
Verify.NotNull(services);
159-
160-
return services.AddKeyedSingleton<IChatCompletionService>(serviceId, (serviceProvider, _) =>
161-
{
162-
chatClient ??= serviceProvider.GetRequiredService<AzureAIInferenceChatClient>();
163-
164-
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
165-
166-
var builder = chatClient
134+
.AsIChatClient(modelId)
167135
.AsBuilder()
168136
.UseFunctionInvocation(loggerFactory, f => f.MaximumIterationsPerRequest = MaxInflightAutoInvokes);
169137

dotnet/src/Connectors/Connectors.AzureAIInference/Services/AzureAIInferenceChatCompletionService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public AzureAIInferenceChatCompletionService(
4141
this._core = new ChatClientCore(modelId, apiKey, endpoint, httpClient);
4242

4343
var builder = this._core.Client
44-
.AsChatClient(modelId)
44+
.AsIChatClient(modelId)
4545
.AsBuilder()
4646
.UseFunctionInvocation(loggerFactory, f => f.MaximumIterationsPerRequest = MaxInflightAutoInvokes);
4747

@@ -71,7 +71,7 @@ public AzureAIInferenceChatCompletionService(
7171
this._core = new ChatClientCore(modelId, credential, endpoint, httpClient);
7272

7373
var builder = this._core.Client
74-
.AsChatClient(modelId)
74+
.AsIChatClient(modelId)
7575
.AsBuilder()
7676
.UseFunctionInvocation(loggerFactory, f => f.MaximumIterationsPerRequest = MaxInflightAutoInvokes);
7777

@@ -99,7 +99,7 @@ public AzureAIInferenceChatCompletionService(
9999
this._core = new ChatClientCore(modelId, chatClient);
100100

101101
var builder = chatClient
102-
.AsChatClient(modelId)
102+
.AsIChatClient(modelId)
103103
.AsBuilder()
104104
.UseFunctionInvocation(loggerFactory, f => f.MaximumIterationsPerRequest = MaxInflightAutoInvokes);
105105

dotnet/src/InternalUtilities/src/Schema/KernelJsonSchemaBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public static KernelJsonSchema Build(
4949
AIJsonSchemaCreateOptions? configuration = null)
5050
{
5151
configuration ??= s_schemaOptions;
52-
JsonElement schemaDocument = AIJsonUtilities.CreateJsonSchema(type, description, serializerOptions: options, inferenceOptions: configuration);
52+
// To be compatible with the previous behavior of MEAI 9.3.0 (when description is empty, should not be included in the schema)
53+
string? schemaDescription = string.IsNullOrEmpty(description) ? null : description;
54+
JsonElement schemaDocument = AIJsonUtilities.CreateJsonSchema(type, schemaDescription, serializerOptions: options, inferenceOptions: configuration);
5355
switch (schemaDocument.ValueKind)
5456
{
5557
case JsonValueKind.False:

dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/AIFunctionKernelFunction.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ public override KernelFunction Clone(string pluginName)
4848
protected override async ValueTask<FunctionResult> InvokeCoreAsync(
4949
Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken)
5050
{
51-
object? result = await this._aiFunction.InvokeAsync(arguments, cancellationToken).ConfigureAwait(false);
51+
object? result = await this._aiFunction.InvokeAsync(new(arguments), cancellationToken).ConfigureAwait(false);
5252
return new FunctionResult(this, result);
5353
}
5454

5555
protected override async IAsyncEnumerable<TResult> InvokeStreamingCoreAsync<TResult>(
5656
Kernel kernel, KernelArguments arguments, [EnumeratorCancellation] CancellationToken cancellationToken)
5757
{
58-
object? result = await this._aiFunction.InvokeAsync(arguments, cancellationToken).ConfigureAwait(false);
58+
object? result = await this._aiFunction.InvokeAsync(new(arguments), cancellationToken).ConfigureAwait(false);
5959
yield return (TResult)result!;
6060
}
6161

@@ -65,7 +65,6 @@ private static IReadOnlyList<KernelParameterMetadata> MapParameterMetadata(AIFun
6565
{
6666
return Array.Empty<KernelParameterMetadata>();
6767
}
68-
6968
HashSet<string>? requiredParameters = GetRequiredParameterNames(aiFunction.JsonSchema);
7069

7170
List<KernelParameterMetadata> kernelParams = [];

dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatClientChatCompletionService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public ChatClientChatCompletionService(IChatClient chatClient, IServiceProvider?
3939
{
4040
attrs[AIServiceExtensions.EndpointKey] = metadata.ProviderUri.ToString();
4141
}
42-
if (metadata?.ModelId is not null)
42+
if (metadata?.DefaultModelId is not null)
4343
{
44-
attrs[AIServiceExtensions.ModelIdKey] = metadata.ModelId;
44+
attrs[AIServiceExtensions.ModelIdKey] = metadata.DefaultModelId;
4545
}
4646
}
4747

0 commit comments

Comments
 (0)