Files
anthropic-client/src/AnthropicClient/Models/Tool.cs
T

120 lines
5.0 KiB
C#
Raw Normal View History

using System.Reflection;
using System.Text.Json.Nodes;
2024-06-27 23:26:53 -05:00
using System.Text.Json.Serialization;
using AnthropicClient.Utils;
namespace AnthropicClient.Models;
/// <summary>
/// Represents a tool that can be used in the chat.
/// </summary>
public class Tool
{
/// <summary>
/// Gets the name of the tool.
/// </summary>
public string Name { get; }
2024-06-27 23:26:53 -05:00
/// <summary>
/// Gets the description of the tool.
/// </summary>
public string Description { get; }
2024-06-27 23:26:53 -05:00
/// <summary>
/// Gets the input schema of the tool.
/// </summary>
[JsonPropertyName("input_schema")]
2024-06-30 13:57:10 -05:00
public JsonObject InputSchema { get; }
2024-06-27 23:26:53 -05:00
/// <summary>
/// Gets the function of the tool.
2024-06-27 23:26:53 -05:00
/// </summary>
[JsonIgnore]
public AnthropicFunction Function { get; }
internal Tool(string name, string description, AnthropicFunction function)
2024-06-27 23:26:53 -05:00
{
ArgumentValidator.ThrowIfNullOrWhitespace(name, nameof(name));
ArgumentValidator.ThrowIfNullOrWhitespace(description, nameof(description));
ArgumentValidator.ThrowIfNull(function, nameof(function));
2024-06-27 23:26:53 -05:00
Name = name;
Description = description;
Function = function;
InputSchema = JsonSchemaGenerator.GenerateInputSchema(function);
}
/// <summary>
/// Creates a tool from a static method.
/// </summary>
/// <param name="name">The name of the tool.</param>
/// <param name="description">The description of the tool.</param>
/// <param name="type">The type that contains the method.</param>
/// <param name="methodName">The name of the method.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="name"/>, <paramref name="description"/>, <paramref name="type"/>, or <paramref name="methodName"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when the method is not found in the type.</exception>
/// <returns>The created tool as instance of <see cref="Tool"/>.</returns>
public static Tool CreateFromStaticMethod(string name, string description, Type type, string methodName)
{
var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
if (method is null)
{
throw new ArgumentException($"Method '{methodName}' not found in type '{type.FullName}'.", nameof(methodName));
}
return new Tool(name, description, new AnthropicFunction(method));
}
/// <summary>
/// Creates a tool from an instance method.
/// </summary>
/// <param name="name">The name of the tool.</param>
/// <param name="description">The description of the tool.</param>
/// <param name="instance">The instance that contains the method.</param>
/// <param name="methodName">The name of the method.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="name"/>, <paramref name="description"/>, <paramref name="instance"/>, or <paramref name="methodName"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when the method is not found in the type.</exception>
/// <returns>The created tool as instance of <see cref="Tool"/>.</returns>
public static Tool CreateFromInstanceMethod(string name, string description, object instance, string methodName)
{
var method = instance.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
if (method is null)
{
throw new ArgumentException($"Method '{methodName}' not found in type '{instance.GetType().FullName}'.", nameof(methodName));
}
return new Tool(name, description, new AnthropicFunction(method, instance));
}
/// <summary>
/// Creates a tool from a function.
/// </summary>
/// <typeparam name="TResult">The type of the result for the delegate.</typeparam>
/// <param name="name">The name of the tool.</param>
/// <param name="description">The description of the tool.</param>
/// <param name="func">The function.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="name"/>, <paramref name="description"/>, or <paramref name="func"/> is null.</exception>
/// <returns>The created tool as instance of <see cref="Tool"/>.</returns>
public static Tool CreateFromFunction<TResult>(string name, string description, Func<TResult> func)
{
return new Tool(name, description, new AnthropicFunction(func.Method, func.Target));
2024-06-27 23:26:53 -05:00
}
2024-06-30 13:57:10 -05:00
/// <summary>
/// Creates a tool from a function.
/// </summary>
/// <typeparam name="T1">The type of the first parameter for the delegate.</typeparam>
/// <typeparam name="TResult">The type of the result for the delegate.</typeparam>
/// <param name="name">The name of the tool.</param>
/// <param name="description">The description of the tool.</param>
/// <param name="func">The function.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="name"/>, <paramref name="description"/>, or <paramref name="func"/> is null.</exception>
/// <returns>The created tool as instance of <see cref="Tool"/>.</returns>
2024-06-30 13:57:10 -05:00
public static Tool CreateFromFunction<T1, TResult>(string name, string description, Func<T1, TResult> func)
{
return new Tool(name, description, new AnthropicFunction(func.Method, func.Target));
}
2024-06-27 23:26:53 -05:00
}