-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathToolsTests.cs
More file actions
180 lines (149 loc) · 6.93 KB
/
ToolsTests.cs
File metadata and controls
180 lines (149 loc) · 6.93 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using GitHub.Copilot.SDK.Test.Harness;
using Microsoft.Extensions.AI;
using System.ComponentModel;
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 Client.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 Client.CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(EncryptString, "encrypt_string")],
});
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 Client.CreateSessionAsync(new SessionConfig
{
Tools = [getUserLocation]
});
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 Client.CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(PerformDbQuery, "db_query", serializerOptions: ToolsTestsJsonContext.Default.Options)],
});
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(new[] { 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(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 Client.CreateSessionAsync(new SessionConfig
{
Tools = [AIFunctionFactory.Create(GetImage, "get_image")],
});
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 ToolResultAIContent(new()
{
BinaryResultsForLlm = [new() {
// 2x2 yellow square
Data = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAADklEQVR4nGP4/5/h/38GABkAA/0k+7UAAAAASUVORK5CYII=",
Type = "base64",
MimeType = "image/png",
}],
SessionLog = "Returned an image",
});
}
}