forked from microsoft/autogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_function_and_tool_calling.py
380 lines (311 loc) · 13.3 KB
/
test_function_and_tool_calling.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import json
from typing import Any, Callable, Dict, List
import pytest
from autogen.agentchat.conversable_agent import ConversableAgent
def _tool_func_1(arg1: str, arg2: str) -> str:
return f"_tool_func_1: {arg1} {arg2}"
def _tool_func_2(arg1: str, arg2: str) -> str:
return f"_tool_func_2: {arg1} {arg2}"
def _tool_func_error(arg1: str, arg2: str) -> str:
raise RuntimeError("Error in tool function")
async def _a_tool_func_1(arg1: str, arg2: str) -> str:
return f"_tool_func_1: {arg1} {arg2}"
async def _a_tool_func_2(arg1: str, arg2: str) -> str:
return f"_tool_func_2: {arg1} {arg2}"
async def _a_tool_func_error(arg1: str, arg2: str) -> str:
raise RuntimeError("Error in tool function")
_tool_use_message_1 = {
"role": "assistant",
"content": None,
"function_call": None,
"tool_calls": [
{
"id": "1",
"type": "function",
"function": {
"name": "_tool_func_1",
"arguments": json.dumps({"arg1": "value1", "arg2": "value2"}),
},
},
{
"id": "2",
"type": "function",
"function": {
"name": "_tool_func_2",
"arguments": json.dumps({"arg1": "value3", "arg2": "value4"}),
},
},
],
}
_tool_use_message_1_bad_json = {
"role": "assistant",
"content": None,
"function_call": None,
"tool_calls": [
{
"id": "1",
"type": "function",
"function": {
"name": "_tool_func_1",
# add extra comma to make json invalid
"arguments": json.dumps({"arg1": "value3", "arg2": "value4"})[:-1] + ",}",
},
},
{
"id": "2",
"type": "function",
"function": {
"name": "_tool_func_2",
"arguments": json.dumps({"arg1": "value3", "arg2": "value4"}),
},
},
],
}
_tool_use_message_1_expected_reply = {
"role": "tool",
"tool_responses": [
{"tool_call_id": "1", "role": "tool", "content": "_tool_func_1: value1 value2"},
{"tool_call_id": "2", "role": "tool", "content": "_tool_func_2: value3 value4"},
],
# "content": "Tool Call Id: 1\n_tool_func_1: value1 value2\n\nTool Call Id: 2\n_tool_func_2: value3 value4",
"content": "_tool_func_1: value1 value2\n\n_tool_func_2: value3 value4",
}
_tool_use_message_1_bad_json_expected_reply = {
"role": "tool",
"tool_responses": [
{
"tool_call_id": "1",
"role": "tool",
"content": "Error: Expecting property name enclosed in double quotes: line 1 column 37 (char 36)\n The argument must be in JSON format.",
},
{"tool_call_id": "2", "role": "tool", "content": "_tool_func_2: value3 value4"},
],
"content": "Error: Expecting property name enclosed in double quotes: line 1 column 37 (char 36)\n The argument must be in JSON format.\n\n_tool_func_2: value3 value4",
}
_tool_use_message_1_error_expected_reply = {
"role": "tool",
"tool_responses": [
{"tool_call_id": "1", "role": "tool", "content": "_tool_func_1: value1 value2"},
{
"tool_call_id": "2",
"role": "tool",
"content": "Error: Error in tool function",
},
],
"content": "_tool_func_1: value1 value2\n\nError: Error in tool function",
}
_tool_use_message_1_not_found_expected_reply = {
"role": "tool",
"tool_responses": [
{"tool_call_id": "1", "role": "tool", "content": "_tool_func_1: value1 value2"},
{
"tool_call_id": "2",
"role": "tool",
"content": "Error: Function _tool_func_2 not found.",
},
],
"content": "_tool_func_1: value1 value2\n\nError: Function _tool_func_2 not found.",
}
_function_use_message_1 = {
"role": "assistant",
"content": None,
"function_call": {
"name": "_tool_func_1",
"arguments": json.dumps({"arg1": "value1", "arg2": "value2"}),
},
}
_function_use_message_1_bad_json = {
"role": "assistant",
"content": None,
"function_call": {
"name": "_tool_func_1",
"arguments": json.dumps({"arg1": "value1", "arg2": "value2"})[:-1] + ",}",
},
}
_function_use_message_1_expected_reply = {
"name": "_tool_func_1",
"role": "function",
"content": "_tool_func_1: value1 value2",
}
_function_use_message_1_bad_json_expected_reply = {
"name": "_tool_func_1",
"role": "function",
"content": "Error: Expecting property name enclosed in double quotes: line 1 column 37 (char 36)\n The argument must be in JSON format.",
}
_function_use_message_1_error_expected_reply = {
"name": "_tool_func_1",
"role": "function",
"content": "Error: Error in tool function",
}
_function_use_message_1_not_found_expected_reply = {
"name": "_tool_func_1",
"role": "function",
"content": "Error: Function _tool_func_1 not found.",
}
_text_message = {"content": "Hi!", "role": "user"}
def _get_function_map(is_function_async: bool, drop_tool_2: bool = False) -> Dict[str, Callable[..., Any]]:
if is_function_async:
return (
{
"_tool_func_1": _a_tool_func_1,
"_tool_func_2": _a_tool_func_2,
}
if not drop_tool_2
else {
"_tool_func_1": _a_tool_func_1,
}
)
else:
return (
{
"_tool_func_1": _tool_func_1,
"_tool_func_2": _tool_func_2,
}
if not drop_tool_2
else {
"_tool_func_1": _tool_func_1,
}
)
def _get_error_function_map(
is_function_async: bool, error_on_tool_func_2: bool = True
) -> Dict[str, Callable[..., Any]]:
if is_function_async:
return {
"_tool_func_1": _a_tool_func_1 if error_on_tool_func_2 else _a_tool_func_error,
"_tool_func_2": _a_tool_func_error if error_on_tool_func_2 else _a_tool_func_2,
}
else:
return {
"_tool_func_1": _tool_func_1 if error_on_tool_func_2 else _tool_func_error,
"_tool_func_2": _tool_func_error if error_on_tool_func_2 else _tool_func_2,
}
@pytest.mark.parametrize("is_function_async", [True, False])
def test_generate_function_call_reply_on_function_call_message(is_function_async: bool) -> None:
agent = ConversableAgent(name="agent", llm_config=False)
# empty function_map
agent._function_map = {}
messages = [_function_use_message_1]
finished, retval = agent.generate_function_call_reply(messages)
assert (finished, retval) == (True, _function_use_message_1_not_found_expected_reply)
# function map set
agent._function_map = _get_function_map(is_function_async)
# correct function call, multiple times to make sure cleanups are done properly
for _ in range(3):
messages = [_function_use_message_1]
finished, retval = agent.generate_function_call_reply(messages)
assert (finished, retval) == (True, _function_use_message_1_expected_reply)
# bad JSON
messages = [_function_use_message_1_bad_json]
finished, retval = agent.generate_function_call_reply(messages)
assert (finished, retval) == (True, _function_use_message_1_bad_json_expected_reply)
# tool call
messages = [_tool_use_message_1]
finished, retval = agent.generate_function_call_reply(messages)
assert (finished, retval) == (False, None)
# text message
messages: List[Dict[str, str]] = [_text_message]
finished, retval = agent.generate_function_call_reply(messages)
assert (finished, retval) == (False, None)
# error in function (raises Exception)
agent._function_map = _get_error_function_map(is_function_async, error_on_tool_func_2=False)
messages = [_function_use_message_1]
finished, retval = agent.generate_function_call_reply(messages)
assert (finished, retval) == (True, _function_use_message_1_error_expected_reply)
@pytest.mark.asyncio()
@pytest.mark.parametrize("is_function_async", [True, False])
async def test_a_generate_function_call_reply_on_function_call_message(is_function_async: bool) -> None:
agent = ConversableAgent(name="agent", llm_config=False)
# empty function_map
agent._function_map = {}
messages = [_function_use_message_1]
finished, retval = await agent.a_generate_function_call_reply(messages)
assert (finished, retval) == (True, _function_use_message_1_not_found_expected_reply)
# function map set
agent._function_map = _get_function_map(is_function_async)
# correct function call, multiple times to make sure cleanups are done properly
for _ in range(3):
messages = [_function_use_message_1]
finished, retval = await agent.a_generate_function_call_reply(messages)
assert (finished, retval) == (True, _function_use_message_1_expected_reply)
# bad JSON
messages = [_function_use_message_1_bad_json]
finished, retval = await agent.a_generate_function_call_reply(messages)
assert (finished, retval) == (True, _function_use_message_1_bad_json_expected_reply)
# tool call
messages = [_tool_use_message_1]
finished, retval = await agent.a_generate_function_call_reply(messages)
assert (finished, retval) == (False, None)
# text message
messages: List[Dict[str, str]] = [_text_message]
finished, retval = await agent.a_generate_function_call_reply(messages)
assert (finished, retval) == (False, None)
# error in function (raises Exception)
agent._function_map = _get_error_function_map(is_function_async, error_on_tool_func_2=False)
messages = [_function_use_message_1]
finished, retval = await agent.a_generate_function_call_reply(messages)
assert (finished, retval) == (True, _function_use_message_1_error_expected_reply)
@pytest.mark.parametrize("is_function_async", [True, False])
def test_generate_tool_calls_reply_on_function_call_message(is_function_async: bool) -> None:
agent = ConversableAgent(name="agent", llm_config=False)
# empty function_map
agent._function_map = _get_function_map(is_function_async, drop_tool_2=True)
messages = [_tool_use_message_1]
finished, retval = agent.generate_tool_calls_reply(messages)
assert (finished, retval) == (True, _tool_use_message_1_not_found_expected_reply)
# function map set
agent._function_map = _get_function_map(is_function_async)
# correct function call, multiple times to make sure cleanups are done properly
for _ in range(3):
messages = [_tool_use_message_1]
finished, retval = agent.generate_tool_calls_reply(messages)
assert (finished, retval) == (True, _tool_use_message_1_expected_reply)
# bad JSON
messages = [_tool_use_message_1_bad_json]
finished, retval = agent.generate_tool_calls_reply(messages)
assert (finished, retval) == (True, _tool_use_message_1_bad_json_expected_reply)
# function call
messages = [_function_use_message_1]
finished, retval = agent.generate_tool_calls_reply(messages)
assert (finished, retval) == (False, None)
# text message
messages: List[Dict[str, str]] = [_text_message]
finished, retval = agent.generate_tool_calls_reply(messages)
assert (finished, retval) == (False, None)
# error in function (raises Exception)
agent._function_map = _get_error_function_map(is_function_async)
messages = [_tool_use_message_1]
finished, retval = agent.generate_tool_calls_reply(messages)
assert (finished, retval) == (True, _tool_use_message_1_error_expected_reply)
@pytest.mark.asyncio()
@pytest.mark.parametrize("is_function_async", [True, False])
async def test_a_generate_tool_calls_reply_on_function_call_message(is_function_async: bool) -> None:
agent = ConversableAgent(name="agent", llm_config=False)
# empty function_map
agent._function_map = _get_function_map(is_function_async, drop_tool_2=True)
messages = [_tool_use_message_1]
finished, retval = await agent.a_generate_tool_calls_reply(messages)
assert (finished, retval) == (True, _tool_use_message_1_not_found_expected_reply)
# function map set
agent._function_map = _get_function_map(is_function_async)
# correct function call, multiple times to make sure cleanups are done properly
for _ in range(3):
messages = [_tool_use_message_1]
finished, retval = await agent.a_generate_tool_calls_reply(messages)
assert (finished, retval) == (True, _tool_use_message_1_expected_reply)
# bad JSON
messages = [_tool_use_message_1_bad_json]
finished, retval = await agent.a_generate_tool_calls_reply(messages)
assert (finished, retval) == (True, _tool_use_message_1_bad_json_expected_reply)
# function call
messages = [_function_use_message_1]
finished, retval = await agent.a_generate_tool_calls_reply(messages)
assert (finished, retval) == (False, None)
# text message
messages: List[Dict[str, str]] = [_text_message]
finished, retval = await agent.a_generate_tool_calls_reply(messages)
assert (finished, retval) == (False, None)
# error in function (raises Exception)
agent._function_map = _get_error_function_map(is_function_async)
messages = [_tool_use_message_1]
finished, retval = await agent.a_generate_tool_calls_reply(messages)
assert (finished, retval) == (True, _tool_use_message_1_error_expected_reply)