-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathToolsTests.cs
More file actions
277 lines (230 loc) · 10.7 KB
/
ToolsTests.cs
File metadata and controls
277 lines (230 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using GitHub.Copilot.SDK.Test.Harness;
using Microsoft.Extensions.AI;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Xunit;
using Xunit.Abstractions;
namespace GitHub.Copilot.SDK.Test;
public partial class ToolsTests(E2ETestFixture fixture, ITestOutputHelper output) : E2ETestBase(fixture, "tools", output)
{
[Fact]
public async Task Invokes_Built_In_Tools()
{
await File.WriteAllTextAsync(
Path.Combine(Ctx.WorkDir, "README.md"),
"# ELIZA, the only chatbot you'll ever need");
var session = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
});
await session.SendAsync(new MessageOptions
{
Prompt = "What's the first line of README.md in this directory?"
});
var assistantMessage = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.NotNull(assistantMessage);
Assert.Contains("ELIZA", assistantMessage!.Data.Content ?? string.Empty);
}
[Fact]
public async Task Invokes_Custom_Tool()
{
var session = await CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(EncryptString, "encrypt_string")],
OnPermissionRequest = PermissionHandler.ApproveAll,
});
await session.SendAsync(new MessageOptions
{
Prompt = "Use encrypt_string to encrypt this string: Hello"
});
var assistantMessage = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.NotNull(assistantMessage);
Assert.Contains("HELLO", assistantMessage!.Data.Content ?? string.Empty);
[Description("Encrypts a string")]
static string EncryptString([Description("String to encrypt")] string input)
=> input.ToUpperInvariant();
}
[Fact]
public async Task Handles_Tool_Calling_Errors()
{
var getUserLocation = AIFunctionFactory.Create(
() => { throw new Exception("Melbourne"); }, "get_user_location", "Gets the user's location");
var session = await CreateSessionAsync(new SessionConfig
{
Tools = [getUserLocation],
OnPermissionRequest = PermissionHandler.ApproveAll,
});
await session.SendAsync(new MessageOptions { Prompt = "What is my location? If you can't find out, just say 'unknown'." });
var answer = await TestHelper.GetFinalAssistantMessageAsync(session);
// Check the underlying traffic
var traffic = await Ctx.GetExchangesAsync();
var lastConversation = traffic[^1];
var toolCalls = lastConversation.Request.Messages
.Where(m => m.Role == "assistant" && m.ToolCalls != null)
.SelectMany(m => m.ToolCalls!)
.ToList();
Assert.Single(toolCalls);
var toolCall = toolCalls[0];
Assert.Equal("function", toolCall.Type);
Assert.Equal("get_user_location", toolCall.Function.Name);
var toolResults = lastConversation.Request.Messages
.Where(m => m.Role == "tool")
.ToList();
Assert.Single(toolResults);
var toolResult = toolResults[0];
Assert.Equal(toolCall.Id, toolResult.ToolCallId);
Assert.DoesNotContain("Melbourne", toolResult.Content);
// Importantly, we're checking that the assistant does not see the
// exception information as if it was the tool's output.
Assert.DoesNotContain("Melbourne", answer?.Data.Content);
Assert.Contains("unknown", answer?.Data.Content?.ToLowerInvariant());
}
[Fact]
public async Task Can_Receive_And_Return_Complex_Types()
{
ToolInvocation? receivedInvocation = null;
var session = await CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(PerformDbQuery, "db_query", serializerOptions: ToolsTestsJsonContext.Default.Options)],
OnPermissionRequest = PermissionHandler.ApproveAll,
});
await session.SendAsync(new MessageOptions
{
Prompt =
"Perform a DB query for the 'cities' table using IDs 12 and 19, sorting ascending. " +
"Reply only with lines of the form: [cityname] [population]"
});
var assistantMessage = await TestHelper.GetFinalAssistantMessageAsync(session);
var responseContent = assistantMessage?.Data.Content!;
Assert.NotNull(assistantMessage);
Assert.NotEmpty(responseContent);
Assert.Contains("Passos", responseContent);
Assert.Contains("San Lorenzo", responseContent);
Assert.Contains("135460", responseContent.Replace(",", ""));
Assert.Contains("204356", responseContent.Replace(",", ""));
// We can access the raw invocation if needed
Assert.Equal(session.SessionId, receivedInvocation!.SessionId);
City[] PerformDbQuery(DbQueryOptions query, AIFunctionArguments rawArgs)
{
Assert.Equal("cities", query.Table);
Assert.Equal([12, 19], query.Ids);
Assert.True(query.SortAscending);
receivedInvocation = (ToolInvocation)rawArgs.Context![typeof(ToolInvocation)]!;
return [new(19, "Passos", 135460), new(12, "San Lorenzo", 204356)];
}
}
record DbQueryOptions(string Table, int[] Ids, bool SortAscending);
record City(int CountryId, string CityName, int Population);
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web)]
[JsonSerializable(typeof(DbQueryOptions))]
[JsonSerializable(typeof(City[]))]
[JsonSerializable(typeof(JsonElement))]
private partial class ToolsTestsJsonContext : JsonSerializerContext;
[Fact]
public async Task Overrides_Built_In_Tool_With_Custom_Tool()
{
var session = await CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create((Delegate)CustomGrep, new AIFunctionFactoryOptions
{
Name = "grep",
AdditionalProperties = new ReadOnlyDictionary<string, object?>(
new Dictionary<string, object?> { ["is_override"] = true })
})],
OnPermissionRequest = PermissionHandler.ApproveAll,
});
await session.SendAsync(new MessageOptions
{
Prompt = "Use grep to search for the word 'hello'"
});
var assistantMessage = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.NotNull(assistantMessage);
Assert.Contains("CUSTOM_GREP_RESULT", assistantMessage!.Data.Content ?? string.Empty);
[Description("A custom grep implementation that overrides the built-in")]
static string CustomGrep([Description("Search query")] string query)
=> $"CUSTOM_GREP_RESULT: {query}";
}
[Fact(Skip = "Behaves as if no content was in the result. Likely that binary results aren't fully implemented yet.")]
public async Task Can_Return_Binary_Result()
{
var session = await CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(GetImage, "get_image")],
OnPermissionRequest = PermissionHandler.ApproveAll,
});
await session.SendAsync(new MessageOptions
{
Prompt = "Use get_image. What color is the square in the image?"
});
var assistantMessage = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.NotNull(assistantMessage);
Assert.Contains("yellow", assistantMessage!.Data.Content?.ToLowerInvariant() ?? string.Empty);
static ToolResultAIContent GetImage() => new(new()
{
BinaryResultsForLlm = [new() {
// 2x2 yellow square
Data = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAADklEQVR4nGP4/5/h/38GABkAA/0k+7UAAAAASUVORK5CYII=",
Type = "base64",
MimeType = "image/png",
}],
SessionLog = "Returned an image",
});
}
[Fact]
public async Task Invokes_Custom_Tool_With_Permission_Handler()
{
var permissionRequests = new List<PermissionRequest>();
var session = await Client.CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(EncryptStringForPermission, "encrypt_string")],
OnPermissionRequest = (request, invocation) =>
{
permissionRequests.Add(request);
return Task.FromResult(new PermissionRequestResult { Kind = PermissionRequestResultKind.Approved });
},
});
await session.SendAsync(new MessageOptions
{
Prompt = "Use encrypt_string to encrypt this string: Hello"
});
var assistantMessage = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.NotNull(assistantMessage);
Assert.Contains("HELLO", assistantMessage!.Data.Content ?? string.Empty);
// Should have received a custom-tool permission request with the correct tool name
var customToolRequest = permissionRequests.OfType<PermissionRequestCustomTool>().FirstOrDefault();
Assert.NotNull(customToolRequest);
Assert.Equal("encrypt_string", customToolRequest!.ToolName);
[Description("Encrypts a string")]
static string EncryptStringForPermission([Description("String to encrypt")] string input)
=> input.ToUpperInvariant();
}
[Fact]
public async Task Denies_Custom_Tool_When_Permission_Denied()
{
var toolHandlerCalled = false;
var session = await Client.CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(EncryptStringDenied, "encrypt_string")],
OnPermissionRequest = async (request, invocation) => new() { Kind = PermissionRequestResultKind.DeniedInteractivelyByUser },
});
await session.SendAsync(new MessageOptions
{
Prompt = "Use encrypt_string to encrypt this string: Hello"
});
await TestHelper.GetFinalAssistantMessageAsync(session);
// The tool handler should NOT have been called since permission was denied
Assert.False(toolHandlerCalled);
[Description("Encrypts a string")]
string EncryptStringDenied([Description("String to encrypt")] string input)
{
toolHandlerCalled = true;
return input.ToUpperInvariant();
}
}
}