Skip to content

Commit aae2551

Browse files
committed
Update to OpenAI 2.4.0 (#6777)
1 parent 933aeb4 commit aae2551

File tree

14 files changed

+136
-177
lines changed

14 files changed

+136
-177
lines changed

eng/packages/General.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="$(MicrosoftMLTokenizersVersion)" />
1717
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
1818
<PackageVersion Include="OllamaSharp" Version="5.1.9" />
19-
<PackageVersion Include="OpenAI" Version="2.3.0" />
19+
<PackageVersion Include="OpenAI" Version="2.4.0" />
2020
<PackageVersion Include="Polly" Version="8.4.2" />
2121
<PackageVersion Include="Polly.Core" Version="8.4.2" />
2222
<PackageVersion Include="Polly.Extensions" Version="8.4.2" />

eng/packages/TestOnly.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup>
44
<PackageVersion Include="AutoFixture.AutoMoq" Version="4.17.0" />
5-
<PackageVersion Include="Azure.AI.OpenAI" Version="2.3.0-beta.1" />
5+
<PackageVersion Include="Azure.AI.OpenAI" Version="2.3.0-beta.2" />
66
<PackageVersion Include="autofixture" Version="4.17.0" />
77
<PackageVersion Include="BenchmarkDotNet" Version="0.13.5" />
88
<PackageVersion Include="AwesomeAssertions" Version="8.0.2" />

src/Libraries/Microsoft.Extensions.AI.OpenAI/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Updated tool mappings to recognize any `AIFunctionDeclaration`.
66
- Updated to accommodate the additions in `Microsoft.Extensions.AI.Abstractions`.
7+
- Updated to depend on OpenAI 2.4.0
78

89
## 9.8.0-preview.1.25412.6
910

src/Libraries/Microsoft.Extensions.AI.OpenAI/MicrosoftExtensionsAIResponsesExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class MicrosoftExtensionsAIResponsesExtensions
1818
/// <param name="function">The function to convert.</param>
1919
/// <returns>An OpenAI <see cref="ResponseTool"/> representing <paramref name="function"/>.</returns>
2020
/// <exception cref="ArgumentNullException"><paramref name="function"/> is <see langword="null"/>.</exception>
21-
public static ResponseTool AsOpenAIResponseTool(this AIFunctionDeclaration function) =>
21+
public static FunctionTool AsOpenAIResponseTool(this AIFunctionDeclaration function) =>
2222
OpenAIResponsesChatClient.ToResponseTool(Throw.IfNull(function));
2323

2424
/// <summary>Creates a sequence of OpenAI <see cref="ResponseItem"/> instances from the specified input messages.</summary>

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIAssistantsChatClient.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Reflection;
87
using System.Runtime.CompilerServices;
98
using System.Text;
109
using System.Text.Json;
@@ -50,17 +49,9 @@ public OpenAIAssistantsChatClient(AssistantClient assistantClient, string assist
5049
{
5150
_client = Throw.IfNull(assistantClient);
5251
_assistantId = Throw.IfNullOrWhitespace(assistantId);
53-
5452
_defaultThreadId = defaultThreadId;
5553

56-
// https://github.com/openai/openai-dotnet/issues/215
57-
// The endpoint isn't currently exposed, so use reflection to get at it, temporarily. Once packages
58-
// implement the abstractions directly rather than providing adapters on top of the public APIs,
59-
// the package can provide such implementations separate from what's exposed in the public API.
60-
Uri providerUrl = typeof(AssistantClient).GetField("_endpoint", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
61-
?.GetValue(assistantClient) as Uri ?? OpenAIClientExtensions.DefaultOpenAIEndpoint;
62-
63-
_metadata = new("openai", providerUrl);
54+
_metadata = new("openai", assistantClient.Endpoint);
6455
}
6556

6657
/// <summary>Initializes a new instance of the <see cref="OpenAIAssistantsChatClient"/> class for the specified <see cref="AssistantClient"/>.</summary>

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Reflection;
87
using System.Runtime.CompilerServices;
98
using System.Text;
109
using System.Text.Json;
@@ -37,18 +36,9 @@ internal sealed class OpenAIChatClient : IChatClient
3736
/// <exception cref="ArgumentNullException"><paramref name="chatClient"/> is <see langword="null"/>.</exception>
3837
public OpenAIChatClient(ChatClient chatClient)
3938
{
40-
_ = Throw.IfNull(chatClient);
39+
_chatClient = Throw.IfNull(chatClient);
4140

42-
_chatClient = chatClient;
43-
44-
// https://github.com/openai/openai-dotnet/issues/215
45-
// The endpoint and model aren't currently exposed, so use reflection to get at them, temporarily. Once packages
46-
// implement the abstractions directly rather than providing adapters on top of the public APIs,
47-
// the package can provide such implementations separate from what's exposed in the public API.
48-
Uri providerUrl = typeof(ChatClient).GetField("_endpoint", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
49-
?.GetValue(chatClient) as Uri ?? OpenAIClientExtensions.DefaultOpenAIEndpoint;
50-
51-
_metadata = new("openai", providerUrl, _chatClient.Model);
41+
_metadata = new("openai", chatClient.Endpoint, _chatClient.Model);
5242
}
5343

5444
/// <inheritdoc />

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIEmbeddingGenerator.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Reflection;
87
using System.Threading;
98
using System.Threading.Tasks;
109
using Microsoft.Shared.Diagnostics;
@@ -18,9 +17,6 @@ namespace Microsoft.Extensions.AI;
1817
/// <summary>An <see cref="IEmbeddingGenerator{String, Embedding}"/> for an OpenAI <see cref="EmbeddingClient"/>.</summary>
1918
internal sealed class OpenAIEmbeddingGenerator : IEmbeddingGenerator<string, Embedding<float>>
2019
{
21-
/// <summary>Default OpenAI endpoint.</summary>
22-
private const string DefaultOpenAIEndpoint = "https://api.openai.com/v1";
23-
2420
/// <summary>Metadata about the embedding generator.</summary>
2521
private readonly EmbeddingGeneratorMetadata _metadata;
2622

@@ -37,24 +33,15 @@ internal sealed class OpenAIEmbeddingGenerator : IEmbeddingGenerator<string, Emb
3733
/// <exception cref="ArgumentOutOfRangeException"><paramref name="defaultModelDimensions"/> is not positive.</exception>
3834
public OpenAIEmbeddingGenerator(EmbeddingClient embeddingClient, int? defaultModelDimensions = null)
3935
{
40-
_ = Throw.IfNull(embeddingClient);
36+
_embeddingClient = Throw.IfNull(embeddingClient);
37+
_dimensions = defaultModelDimensions;
38+
4139
if (defaultModelDimensions < 1)
4240
{
4341
Throw.ArgumentOutOfRangeException(nameof(defaultModelDimensions), "Value must be greater than 0.");
4442
}
4543

46-
_embeddingClient = embeddingClient;
47-
_dimensions = defaultModelDimensions;
48-
49-
// https://github.com/openai/openai-dotnet/issues/215
50-
// The endpoint and model aren't currently exposed, so use reflection to get at them, temporarily. Once packages
51-
// implement the abstractions directly rather than providing adapters on top of the public APIs,
52-
// the package can provide such implementations separate from what's exposed in the public API.
53-
string providerUrl = (typeof(EmbeddingClient).GetField("_endpoint", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
54-
?.GetValue(embeddingClient) as Uri)?.ToString() ??
55-
DefaultOpenAIEndpoint;
56-
57-
_metadata = CreateMetadata("openai", providerUrl, _embeddingClient.Model, defaultModelDimensions);
44+
_metadata = new("openai", embeddingClient.Endpoint, _embeddingClient.Model, defaultModelDimensions);
5845
}
5946

6047
/// <inheritdoc />
@@ -98,10 +85,6 @@ void IDisposable.Dispose()
9885
null;
9986
}
10087

101-
/// <summary>Creates the <see cref="EmbeddingGeneratorMetadata"/> for this instance.</summary>
102-
private static EmbeddingGeneratorMetadata CreateMetadata(string providerName, string providerUrl, string? defaultModelId, int? defaultModelDimensions) =>
103-
new(providerName, Uri.TryCreate(providerUrl, UriKind.Absolute, out Uri? providerUri) ? providerUri : null, defaultModelId, defaultModelDimensions);
104-
10588
/// <summary>Converts an extensions options instance to an OpenAI options instance.</summary>
10689
private OpenAI.Embeddings.EmbeddingGenerationOptions ToOpenAIOptions(EmbeddingGenerationOptions? options)
10790
{

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIImageGenerator.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,9 @@ internal sealed class OpenAIImageGenerator : IImageGenerator
4545
/// <exception cref="ArgumentNullException"><paramref name="imageClient"/> is <see langword="null"/>.</exception>
4646
public OpenAIImageGenerator(ImageClient imageClient)
4747
{
48-
_ = Throw.IfNull(imageClient);
48+
_imageClient = Throw.IfNull(imageClient);
4949

50-
_imageClient = imageClient;
51-
52-
// https://github.com/openai/openai-dotnet/issues/215
53-
// The endpoint and model aren't currently exposed, so use reflection to get at them, temporarily. Once packages
54-
// implement the abstractions directly rather than providing adapters on top of the public APIs,
55-
// the package can provide such implementations separate from what's exposed in the public API.
56-
Uri providerUrl = typeof(ImageClient).GetField("_endpoint", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
57-
?.GetValue(imageClient) as Uri ?? OpenAIClientExtensions.DefaultOpenAIEndpoint;
58-
59-
_metadata = new("openai", providerUrl, _imageClient.Model);
50+
_metadata = new("openai", imageClient.Endpoint, _imageClient.Model);
6051
}
6152

6253
/// <inheritdoc />
@@ -143,7 +134,7 @@ private static ImageGenerationResponse ToImageGenerationResponse(GeneratedImageC
143134

144135
// OpenAI doesn't expose the content type, so we need to read from the internal JSON representation.
145136
// https://github.com/openai/openai-dotnet/issues/561
146-
IDictionary<string, BinaryData>? additionalRawData = typeof(GeneratedImageCollection)
137+
var additionalRawData = typeof(GeneratedImageCollection)
147138
.GetProperty("SerializedAdditionalRawData", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
148139
?.GetValue(generatedImages) as IDictionary<string, BinaryData>;
149140

@@ -154,7 +145,7 @@ private static ImageGenerationResponse ToImageGenerationResponse(GeneratedImageC
154145
contentType = $"image/{outputFormatString}";
155146
}
156147

157-
List<AIContent> contents = new();
148+
List<AIContent> contents = [];
158149

159150
foreach (GeneratedImage image in generatedImages)
160151
{

0 commit comments

Comments
 (0)