Skip to content

Commit 3d61789

Browse files
authored
Add AsChatClient for OpenAIResponseClient (#6103)
* Add AsChatClient for OpenAIResponseClient * Address feedback
1 parent a2aa47e commit 3d61789

File tree

15 files changed

+1031
-40
lines changed

15 files changed

+1031
-40
lines changed

eng/packages/General.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
1414
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="$(MicrosoftMLTokenizersVersion)" />
1515
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
16-
<PackageVersion Include="OpenAI" Version="2.2.0-beta.1" />
16+
<PackageVersion Include="OpenAI" Version="2.2.0-beta.3" />
1717
<PackageVersion Include="Polly" Version="8.4.2" />
1818
<PackageVersion Include="Polly.Core" Version="8.4.2" />
1919
<PackageVersion Include="Polly.Extensions" Version="8.4.2" />

eng/packages/TestOnly.props

+1-1
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.2.0-beta.1" />
5+
<PackageVersion Include="Azure.AI.OpenAI" Version="2.2.0-beta.2" />
66
<PackageVersion Include="Azure.Identity" Version="1.13.2" />
77
<PackageVersion Include="autofixture" Version="4.17.0" />
88
<PackageVersion Include="BenchmarkDotNet" Version="0.13.5" />

src/Libraries/Microsoft.Extensions.AI.Abstractions/CodeInterpreterTool.cs

-17
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.Extensions.AI;
5+
6+
/// <summary>Represents a hosted tool that can be specified to an AI service to enable it to execute code it generates.</summary>
7+
/// <remarks>
8+
/// This tool does not itself implement code interpretation. It is a marker that can be used to inform a service
9+
/// that the service is allowed to execute its generated code if the service is capable of doing so.
10+
/// </remarks>
11+
public class HostedCodeInterpreterTool : AITool
12+
{
13+
/// <summary>Initializes a new instance of the <see cref="HostedCodeInterpreterTool"/> class.</summary>
14+
public HostedCodeInterpreterTool()
15+
{
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.Extensions.AI;
5+
6+
/// <summary>Represents a hosted tool that can be specified to an AI service to enable it to perform web searches.</summary>
7+
/// <remarks>
8+
/// This tool does not itself implement web searches. It is a marker that can be used to inform a service
9+
/// that the service is allowed to perform web searches if the service is capable of doing so.
10+
/// </remarks>
11+
public class HostedWebSearchTool : AITool
12+
{
13+
/// <summary>Initializes a new instance of the <see cref="HostedWebSearchTool"/> class.</summary>
14+
public HostedWebSearchTool()
15+
{
16+
}
17+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ strictObj is bool strictValue ?
230230
runOptions.ToolsOverride.Add(ToolDefinition.CreateFunction(aiFunction.Name, aiFunction.Description, functionParameters, strict));
231231
break;
232232

233-
case CodeInterpreterTool:
233+
case HostedCodeInterpreterTool:
234234
runOptions.ToolsOverride.Add(ToolDefinition.CreateCodeInterpreter());
235235
break;
236236
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using OpenAI.Assistants;
66
using OpenAI.Chat;
77
using OpenAI.Embeddings;
8+
using OpenAI.Responses;
89

910
namespace Microsoft.Extensions.AI;
1011

@@ -24,6 +25,12 @@ public static IChatClient AsChatClient(this OpenAIClient openAIClient, string mo
2425
public static IChatClient AsChatClient(this ChatClient chatClient) =>
2526
new OpenAIChatClient(chatClient);
2627

28+
/// <summary>Gets an <see cref="IChatClient"/> for use with this <see cref="OpenAIResponseClient"/>.</summary>
29+
/// <param name="responseClient">The client.</param>
30+
/// <returns>An <see cref="IChatClient"/> that can be used to converse via the <see cref="OpenAIResponseClient"/>.</returns>
31+
public static IChatClient AsChatClient(this OpenAIResponseClient responseClient) =>
32+
new OpenAIResponseChatClient(responseClient);
33+
2734
#pragma warning disable OPENAI001 // Type is for evaluation purposes only
2835
/// <summary>Gets an <see cref="IChatClient"/> for use with this <see cref="AssistantClient"/>.</summary>
2936
/// <param name="assistantClient">The client.</param>

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIModelMapper.ChatCompletion.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics.CodeAnalysis;
7-
using System.Globalization;
87
using System.Text;
98
using System.Text.Json;
109
using System.Text.Json.Serialization;
@@ -620,7 +619,7 @@ private static FunctionCallContent ParseCallContentFromBinaryData(BinaryData ut8
620619
private static T? GetValueOrDefault<T>(this AdditionalPropertiesDictionary? dict, string key) =>
621620
dict?.TryGetValue(key, out T? value) is true ? value : default;
622621

623-
private static string CreateCompletionId() => $"chatcmpl-{Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture)}";
622+
private static string CreateCompletionId() => $"chatcmpl-{Guid.NewGuid():N}";
624623

625624
/// <summary>Used to create the JSON payload for an OpenAI chat tool description.</summary>
626625
public sealed class OpenAIChatToolJson
@@ -633,6 +632,9 @@ public sealed class OpenAIChatToolJson
633632

634633
[JsonPropertyName("properties")]
635634
public Dictionary<string, JsonElement> Properties { get; set; } = [];
635+
636+
[JsonPropertyName("additionalProperties")]
637+
public bool AdditionalProperties { get; set; }
636638
}
637639

638640
/// <summary>POCO representing function calling info. Used to concatenation information for a single function call from across multiple streaming updates.</summary>

0 commit comments

Comments
 (0)