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.4.0" />
<PackageVersion Include="OpenAI" Version="2.5.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
3 changes: 2 additions & 1 deletion src/Libraries/Microsoft.Extensions.AI.OpenAI/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

## NOT YET RELEASED

- Updated to depend on OpenAI 2.5.0.
- Added M.E.AI to OpenAI conversions for response format types.
- Added `ResponseTool` to `AITool` conversions.

## 9.9.0-preview.1.25458.4

- Updated to depend on OpenAI 2.4.0
- Updated to depend on OpenAI 2.4.0.
- Updated tool mappings to recognize any `AIFunctionDeclaration`.
- Updated to accommodate the additions in `Microsoft.Extensions.AI.Abstractions`.
- Fixed handling of annotated but empty content in the `AsIChatClient` for `AssistantClient`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,37 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Shared.Diagnostics;
using OpenAI.Embeddings;

#pragma warning disable S1067 // Expressions should not be too complex
#pragma warning disable S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields
#pragma warning disable EA0011 // Consider removing unnecessary conditional access operator (?)

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>>
{
// This delegate instance is used to call the internal overload of GenerateEmbeddingsAsync that accepts
// a RequestOptions. This should be replaced once a better way to pass RequestOptions is available.
private static readonly Func<EmbeddingClient, IEnumerable<string>, OpenAI.Embeddings.EmbeddingGenerationOptions, RequestOptions, Task<ClientResult<OpenAIEmbeddingCollection>>>?
_generateEmbeddingsAsync =
(Func<EmbeddingClient, IEnumerable<string>, OpenAI.Embeddings.EmbeddingGenerationOptions, RequestOptions, Task<ClientResult<OpenAIEmbeddingCollection>>>?)
typeof(EmbeddingClient)
.GetMethod(
nameof(EmbeddingClient.GenerateEmbeddingsAsync), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
null, [typeof(IEnumerable<string>), typeof(OpenAI.Embeddings.EmbeddingGenerationOptions), typeof(RequestOptions)], null)
?.CreateDelegate(
typeof(Func<EmbeddingClient, IEnumerable<string>, OpenAI.Embeddings.EmbeddingGenerationOptions, RequestOptions, Task<ClientResult<OpenAIEmbeddingCollection>>>));

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

Expand Down Expand Up @@ -49,7 +65,10 @@ public async Task<GeneratedEmbeddings<Embedding<float>>> GenerateAsync(IEnumerab
{
OpenAI.Embeddings.EmbeddingGenerationOptions? openAIOptions = ToOpenAIOptions(options);

var embeddings = (await _embeddingClient.GenerateEmbeddingsAsync(values, openAIOptions, cancellationToken).ConfigureAwait(false)).Value;
var t = _generateEmbeddingsAsync is not null ?
_generateEmbeddingsAsync(_embeddingClient, values, openAIOptions, cancellationToken.ToRequestOptions(streaming: false)) :
_embeddingClient.GenerateEmbeddingsAsync(values, openAIOptions, cancellationToken);
var embeddings = (await t.ConfigureAwait(false)).Value;

return new(embeddings.Select(e =>
new Embedding<float>(e.ToFloats())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ public OpenAIResponsesChatClient(OpenAIResponseClient responseClient)

_responseClient = responseClient;

// https://github.com/openai/openai-dotnet/issues/662
// Update to avoid reflection once OpenAIResponseClient.Model is exposed publicly.
string? model = typeof(OpenAIResponseClient).GetField("_model", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
?.GetValue(responseClient) as string;

_metadata = new("openai", responseClient.Endpoint, model);
_metadata = new("openai", responseClient.Endpoint, responseClient.Model);
}

/// <inheritdoc />
Expand Down Expand Up @@ -469,27 +464,19 @@ private ResponseCreationOptions ToOpenAIResponseCreationOptions(ChatOptions? opt
break;

case HostedCodeInterpreterTool codeTool:
string json;
if (codeTool.Inputs is { Count: > 0 } inputs)
{
string jsonArray = JsonSerializer.Serialize(
inputs.OfType<HostedFileContent>().Select(c => c.FileId),
OpenAIJsonContext.Default.IEnumerableString);
json = $$"""{"type":"code_interpreter","container":{"type":"auto",files:{{jsonArray}}} }""";
}
else
{
json = """{"type":"code_interpreter","container":{"type":"auto"}}""";
}

result.Tools.Add(ModelReaderWriter.Read<ResponseTool>(BinaryData.FromString(json)));
result.Tools.Add(
ResponseTool.CreateCodeInterpreterTool(
new CodeInterpreterToolContainer(codeTool.Inputs?.OfType<HostedFileContent>().Select(f => f.FileId).ToList() is { Count: > 0 } ids ?
CodeInterpreterToolContainerConfiguration.CreateAutomaticContainerConfiguration(ids) :
new())));
break;

case HostedMcpServerTool mcpTool:
McpTool responsesMcpTool = ResponseTool.CreateMcpTool(
mcpTool.ServerName,
mcpTool.Url,
mcpTool.Headers);
serverDescription: mcpTool.ServerDescription,
headers: mcpTool.Headers);

if (mcpTool.AllowedTools is not null)
{
Expand Down Expand Up @@ -673,8 +660,8 @@ internal static IEnumerable<ResponseItem> ToOpenAIResponseItems(IEnumerable<Chat
break;

case McpServerToolApprovalRequestContent mcpApprovalRequestContent:
// BUG https://github.com/openai/openai-dotnet/issues/664: Needs to be able to set an approvalRequestId
yield return ResponseItem.CreateMcpApprovalRequestItem(
mcpApprovalRequestContent.Id,
mcpApprovalRequestContent.ToolCall.ServerName,
mcpApprovalRequestContent.ToolCall.ToolName,
BinaryData.FromBytes(JsonSerializer.SerializeToUtf8Bytes(mcpApprovalRequestContent.ToolCall.Arguments!, OpenAIJsonContext.Default.IReadOnlyDictionaryStringObject)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,19 @@ private static async Task DataContentMessage_Image_AdditionalPropertyDetail_NonS
}, response.Usage.AdditionalCounts);
}

[Fact]
public async Task RequestHeaders_UserAgent_ContainsMEAI()
{
using var handler = new ThrowUserAgentExceptionHandler();
using HttpClient httpClient = new(handler);
using IChatClient client = CreateChatClient(httpClient, "gpt-4o-mini");

InvalidOperationException e = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetResponseAsync("hello"));

Assert.StartsWith("User-Agent header: OpenAI", e.Message);
Assert.Contains("MEAI", e.Message);
}

private static IChatClient CreateChatClient(HttpClient httpClient, string modelId) =>
new OpenAIClient(new ApiKeyCredential("apikey"), new OpenAIClientOptions { Transport = new HttpClientPipelineTransport(httpClient) })
.GetChatClient(modelId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ public async Task GetEmbeddingsAsync_ExpectedRequestResponse()

using VerbatimHttpHandler handler = new(Input, Output);
using HttpClient httpClient = new(handler);
using IEmbeddingGenerator<string, Embedding<float>> generator = new OpenAIClient(new ApiKeyCredential("apikey"), new OpenAIClientOptions
{
Transport = new HttpClientPipelineTransport(httpClient),
}).GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator();
using IEmbeddingGenerator<string, Embedding<float>> generator = CreateEmbeddingGenerator(httpClient, "text-embedding-3-small");

var response = await generator.GenerateAsync([
"hello, world!",
Expand Down Expand Up @@ -188,10 +185,7 @@ public async Task EmbeddingGenerationOptions_DoNotOverwrite_NotNullPropertiesInR

using VerbatimHttpHandler handler = new(Input, Output);
using HttpClient httpClient = new(handler);
using IEmbeddingGenerator<string, Embedding<float>> generator = new OpenAIClient(new ApiKeyCredential("apikey"), new OpenAIClientOptions
{
Transport = new HttpClientPipelineTransport(httpClient),
}).GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator();
using IEmbeddingGenerator<string, Embedding<float>> generator = CreateEmbeddingGenerator(httpClient, "text-embedding-3-small");

var response = await generator.GenerateAsync([
"hello, world!",
Expand Down Expand Up @@ -221,4 +215,24 @@ public async Task EmbeddingGenerationOptions_DoNotOverwrite_NotNullPropertiesInR
Assert.Contains(e.Vector.ToArray(), f => !f.Equals(0));
}
}

[Fact]
public async Task RequestHeaders_UserAgent_ContainsMEAI()
{
using var handler = new ThrowUserAgentExceptionHandler();
using HttpClient httpClient = new(handler);
using IEmbeddingGenerator<string, Embedding<float>> generator = CreateEmbeddingGenerator(httpClient, "text-embedding-3-small");

InvalidOperationException e = await Assert.ThrowsAsync<InvalidOperationException>(() => generator.GenerateAsync("hello"));

Assert.StartsWith("User-Agent header: OpenAI", e.Message);
Assert.Contains("MEAI", e.Message);
}

private static IEmbeddingGenerator<string, Embedding<float>> CreateEmbeddingGenerator(HttpClient httpClient, string modelId) =>
new OpenAIClient(
new ApiKeyCredential("apikey"),
new OpenAIClientOptions { Transport = new HttpClientPipelineTransport(httpClient) })
.GetEmbeddingClient(modelId)
.AsIEmbeddingGenerator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ public async Task McpToolCall_ApprovalNotRequired_NonStreaming(bool rawTool)
using IChatClient client = CreateResponseClient(httpClient, "gpt-4o-mini");

AITool mcpTool = rawTool ?
ResponseTool.CreateMcpTool("deepwiki", new("https://mcp.deepwiki.com/mcp"), toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)).AsAITool() :
ResponseTool.CreateMcpTool("deepwiki", serverUri: new("https://mcp.deepwiki.com/mcp"), toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)).AsAITool() :
new HostedMcpServerTool("deepwiki", "https://mcp.deepwiki.com/mcp")
{
ApprovalMode = HostedMcpServerToolApprovalMode.NeverRequire,
Expand Down Expand Up @@ -1506,6 +1506,19 @@ public async Task McpToolCall_ApprovalNotRequired_Streaming()
Assert.Equal(1569, response.Usage.TotalTokenCount);
}

[Fact]
public async Task RequestHeaders_UserAgent_ContainsMEAI()
{
using var handler = new ThrowUserAgentExceptionHandler();
using HttpClient httpClient = new(handler);
using IChatClient client = CreateResponseClient(httpClient, "gpt-4o-mini");

InvalidOperationException e = await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetResponseAsync("hello"));

Assert.StartsWith("User-Agent header: OpenAI", e.Message);
Assert.Contains("MEAI", e.Message);
}

private static IChatClient CreateResponseClient(HttpClient httpClient, string modelId) =>
new OpenAIClient(
new ApiKeyCredential("apikey"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Extensions.AI;

internal sealed class ThrowUserAgentExceptionHandler : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) =>
throw new InvalidOperationException($"User-Agent header: {request.Headers.UserAgent}");
}
Loading