Skip to content

Commit

Permalink
fix: Change InputSchema type to object.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Dec 10, 2024
1 parent 0357382 commit 1bc3ad6
Show file tree
Hide file tree
Showing 15 changed files with 488 additions and 124 deletions.
7 changes: 7 additions & 0 deletions src/helpers/FixOpenApiSpec/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
},
});

openApiDocument.Components.Schemas["Tool"].Properties["input_schema"].AllOf.Clear();
openApiDocument.Components.Schemas["Tool"].Properties["input_schema"].Type = "object";
openApiDocument.Components.Schemas["BetaTool"].Properties["input_schema"].AllOf.Clear();
openApiDocument.Components.Schemas["BetaTool"].Properties["input_schema"].Type = "object";
openApiDocument.Components.Schemas["PromptCachingBetaTool"].Properties["input_schema"].AllOf.Clear();
openApiDocument.Components.Schemas["PromptCachingBetaTool"].Properties["input_schema"].Type = "object";

openApiDocument.Components.SecuritySchemes.Clear();
openApiDocument.Components.SecuritySchemes.Add("ApiKeyAuth", new OpenApiSecurityScheme
{
Expand Down
18 changes: 9 additions & 9 deletions src/libs/Anthropic/Extensions/AnthropicClient.ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async Task<ChatCompletion> IChatClient.CompleteAsync(
MessageStopReason.EndTurn or MessageStopReason.StopSequence => ChatFinishReason.Stop,
MessageStopReason.MaxTokens => ChatFinishReason.Length,
MessageStopReason.ToolUse => ChatFinishReason.ToolCalls,
_ => new ChatFinishReason(response.StopReason.ToString()),
_ => new ChatFinishReason(response.StopReason?.ToString() ?? "Unknown"),
},
};

Expand Down Expand Up @@ -244,14 +244,14 @@ private static CreateMessageParams CreateRequest(IList<ChatMessage> chatMessages
})
: new ToolChoice(new ToolChoiceAny())
: (ToolChoice?)null,
// Tools = options?.Tools is IList<AITool> tools ?
// tools.OfType<AIFunction>().Select(f => new Tool
// {
// Name = f.Metadata.Name,
// Description = f.Metadata.Description,
// InputSchema = CreateSchema(f),
// }).ToList() :
// null,
Tools = options?.Tools is IList<AITool> tools ?
tools.OfType<AIFunction>().Select(f => new Tool
{
Name = f.Metadata.Name,
Description = f.Metadata.Description,
InputSchema = CreateSchema(f),
}).ToList() :
null,
};
return request;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Anthropic/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static IList<Tool> AsAnthropicTools(
{
Description = x.Description ?? string.Empty,
Name = x.Name ?? string.Empty,
InputSchema = new InputSchema(), // x.Parameters ??
InputSchema = x.Parameters ?? new InputSchema(),
})
.ToList();
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/Anthropic/Generated/Anthropic.Models.BetaTool.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public sealed partial class BetaTool
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("input_schema")]
[global::System.Text.Json.Serialization.JsonRequired]
public required global::Anthropic.BetaInputSchema InputSchema { get; set; }
public required object InputSchema { get; set; }

