diff --git a/OpenAI.sln b/OpenAI.sln index 6c0dae33..d6d2d83e 100755 --- a/OpenAI.sln +++ b/OpenAI.sln @@ -21,20 +21,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AAA11B78 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI", "src\libs\OpenAI\OpenAI.csproj", "{0028BC85-0064-4CE8-A21A-C1F5E922BD59}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.Generators", "src\libs\OpenAI.Generators\OpenAI.Generators.csproj", "{5504353C-9B7F-402B-9268-4821AB5E73F9}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FixOpenApiSpec", "src\helpers\FixOpenApiSpec\FixOpenApiSpec.csproj", "{594DCFD8-E707-4232-B878-90FA3D880474}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "helpers", "helpers", "{1A008ECD-2300-4BE4-A302-49DDF8BE0D54}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.Generators.Core", "src\libs\OpenAI.Generators.Core\OpenAI.Generators.Core.csproj", "{FCBE699C-CCD3-4809-8128-78956BC3F2C3}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrimmingHelper", "src\helpers\TrimmingHelper\TrimmingHelper.csproj", "{9F7DC3A1-B113-4577-875D-73B2DDD6647A}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.IntegrationTests", "src\tests\OpenAI.IntegrationTests\OpenAI.IntegrationTests.csproj", "{A3F06E45-DFA8-4236-BFF5-425091762548}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.SnapshotTests", "src\tests\OpenAI.SnapshotTests\OpenAI.SnapshotTests.csproj", "{F1A9D4C9-0075-4AB0-95A2-095B7DF31FB1}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{2D8B78DE-7269-417B-9D0B-8981FA513ACB}" ProjectSection(SolutionItems) = preProject .github\workflows\auto-merge.yml = .github\workflows\auto-merge.yml @@ -48,6 +42,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenerateDocs", "src\helpers\GenerateDocs\GenerateDocs.csproj", "{ECC219F0-209A-412B-ADEC-6D97AB379E7C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpToJsonSchema", "src\libs\CSharpToJsonSchema\CSharpToJsonSchema.csproj", "{93367DED-6C55-4267-923A-4412D03376FB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpToJsonSchema.Generators", "src\libs\CSharpToJsonSchema.Generators\CSharpToJsonSchema.Generators.csproj", "{4A7B892E-94C7-493D-8B2F-34ED64AE4DCF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpToJsonSchema.SnapshotTests", "src\tests\CSharpToJsonSchema.SnapshotTests\CSharpToJsonSchema.SnapshotTests.csproj", "{8BDEB07E-C8C8-436C-B736-F3C093CB27AD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpToJsonSchema.UnitTests", "src\tests\CSharpToJsonSchema.UnitTests\CSharpToJsonSchema.UnitTests.csproj", "{8AFCC30C-C59D-498D-BE68-9A328B3E5599}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -58,18 +60,10 @@ Global {0028BC85-0064-4CE8-A21A-C1F5E922BD59}.Debug|Any CPU.Build.0 = Debug|Any CPU {0028BC85-0064-4CE8-A21A-C1F5E922BD59}.Release|Any CPU.ActiveCfg = Release|Any CPU {0028BC85-0064-4CE8-A21A-C1F5E922BD59}.Release|Any CPU.Build.0 = Release|Any CPU - {5504353C-9B7F-402B-9268-4821AB5E73F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5504353C-9B7F-402B-9268-4821AB5E73F9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5504353C-9B7F-402B-9268-4821AB5E73F9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5504353C-9B7F-402B-9268-4821AB5E73F9}.Release|Any CPU.Build.0 = Release|Any CPU {594DCFD8-E707-4232-B878-90FA3D880474}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {594DCFD8-E707-4232-B878-90FA3D880474}.Debug|Any CPU.Build.0 = Debug|Any CPU {594DCFD8-E707-4232-B878-90FA3D880474}.Release|Any CPU.ActiveCfg = Release|Any CPU {594DCFD8-E707-4232-B878-90FA3D880474}.Release|Any CPU.Build.0 = Release|Any CPU - {FCBE699C-CCD3-4809-8128-78956BC3F2C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FCBE699C-CCD3-4809-8128-78956BC3F2C3}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FCBE699C-CCD3-4809-8128-78956BC3F2C3}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FCBE699C-CCD3-4809-8128-78956BC3F2C3}.Release|Any CPU.Build.0 = Release|Any CPU {9F7DC3A1-B113-4577-875D-73B2DDD6647A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9F7DC3A1-B113-4577-875D-73B2DDD6647A}.Debug|Any CPU.Build.0 = Debug|Any CPU {9F7DC3A1-B113-4577-875D-73B2DDD6647A}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -78,28 +72,41 @@ Global {A3F06E45-DFA8-4236-BFF5-425091762548}.Debug|Any CPU.Build.0 = Debug|Any CPU {A3F06E45-DFA8-4236-BFF5-425091762548}.Release|Any CPU.ActiveCfg = Release|Any CPU {A3F06E45-DFA8-4236-BFF5-425091762548}.Release|Any CPU.Build.0 = Release|Any CPU - {F1A9D4C9-0075-4AB0-95A2-095B7DF31FB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F1A9D4C9-0075-4AB0-95A2-095B7DF31FB1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F1A9D4C9-0075-4AB0-95A2-095B7DF31FB1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F1A9D4C9-0075-4AB0-95A2-095B7DF31FB1}.Release|Any CPU.Build.0 = Release|Any CPU {ECC219F0-209A-412B-ADEC-6D97AB379E7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {ECC219F0-209A-412B-ADEC-6D97AB379E7C}.Debug|Any CPU.Build.0 = Debug|Any CPU {ECC219F0-209A-412B-ADEC-6D97AB379E7C}.Release|Any CPU.ActiveCfg = Release|Any CPU {ECC219F0-209A-412B-ADEC-6D97AB379E7C}.Release|Any CPU.Build.0 = Release|Any CPU + {93367DED-6C55-4267-923A-4412D03376FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {93367DED-6C55-4267-923A-4412D03376FB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {93367DED-6C55-4267-923A-4412D03376FB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {93367DED-6C55-4267-923A-4412D03376FB}.Release|Any CPU.Build.0 = Release|Any CPU + {4A7B892E-94C7-493D-8B2F-34ED64AE4DCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4A7B892E-94C7-493D-8B2F-34ED64AE4DCF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4A7B892E-94C7-493D-8B2F-34ED64AE4DCF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4A7B892E-94C7-493D-8B2F-34ED64AE4DCF}.Release|Any CPU.Build.0 = Release|Any CPU + {8BDEB07E-C8C8-436C-B736-F3C093CB27AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8BDEB07E-C8C8-436C-B736-F3C093CB27AD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8BDEB07E-C8C8-436C-B736-F3C093CB27AD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8BDEB07E-C8C8-436C-B736-F3C093CB27AD}.Release|Any CPU.Build.0 = Release|Any CPU + {8AFCC30C-C59D-498D-BE68-9A328B3E5599}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8AFCC30C-C59D-498D-BE68-9A328B3E5599}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AFCC30C-C59D-498D-BE68-9A328B3E5599}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8AFCC30C-C59D-498D-BE68-9A328B3E5599}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {0028BC85-0064-4CE8-A21A-C1F5E922BD59} = {61E7E11E-4558-434C-ACE8-06316A3097B3} - {5504353C-9B7F-402B-9268-4821AB5E73F9} = {61E7E11E-4558-434C-ACE8-06316A3097B3} {594DCFD8-E707-4232-B878-90FA3D880474} = {1A008ECD-2300-4BE4-A302-49DDF8BE0D54} - {FCBE699C-CCD3-4809-8128-78956BC3F2C3} = {61E7E11E-4558-434C-ACE8-06316A3097B3} {9F7DC3A1-B113-4577-875D-73B2DDD6647A} = {1A008ECD-2300-4BE4-A302-49DDF8BE0D54} {A3F06E45-DFA8-4236-BFF5-425091762548} = {AAA11B78-2764-4520-A97E-46AA7089A588} - {F1A9D4C9-0075-4AB0-95A2-095B7DF31FB1} = {AAA11B78-2764-4520-A97E-46AA7089A588} {2D8B78DE-7269-417B-9D0B-8981FA513ACB} = {E793AF18-4371-4EBD-96FC-195EB1798855} {ECC219F0-209A-412B-ADEC-6D97AB379E7C} = {1A008ECD-2300-4BE4-A302-49DDF8BE0D54} + {93367DED-6C55-4267-923A-4412D03376FB} = {61E7E11E-4558-434C-ACE8-06316A3097B3} + {4A7B892E-94C7-493D-8B2F-34ED64AE4DCF} = {61E7E11E-4558-434C-ACE8-06316A3097B3} + {8BDEB07E-C8C8-436C-B736-F3C093CB27AD} = {AAA11B78-2764-4520-A97E-46AA7089A588} + {8AFCC30C-C59D-498D-BE68-9A328B3E5599} = {AAA11B78-2764-4520-A97E-46AA7089A588} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {CED9A020-DBA5-4BE6-8096-75E528648EC1} diff --git a/src/libs/CSharpToJsonSchema.Generators/CSharpToJsonSchema.Generators.csproj b/src/libs/CSharpToJsonSchema.Generators/CSharpToJsonSchema.Generators.csproj new file mode 100644 index 00000000..e1b7614a --- /dev/null +++ b/src/libs/CSharpToJsonSchema.Generators/CSharpToJsonSchema.Generators.csproj @@ -0,0 +1,29 @@ + + + + netstandard2.0 + false + false + true + true + $(NoWarn);CA1014;CA1031;CA1308 + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + \ No newline at end of file diff --git a/src/libs/OpenAI.Generators.Core/Conversion/ToModels.cs b/src/libs/CSharpToJsonSchema.Generators/Conversion/ToModels.cs similarity index 100% rename from src/libs/OpenAI.Generators.Core/Conversion/ToModels.cs rename to src/libs/CSharpToJsonSchema.Generators/Conversion/ToModels.cs diff --git a/src/libs/OpenAI.Generators/OpenAiFunctionsGenerator.cs b/src/libs/CSharpToJsonSchema.Generators/JsonSchemaGenerator.cs similarity index 85% rename from src/libs/OpenAI.Generators/OpenAiFunctionsGenerator.cs rename to src/libs/CSharpToJsonSchema.Generators/JsonSchemaGenerator.cs index c3e4de26..34bd4a94 100755 --- a/src/libs/OpenAI.Generators/OpenAiFunctionsGenerator.cs +++ b/src/libs/CSharpToJsonSchema.Generators/JsonSchemaGenerator.cs @@ -1,17 +1,17 @@ -using H.Generators; +using H.Generators; using H.Generators.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using OpenAI.Generators.Core.Conversion; -namespace OpenAI.Generators; +namespace CSharpToJsonSchema.Generators; [Generator] -public class OpenAiToolsGenerator : IIncrementalGenerator +public class JsonSchemaGenerator : IIncrementalGenerator { #region Constants - public const string Name = nameof(OpenAiToolsGenerator); + public const string Name = nameof(JsonSchemaGenerator); public const string Id = "OATG"; #endregion @@ -22,7 +22,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) { var attributes = context.SyntaxProvider - .ForAttributeWithMetadataName("OpenAI.OpenAiToolsAttribute") + .ForAttributeWithMetadataName("CSharpToJsonSchema.GenerateJsonSchemaAttribute") .SelectManyAllAttributesOfCurrentInterfaceSyntax() .SelectAndReportExceptions(PrepareData, context, Id); diff --git a/src/libs/OpenAI.Generators.Core/Models/InterfaceData.cs b/src/libs/CSharpToJsonSchema.Generators/Models/InterfaceData.cs similarity index 100% rename from src/libs/OpenAI.Generators.Core/Models/InterfaceData.cs rename to src/libs/CSharpToJsonSchema.Generators/Models/InterfaceData.cs diff --git a/src/libs/OpenAI.Generators.Core/Models/MethodData.cs b/src/libs/CSharpToJsonSchema.Generators/Models/MethodData.cs similarity index 100% rename from src/libs/OpenAI.Generators.Core/Models/MethodData.cs rename to src/libs/CSharpToJsonSchema.Generators/Models/MethodData.cs diff --git a/src/libs/OpenAI.Generators.Core/Models/OpenApiSchema.cs b/src/libs/CSharpToJsonSchema.Generators/Models/OpenApiSchema.cs similarity index 100% rename from src/libs/OpenAI.Generators.Core/Models/OpenApiSchema.cs rename to src/libs/CSharpToJsonSchema.Generators/Models/OpenApiSchema.cs diff --git a/src/libs/OpenAI.Generators/Sources.Calls.cs b/src/libs/CSharpToJsonSchema.Generators/Sources.Calls.cs similarity index 98% rename from src/libs/OpenAI.Generators/Sources.Calls.cs rename to src/libs/CSharpToJsonSchema.Generators/Sources.Calls.cs index 68d1c8a7..adc6d016 100755 --- a/src/libs/OpenAI.Generators/Sources.Calls.cs +++ b/src/libs/CSharpToJsonSchema.Generators/Sources.Calls.cs @@ -1,7 +1,7 @@ -using H.Generators; +using H.Generators; using H.Generators.Extensions; -namespace OpenAI.Generators; +namespace CSharpToJsonSchema.Generators; internal static partial class Sources { @@ -9,10 +9,7 @@ public static string GenerateCalls(InterfaceData @interface) { var extensionsClassName = @interface.Name.Substring(startIndex: 1) + "Extensions"; - return @$" -using System.Collections.Generic; - -#nullable enable + return @$"#nullable enable namespace {@interface.Namespace} {{ diff --git a/src/libs/OpenAI.Generators/Sources.Tools.cs b/src/libs/CSharpToJsonSchema.Generators/Sources.Tools.cs similarity index 79% rename from src/libs/OpenAI.Generators/Sources.Tools.cs rename to src/libs/CSharpToJsonSchema.Generators/Sources.Tools.cs index 199a772e..75645801 100755 --- a/src/libs/OpenAI.Generators/Sources.Tools.cs +++ b/src/libs/CSharpToJsonSchema.Generators/Sources.Tools.cs @@ -1,8 +1,8 @@ -using H.Generators; +using H.Generators; using H.Generators.Extensions; using OpenAI.Generators.Core; -namespace OpenAI.Generators; +namespace CSharpToJsonSchema.Generators; internal static partial class Sources { @@ -16,7 +16,7 @@ internal static partial class Sources public static string GenerateOpenApiSchema(OpenApiSchema parameter, int depth = 0, bool schema = true) { var indent = new string(' ', depth * 4); - var name = schema ? "global::OpenAI.OpenApiSchema" : "global::OpenAI.FunctionParameters"; + const string name = "global::CSharpToJsonSchema.OpenApiSchema"; if (parameter.ArrayItem.Count != 0) { return $@"new {name} @@ -32,7 +32,7 @@ public static string GenerateOpenApiSchema(OpenApiSchema parameter, int depth = {indent} {{ {indent} Type = ""{parameter.SchemaType}"", {indent} Description = ""{parameter.Description}"", -{indent} Properties = new global::System.Collections.Generic.Dictionary +{indent} Properties = new global::System.Collections.Generic.Dictionary {indent} {{ {indent} {string.Join(",\n " + indent, parameter.Properties.Select(x => $@"[""{x.Name}""] = " + GenerateOpenApiSchema(x, depth: depth + 2)))} {indent} }}, @@ -71,21 +71,17 @@ namespace {@interface.Namespace} {{ public static partial class {extensionsClassName} {{ - public static global::System.Collections.Generic.IList AsTools(this {@interface.Name} functions) + public static global::System.Collections.Generic.IList AsTools(this {@interface.Name} functions) {{ - return new global::System.Collections.Generic.List + return new global::System.Collections.Generic.List {{ {@interface.Methods.Select(method => $@" - new global::OpenAI.ChatCompletionTool + new global::CSharpToJsonSchema.Tool {{ - Function = new global::OpenAI.FunctionObject - {{ - Name = ""{method.Name}"", - Description = ""{method.Description}"", - Strict = {(method.IsStrict ? "true" : "false")}, - Parameters = {GenerateOpenApiSchema(method.Parameters, schema: false)}, - }}, - Type = global::OpenAI.ChatCompletionToolType.Function, + Name = ""{method.Name}"", + Description = ""{method.Description}"", + Strict = {(method.IsStrict ? "true" : "false")}, + Parameters = {GenerateOpenApiSchema(method.Parameters, schema: false)}, }}, ").Inject()} }}; diff --git a/src/libs/OpenAI.Generators/Steps.cs b/src/libs/CSharpToJsonSchema.Generators/Steps.cs similarity index 94% rename from src/libs/OpenAI.Generators/Steps.cs rename to src/libs/CSharpToJsonSchema.Generators/Steps.cs index 2cf00044..f3e084f9 100644 --- a/src/libs/OpenAI.Generators/Steps.cs +++ b/src/libs/CSharpToJsonSchema.Generators/Steps.cs @@ -1,7 +1,7 @@ -using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; -namespace OpenAI.Generators; +namespace CSharpToJsonSchema.Generators; public static class CommonSteps { diff --git a/src/libs/CSharpToJsonSchema/CSharpToJsonSchema.csproj b/src/libs/CSharpToJsonSchema/CSharpToJsonSchema.csproj new file mode 100644 index 00000000..bf754361 --- /dev/null +++ b/src/libs/CSharpToJsonSchema/CSharpToJsonSchema.csproj @@ -0,0 +1,43 @@ + + + + netstandard2.0;net4.6.2;net6.0;net8.0 + $(NoWarn);CA1724;CA2227;CA1819 + + + + + + + + + Generated C# SDK based on official OpenAI OpenAPI specification. + Includes C# Source Generator which allows you to define functions natively through a C# interface, and also provides extensions that make it easier to call this interface later + api;client;sdk;dotnet;swagger;openapi;specification;openai;generated;nswag;functions;gpt-3.5;gpt-4;chatgpt;generator;source generator;constants;prices;pricing;vision;audio;embedding;moderation;image;chat + + + + + + + + + + + + + + + <_Parameter1>true + + + + + true + true + true + false + false + + + diff --git a/src/libs/CSharpToJsonSchema/GenerateJsonSchemaAttribute.cs b/src/libs/CSharpToJsonSchema/GenerateJsonSchemaAttribute.cs new file mode 100644 index 00000000..3d9502f1 --- /dev/null +++ b/src/libs/CSharpToJsonSchema/GenerateJsonSchemaAttribute.cs @@ -0,0 +1,15 @@ +// ReSharper disable once CheckNamespace +namespace CSharpToJsonSchema; + +/// +/// +/// +[AttributeUsage(AttributeTargets.Interface)] +[System.Diagnostics.Conditional("GENERATE_JSON_SCHEMA_ATTRIBUTES")] +public sealed class GenerateJsonSchemaAttribute : Attribute +{ + /// + /// + /// + public bool Strict { get; set; } +} \ No newline at end of file diff --git a/src/libs/OpenAI/Attributes/OpenApiSchema.cs b/src/libs/CSharpToJsonSchema/OpenApiSchema.cs similarity index 98% rename from src/libs/OpenAI/Attributes/OpenApiSchema.cs rename to src/libs/CSharpToJsonSchema/OpenApiSchema.cs index 46a7d9ed..a249ba9f 100644 --- a/src/libs/OpenAI/Attributes/OpenApiSchema.cs +++ b/src/libs/CSharpToJsonSchema/OpenApiSchema.cs @@ -1,5 +1,5 @@ // ReSharper disable once CheckNamespace -namespace OpenAI; +namespace CSharpToJsonSchema; /// /// diff --git a/src/libs/CSharpToJsonSchema/Tool.cs b/src/libs/CSharpToJsonSchema/Tool.cs new file mode 100644 index 00000000..6ce3fd15 --- /dev/null +++ b/src/libs/CSharpToJsonSchema/Tool.cs @@ -0,0 +1,42 @@ +#nullable enable + +namespace CSharpToJsonSchema; + +/// +/// +/// +public sealed partial class Tool +{ + /// + /// The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + /// + [global::System.Text.Json.Serialization.JsonPropertyName("name")] + [global::System.Text.Json.Serialization.JsonRequired] + public string? Name { get; set; } + + /// + /// A description of what the function does, used by the model to choose when and how to call the function. + /// + [global::System.Text.Json.Serialization.JsonPropertyName("description")] + public string? Description { get; set; } + + /// + /// The parameters the functions accepts, described as a JSON Schema object. See the [guide](/docs/guides/function-calling) for examples, and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.
+ /// Omitting `parameters` defines a function with an empty parameter list. + ///
+ [global::System.Text.Json.Serialization.JsonPropertyName("parameters")] + public object? Parameters { get; set; } + + /// + /// Whether to enable strict schema adherence when generating the function call. If set to true, the model will follow the exact schema defined in the `parameters` field. Only a subset of JSON Schema is supported when `strict` is `true`. Learn more about Structured Outputs in the [function calling guide](docs/guides/function-calling).
+ /// Default Value: false + ///
+ [global::System.Text.Json.Serialization.JsonPropertyName("strict")] + public bool? Strict { get; set; } = false; + + /// + /// Additional properties that are not explicitly defined in the schema + /// + [global::System.Text.Json.Serialization.JsonExtensionData] + public global::System.Collections.Generic.IDictionary AdditionalProperties { get; set; } = new global::System.Collections.Generic.Dictionary(); +} \ No newline at end of file diff --git a/src/libs/CSharpToJsonSchema/TypeToSchemaHelpers.cs b/src/libs/CSharpToJsonSchema/TypeToSchemaHelpers.cs new file mode 100644 index 00000000..7829c495 --- /dev/null +++ b/src/libs/CSharpToJsonSchema/TypeToSchemaHelpers.cs @@ -0,0 +1,169 @@ +using System.Diagnostics.CodeAnalysis; +using System.Text.Json.Schema; +using System.Text.Json.Serialization.Metadata; + +namespace CSharpToJsonSchema; + +/// +/// +/// +public static class TypeToSchemaHelpers +{ + /// + /// + /// + /// + /// + /// + /// + /// + public static OpenApiSchema AsJsonSchema( + JsonTypeInfo typeInfo, + bool strict) + { + typeInfo = typeInfo ?? throw new ArgumentNullException(nameof(typeInfo)); + var resolver = typeInfo.OriginatingResolver ?? throw new InvalidOperationException("OriginatingResolver is required."); + + var node = typeInfo.GetJsonSchemaAsNode(exporterOptions: new JsonSchemaExporterOptions + { + TransformSchemaNode = (context, node) => node, + TreatNullObliviousAsNonNullable = true, + }); +#pragma warning disable IL2026 + return AsJsonSchema(typeInfo.Type, strict, resolver, typeInfo.Options); +#pragma warning restore IL2026 + } + + /// + /// + /// + /// + /// + /// + /// + /// +#if NET6_0_OR_GREATER + [RequiresUnreferencedCode("This method uses reflection to generate a JSON schema. Use overload with IJsonTypeInfoResolver parameter to avoid this")] +#endif + public static OpenApiSchema AsJsonSchema( + Type type, + bool strict, + IJsonTypeInfoResolver? jsonTypeInfoResolver = null, + JsonSerializerOptions? options = null) + { + type = type ?? throw new ArgumentNullException(nameof(type)); + + var node = new JsonSerializerOptions + { + TypeInfoResolver = jsonTypeInfoResolver ?? new DefaultJsonTypeInfoResolver(), + }.GetJsonSchemaAsNode(type, exporterOptions: new JsonSchemaExporterOptions + { + TransformSchemaNode = (context, node) => node, + TreatNullObliviousAsNonNullable = true, + }); + + var schema = Create(type, strict); + if (schema.Type == "object") + { + var properties = new Dictionary(); + if (jsonTypeInfoResolver is not null) + { + var jsonTypeInfo = jsonTypeInfoResolver.GetTypeInfo( + type, + options ?? throw new InvalidOperationException("options is required.")) ?? + throw new InvalidOperationException($"JsonTypeInfo for {type.FullName} is not found."); + + foreach (var property in jsonTypeInfo.Properties) + { + properties.Add(property.Name, AsJsonSchema(property.PropertyType, strict, jsonTypeInfoResolver, options)); + } + } + else + { + foreach (var property in type.GetProperties()) + { + properties.Add(property.Name, AsJsonSchema(property.PropertyType, strict)); + } + } + schema.Properties = properties; + schema.Required = properties.Keys.ToArray(); + } + else if (schema.Type == "array") + { + schema.Items = AsJsonSchema( + type.GetElementType() ?? throw new InvalidOperationException("Array type must have an element type."), + strict: strict, + jsonTypeInfoResolver, + options); + } + + return schema; + } + + /// + /// + /// + /// + /// + /// + public static (string Type, string? Format) ToOpenApiTypeAndFormat(Type type) + { + type = type ?? throw new ArgumentNullException(nameof(type)); + + if (type.FullName == "System.String") + { + return ("string", null); + } + if (type.FullName == "System.Int64") + { + return ("integer", "int64"); + } + if (type.FullName == "System.Int32") + { + return ("integer", "int32"); + } + if (type.FullName == "System.Single") + { + return ("number", "float"); + } + if (type.FullName == "System.Double") + { + return ("number", "double"); + } + if (type.FullName == "System.DateTime") + { + return ("string", "date-time"); + } + if (type.FullName == "System.Boolean") + { + return ("boolean", null); + } + if (type.IsEnum) + { + return ("string", null); + } + if (type.IsArray) + { + return ("array", null); + } + + return ("object", null); + } + + + private static OpenApiSchema Create(Type type, bool strict) + { + var (schemaType, format) = ToOpenApiTypeAndFormat(type); + var schema = new OpenApiSchema + { + Type = schemaType, + Format = strict ? null : format, + }; + if (type.IsEnum) + { + schema.Enum = type.GetEnumNames(); + } + + return schema; + } +} \ No newline at end of file diff --git a/src/libs/OpenAI.Generators.Core/OpenAI.Generators.Core.csproj b/src/libs/OpenAI.Generators.Core/OpenAI.Generators.Core.csproj deleted file mode 100644 index 8e72f615..00000000 --- a/src/libs/OpenAI.Generators.Core/OpenAI.Generators.Core.csproj +++ /dev/null @@ -1,26 +0,0 @@ - - - - net4.6.2;netstandard2.0;net6.0;net8.0 - false - false - $(NoWarn);CA1308 - - - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - \ No newline at end of file diff --git a/src/libs/OpenAI.Generators/OpenAI.Generators.csproj b/src/libs/OpenAI.Generators/OpenAI.Generators.csproj deleted file mode 100644 index b0cadef9..00000000 --- a/src/libs/OpenAI.Generators/OpenAI.Generators.csproj +++ /dev/null @@ -1,26 +0,0 @@ - - - - netstandard2.0 - false - false - true - true - $(NoWarn);CA1014;CA1031;CA1308 - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - \ No newline at end of file diff --git a/src/libs/OpenAI/Attributes/OpenAiFunctionsAttribute.cs b/src/libs/OpenAI/Attributes/OpenAiFunctionsAttribute.cs deleted file mode 100644 index 6d5b1472..00000000 --- a/src/libs/OpenAI/Attributes/OpenAiFunctionsAttribute.cs +++ /dev/null @@ -1,15 +0,0 @@ -// ReSharper disable once CheckNamespace -namespace OpenAI; - -/// -/// -/// -[AttributeUsage(AttributeTargets.Interface)] -[System.Diagnostics.Conditional("OPENAI_TOOLS_ATTRIBUTES")] -public sealed class OpenAiToolsAttribute : Attribute -{ - /// - /// - /// - public bool Strict { get; set; } -} \ No newline at end of file diff --git a/src/libs/OpenAI/ChatClient.CreateChatCompletion.As.cs b/src/libs/OpenAI/ChatClient.CreateChatCompletion.As.cs index 0a223910..fb6aa233 100644 --- a/src/libs/OpenAI/ChatClient.CreateChatCompletion.As.cs +++ b/src/libs/OpenAI/ChatClient.CreateChatCompletion.As.cs @@ -7,7 +7,7 @@ namespace OpenAI { public partial class ChatClient { - /// + /// #if NET6_0_OR_GREATER [RequiresUnreferencedCode( "This method uses reflection to generate a JSON schema. Use overload with JsonTypeInfo parameter to avoid this.")] @@ -20,7 +20,7 @@ public partial class ChatClient global::System.Collections.Generic.IList messages, global::OpenAI.AnyOf model, double? frequencyPenalty = 0, - global::OpenAI.CreateChatCompletionRequestLogitBias? logitBias = default, + global::System.Collections.Generic.Dictionary? logitBias = default, bool? logprobs = false, int? topLogprobs = default, int? maxCompletionTokens = default, @@ -56,7 +56,7 @@ public partial class ChatClient ResponseFormat = new ResponseFormatJsonSchema { Type = ResponseFormatJsonSchemaType.JsonSchema, - JsonSchema = TypeToSchemaHelpers.AsResponseFormat(typeof(T), strict ?? false), + JsonSchema = TypeToSchemaHelpers2.AsResponseFormat(typeof(T), strict ?? false), }, Seed = seed, ServiceTier = serviceTier, @@ -82,14 +82,14 @@ public partial class ChatClient response); } - /// + /// public async global::System.Threading.Tasks.Task> CreateChatCompletionAsAsync( JsonTypeInfo jsonTypeInfo, global::System.Collections.Generic.IList messages, global::OpenAI.AnyOf model, double? frequencyPenalty = 0, - global::OpenAI.CreateChatCompletionRequestLogitBias? logitBias = default, + global::System.Collections.Generic.Dictionary? logitBias = default, bool? logprobs = false, int? topLogprobs = default, int? maxCompletionTokens = default, @@ -124,7 +124,7 @@ public partial class ChatClient ResponseFormat = new ResponseFormatJsonSchema { Type = ResponseFormatJsonSchemaType.JsonSchema, - JsonSchema = TypeToSchemaHelpers.AsResponseFormat(jsonTypeInfo, strict ?? false), + JsonSchema = TypeToSchemaHelpers2.AsResponseFormat(jsonTypeInfo, strict ?? false), }, Seed = seed, ServiceTier = serviceTier, diff --git a/src/libs/OpenAI/ChatClient.CreateChatCompletion.AsStream.cs b/src/libs/OpenAI/ChatClient.CreateChatCompletion.AsStream.cs index 10170ee6..bca6ac64 100644 --- a/src/libs/OpenAI/ChatClient.CreateChatCompletion.AsStream.cs +++ b/src/libs/OpenAI/ChatClient.CreateChatCompletion.AsStream.cs @@ -204,7 +204,7 @@ public partial class ChatClient global::System.Collections.Generic.IList messages, global::OpenAI.AnyOf model, double? frequencyPenalty = 0, - global::OpenAI.CreateChatCompletionRequestLogitBias? logitBias = default, + global::System.Collections.Generic.Dictionary? logitBias = default, bool? logprobs = false, int? topLogprobs = default, int? maxCompletionTokens = default, diff --git a/src/libs/OpenAI/Extensions/StringExtensions.cs b/src/libs/OpenAI/Extensions/StringExtensions.cs index 1c00dab5..b8da29e5 100755 --- a/src/libs/OpenAI/Extensions/StringExtensions.cs +++ b/src/libs/OpenAI/Extensions/StringExtensions.cs @@ -141,4 +141,27 @@ public static ChatCompletionRequestMessage AsUserMessage(this byte[] bytes, stri }, }; } + + /// + /// + /// + /// + /// + public static IList AsOpenAiTools( + this IList tools) + { + return tools + .Select(x => new ChatCompletionTool + { + Type = ChatCompletionToolType.Function, + Function = new FunctionObject + { + Name = x.Name!, + Description = x.Description, + Strict = x.Strict, + Parameters = x.Parameters, + }, + }) + .ToList(); + } } \ No newline at end of file diff --git a/src/libs/OpenAI/OpenAI.csproj b/src/libs/OpenAI/OpenAI.csproj index a8f2ec2e..44e2c9a9 100644 --- a/src/libs/OpenAI/OpenAI.csproj +++ b/src/libs/OpenAI/OpenAI.csproj @@ -23,26 +23,18 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + - - - - - - - - - - - - - + + + + diff --git a/src/libs/OpenAI/Attributes/FunctionParameters.Extended.cs b/src/libs/OpenAI/Tools/FunctionParameters.Extended.cs similarity index 81% rename from src/libs/OpenAI/Attributes/FunctionParameters.Extended.cs rename to src/libs/OpenAI/Tools/FunctionParameters.Extended.cs index d6f267a2..5322912b 100644 --- a/src/libs/OpenAI/Attributes/FunctionParameters.Extended.cs +++ b/src/libs/OpenAI/Tools/FunctionParameters.Extended.cs @@ -1,3 +1,5 @@ +using CSharpToJsonSchema; + // ReSharper disable once CheckNamespace namespace OpenAI; diff --git a/src/libs/OpenAI/Attributes/ResponseFormatJsonSchemaSchema.Extended.cs b/src/libs/OpenAI/Tools/ResponseFormatJsonSchemaSchema.Extended.cs similarity index 82% rename from src/libs/OpenAI/Attributes/ResponseFormatJsonSchemaSchema.Extended.cs rename to src/libs/OpenAI/Tools/ResponseFormatJsonSchemaSchema.Extended.cs index ab2aebe7..8ccbb9b8 100644 --- a/src/libs/OpenAI/Attributes/ResponseFormatJsonSchemaSchema.Extended.cs +++ b/src/libs/OpenAI/Tools/ResponseFormatJsonSchemaSchema.Extended.cs @@ -1,4 +1,7 @@ // ReSharper disable once CheckNamespace + +using CSharpToJsonSchema; + namespace OpenAI; public partial class ResponseFormatJsonSchemaSchema : OpenApiSchema; \ No newline at end of file diff --git a/src/libs/OpenAI/TypeToSchemaHelpers.cs b/src/libs/OpenAI/TypeToSchemaHelpers.cs index 6987c930..b36f1493 100644 --- a/src/libs/OpenAI/TypeToSchemaHelpers.cs +++ b/src/libs/OpenAI/TypeToSchemaHelpers.cs @@ -1,157 +1,14 @@ using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization.Metadata; +using CSharpToJsonSchema; namespace OpenAI; /// /// /// -public static class TypeToSchemaHelpers +public static class TypeToSchemaHelpers2 { - /// - /// - /// - /// - /// - /// - /// - /// - public static ResponseFormatJsonSchemaSchema AsJsonSchema( - JsonTypeInfo typeInfo, - bool strict) - { - typeInfo = typeInfo ?? throw new ArgumentNullException(nameof(typeInfo)); - var resolver = typeInfo.OriginatingResolver ?? throw new InvalidOperationException("OriginatingResolver is required."); - -#pragma warning disable IL2026 - return AsJsonSchema(typeInfo.Type, strict, resolver, typeInfo.Options); -#pragma warning restore IL2026 - } - - /// - /// - /// - /// - /// - /// - /// - /// -#if NET6_0_OR_GREATER - [RequiresUnreferencedCode("This method uses reflection to generate a JSON schema. Use overload with IJsonTypeInfoResolver parameter to avoid this")] -#endif - public static ResponseFormatJsonSchemaSchema AsJsonSchema( - Type type, - bool strict, - IJsonTypeInfoResolver? jsonTypeInfoResolver = null, - JsonSerializerOptions? options = null) - { - type = type ?? throw new ArgumentNullException(nameof(type)); - - var schema = Create(type, strict); - if (schema.Type == "object") - { - var properties = new Dictionary(); - if (jsonTypeInfoResolver is not null) - { - var jsonTypeInfo = jsonTypeInfoResolver.GetTypeInfo( - type, - options ?? throw new InvalidOperationException("options is required.")) ?? - throw new InvalidOperationException($"JsonTypeInfo for {type.FullName} is not found."); - - foreach (var property in jsonTypeInfo.Properties) - { - properties.Add(property.Name, AsJsonSchema(property.PropertyType, strict, jsonTypeInfoResolver, options)); - } - } - else - { - foreach (var property in type.GetProperties()) - { - properties.Add(property.Name, AsJsonSchema(property.PropertyType, strict)); - } - } - schema.Properties = properties; - schema.Required = properties.Keys.ToArray(); - } - else if (schema.Type == "array") - { - schema.Items = AsJsonSchema( - type.GetElementType() ?? throw new InvalidOperationException("Array type must have an element type."), - strict: strict, - jsonTypeInfoResolver, - options); - } - - return schema; - } - - /// - /// - /// - /// - /// - /// - public static (string Type, string? Format) ToOpenApiTypeAndFormat(Type type) - { - type = type ?? throw new ArgumentNullException(nameof(type)); - - if (type.FullName == "System.String") - { - return ("string", null); - } - if (type.FullName == "System.Int64") - { - return ("integer", "int64"); - } - if (type.FullName == "System.Int32") - { - return ("integer", "int32"); - } - if (type.FullName == "System.Single") - { - return ("number", "float"); - } - if (type.FullName == "System.Double") - { - return ("number", "double"); - } - if (type.FullName == "System.DateTime") - { - return ("string", "date-time"); - } - if (type.FullName == "System.Boolean") - { - return ("boolean", null); - } - if (type.IsEnum) - { - return ("string", null); - } - if (type.IsArray) - { - return ("array", null); - } - - return ("object", null); - } - - - private static ResponseFormatJsonSchemaSchema Create(Type type, bool strict) - { - var (schemaType, format) = ToOpenApiTypeAndFormat(type); - var schema = new ResponseFormatJsonSchemaSchema - { - Type = schemaType, - Format = strict ? null : format, - }; - if (type.IsEnum) - { - schema.Enum = type.GetEnumNames(); - } - - return schema; - } - /// /// /// @@ -171,7 +28,7 @@ public static ResponseFormatJsonSchemaJsonSchema AsResponseFormat(Type type, boo Description = string.Empty, Name = type.Name, Strict = strict, - Schema = AsJsonSchema(type, strict), + Schema = TypeToSchemaHelpers.AsJsonSchema(type, strict), }; } @@ -193,7 +50,7 @@ public static ResponseFormatJsonSchemaJsonSchema AsResponseFormat(JsonTypeInfo t Name = typeInfo.Type.Name, Strict = strict, #pragma warning disable IL2026 - Schema = AsJsonSchema(typeInfo.Type, strict, resolver, typeInfo.Options), + Schema = TypeToSchemaHelpers.AsJsonSchema(typeInfo.Type, strict, resolver, typeInfo.Options), #pragma warning restore IL2026 }; } diff --git a/src/tests/OpenAI.SnapshotTests/OpenAI.SnapshotTests.csproj b/src/tests/CSharpToJsonSchema.SnapshotTests/CSharpToJsonSchema.SnapshotTests.csproj similarity index 94% rename from src/tests/OpenAI.SnapshotTests/OpenAI.SnapshotTests.csproj rename to src/tests/CSharpToJsonSchema.SnapshotTests/CSharpToJsonSchema.SnapshotTests.csproj index 3505067e..17044442 100644 --- a/src/tests/OpenAI.SnapshotTests/OpenAI.SnapshotTests.csproj +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/CSharpToJsonSchema.SnapshotTests.csproj @@ -39,7 +39,7 @@ - + diff --git a/src/tests/OpenAI.SnapshotTests/ModuleInitializer.cs b/src/tests/CSharpToJsonSchema.SnapshotTests/ModuleInitializer.cs similarity index 83% rename from src/tests/OpenAI.SnapshotTests/ModuleInitializer.cs rename to src/tests/CSharpToJsonSchema.SnapshotTests/ModuleInitializer.cs index 8f9e9945..8901978e 100755 --- a/src/tests/OpenAI.SnapshotTests/ModuleInitializer.cs +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/ModuleInitializer.cs @@ -1,4 +1,4 @@ -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using VerifyTests; namespace OpenAI.SnapshotTests; diff --git a/src/tests/OpenAI.SnapshotTests/SnapshotTests.cs b/src/tests/CSharpToJsonSchema.SnapshotTests/SnapshotTests.cs similarity index 93% rename from src/tests/OpenAI.SnapshotTests/SnapshotTests.cs rename to src/tests/CSharpToJsonSchema.SnapshotTests/SnapshotTests.cs index f36fbce6..91e77ebf 100755 --- a/src/tests/OpenAI.SnapshotTests/SnapshotTests.cs +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/SnapshotTests.cs @@ -1,4 +1,4 @@ -namespace OpenAI.SnapshotTests; +namespace OpenAI.SnapshotTests; [TestClass] public class ToolTests : VerifyBase diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Calls.generated.verified.cs b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Calls.generated.verified.cs similarity index 98% rename from src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Calls.generated.verified.cs rename to src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Calls.generated.verified.cs index 58bb53b6..a6ffc71a 100644 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Calls.generated.verified.cs +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Calls.generated.verified.cs @@ -1,7 +1,4 @@ //HintName: IVariousTypesTools.Calls.generated.cs - -using System.Collections.Generic; - #nullable enable namespace OpenAI.IntegrationTests diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs similarity index 65% rename from src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs rename to src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs index 050a43e6..74caa3be 100644 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes#IVariousTypesTools.Tools.generated.verified.cs @@ -6,59 +6,57 @@ namespace OpenAI.IntegrationTests { public static partial class VariousTypesToolsExtensions { - public static global::System.Collections.Generic.IList AsTools(this IVariousTypesTools functions) + public static global::System.Collections.Generic.IList AsTools(this IVariousTypesTools functions) { - return new global::System.Collections.Generic.List + return new global::System.Collections.Generic.List { - new global::OpenAI.ChatCompletionTool + new global::CSharpToJsonSchema.Tool { - Function = new global::OpenAI.FunctionObject - { - Name = "GetCurrentWeather", - Description = "Get the current weather in a given location", - Strict = false, - Parameters = new global::OpenAI.FunctionParameters + Name = "GetCurrentWeather", + Description = "Get the current weather in a given location", + Strict = false, + Parameters = new global::CSharpToJsonSchema.OpenApiSchema { Type = "object", Description = "Get the current weather in a given location", - Properties = new global::System.Collections.Generic.Dictionary + Properties = new global::System.Collections.Generic.Dictionary { - ["parameter1"] = new global::OpenAI.OpenApiSchema + ["parameter1"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "integer", Format = "int64", Description = "", }, - ["parameter2"] = new global::OpenAI.OpenApiSchema + ["parameter2"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "integer", Format = "int32", Description = "", }, - ["parameter3"] = new global::OpenAI.OpenApiSchema + ["parameter3"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "number", Format = "float", Description = "", }, - ["parameter4"] = new global::OpenAI.OpenApiSchema + ["parameter4"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "number", Format = "double", Description = "", }, - ["parameter5"] = new global::OpenAI.OpenApiSchema + ["parameter5"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "boolean", Description = "", }, - ["dateTime"] = new global::OpenAI.OpenApiSchema + ["dateTime"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Format = "date-time", Description = "", }, - ["date"] = new global::OpenAI.OpenApiSchema + ["date"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Format = "date", @@ -67,8 +65,6 @@ public static partial class VariousTypesToolsExtensions }, Required = new string[] { "parameter1", "parameter2", "parameter3", "parameter4", "parameter5", "dateTime", "date" }, }, - }, - Type = global::OpenAI.ChatCompletionToolType.Function, }, }; } diff --git a/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes_Diagnostics.verified.txt b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes_Diagnostics.verified.txt new file mode 100644 index 00000000..ad47dbb9 --- /dev/null +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.VariousTypes_Diagnostics.verified.txt @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Calls.generated.verified.cs b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Calls.generated.verified.cs similarity index 99% rename from src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Calls.generated.verified.cs rename to src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Calls.generated.verified.cs index bf4d1446..99311bcd 100644 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Calls.generated.verified.cs +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Calls.generated.verified.cs @@ -1,7 +1,4 @@ //HintName: IWeatherTools.Calls.generated.cs - -using System.Collections.Generic; - #nullable enable namespace OpenAI.IntegrationTests diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Tools.generated.verified.cs b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Tools.generated.verified.cs similarity index 57% rename from src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Tools.generated.verified.cs rename to src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Tools.generated.verified.cs index 3762e958..77a2721c 100644 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Tools.generated.verified.cs +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather#IWeatherTools.Tools.generated.verified.cs @@ -6,29 +6,27 @@ namespace OpenAI.IntegrationTests { public static partial class WeatherToolsExtensions { - public static global::System.Collections.Generic.IList AsTools(this IWeatherTools functions) + public static global::System.Collections.Generic.IList AsTools(this IWeatherTools functions) { - return new global::System.Collections.Generic.List + return new global::System.Collections.Generic.List { - new global::OpenAI.ChatCompletionTool + new global::CSharpToJsonSchema.Tool { - Function = new global::OpenAI.FunctionObject - { - Name = "GetCurrentWeather", - Description = "Get the current weather in a given location", - Strict = false, - Parameters = new global::OpenAI.FunctionParameters + Name = "GetCurrentWeather", + Description = "Get the current weather in a given location", + Strict = false, + Parameters = new global::CSharpToJsonSchema.OpenApiSchema { Type = "object", Description = "Get the current weather in a given location", - Properties = new global::System.Collections.Generic.Dictionary + Properties = new global::System.Collections.Generic.Dictionary { - ["location"] = new global::OpenAI.OpenApiSchema + ["location"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Description = "The city and state, e.g. San Francisco, CA", }, - ["unit"] = new global::OpenAI.OpenApiSchema + ["unit"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Description = "", @@ -37,29 +35,25 @@ public static partial class WeatherToolsExtensions }, Required = new string[] { "location" }, }, - }, - Type = global::OpenAI.ChatCompletionToolType.Function, }, - new global::OpenAI.ChatCompletionTool + new global::CSharpToJsonSchema.Tool { - Function = new global::OpenAI.FunctionObject - { - Name = "GetCurrentWeatherAsync", - Description = "Get the current weather in a given location", - Strict = false, - Parameters = new global::OpenAI.FunctionParameters + Name = "GetCurrentWeatherAsync", + Description = "Get the current weather in a given location", + Strict = false, + Parameters = new global::CSharpToJsonSchema.OpenApiSchema { Type = "object", Description = "Get the current weather in a given location", - Properties = new global::System.Collections.Generic.Dictionary + Properties = new global::System.Collections.Generic.Dictionary { - ["location"] = new global::OpenAI.OpenApiSchema + ["location"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Description = "The city and state, e.g. San Francisco, CA", }, - ["unit"] = new global::OpenAI.OpenApiSchema + ["unit"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Description = "", @@ -68,8 +62,6 @@ public static partial class WeatherToolsExtensions }, Required = new string[] { "location" }, }, - }, - Type = global::OpenAI.ChatCompletionToolType.Function, }, }; } diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Calls.generated.verified.cs b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Calls.generated.verified.cs similarity index 99% rename from src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Calls.generated.verified.cs rename to src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Calls.generated.verified.cs index 25c29c05..f0ad7e7a 100644 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Calls.generated.verified.cs +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Calls.generated.verified.cs @@ -1,7 +1,4 @@ //HintName: IWeatherStrictTools.Calls.generated.cs - -using System.Collections.Generic; - #nullable enable namespace OpenAI.IntegrationTests diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Tools.generated.verified.cs b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Tools.generated.verified.cs similarity index 57% rename from src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Tools.generated.verified.cs rename to src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Tools.generated.verified.cs index 0bafec61..eb032938 100644 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Tools.generated.verified.cs +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict#IWeatherStrictTools.Tools.generated.verified.cs @@ -6,29 +6,27 @@ namespace OpenAI.IntegrationTests { public static partial class WeatherStrictToolsExtensions { - public static global::System.Collections.Generic.IList AsTools(this IWeatherStrictTools functions) + public static global::System.Collections.Generic.IList AsTools(this IWeatherStrictTools functions) { - return new global::System.Collections.Generic.List + return new global::System.Collections.Generic.List { - new global::OpenAI.ChatCompletionTool + new global::CSharpToJsonSchema.Tool { - Function = new global::OpenAI.FunctionObject - { - Name = "GetCurrentWeather", - Description = "Get the current weather in a given location", - Strict = true, - Parameters = new global::OpenAI.FunctionParameters + Name = "GetCurrentWeather", + Description = "Get the current weather in a given location", + Strict = true, + Parameters = new global::CSharpToJsonSchema.OpenApiSchema { Type = "object", Description = "Get the current weather in a given location", - Properties = new global::System.Collections.Generic.Dictionary + Properties = new global::System.Collections.Generic.Dictionary { - ["location"] = new global::OpenAI.OpenApiSchema + ["location"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Description = "The city and state, e.g. San Francisco, CA", }, - ["unit"] = new global::OpenAI.OpenApiSchema + ["unit"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Description = "", @@ -37,29 +35,25 @@ public static partial class WeatherStrictToolsExtensions }, Required = new string[] { "location", "unit" }, }, - }, - Type = global::OpenAI.ChatCompletionToolType.Function, }, - new global::OpenAI.ChatCompletionTool + new global::CSharpToJsonSchema.Tool { - Function = new global::OpenAI.FunctionObject - { - Name = "GetCurrentWeatherAsync", - Description = "Get the current weather in a given location", - Strict = true, - Parameters = new global::OpenAI.FunctionParameters + Name = "GetCurrentWeatherAsync", + Description = "Get the current weather in a given location", + Strict = true, + Parameters = new global::CSharpToJsonSchema.OpenApiSchema { Type = "object", Description = "Get the current weather in a given location", - Properties = new global::System.Collections.Generic.Dictionary + Properties = new global::System.Collections.Generic.Dictionary { - ["location"] = new global::OpenAI.OpenApiSchema + ["location"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Description = "The city and state, e.g. San Francisco, CA", }, - ["unit"] = new global::OpenAI.OpenApiSchema + ["unit"] = new global::CSharpToJsonSchema.OpenApiSchema { Type = "string", Description = "", @@ -68,8 +62,6 @@ public static partial class WeatherStrictToolsExtensions }, Required = new string[] { "location", "unit" }, }, - }, - Type = global::OpenAI.ChatCompletionToolType.Function, }, }; } diff --git a/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict_Diagnostics.verified.txt b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict_Diagnostics.verified.txt new file mode 100644 index 00000000..ad47dbb9 --- /dev/null +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.WeatherStrict_Diagnostics.verified.txt @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather_Diagnostics.verified.txt b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather_Diagnostics.verified.txt new file mode 100644 index 00000000..ad47dbb9 --- /dev/null +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/Snapshots/ToolTests.Weather_Diagnostics.verified.txt @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/src/tests/OpenAI.SnapshotTests/TestHelper.cs b/src/tests/CSharpToJsonSchema.SnapshotTests/TestHelper.cs similarity index 85% rename from src/tests/OpenAI.SnapshotTests/TestHelper.cs rename to src/tests/CSharpToJsonSchema.SnapshotTests/TestHelper.cs index 0108e870..bd686c01 100755 --- a/src/tests/OpenAI.SnapshotTests/TestHelper.cs +++ b/src/tests/CSharpToJsonSchema.SnapshotTests/TestHelper.cs @@ -1,7 +1,7 @@ -using H.Generators.Tests.Extensions; +using CSharpToJsonSchema.Generators; +using H.Generators.Tests.Extensions; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; -using OpenAI.Generators; namespace OpenAI.SnapshotTests; @@ -15,7 +15,7 @@ public static async Task CheckSourceAsync( var referenceAssemblies = LatestReferenceAssemblies.Net80; var references = await referenceAssemblies.ResolveAsync(null, cancellationToken); references = references - .Add(MetadataReference.CreateFromFile(typeof(OpenAI.OpenAiToolsAttribute).Assembly.Location)); + .Add(MetadataReference.CreateFromFile(typeof(CSharpToJsonSchema.GenerateJsonSchemaAttribute).Assembly.Location)); var compilation = (Compilation)CSharpCompilation.Create( assemblyName: "Tests", @@ -26,7 +26,7 @@ public static async Task CheckSourceAsync( references: references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); var driver = CSharpGeneratorDriver - .Create(new OpenAiToolsGenerator()) + .Create(new JsonSchemaGenerator()) .RunGeneratorsAndUpdateCompilation(compilation, out compilation, out _, cancellationToken); var diagnostics = compilation.GetDiagnostics(cancellationToken); diff --git a/src/tests/CSharpToJsonSchema.UnitTests/CSharpToJsonSchema.UnitTests.csproj b/src/tests/CSharpToJsonSchema.UnitTests/CSharpToJsonSchema.UnitTests.csproj new file mode 100644 index 00000000..17044442 --- /dev/null +++ b/src/tests/CSharpToJsonSchema.UnitTests/CSharpToJsonSchema.UnitTests.csproj @@ -0,0 +1,45 @@ + + + + net8.0 + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_NonStrict_JsonTypeInfo.verified.txt b/src/tests/CSharpToJsonSchema.UnitTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_NonStrict_JsonTypeInfo.verified.txt similarity index 100% rename from src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_NonStrict_JsonTypeInfo.verified.txt rename to src/tests/CSharpToJsonSchema.UnitTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_NonStrict_JsonTypeInfo.verified.txt diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_Strict_JsonTypeInfo.verified.txt b/src/tests/CSharpToJsonSchema.UnitTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_Strict_JsonTypeInfo.verified.txt similarity index 100% rename from src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_Strict_JsonTypeInfo.verified.txt rename to src/tests/CSharpToJsonSchema.UnitTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_Strict_JsonTypeInfo.verified.txt diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_NonStrict_JsonTypeInfo.verified.txt b/src/tests/CSharpToJsonSchema.UnitTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_NonStrict_JsonTypeInfo.verified.txt similarity index 100% rename from src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_NonStrict_JsonTypeInfo.verified.txt rename to src/tests/CSharpToJsonSchema.UnitTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_NonStrict_JsonTypeInfo.verified.txt diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_Strict_JsonTypeInfo.verified.txt b/src/tests/CSharpToJsonSchema.UnitTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_Strict_JsonTypeInfo.verified.txt similarity index 100% rename from src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_Strict_JsonTypeInfo.verified.txt rename to src/tests/CSharpToJsonSchema.UnitTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_Strict_JsonTypeInfo.verified.txt diff --git a/src/tests/OpenAI.SnapshotTests/Tests.AsJsonSchema.cs b/src/tests/CSharpToJsonSchema.UnitTests/Tests.AsJsonSchema.cs similarity index 54% rename from src/tests/OpenAI.SnapshotTests/Tests.AsJsonSchema.cs rename to src/tests/CSharpToJsonSchema.UnitTests/Tests.AsJsonSchema.cs index 1a2c26a1..b0330b96 100755 --- a/src/tests/OpenAI.SnapshotTests/Tests.AsJsonSchema.cs +++ b/src/tests/CSharpToJsonSchema.UnitTests/Tests.AsJsonSchema.cs @@ -1,5 +1,6 @@ using System.Text.Json; using System.Text.Json.Serialization; +using CSharpToJsonSchema; namespace OpenAI.SnapshotTests; @@ -11,40 +12,42 @@ public class UnitTests : VerifyBase [DataRow(typeof(Weather))] public async Task TypeAsJsonSchema(Type type) { - var strictSchema = TypeToSchemaHelpers.AsResponseFormat(type, strict: true); - var nonStrictSchema = TypeToSchemaHelpers.AsResponseFormat(type, strict: false); - - var options = new JsonSerializerOptions - { - WriteIndented = true, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - }; - var strictJson = JsonSerializer.Serialize(strictSchema, options); - var nonStrictJson = JsonSerializer.Serialize(nonStrictSchema, options); + // var strictSchema = TypeToSchemaHelpers.AsResponseFormat(type, strict: true); + // var nonStrictSchema = TypeToSchemaHelpers.AsResponseFormat(type, strict: false); + // + // var options = new JsonSerializerOptions + // { + // WriteIndented = true, + // DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + // }; + // var strictJson = JsonSerializer.Serialize(strictSchema, options); + // var nonStrictJson = JsonSerializer.Serialize(nonStrictSchema, options); var typeInfo = SourceGeneratedContext.Default.GetTypeInfo(type); typeInfo.Should().NotBeNull(); - var strictSchemaTrimmable = TypeToSchemaHelpers.AsResponseFormat(type, strict: true); - var nonStrictSchemaTrimmable = TypeToSchemaHelpers.AsResponseFormat(type, strict: false); + var strictSchemaTrimmable = TypeToSchemaHelpers2.AsResponseFormat(typeInfo!, strict: true); + var nonStrictSchemaTrimmable = TypeToSchemaHelpers2.AsResponseFormat(typeInfo!, strict: false); var strictJsonTrimmable = JsonSerializer.Serialize(strictSchemaTrimmable, SourceGeneratedContext.Default.ResponseFormatJsonSchemaJsonSchema); var nonStrictJsonTrimmable = JsonSerializer.Serialize(nonStrictSchemaTrimmable, SourceGeneratedContext.Default.ResponseFormatJsonSchemaJsonSchema); - strictJson.Should().Be(strictJsonTrimmable); - nonStrictJson.Should().Be(nonStrictJsonTrimmable); + // strictJson.Should().Be(strictJsonTrimmable); + // nonStrictJson.Should().Be(nonStrictJsonTrimmable); await Task.WhenAll( - Verify(strictJson) - .UseDirectory($"Snapshots/Schemas/{type.Name}") - .UseTextForParameters("Strict_Type"), - Verify(nonStrictJson) - .UseDirectory($"Snapshots/Schemas/{type.Name}") - .UseTextForParameters("NonStrict_Type"), + // Verify(strictJson) + // .UseDirectory($"Snapshots/Schemas/{type.Name}") + // .UseTextForParameters("Strict_Type"), + // Verify(nonStrictJson) + // .UseDirectory($"Snapshots/Schemas/{type.Name}") + // .UseTextForParameters("NonStrict_Type"), Verify(strictJsonTrimmable) .UseDirectory($"Snapshots/Schemas/{type.Name}") - .UseTextForParameters("Strict_JsonTypeInfo"), + .UseTextForParameters("Strict_JsonTypeInfo") + .AutoVerify(), Verify(nonStrictJsonTrimmable) .UseDirectory($"Snapshots/Schemas/{type.Name}") - .UseTextForParameters("NonStrict_JsonTypeInfo")); + .UseTextForParameters("NonStrict_JsonTypeInfo") + .AutoVerify()); } } @@ -61,6 +64,7 @@ public class WordsResponse [JsonSerializable(typeof(WordsResponse))] [JsonSerializable(typeof(Weather))] [JsonSerializable(typeof(ResponseFormatJsonSchemaJsonSchema))] +[JsonSerializable(typeof(OpenApiSchema))] public partial class SourceGeneratedContext : JsonSerializerContext; diff --git a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AllTools.cs b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AllTools.cs index bfc3e606..64cad786 100644 --- a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AllTools.cs +++ b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AllTools.cs @@ -20,7 +20,7 @@ public async Task Assistants_AllTools() purpose: CreateFileRequestPurpose.Assistants); var service = new AllToolsService(); - IList tools = service.AsTools(); + IList tools = service.AsTools().AsOpenAiTools(); AssistantObject assistant = await api.Assistants.CreateAssistantAsync( model: CreateAssistantRequestModel.Gpt4o, diff --git a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AllToolsService.cs b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AllToolsService.cs index d26db46f..9f6f8dc8 100644 --- a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AllToolsService.cs +++ b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.AllToolsService.cs @@ -1,10 +1,11 @@ +using CSharpToJsonSchema; using DescriptionAttribute = System.ComponentModel.DescriptionAttribute; namespace OpenAI.IntegrationTests.Examples; // # START EXAMPLE # -[OpenAiTools(Strict = false)] +[GenerateJsonSchema(Strict = false)] public interface IAllToolsService { [Description("Provided a family relation type like 'father' or 'mother', " diff --git a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.FunctionCalling.cs b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.FunctionCalling.cs index 501a4797..73b4de7f 100644 --- a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.FunctionCalling.cs +++ b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.FunctionCalling.cs @@ -9,7 +9,7 @@ public async Task Assistants_FunctionCalling() using var api = GetAuthenticatedClient(); var service = new FunctionCallingService(); - IList tools = service.AsTools(); + IList tools = service.AsTools().AsOpenAiTools(); AssistantObject assistant = await api.Assistants.CreateAssistantAsync( model: CreateAssistantRequestModel.Gpt4o, diff --git a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.FunctionCallingStreaming.cs b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.FunctionCallingStreaming.cs index 530dc22c..8f01d321 100644 --- a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.FunctionCallingStreaming.cs +++ b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Assistants.FunctionCallingStreaming.cs @@ -9,7 +9,7 @@ public async Task Assistants_FunctionCallingStreaming() using var api = GetAuthenticatedClient(); var service = new FunctionCallingService(); - IList tools = service.AsTools(); + IList tools = service.AsTools().AsOpenAiTools(); AssistantObject assistant = await api.Assistants.CreateAssistantAsync( model: CreateAssistantRequestModel.Gpt4o, diff --git a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCalling.cs b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCalling.cs index c01cbac9..849b35d9 100644 --- a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCalling.cs +++ b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCalling.cs @@ -13,7 +13,7 @@ public async Task FunctionCalling() ]; var service = new FunctionCallingService(); - IList tools = service.AsTools(); + IList tools = service.AsTools().AsOpenAiTools(); bool requiresAction; diff --git a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCallingService.cs b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCallingService.cs index b02d1de2..dd19801b 100644 --- a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCallingService.cs +++ b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCallingService.cs @@ -1,3 +1,4 @@ +using CSharpToJsonSchema; using DescriptionAttribute = System.ComponentModel.DescriptionAttribute; namespace OpenAI.IntegrationTests.Examples; @@ -9,7 +10,7 @@ public enum WeatherUnit Fahrenheit, } -[OpenAiTools(Strict = false)] +[GenerateJsonSchema(Strict = false)] public interface IFunctionCallingService { [Description("Get the user's current location")] diff --git a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCallingStreaming.cs b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCallingStreaming.cs index bfdd5a73..bda351fa 100644 --- a/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCallingStreaming.cs +++ b/src/tests/OpenAI.IntegrationTests/Examples/Examples.Chat.FunctionCallingStreaming.cs @@ -15,7 +15,7 @@ public async Task FunctionCallingStreaming() ]; var service = new FunctionCallingService(); - IList tools = service.AsTools(); + IList tools = service.AsTools().AsOpenAiTools(); bool requiresAction; diff --git a/src/tests/OpenAI.IntegrationTests/OpenAI.IntegrationTests.csproj b/src/tests/OpenAI.IntegrationTests/OpenAI.IntegrationTests.csproj index 1bf007a2..60ecfa4d 100644 --- a/src/tests/OpenAI.IntegrationTests/OpenAI.IntegrationTests.csproj +++ b/src/tests/OpenAI.IntegrationTests/OpenAI.IntegrationTests.csproj @@ -31,8 +31,7 @@ - - + \ No newline at end of file diff --git a/src/tests/OpenAI.IntegrationTests/Tools/Tests.Tools.cs b/src/tests/OpenAI.IntegrationTests/Tools/Tests.Tools.cs index cc08c0b5..1f7705b8 100755 --- a/src/tests/OpenAI.IntegrationTests/Tools/Tests.Tools.cs +++ b/src/tests/OpenAI.IntegrationTests/Tools/Tests.Tools.cs @@ -31,7 +31,7 @@ public async Task WeatherTools(CustomProvider customProvider) try { var service = new WeatherService(); - var tools = service.AsTools(); + var tools = service.AsTools().AsOpenAiTools(); var result = await api.Chat.CreateChatCompletionAsync( messages, model: pair.Model, @@ -95,7 +95,7 @@ public async Task WeatherToolsStrict(CustomProvider customProvider) try { var service = new WeatherStrictService(); - var tools = service.AsTools(); + var tools = service.AsTools().AsOpenAiTools(); var result = await api.Chat.CreateChatCompletionAsync( messages, model: pair.Model, diff --git a/src/tests/OpenAI.IntegrationTests/Tools/VariousTypesTools.cs b/src/tests/OpenAI.IntegrationTests/Tools/VariousTypesTools.cs index 3b6844ca..733bb63b 100755 --- a/src/tests/OpenAI.IntegrationTests/Tools/VariousTypesTools.cs +++ b/src/tests/OpenAI.IntegrationTests/Tools/VariousTypesTools.cs @@ -1,10 +1,11 @@ // ReSharper disable RedundantUsingDirective using System; +using CSharpToJsonSchema; using DescriptionAttribute = System.ComponentModel.DescriptionAttribute; namespace OpenAI.IntegrationTests; -[OpenAiTools] +[GenerateJsonSchema] public interface IVariousTypesTools { [Description("Get the current weather in a given location")] diff --git a/src/tests/OpenAI.IntegrationTests/Tools/WeatherStrictTools.cs b/src/tests/OpenAI.IntegrationTests/Tools/WeatherStrictTools.cs index 141d65bd..bd45adc9 100755 --- a/src/tests/OpenAI.IntegrationTests/Tools/WeatherStrictTools.cs +++ b/src/tests/OpenAI.IntegrationTests/Tools/WeatherStrictTools.cs @@ -1,6 +1,7 @@ // ReSharper disable RedundantUsingDirective using System.Threading; using System.Threading.Tasks; +using CSharpToJsonSchema; using DescriptionAttribute = System.ComponentModel.DescriptionAttribute; namespace OpenAI.IntegrationTests; @@ -19,7 +20,7 @@ public class Weather2 public string Description { get; set; } = string.Empty; } -[OpenAiTools(Strict = true)] +[GenerateJsonSchema(Strict = true)] public interface IWeatherStrictTools { [Description("Get the current weather in a given location")] diff --git a/src/tests/OpenAI.IntegrationTests/Tools/WeatherTools.cs b/src/tests/OpenAI.IntegrationTests/Tools/WeatherTools.cs index 270127c7..5603ae44 100755 --- a/src/tests/OpenAI.IntegrationTests/Tools/WeatherTools.cs +++ b/src/tests/OpenAI.IntegrationTests/Tools/WeatherTools.cs @@ -1,6 +1,7 @@ // ReSharper disable RedundantUsingDirective using System.Threading; using System.Threading.Tasks; +using CSharpToJsonSchema; using DescriptionAttribute = System.ComponentModel.DescriptionAttribute; namespace OpenAI.IntegrationTests; @@ -19,7 +20,7 @@ public class Weather public string Description { get; set; } = string.Empty; } -[OpenAiTools] +[GenerateJsonSchema] public interface IWeatherTools { [Description("Get the current weather in a given location")] diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_NonStrict_Type.verified.txt b/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_NonStrict_Type.verified.txt deleted file mode 100644 index 20e58058..00000000 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_NonStrict_Type.verified.txt +++ /dev/null @@ -1,38 +0,0 @@ -{ - "description": "", - "name": "Weather", - "schema": { - "type": "object", - "required": [ - "Location", - "Temperature", - "Unit", - "Description" - ], - "properties": { - "Location": { - "type": "string", - "additionalProperties": false - }, - "Temperature": { - "type": "number", - "format": "double", - "additionalProperties": false - }, - "Unit": { - "type": "string", - "enum": [ - "Celsius", - "Fahrenheit" - ], - "additionalProperties": false - }, - "Description": { - "type": "string", - "additionalProperties": false - } - }, - "additionalProperties": false - }, - "strict": false -} \ No newline at end of file diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_Strict_Type.verified.txt b/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_Strict_Type.verified.txt deleted file mode 100644 index 0b3e56c1..00000000 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/Weather/UnitTests.TypeAsJsonSchema_Strict_Type.verified.txt +++ /dev/null @@ -1,37 +0,0 @@ -{ - "description": "", - "name": "Weather", - "schema": { - "type": "object", - "required": [ - "Location", - "Temperature", - "Unit", - "Description" - ], - "properties": { - "Location": { - "type": "string", - "additionalProperties": false - }, - "Temperature": { - "type": "number", - "additionalProperties": false - }, - "Unit": { - "type": "string", - "enum": [ - "Celsius", - "Fahrenheit" - ], - "additionalProperties": false - }, - "Description": { - "type": "string", - "additionalProperties": false - } - }, - "additionalProperties": false - }, - "strict": true -} \ No newline at end of file diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_NonStrict_Type.verified.txt b/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_NonStrict_Type.verified.txt deleted file mode 100644 index 4faed352..00000000 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_NonStrict_Type.verified.txt +++ /dev/null @@ -1,22 +0,0 @@ -{ - "description": "", - "name": "WordsResponse", - "schema": { - "type": "object", - "required": [ - "Words" - ], - "properties": { - "Words": { - "type": "array", - "items": { - "type": "string", - "additionalProperties": false - }, - "additionalProperties": false - } - }, - "additionalProperties": false - }, - "strict": false -} \ No newline at end of file diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_Strict_Type.verified.txt b/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_Strict_Type.verified.txt deleted file mode 100644 index 6825a7f7..00000000 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/Schemas/WordsResponse/UnitTests.TypeAsJsonSchema_Strict_Type.verified.txt +++ /dev/null @@ -1,22 +0,0 @@ -{ - "description": "", - "name": "WordsResponse", - "schema": { - "type": "object", - "required": [ - "Words" - ], - "properties": { - "Words": { - "type": "array", - "items": { - "type": "string", - "additionalProperties": false - }, - "additionalProperties": false - } - }, - "additionalProperties": false - }, - "strict": true -} \ No newline at end of file diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.VariousTypes_Diagnostics.verified.txt b/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.VariousTypes_Diagnostics.verified.txt deleted file mode 100644 index ee8e42c0..00000000 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.VariousTypes_Diagnostics.verified.txt +++ /dev/null @@ -1,27 +0,0 @@ -[ - { - Location: /* - -using System.Collections.Generic; -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -*/ - OpenAI.Generators\OpenAI.Generators.OpenAiToolsGenerator\IVariousTypesTools.Calls.generated.cs: (1,0)-(1,33), - Message: Unnecessary using directive., - Severity: Hidden, - WarningLevel: 1, - Descriptor: { - Id: CS8019, - Title: Unnecessary using directive, - HelpLink: https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS8019), - MessageFormat: Unnecessary using directive., - Category: Compiler, - DefaultSeverity: Hidden, - IsEnabledByDefault: true, - CustomTags: [ - Compiler, - Telemetry - ] - } - } -] \ No newline at end of file diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.WeatherStrict_Diagnostics.verified.txt b/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.WeatherStrict_Diagnostics.verified.txt deleted file mode 100644 index 5a698967..00000000 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.WeatherStrict_Diagnostics.verified.txt +++ /dev/null @@ -1,27 +0,0 @@ -[ - { - Location: /* - -using System.Collections.Generic; -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -*/ - OpenAI.Generators\OpenAI.Generators.OpenAiToolsGenerator\IWeatherStrictTools.Calls.generated.cs: (1,0)-(1,33), - Message: Unnecessary using directive., - Severity: Hidden, - WarningLevel: 1, - Descriptor: { - Id: CS8019, - Title: Unnecessary using directive, - HelpLink: https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS8019), - MessageFormat: Unnecessary using directive., - Category: Compiler, - DefaultSeverity: Hidden, - IsEnabledByDefault: true, - CustomTags: [ - Compiler, - Telemetry - ] - } - } -] \ No newline at end of file diff --git a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.Weather_Diagnostics.verified.txt b/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.Weather_Diagnostics.verified.txt deleted file mode 100644 index 94546ba7..00000000 --- a/src/tests/OpenAI.SnapshotTests/Snapshots/ToolTests.Weather_Diagnostics.verified.txt +++ /dev/null @@ -1,27 +0,0 @@ -[ - { - Location: /* - -using System.Collections.Generic; -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -*/ - OpenAI.Generators\OpenAI.Generators.OpenAiToolsGenerator\IWeatherTools.Calls.generated.cs: (1,0)-(1,33), - Message: Unnecessary using directive., - Severity: Hidden, - WarningLevel: 1, - Descriptor: { - Id: CS8019, - Title: Unnecessary using directive, - HelpLink: https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS8019), - MessageFormat: Unnecessary using directive., - Category: Compiler, - DefaultSeverity: Hidden, - IsEnabledByDefault: true, - CustomTags: [ - Compiler, - Telemetry - ] - } - } -] \ No newline at end of file