|
| 1 | +using System.ComponentModel; |
| 2 | +using Microsoft.Extensions.AI; |
| 3 | +using static ConfigurationExtensions; |
| 4 | + |
| 5 | +namespace Devlooped.Extensions.AI; |
| 6 | + |
| 7 | +public class ToolsTests(ITestOutputHelper output) |
| 8 | +{ |
| 9 | + public record ToolResult(string Name, string Description, string Content); |
| 10 | + |
| 11 | + [SecretsFact("OPENAI_API_KEY")] |
| 12 | + public async Task RunToolResult() |
| 13 | + { |
| 14 | + var chat = new Chat() |
| 15 | + { |
| 16 | + { "system", "You make up a tool run by making up a name, description and content based on whatever the user says." }, |
| 17 | + { "user", "I want to create an order for a dozen eggs" }, |
| 18 | + }; |
| 19 | + |
| 20 | + var client = new OpenAIChatClient(Configuration["OPENAI_API_KEY"]!, "gpt-4.1", |
| 21 | + OpenAI.OpenAIClientOptions.WriteTo(output)) |
| 22 | + .AsBuilder() |
| 23 | + .UseFunctionInvocation() |
| 24 | + .Build(); |
| 25 | + |
| 26 | + var tool = ToolFactory.Create(RunTool); |
| 27 | + var options = new ChatOptions |
| 28 | + { |
| 29 | + ToolMode = ChatToolMode.RequireSpecific(tool.Name), |
| 30 | + Tools = [tool] |
| 31 | + }; |
| 32 | + |
| 33 | + var response = await client.GetResponseAsync(chat, options); |
| 34 | + var result = response.FindCalls<ToolResult>(tool).FirstOrDefault(); |
| 35 | + |
| 36 | + Assert.NotNull(result); |
| 37 | + Assert.NotNull(result.Call); |
| 38 | + Assert.Equal(tool.Name, result.Call.Name); |
| 39 | + Assert.NotNull(result.Outcome); |
| 40 | + Assert.Null(result.Outcome.Exception); |
| 41 | + } |
| 42 | + |
| 43 | + [SecretsFact("OPENAI_API_KEY")] |
| 44 | + public async Task RunToolTerminateResult() |
| 45 | + { |
| 46 | + var chat = new Chat() |
| 47 | + { |
| 48 | + { "system", "You make up a tool run by making up a name, description and content based on whatever the user says." }, |
| 49 | + { "user", "I want to create an order for a dozen eggs" }, |
| 50 | + }; |
| 51 | + |
| 52 | + var client = new OpenAIChatClient(Configuration["OPENAI_API_KEY"]!, "gpt-4.1", |
| 53 | + OpenAI.OpenAIClientOptions.WriteTo(output)) |
| 54 | + .AsBuilder() |
| 55 | + .UseFunctionInvocation() |
| 56 | + .Build(); |
| 57 | + |
| 58 | + var tool = ToolFactory.Create(RunToolTerminate); |
| 59 | + var options = new ChatOptions |
| 60 | + { |
| 61 | + ToolMode = ChatToolMode.RequireSpecific(tool.Name), |
| 62 | + Tools = [tool] |
| 63 | + }; |
| 64 | + |
| 65 | + var response = await client.GetResponseAsync(chat, options); |
| 66 | + var result = response.FindCalls<ToolResult>(tool).FirstOrDefault(); |
| 67 | + |
| 68 | + Assert.NotNull(result); |
| 69 | + Assert.NotNull(result.Call); |
| 70 | + Assert.Equal(tool.Name, result.Call.Name); |
| 71 | + Assert.NotNull(result.Outcome); |
| 72 | + Assert.Null(result.Outcome.Exception); |
| 73 | + } |
| 74 | + |
| 75 | + [SecretsFact("OPENAI_API_KEY")] |
| 76 | + public async Task RunToolExceptionOutcome() |
| 77 | + { |
| 78 | + var chat = new Chat() |
| 79 | + { |
| 80 | + { "system", "You make up a tool run by making up a name, description and content based on whatever the user says." }, |
| 81 | + { "user", "I want to create an order for a dozen eggs" }, |
| 82 | + }; |
| 83 | + |
| 84 | + var client = new OpenAIChatClient(Configuration["OPENAI_API_KEY"]!, "gpt-4.1", |
| 85 | + OpenAI.OpenAIClientOptions.WriteTo(output)) |
| 86 | + .AsBuilder() |
| 87 | + .UseFunctionInvocation() |
| 88 | + .Build(); |
| 89 | + |
| 90 | + var tool = ToolFactory.Create(RunToolThrows); |
| 91 | + var options = new ChatOptions |
| 92 | + { |
| 93 | + ToolMode = ChatToolMode.RequireSpecific(tool.Name), |
| 94 | + Tools = [tool] |
| 95 | + }; |
| 96 | + |
| 97 | + var response = await client.GetResponseAsync(chat, options); |
| 98 | + var result = response.FindCalls(tool).FirstOrDefault(); |
| 99 | + |
| 100 | + Assert.NotNull(result); |
| 101 | + Assert.NotNull(result.Call); |
| 102 | + Assert.Equal(tool.Name, result.Call.Name); |
| 103 | + Assert.NotNull(result.Outcome); |
| 104 | + Assert.NotNull(result.Outcome.Exception); |
| 105 | + } |
| 106 | + |
| 107 | + [Description("Runs a tool to provide a result based on user input.")] |
| 108 | + ToolResult RunTool( |
| 109 | + [Description("The name")] string name, |
| 110 | + [Description("The description")] string description, |
| 111 | + [Description("The content")] string content) |
| 112 | + { |
| 113 | + // Simulate running a tool and returning a result |
| 114 | + return new ToolResult(name, description, content); |
| 115 | + } |
| 116 | + |
| 117 | + [Description("Runs a tool to provide a result based on user input.")] |
| 118 | + ToolResult RunToolTerminate( |
| 119 | + [Description("The name")] string name, |
| 120 | + [Description("The description")] string description, |
| 121 | + [Description("The content")] string content) |
| 122 | + { |
| 123 | + FunctionInvokingChatClient.CurrentContext?.Terminate = true; |
| 124 | + // Simulate running a tool and returning a result |
| 125 | + return new ToolResult(name, description, content); |
| 126 | + } |
| 127 | + |
| 128 | + [Description("Runs a tool to provide a result based on user input.")] |
| 129 | + ToolResult RunToolThrows( |
| 130 | + [Description("The name")] string name, |
| 131 | + [Description("The description")] string description, |
| 132 | + [Description("The content")] string content) |
| 133 | + { |
| 134 | + FunctionInvokingChatClient.CurrentContext?.Terminate = true; |
| 135 | + throw new ArgumentException("BOOM"); |
| 136 | + } |
| 137 | +} |
0 commit comments