feat: add parameter attribute to allow customization of name, description, and requiredness
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using AnthropicClient.Utils;
|
||||
|
||||
namespace AnthropicClient.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Attribute to describe a function parameter.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Parameter)]
|
||||
public sealed class FunctionParameterAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the parameter.
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The description of the parameter.
|
||||
/// </summary>
|
||||
public string Description { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the parameter is required.
|
||||
/// </summary>
|
||||
public bool Required { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FunctionParameterAttribute"/> class.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the parameter.</param>
|
||||
/// <param name="description">The description of the parameter.</param>
|
||||
/// <param name="required">Whether the parameter is required.</param>
|
||||
/// <returns>A new instance of the <see cref="FunctionParameterAttribute"/> class.</returns>
|
||||
public FunctionParameterAttribute(string description, string name = "", bool required = false)
|
||||
{
|
||||
ArgumentValidator.ThrowIfNullOrWhitespace(description, nameof(description));
|
||||
ArgumentValidator.ThrowIfNull(name, nameof(name));
|
||||
|
||||
Name = name;
|
||||
Description = description;
|
||||
Required = required;
|
||||
}
|
||||
}
|
||||
@@ -36,9 +36,11 @@ static class JsonSchemaGenerator
|
||||
continue;
|
||||
}
|
||||
|
||||
var paramName = parameter.Name;
|
||||
var paramDescription = string.Empty;
|
||||
var paramRequired = parameter.HasDefaultValue;
|
||||
var attribute = parameter.GetCustomAttribute<FunctionParameterAttribute>();
|
||||
|
||||
var paramName = attribute?.Name ?? parameter.Name;
|
||||
var paramDescription = attribute?.Description ?? string.Empty;
|
||||
var paramRequired = attribute?.Required ?? !parameter.HasDefaultValue;
|
||||
|
||||
var paramObject = new JsonObject();
|
||||
paramObject[DescriptionKey] = paramDescription;
|
||||
|
||||
@@ -42,4 +42,58 @@ public class JsonSchemaGeneratorTests
|
||||
|
||||
JsonAssert.Equal(expectedSchema, schema);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenerateInputSchema_GivenFunctionWithParameterThatDoesNotHaveDefaultValue_ItShouldReturnSchemaWithPropertyNameDescriptionAndRequiredProperties()
|
||||
{
|
||||
var expectedSchema = new JsonObject()
|
||||
{
|
||||
["type"] = "object",
|
||||
["properties"] = new JsonObject()
|
||||
{
|
||||
["name"] = new JsonObject()
|
||||
{
|
||||
["description"] = string.Empty
|
||||
}
|
||||
},
|
||||
["required"] = new JsonArray()
|
||||
{
|
||||
"name"
|
||||
},
|
||||
};
|
||||
|
||||
var testMethod = (string name) => name;
|
||||
var function = new AnthropicFunction(testMethod.Method);
|
||||
|
||||
var schema = JsonSchemaGenerator.GenerateInputSchema(function);
|
||||
|
||||
JsonAssert.Equal(expectedSchema, schema);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenerateInputSchema_GivenFunctionWithParameterThatHasAttribute_ItShouldReturnSchemaWithCorrectPropertyNameDescriptionAndRequiredProperty()
|
||||
{
|
||||
var expectedSchema = new JsonObject()
|
||||
{
|
||||
["type"] = "object",
|
||||
["properties"] = new JsonObject()
|
||||
{
|
||||
["Person's Age"] = new JsonObject()
|
||||
{
|
||||
["description"] = "The age of the person."
|
||||
}
|
||||
},
|
||||
["required"] = new JsonArray()
|
||||
{
|
||||
"Person's Age"
|
||||
},
|
||||
};
|
||||
|
||||
var testMethod = ([FunctionParameter("The age of the person.", "Person's Age", true)] int name) => name;
|
||||
var function = new AnthropicFunction(testMethod.Method);
|
||||
|
||||
var schema = JsonSchemaGenerator.GenerateInputSchema(function);
|
||||
|
||||
JsonAssert.Equal(expectedSchema, schema);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user