Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion eng/packages/General.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="$(MicrosoftMLTokenizersVersion)" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="OllamaSharp" Version="5.1.9" />
<PackageVersion Include="OpenAI" Version="2.3.0" />
<PackageVersion Include="OpenAI" Version="2.4.0" />
<PackageVersion Include="Polly" Version="8.4.2" />
<PackageVersion Include="Polly.Core" Version="8.4.2" />
<PackageVersion Include="Polly.Extensions" Version="8.4.2" />
Expand Down
2 changes: 1 addition & 1 deletion eng/packages/TestOnly.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PackageVersion Include="AutoFixture.AutoMoq" Version="4.17.0" />
<PackageVersion Include="Azure.AI.OpenAI" Version="2.3.0-beta.1" />
<PackageVersion Include="Azure.AI.OpenAI" Version="2.3.0-beta.2" />
<PackageVersion Include="autofixture" Version="4.17.0" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.5" />
<PackageVersion Include="AwesomeAssertions" Version="8.0.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

## 9.8.0-preview.1.25412.6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class MicrosoftExtensionsAIResponsesExtensions
/// <param name="function">The function to convert.</param>
/// <returns>An OpenAI <see cref="ResponseTool"/> representing <paramref name="function"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="function"/> is <see langword="null"/>.</exception>
public static ResponseTool AsOpenAIResponseTool(this AIFunctionDeclaration function) =>
public static FunctionTool AsOpenAIResponseTool(this AIFunctionDeclaration function) =>
OpenAIResponsesChatClient.ToResponseTool(Throw.IfNull(function));

/// <summary>Creates a sequence of OpenAI <see cref="ResponseItem"/> instances from the specified input messages.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -50,17 +49,9 @@ public OpenAIAssistantsChatClient(AssistantClient assistantClient, string assist
{
_client = Throw.IfNull(assistantClient);
_assistantId = Throw.IfNullOrWhitespace(assistantId);

_defaultThreadId = defaultThreadId;

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

_metadata = new("openai", providerUrl);
_metadata = new("openai", assistantClient.Endpoint);
}

/// <summary>Initializes a new instance of the <see cref="OpenAIAssistantsChatClient"/> class for the specified <see cref="AssistantClient"/>.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -37,18 +36,9 @@ internal sealed class OpenAIChatClient : IChatClient
/// <exception cref="ArgumentNullException"><paramref name="chatClient"/> is <see langword="null"/>.</exception>
public OpenAIChatClient(ChatClient chatClient)
{
_ = Throw.IfNull(chatClient);
_chatClient = Throw.IfNull(chatClient);

_chatClient = chatClient;

// https://github.com/openai/openai-dotnet/issues/215
// The endpoint and model aren't currently exposed, so use reflection to get at them, temporarily. Once packages
// implement the abstractions directly rather than providing adapters on top of the public APIs,
// the package can provide such implementations separate from what's exposed in the public API.
Uri providerUrl = typeof(ChatClient).GetField("_endpoint", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
?.GetValue(chatClient) as Uri ?? OpenAIClientExtensions.DefaultOpenAIEndpoint;

_metadata = new("openai", providerUrl, _chatClient.Model);
_metadata = new("openai", chatClient.Endpoint, _chatClient.Model);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Shared.Diagnostics;
Expand All @@ -18,9 +17,6 @@ namespace Microsoft.Extensions.AI;
/// <summary>An <see cref="IEmbeddingGenerator{String, Embedding}"/> for an OpenAI <see cref="EmbeddingClient"/>.</summary>
internal sealed class OpenAIEmbeddingGenerator : IEmbeddingGenerator<string, Embedding<float>>
{
/// <summary>Default OpenAI endpoint.</summary>
private const string DefaultOpenAIEndpoint = "https://api.openai.com/v1";

/// <summary>Metadata about the embedding generator.</summary>
private readonly EmbeddingGeneratorMetadata _metadata;

Expand All @@ -37,24 +33,15 @@ internal sealed class OpenAIEmbeddingGenerator : IEmbeddingGenerator<string, Emb
/// <exception cref="ArgumentOutOfRangeException"><paramref name="defaultModelDimensions"/> is not positive.</exception>
public OpenAIEmbeddingGenerator(EmbeddingClient embeddingClient, int? defaultModelDimensions = null)
{
_ = Throw.IfNull(embeddingClient);
_embeddingClient = Throw.IfNull(embeddingClient);
_dimensions = defaultModelDimensions;

if (defaultModelDimensions < 1)
{
Throw.ArgumentOutOfRangeException(nameof(defaultModelDimensions), "Value must be greater than 0.");
}

_embeddingClient = embeddingClient;
_dimensions = defaultModelDimensions;

// https://github.com/openai/openai-dotnet/issues/215
// The endpoint and model aren't currently exposed, so use reflection to get at them, temporarily. Once packages
// implement the abstractions directly rather than providing adapters on top of the public APIs,
// the package can provide such implementations separate from what's exposed in the public API.
string providerUrl = (typeof(EmbeddingClient).GetField("_endpoint", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
?.GetValue(embeddingClient) as Uri)?.ToString() ??
DefaultOpenAIEndpoint;

_metadata = CreateMetadata("openai", providerUrl, _embeddingClient.Model, defaultModelDimensions);
_metadata = new("openai", embeddingClient.Endpoint, _embeddingClient.Model, defaultModelDimensions);
}

/// <inheritdoc />
Expand Down Expand Up @@ -98,10 +85,6 @@ void IDisposable.Dispose()
null;
}

/// <summary>Creates the <see cref="EmbeddingGeneratorMetadata"/> for this instance.</summary>
private static EmbeddingGeneratorMetadata CreateMetadata(string providerName, string providerUrl, string? defaultModelId, int? defaultModelDimensions) =>
new(providerName, Uri.TryCreate(providerUrl, UriKind.Absolute, out Uri? providerUri) ? providerUri : null, defaultModelId, defaultModelDimensions);

/// <summary>Converts an extensions options instance to an OpenAI options instance.</summary>
private OpenAI.Embeddings.EmbeddingGenerationOptions ToOpenAIOptions(EmbeddingGenerationOptions? options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,9 @@ internal sealed class OpenAIImageGenerator : IImageGenerator
/// <exception cref="ArgumentNullException"><paramref name="imageClient"/> is <see langword="null"/>.</exception>
public OpenAIImageGenerator(ImageClient imageClient)
{
_ = Throw.IfNull(imageClient);
_imageClient = Throw.IfNull(imageClient);

_imageClient = imageClient;

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

_metadata = new("openai", providerUrl, _imageClient.Model);
_metadata = new("openai", imageClient.Endpoint, _imageClient.Model);
}

/// <inheritdoc />
Expand Down Expand Up @@ -143,7 +134,7 @@ private static ImageGenerationResponse ToImageGenerationResponse(GeneratedImageC

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

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

List<AIContent> contents = new();
List<AIContent> contents = [];

foreach (GeneratedImage image in generatedImages)
{
Expand Down
Loading
Loading