Skip to content
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

Add OpenAIRealtimeExtensions with ToConversationFunctionTool #5666

Merged
merged 8 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -11,7 +11,7 @@
<PackageVersion Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="OpenAI" Version="2.0.0" />
<PackageVersion Include="OpenAI" Version="2.1.0-beta.2" />
<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
16 changes: 16 additions & 0 deletions src/Libraries/Microsoft.Extensions.AI.OpenAI/JsonContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.Extensions.AI;

/// <summary>Source-generated JSON type information.</summary>
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web,
UseStringEnumConverter = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true)]
[JsonSerializable(typeof(OpenAIChatClient.OpenAIChatToolJson))]
[JsonSerializable(typeof(OpenAIRealtimeExtensions.ConversationFunctionToolParametersSchema))]
internal sealed partial class OpenAIJsonContext : JsonSerializerContext;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
Expand All @@ -15,7 +15,7 @@

<PropertyGroup>
<TargetFrameworks>$(TargetFrameworks);netstandard2.0</TargetFrameworks>
<NoWarn>$(NoWarn);CA1063;CA1508;CA2227;SA1316;S1121;S3358;EA0002</NoWarn>
<NoWarn>$(NoWarn);CA1063;CA1508;CA2227;SA1316;S1121;S3358;EA0002;OPENAI002</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DisableNETStandardCompatErrors>true</DisableNETStandardCompatErrors>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace Microsoft.Extensions.AI;

/// <summary>Represents an <see cref="IChatClient"/> for an OpenAI <see cref="OpenAIClient"/> or <see cref="OpenAI.Chat.ChatClient"/>.</summary>
public sealed partial class OpenAIChatClient : IChatClient
public sealed class OpenAIChatClient : IChatClient
{
private static readonly JsonElement _defaultParameterSchema = JsonDocument.Parse("{}").RootElement;

Expand Down Expand Up @@ -513,14 +513,14 @@ strictObj is bool strictValue ?
}

resultParameters = BinaryData.FromBytes(
JsonSerializer.SerializeToUtf8Bytes(tool, JsonContext.Default.OpenAIChatToolJson));
JsonSerializer.SerializeToUtf8Bytes(tool, OpenAIJsonContext.Default.OpenAIChatToolJson));
}

return ChatTool.CreateFunctionTool(aiFunction.Metadata.Name, aiFunction.Metadata.Description, resultParameters, strict);
}

/// <summary>Used to create the JSON payload for an OpenAI chat tool description.</summary>
private sealed class OpenAIChatToolJson
internal sealed class OpenAIChatToolJson
{
/// <summary>Gets a singleton JSON data for empty parameters. Optimization for the reasonably common case of a parameterless function.</summary>
public static BinaryData ZeroFunctionParametersSchema { get; } = new("""{"type":"object","required":[],"properties":{}}"""u8.ToArray());
Expand Down Expand Up @@ -681,12 +681,4 @@ private static FunctionCallContent ParseCallContentFromBinaryData(BinaryData ut8
FunctionCallContent.CreateFromParsedArguments(ut8Json, callId, name,
argumentParser: static json => JsonSerializer.Deserialize(json,
(JsonTypeInfo<IDictionary<string, object>>)AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(IDictionary<string, object>)))!);

/// <summary>Source-generated JSON type information.</summary>
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web,
UseStringEnumConverter = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true)]
[JsonSerializable(typeof(OpenAIChatToolJson))]
private sealed partial class JsonContext : JsonSerializerContext;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Microsoft.Shared.Diagnostics;
using OpenAI.RealtimeConversation;

namespace Microsoft.Extensions.AI;

/// <summary>
/// Provides extension methods for working with <see cref="RealtimeConversationSession"/> and related types.
/// </summary>
public static class OpenAIRealtimeExtensions
{
/// <summary>
/// Converts a <see cref="AIFunction"/> into a <see cref="ConversationFunctionTool"/> so that
/// it can be used with <see cref="RealtimeConversationClient"/>.
/// </summary>
/// <returns>A <see cref="ConversationFunctionTool"/> that can be used with <see cref="RealtimeConversationClient"/>.</returns>
public static ConversationFunctionTool ToConversationFunctionTool(this AIFunction aiFunction)
{
_ = Throw.IfNull(aiFunction);

var parametersSchema = new ConversationFunctionToolParametersSchema
{
Type = "object",
Properties = aiFunction.Metadata.Parameters
.Where(p => p.Schema is JsonElement)
.ToDictionary(p => p.Name, p => (JsonElement)p.Schema!),
Required = aiFunction.Metadata.Parameters
.Where(p => p.IsRequired)
.Select(p => p.Name),
};

return new ConversationFunctionTool
{
Name = aiFunction.Metadata.Name,
Description = aiFunction.Metadata.Description,
Parameters = new BinaryData(JsonSerializer.SerializeToUtf8Bytes(
parametersSchema, OpenAIJsonContext.Default.ConversationFunctionToolParametersSchema))
};
}

internal sealed class ConversationFunctionToolParametersSchema
{
public string? Type { get; set; }
public IDictionary<string, JsonElement>? Properties { get; set; }
public IEnumerable<string>? Required { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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.ComponentModel;
using Xunit;

namespace Microsoft.Extensions.AI;

public class OpenAIRealtimeTests
{
[Fact]
public void ConvertsAIFunctionToConversationFunctionTool_Basics()
{
var input = AIFunctionFactory.Create(() => { }, "MyFunction", "MyDescription");
var result = input.ToConversationFunctionTool();

Assert.Equal("MyFunction", result.Name);
Assert.Equal("MyDescription", result.Description);
}

[Fact]
public void ConvertsAIFunctionToConversationFunctionTool_Parameters()
{
var input = AIFunctionFactory.Create(MyFunction);
var result = input.ToConversationFunctionTool();

Assert.Equal(nameof(MyFunction), result.Name);
Assert.Equal("This is a description", result.Description);
Assert.Equal("""
{
"type": "object",
"properties": {
"a": {
"type": "integer"
},
"b": {
"description": "Another param",
"type": "string"
},
"c": {
"type": "object",
"properties": {
"a": {
"type": "integer"
}
},
"additionalProperties": false,
"required": [
"a"
],
"default": "null"
}
},
"required": [
"a",
"b"
]
}
""", result.Parameters.ToString());
}

[Description("This is a description")]
private MyType MyFunction(int a, [Description("Another param")] string b, MyType? c = null)
=> throw new NotSupportedException();

public class MyType
{
public int A { get; set; }
}
}
Loading