Skip to content

Add AsChatClient for OpenAIResponseClient #6103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2025
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 @@ -13,7 +13,7 @@
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="$(MicrosoftMLTokenizersVersion)" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="OpenAI" Version="2.2.0-beta.1" />
<PackageVersion Include="OpenAI" Version="2.2.0-beta.3" />
<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.2.0-beta.1" />
<PackageVersion Include="Azure.AI.OpenAI" Version="2.2.0-beta.2" />
<PackageVersion Include="Azure.Identity" Version="1.13.2" />
<PackageVersion Include="autofixture" Version="4.17.0" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.5" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Extensions.AI;

/// <summary>Represents a hosted tool that can be specified to an AI service to enable it to execute code it generates.</summary>
/// <remarks>
/// This tool does not itself implement code interpretation. It is a marker that can be used to inform a service
/// that the service is allowed to execute its generated code if the service is capable of doing so.
/// </remarks>
public class HostedCodeInterpreterTool : AITool
{
/// <summary>Initializes a new instance of the <see cref="HostedCodeInterpreterTool"/> class.</summary>
public HostedCodeInterpreterTool()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Extensions.AI;

/// <summary>Represents a hosted tool that can be specified to an AI service to enable it to perform web searches.</summary>
/// <remarks>
/// This tool does not itself implement web searches. It is a marker that can be used to inform a service
/// that the service is allowed to perform web searches if the service is capable of doing so.
/// </remarks>
public class HostedWebSearchTool : AITool
{
/// <summary>Initializes a new instance of the <see cref="HostedWebSearchTool"/> class.</summary>
public HostedWebSearchTool()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ strictObj is bool strictValue ?
runOptions.ToolsOverride.Add(ToolDefinition.CreateFunction(aiFunction.Name, aiFunction.Description, functionParameters, strict));
break;

case CodeInterpreterTool:
case HostedCodeInterpreterTool:
runOptions.ToolsOverride.Add(ToolDefinition.CreateCodeInterpreter());
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using OpenAI.Assistants;
using OpenAI.Chat;
using OpenAI.Embeddings;
using OpenAI.Responses;

namespace Microsoft.Extensions.AI;

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

/// <summary>Gets an <see cref="IChatClient"/> for use with this <see cref="OpenAIResponseClient"/>.</summary>
/// <param name="responseClient">The client.</param>
/// <returns>An <see cref="IChatClient"/> that can be used to converse via the <see cref="OpenAIResponseClient"/>.</returns>
public static IChatClient AsChatClient(this OpenAIResponseClient responseClient) =>
new OpenAIResponseChatClient(responseClient);

#pragma warning disable OPENAI001 // Type is for evaluation purposes only
/// <summary>Gets an <see cref="IChatClient"/> for use with this <see cref="AssistantClient"/>.</summary>
/// <param name="assistantClient">The client.</param>
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.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -620,7 +619,7 @@ private static FunctionCallContent ParseCallContentFromBinaryData(BinaryData ut8
private static T? GetValueOrDefault<T>(this AdditionalPropertiesDictionary? dict, string key) =>
dict?.TryGetValue(key, out T? value) is true ? value : default;

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

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

[JsonPropertyName("properties")]
public Dictionary<string, JsonElement> Properties { get; set; } = [];

[JsonPropertyName("additionalProperties")]
public bool AdditionalProperties { get; set; }
}

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