Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ else
}
```

> [!IMPORTANT]
> The `ToolFactory` will also automatically sanitize the tool name
> when using local functions to avoid invalid characters and honor
> its original name.

## Console Logging

Additional `UseJsonConsoleLogging` extension for rich JSON-formatted console logging of AI requests
Expand Down
10 changes: 10 additions & 0 deletions src/AI.Tests/ToolsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ public class ToolsTests(ITestOutputHelper output)
{
public record ToolResult(string Name, string Description, string Content);

[Fact]
public void SanitizesToolName()
{
static void DoSomething() { }

var tool = ToolFactory.Create(DoSomething);

Assert.Equal("do_something", tool.Name);
}

[SecretsFact("OPENAI_API_KEY")]
public async Task RunToolResult()
{
Expand Down
31 changes: 27 additions & 4 deletions src/AI/ToolFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@ namespace Devlooped.Extensions.AI;
/// Creates tools for function calling that can leverage the <see cref="ToolExtensions"/>
/// extension methods for locating invocations and their results.
/// </summary>
public static class ToolFactory
public static partial class ToolFactory
{
/// <summary>
/// Invokes <see cref="AIFunctionFactory.Create(Delegate, string?, string?, System.Text.Json.JsonSerializerOptions?)"/>
/// using the method name following the naming convention and serialization options from <see cref="ToolJsonOptions.Default"/>.
/// using the method name following the naming convention and serialization options from <see cref="ToolJsonOptions.Default"/>
/// so that <c>FindCalls</c> extension methods on <see cref="ChatResponse"/> can be used.
/// </summary>
public static AIFunction Create(Delegate method)
public static AIFunction Create(Delegate method, string? name = default)
=> AIFunctionFactory.Create(method,
ToolJsonOptions.Default.PropertyNamingPolicy!.ConvertName(method.Method.Name),
name ?? ToolJsonOptions.Default.PropertyNamingPolicy!.ConvertName(SanitizeName(method.Method.Name)),
serializerOptions: ToolJsonOptions.Default);

static string SanitizeName(string name)
{
if (!name.Contains('<'))
return name;

// i.e.: <GetResponsesAsync>g__SetCandidates|0 > SetCandidates
var match = AnonymousMethodExpr().Match(name);
if (match.Success)
{
return match.Groups[1].Value;
}

return name
.Replace("<", string.Empty)
.Replace(">", string.Empty)
.Replace("g__", "_")
.Replace("|", string.Empty);
}

[System.Text.RegularExpressions.GeneratedRegex(@"__(.+?)\|")]
private static partial System.Text.RegularExpressions.Regex AnonymousMethodExpr();
}
Loading