Skip to content

Commit

Permalink
OpenAI-DotNet 8.3.0 (#369)
Browse files Browse the repository at this point in the history
- Updated library to .net 8
- Refactored TypeExtensions and JsonSchema generation
  - Improved JsonSchema generation for enums and dictionaries
  - Ensured JsonSchema properly handles nullable types
- Ensure that function args are not re-serialized and passed back into
tool function for future calls
  • Loading branch information
StephenHodgson authored Sep 19, 2024
1 parent b2c0aa7 commit 09ddbdc
Show file tree
Hide file tree
Showing 29 changed files with 348 additions and 152 deletions.
63 changes: 0 additions & 63 deletions .gitattributes

This file was deleted.

2 changes: 1 addition & 1 deletion OpenAI-DotNet-Proxy/OpenAI-DotNet-Proxy.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>disable</Nullable>
<Title>OpenAI API Proxy</Title>
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet-Tests-Proxy/OpenAI-DotNet-Tests-Proxy.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>false</ImplicitUsings>
<IsPackable>false</IsPackable>
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet-Tests/OpenAI-DotNet-Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<SignAssembly>false</SignAssembly>
<LangVersion>latest</LangVersion>
Expand Down
46 changes: 46 additions & 0 deletions OpenAI-DotNet-Tests/TestFixture_00_02_Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,51 @@ public void Test_02_01_GenerateJsonSchema()
JsonSchema mathSchema = typeof(MathResponse);
Console.WriteLine(mathSchema.ToString());
}

[Test]
public void Test_02_02_GenerateJsonSchema_PrimitiveTypes()
{
JsonSchema schema = typeof(TestSchema);
Console.WriteLine(schema.ToString());
}

private class TestSchema
{
// test all primitive types can be serialized
public bool Bool { get; set; }
public byte Byte { get; set; }
public sbyte SByte { get; set; }
public short Short { get; set; }
public ushort UShort { get; set; }
public int Integer { get; set; }
public uint UInteger { get; set; }
public long Long { get; set; }
public ulong ULong { get; set; }
public float Float { get; set; }
public double Double { get; set; }
public decimal Decimal { get; set; }
public char Char { get; set; }
public string String { get; set; }
public DateTime DateTime { get; set; }
public DateTimeOffset DateTimeOffset { get; set; }
public Guid Guid { get; set; }
// test nullables
public int? NullInt { get; set; }
public DateTime? NullDateTime { get; set; }
public TestEnum TestEnum { get; set; }
public TestEnum? NullEnum { get; set; }
public Dictionary<string, object> Dictionary { get; set; }
public IDictionary<string, int> IntDictionary { get; set; }
public IReadOnlyDictionary<string, string> StringDictionary { get; set; }
public Dictionary<string, MathResponse> CustomDictionary { get; set; }
}

private enum TestEnum
{
Enum1,
Enum2,
Enum3,
Enum4
}
}
}
3 changes: 1 addition & 2 deletions OpenAI-DotNet/Audio/SpeechRequest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using OpenAI.Extensions;
using OpenAI.Models;
using System;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -49,7 +48,7 @@ public SpeechRequest(string input, Model model = null, SpeechVoice voice = Speec
/// </summary>
[JsonPropertyName("response_format")]
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
[JsonConverter(typeof(JsonStringEnumConverter<SpeechResponseFormat>))]
[JsonConverter(typeof(Extensions.JsonStringEnumConverter<SpeechResponseFormat>))]
public SpeechResponseFormat ResponseFormat { get; }

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions OpenAI-DotNet/Batch/BatchResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using OpenAI.Extensions;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -53,7 +52,7 @@ public sealed class BatchResponse : BaseResponse
/// </summary>
[JsonInclude]
[JsonPropertyName("status")]
[JsonConverter(typeof(JsonStringEnumConverter<BatchStatus>))]
[JsonConverter(typeof(Extensions.JsonStringEnumConverter<BatchStatus>))]
public BatchStatus Status { get; private set; }

/// <summary>
Expand Down
11 changes: 10 additions & 1 deletion OpenAI-DotNet/Chat/ChatRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ChatRequest(
{
var toolList = tools?.ToList();

if (toolList != null && toolList.Any())
if (toolList is { Count: > 0 })
{
if (string.IsNullOrWhiteSpace(toolChoice))
{
Expand All @@ -57,6 +57,15 @@ public ChatRequest(
ToolChoice = toolChoice;
}
}

foreach (var tool in toolList)
{
if (tool?.Function?.Arguments != null)
{
// just in case clear any lingering func args.
tool.Function.Arguments = null;
}
}
}

Tools = toolList?.ToList();
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Common/Annotation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class Annotation : IAppendable<Annotation>

[JsonInclude]
[JsonPropertyName("type")]
[JsonConverter(typeof(JsonStringEnumConverter<AnnotationType>))]
[JsonConverter(typeof(Extensions.JsonStringEnumConverter<AnnotationType>))]
public AnnotationType Type { get; private set; }

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet/Common/Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Content(ContentType type, string input)

[JsonInclude]
[JsonPropertyName("type")]
[JsonConverter(typeof(JsonStringEnumConverter<ContentType>))]
[JsonConverter(typeof(Extensions.JsonStringEnumConverter<ContentType>))]
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public ContentType Type { get; private set; }

Expand Down
1 change: 1 addition & 0 deletions OpenAI-DotNet/Common/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ internal Function(string name, JsonNode arguments, bool? strict = null)
{
Name = name;
Arguments = arguments;
Strict = strict;
}

private Function(string name, string description, MethodInfo method, object instance = null, bool? strict = null)
Expand Down
3 changes: 1 addition & 2 deletions OpenAI-DotNet/Common/ResponseFormatObject.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using OpenAI.Extensions;
using System.Text.Json.Serialization;

namespace OpenAI
Expand All @@ -26,7 +25,7 @@ public ResponseFormatObject(JsonSchema schema)

[JsonInclude]
[JsonPropertyName("type")]
[JsonConverter(typeof(JsonStringEnumConverter<ChatResponseFormat>))]
[JsonConverter(typeof(Extensions.JsonStringEnumConverter<ChatResponseFormat>))]
public ChatResponseFormat Type { get; private set; }

[JsonInclude]
Expand Down
6 changes: 3 additions & 3 deletions OpenAI-DotNet/Common/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ public async Task<T> InvokeFunctionAsync<T>(CancellationToken cancellationToken

#region Tool Cache

private static readonly List<Tool> toolCache = new()
{
private static readonly List<Tool> toolCache =
[
FileSearch,
CodeInterpreter
};
];

/// <summary>
/// Gets a list of all available tools.
Expand Down
Loading

0 comments on commit 09ddbdc

Please sign in to comment.