mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-09-27 01:33:37 +00:00
Let tools take a list of strings as an argument
This commit is contained in:
parent
51e7f621bb
commit
e9460a9cde
@ -33,6 +33,34 @@ public sealed class ToolParameterSchemaBuilder
|
||||
|
||||
public ToolParameterSchemaBuilder OptionalEnum(string name, string description, params string[] allowedValues) => this.Add(name, "string", description, isRequired: false, allowedValues);
|
||||
|
||||
/// <summary>
|
||||
/// An argument the model may leave out or pass as a list of strings.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// With allowed values, every entry of the list has to be one of them, such as the data sources
|
||||
/// Semantic Search may be asked to search. How many entries the list holds is for the tool to
|
||||
/// check, like everything else a model passes.
|
||||
/// </remarks>
|
||||
public ToolParameterSchemaBuilder OptionalStringArray(string name, string description, params string[] allowedValues)
|
||||
{
|
||||
var items = new JsonObject
|
||||
{
|
||||
["type"] = "string",
|
||||
};
|
||||
|
||||
if (allowedValues is { Length: > 0 })
|
||||
items["enum"] = new JsonArray([..allowedValues.Select(value => JsonValue.Create(value))]);
|
||||
|
||||
this.properties[name] = new JsonObject
|
||||
{
|
||||
["type"] = "array",
|
||||
["description"] = description,
|
||||
["items"] = items,
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Produces the finished schema.
|
||||
/// </summary>
|
||||
|
||||
@ -45,6 +45,21 @@ public sealed class OpenAIStrictToolSchemaTests
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnOptionalListMayBeNullWhileItsEntriesKeepTheirChoice()
|
||||
{
|
||||
var dataSourceIds = Converted(ToolParameterSchemaBuilder.Create()
|
||||
.RequiredString("query", "The search query.")
|
||||
.OptionalStringArray("data_source_ids", "The data sources.", "first", "second"))["properties"]!["data_source_ids"]!;
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(Types(dataSourceIds), Is.EqualTo(["array", "null"]), "Leaving the list out is said by allowing null for the list itself.");
|
||||
Assert.That(dataSourceIds["items"]!["enum"]!.AsArray().Select(value => value?.GetValue<string>()), Is.EqualTo(["first", "second"]), "Null is a way to leave the list out, not an entry it may hold.");
|
||||
Assert.That(dataSourceIds["enum"], Is.Null, "The list itself names no values of its own.");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ARequiredArgumentStaysAsItIs()
|
||||
{
|
||||
|
||||
@ -0,0 +1,52 @@
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
using AIStudio.Tools.ToolCallingSystem;
|
||||
|
||||
namespace AIStudio.Tests.Tools.ToolCalling;
|
||||
|
||||
/// <summary>
|
||||
/// Checks the plain JSON Schema a tool describes its arguments with.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is the form Anthropic and every host without strict mode receive as written, so it has to
|
||||
/// mean exactly what the tool expects. The translation for strict mode is checked on its own, see
|
||||
/// OpenAIStrictToolSchemaTests.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class ToolParameterSchemaBuilderTests
|
||||
{
|
||||
[Test]
|
||||
public void AListOfChoicesRestrictsEveryEntry()
|
||||
{
|
||||
var schema = Built(ToolParameterSchemaBuilder.Create().OptionalStringArray("data_source_ids", "The data sources.", "first", "second"));
|
||||
var property = schema["properties"]!["data_source_ids"]!;
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(property["type"]!.GetValue<string>(), Is.EqualTo("array"));
|
||||
Assert.That(property["description"]!.GetValue<string>(), Is.EqualTo("The data sources."));
|
||||
Assert.That(property["items"]!["type"]!.GetValue<string>(), Is.EqualTo("string"));
|
||||
Assert.That(property["items"]!["enum"]!.AsArray().Select(value => value!.GetValue<string>()), Is.EqualTo(new[] { "first", "second" }));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AListWithoutChoicesTakesAnyString()
|
||||
{
|
||||
var items = Built(ToolParameterSchemaBuilder.Create().OptionalStringArray("tags", "Some tags."))["properties"]!["tags"]!["items"]!;
|
||||
|
||||
Assert.That(items["enum"], Is.Null, "An empty enum would allow no entry at all rather than any.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnOptionalListIsNotRequired()
|
||||
{
|
||||
var schema = Built(ToolParameterSchemaBuilder.Create()
|
||||
.RequiredString("query", "The search query.")
|
||||
.OptionalStringArray("data_source_ids", "The data sources.", "first"));
|
||||
|
||||
Assert.That(schema["required"]!.AsArray().Select(name => name!.GetValue<string>()), Is.EqualTo(new[] { "query" }));
|
||||
}
|
||||
|
||||
private static JsonNode Built(ToolParameterSchemaBuilder builder) => JsonNode.Parse(builder.Build().GetRawText())!;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user