/// <summary>
///
Expand Down Expand Up @@ -73,7 +73,7 @@ public sealed partial class BetaTool
[global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
public BetaTool(
string name,
global::Anthropic.BetaInputSchema inputSchema,
object inputSchema,
global::Anthropic.BetaToolType? type,
string? description,
global::Anthropic.BetaCacheControlEphemeral? cacheControl)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#nullable enable

namespace Anthropic
{
public sealed partial class BetaToolInputSchema
{
/// <summary>
/// Serializes the current instance to a JSON string using the provided JsonSerializerContext.
/// </summary>
public string ToJson(
global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext)
{
return global::System.Text.Json.JsonSerializer.Serialize(
this,
this.GetType(),
jsonSerializerContext);
}

/// <summary>
/// Serializes the current instance to a JSON string using the provided JsonSerializerOptions.
/// </summary>
#if NET8_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
[global::System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
#endif
public string ToJson(
global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = null)
{
return global::System.Text.Json.JsonSerializer.Serialize(
this,
jsonSerializerOptions);
}

/// <summary>
/// Deserializes a JSON string using the provided JsonSerializerContext.
/// </summary>
public static global::Anthropic.BetaToolInputSchema? FromJson(
string json,
global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext)
{
return global::System.Text.Json.JsonSerializer.Deserialize(
json,
typeof(global::Anthropic.BetaToolInputSchema),
jsonSerializerContext) as global::Anthropic.BetaToolInputSchema;
}

/// <summary>
/// Deserializes a JSON string using the provided JsonSerializerOptions.
/// </summary>
#if NET8_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
[global::System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
#endif
public static global::Anthropic.BetaToolInputSchema? FromJson(
string json,
global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = null)
{
return global::System.Text.Json.JsonSerializer.Deserialize<global::Anthropic.BetaToolInputSchema>(
json,
jsonSerializerOptions);
}

/// <summary>
/// Deserializes a JSON stream using the provided JsonSerializerContext.
/// </summary>
public static async global::System.Threading.Tasks.ValueTask<global::Anthropic.BetaToolInputSchema?> FromJsonStreamAsync(
global::System.IO.Stream jsonStream,
global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext)
{
return (await global::System.Text.Json.JsonSerializer.DeserializeAsync(
jsonStream,
typeof(global::Anthropic.BetaToolInputSchema),
jsonSerializerContext).ConfigureAwait(false)) as global::Anthropic.BetaToolInputSchema;
}

/// <summary>
/// Deserializes a JSON stream using the provided JsonSerializerOptions.
/// </summary>
#if NET8_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
[global::System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
#endif
public static global::System.Threading.Tasks.ValueTask<global::Anthropic.BetaToolInputSchema?> FromJsonStreamAsync(
global::System.IO.Stream jsonStream,
global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = null)
{
return global::System.Text.Json.JsonSerializer.DeserializeAsync<global::Anthropic.BetaToolInputSchema?>(
jsonStream,
jsonSerializerOptions);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#nullable enable

namespace Anthropic
{
/// <summary>
/// [JSON schema](https://json-schema.org/) for this tool's input.<br/>
/// This defines the shape of the `input` that your tool accepts and that the model will produce.
/// </summary>
public sealed partial class BetaToolInputSchema
{

/// <summary>
/// Additional properties that are not explicitly defined in the schema
/// </summary>
[global::System.Text.Json.Serialization.JsonExtensionData]
public global::System.Collections.Generic.IDictionary<string, object> AdditionalProperties { get; set; } = new global::System.Collections.Generic.Dictionary<string, object>();

/// <summary>
/// Initializes a new instance of the <see cref="BetaToolInputSchema" /> class.
/// </summary>
[global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
public BetaToolInputSchema(
)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public sealed partial class PromptCachingBetaTool
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("input_schema")]
[global::System.Text.Json.Serialization.JsonRequired]
public required global::Anthropic.InputSchema InputSchema { get; set; }
public required object InputSchema { get; set; }

/// <summary>
///
Expand Down Expand Up @@ -65,7 +65,7 @@ public sealed partial class PromptCachingBetaTool
[global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
public PromptCachingBetaTool(
string name,
global::Anthropic.InputSchema inputSchema,
object inputSchema,
string? description,
global::Anthropic.CacheControlEphemeral? cacheControl)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#nullable enable

namespace Anthropic
{
public sealed partial class PromptCachingBetaToolInputSchema
{
/// <summary>
/// Serializes the current instance to a JSON string using the provided JsonSerializerContext.
/// </summary>
public string ToJson(
global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext)
{
return global::System.Text.Json.JsonSerializer.Serialize(
this,
this.GetType(),
jsonSerializerContext);
}

/// <summary>
/// Serializes the current instance to a JSON string using the provided JsonSerializerOptions.
/// </summary>
#if NET8_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
[global::System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
#endif
public string ToJson(
global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = null)
{
return global::System.Text.Json.JsonSerializer.Serialize(
this,
jsonSerializerOptions);
}

/// <summary>
/// Deserializes a JSON string using the provided JsonSerializerContext.
/// </summary>
public static global::Anthropic.PromptCachingBetaToolInputSchema? FromJson(
string json,
global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext)
{
return global::System.Text.Json.JsonSerializer.Deserialize(
json,
typeof(global::Anthropic.PromptCachingBetaToolInputSchema),
jsonSerializerContext) as global::Anthropic.PromptCachingBetaToolInputSchema;
}

/// <summary>
/// Deserializes a JSON string using the provided JsonSerializerOptions.
/// </summary>
#if NET8_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
[global::System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
#endif
public static global::Anthropic.PromptCachingBetaToolInputSchema? FromJson(
string json,
global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = null)
{
return global::System.Text.Json.JsonSerializer.Deserialize<global::Anthropic.PromptCachingBetaToolInputSchema>(
json,
jsonSerializerOptions);
}

/// <summary>
/// Deserializes a JSON stream using the provided JsonSerializerContext.
/// </summary>
public static async global::System.Threading.Tasks.ValueTask<global::Anthropic.PromptCachingBetaToolInputSchema?> FromJsonStreamAsync(
global::System.IO.Stream jsonStream,
global::System.Text.Json.Serialization.JsonSerializerContext jsonSerializerContext)
{
return (await global::System.Text.Json.JsonSerializer.DeserializeAsync(
jsonStream,
typeof(global::Anthropic.PromptCachingBetaToolInputSchema),
jsonSerializerContext).ConfigureAwait(false)) as global::Anthropic.PromptCachingBetaToolInputSchema;
}

/// <summary>
/// Deserializes a JSON stream using the provided JsonSerializerOptions.
/// </summary>
#if NET8_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
[global::System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
#endif
public static global::System.Threading.Tasks.ValueTask<global::Anthropic.PromptCachingBetaToolInputSchema?> FromJsonStreamAsync(
global::System.IO.Stream jsonStream,
global::System.Text.Json.JsonSerializerOptions? jsonSerializerOptions = null)
{
return global::System.Text.Json.JsonSerializer.DeserializeAsync<global::Anthropic.PromptCachingBetaToolInputSchema?>(
jsonStream,
jsonSerializerOptions);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#nullable enable

namespace Anthropic
{
/// <summary>
/// [JSON schema](https://json-schema.org/) for this tool's input.<br/>
/// This defines the shape of the `input` that your tool accepts and that the model will produce.
/// </summary>
public sealed partial class PromptCachingBetaToolInputSchema
{

/// <summary>
/// Additional properties that are not explicitly defined in the schema
/// </summary>
[global::System.Text.Json.Serialization.JsonExtensionData]
public global::System.Collections.Generic.IDictionary<string, object> AdditionalProperties { get; set; } = new global::System.Collections.Generic.Dictionary<string, object>();

/// <summary>
/// Initializes a new instance of the <see cref="PromptCachingBetaToolInputSchema" /> class.
/// </summary>
[global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
public PromptCachingBetaToolInputSchema(
)
{
}
}
}
4 changes: 2 additions & 2 deletions src/libs/Anthropic/Generated/Anthropic.Models.Tool.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public sealed partial class Tool
/// </summary>
[global::System.Text.Json.Serialization.JsonPropertyName("input_schema")]
[global::System.Text.Json.Serialization.JsonRequired]
public required global::Anthropic.InputSchema InputSchema { get; set; }
public required object InputSchema { get; set; }

/// <summary>
/// Additional properties that are not explicitly defined in the schema
Expand All @@ -58,7 +58,7 @@ public sealed partial class Tool
[global::System.Diagnostics.CodeAnalysis.SetsRequiredMembers]
public Tool(
string name,
global::Anthropic.InputSchema inputSchema,
object inputSchema,
string? description)
{
this.Name = name ?? throw new global::System.ArgumentNullException(nameof(name));
Expand Down
Loading

0 comments on commit 1bc3ad6

Please sign in to comment.