-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
test_batch.py
346 lines (297 loc) · 10.3 KB
/
test_batch.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
import asyncio
import time
from typing import Optional
import pytest
from langroid import ChatDocument
from langroid.agent.batch import (
llm_response_batch,
run_batch_agent_method,
run_batch_task_gen,
run_batch_tasks,
)
from langroid.agent.chat_agent import ChatAgent, ChatAgentConfig
from langroid.agent.task import Task
from langroid.agent.tool_message import ToolMessage
from langroid.agent.tools.orchestration import DoneTool
from langroid.language_models.mock_lm import MockLMConfig
from langroid.language_models.openai_gpt import OpenAIGPTConfig
from langroid.mytypes import Entity
from langroid.utils.configuration import Settings, set_global
from langroid.utils.constants import DONE
from langroid.vector_store.base import VectorStoreConfig
def process_int(x: str) -> str:
if int(x) == 0:
return str(int(x) + 1)
else:
time.sleep(2)
return str(int(x) + 1)
class _TestChatAgentConfig(ChatAgentConfig):
vecdb: VectorStoreConfig = None
llm = MockLMConfig(response_fn=lambda x: process_int(x))
@pytest.mark.parametrize("batch_size", [1, 2, 3, None])
@pytest.mark.parametrize("sequential", [True, False])
@pytest.mark.parametrize("stop_on_first", [True, False])
def test_task_batch(
test_settings: Settings,
sequential: bool,
batch_size: Optional[int],
stop_on_first: bool,
):
set_global(test_settings)
cfg = _TestChatAgentConfig()
agent = ChatAgent(cfg)
task = Task(
agent,
name="Test",
interactive=False,
done_if_response=[Entity.LLM],
done_if_no_response=[Entity.LLM],
)
# run clones of this task on these inputs
N = 3
questions = list(range(N))
expected_answers = [(i + 1) for i in range(N)]
# batch run
answers = run_batch_tasks(
task,
questions,
input_map=lambda x: str(x), # what to feed to each task
output_map=lambda x: x, # how to process the result of each task
sequential=sequential,
batch_size=batch_size,
stop_on_first_result=stop_on_first,
)
if stop_on_first:
# only the task with input 0 succeeds since it's fastest
non_null_answer = [a for a in answers if a is not None][0]
assert non_null_answer is not None
assert non_null_answer.content == str(expected_answers[0])
else:
for e in expected_answers:
assert any(str(e) in a.content.lower() for a in answers)
@pytest.mark.parametrize("batch_size", [1, 2, 3, None])
@pytest.mark.parametrize("sequential", [True, False])
@pytest.mark.parametrize("use_done_tool", [True, False])
def test_task_batch_turns(
test_settings: Settings,
sequential: bool,
batch_size: Optional[int],
use_done_tool: bool,
):
"""Test if `turns`, `max_cost`, `max_tokens` params work as expected.
The latter two are not really tested (since we need to turn off caching etc)
we just make sure they don't break anything.
"""
set_global(test_settings)
cfg = _TestChatAgentConfig()
class _TestChatAgent(ChatAgent):
def handle_message_fallback(
self, msg: str | ChatDocument
) -> str | DoneTool | None:
if isinstance(msg, ChatDocument) and msg.metadata.sender == Entity.LLM:
return (
DoneTool(content=str(msg.content))
if use_done_tool
else DONE + " " + str(msg.content)
)
agent = _TestChatAgent(cfg)
agent.llm.reset_usage_cost()
task = Task(
agent,
name="Test",
interactive=False,
)
# run clones of this task on these inputs
N = 3
questions = list(range(N))
expected_answers = [(i + 1) for i in range(N)]
# batch run
answers = run_batch_tasks(
task,
questions,
input_map=lambda x: str(x), # what to feed to each task
output_map=lambda x: x, # how to process the result of each task
sequential=sequential,
batch_size=batch_size,
turns=2,
max_cost=0.005,
max_tokens=100,
)
# expected_answers are simple numbers, but
# actual answers may be more wordy like "sum of 1 and 3 is 4",
# so we just check if the expected answer is contained in the actual answer
for e in expected_answers:
assert any(str(e) in a.content.lower() for a in answers)
@pytest.mark.parametrize("sequential", [True, False])
@pytest.mark.parametrize("stop_on_first", [True, False])
def test_agent_llm_response_batch(
test_settings: Settings,
sequential: bool,
stop_on_first: bool,
):
set_global(test_settings)
cfg = _TestChatAgentConfig()
agent = ChatAgent(cfg)
# get llm_response_async result on clones of this agent, on these inputs:
N = 3
questions = list(range(N))
expected_answers = [(i + 1) for i in range(N)]
# batch run
answers = run_batch_agent_method(
agent,
agent.llm_response_async,
questions,
input_map=lambda x: str(x), # what to feed to each task
output_map=lambda x: x, # how to process the result of each task
sequential=sequential,
stop_on_first_result=stop_on_first,
)
if stop_on_first:
# only the task with input 0 succeeds since it's fastest
non_null_answer = [a for a in answers if a is not None][0]
assert non_null_answer is not None
assert non_null_answer.content == str(expected_answers[0])
else:
for e in expected_answers:
assert any(str(e) in a.content.lower() for a in answers)
answers = llm_response_batch(
agent,
questions,
input_map=lambda x: str(x), # what to feed to each task
output_map=lambda x: x, # how to process the result of each task
sequential=sequential,
stop_on_first_result=stop_on_first,
)
if stop_on_first:
# only the task with input 0 succeeds since it's fastest
non_null_answer = [a for a in answers if a is not None][0]
assert non_null_answer is not None
assert non_null_answer.content == str(expected_answers[0])
else:
for e in expected_answers:
assert any(str(e) in a.content.lower() for a in answers)
@pytest.mark.parametrize("stop_on_first", [True, False])
@pytest.mark.parametrize("batch_size", [1, 2, 3, None])
@pytest.mark.parametrize("sequential", [True, False])
def test_task_gen_batch(
test_settings: Settings,
sequential: bool,
stop_on_first: bool,
batch_size: Optional[int],
):
set_global(test_settings)
def task_gen(i: int) -> Task:
async def response_fn_async(x):
match i:
case 0:
await asyncio.sleep(0.1)
return str(x)
case 1:
return "hmm"
case _:
await asyncio.sleep(0.2)
return str(2 * int(x))
class _TestChatAgentConfig(ChatAgentConfig):
vecdb: VectorStoreConfig = None
llm = MockLMConfig(response_fn_async=response_fn_async)
cfg = _TestChatAgentConfig()
return Task(
ChatAgent(cfg),
name=f"Test-{i}",
single_round=True,
)
# run the generated tasks on these inputs
questions = list(range(3))
expected_answers = ["0", "hmm", "4"]
# batch run
answers = run_batch_task_gen(
task_gen,
questions,
sequential=sequential,
stop_on_first_result=stop_on_first,
batch_size=batch_size,
)
if stop_on_first:
non_null_answer = [a for a in answers if a is not None][0].content
# Unless the first task is scheduled alone,
# the second task should always finish first
if batch_size == 1:
assert "0" in non_null_answer
else:
assert "hmm" in non_null_answer
else:
for answer, expected in zip(answers, expected_answers):
assert answer is not None
assert expected in answer.content.lower()
@pytest.mark.parametrize("batch_size", [None, 1, 2, 3])
@pytest.mark.parametrize("handle_exceptions", [False, True])
@pytest.mark.parametrize("sequential", [True, False])
@pytest.mark.parametrize("fn_api", [False, True])
@pytest.mark.parametrize("tools_api", [False, True])
@pytest.mark.parametrize("use_done_tool", [True, False])
def test_task_gen_batch_exceptions(
test_settings: Settings,
fn_api: bool,
tools_api: bool,
use_done_tool: bool,
sequential: bool,
handle_exceptions: bool,
batch_size: Optional[int],
):
set_global(test_settings)
class ComputeTool(ToolMessage):
request: str = "compute"
purpose: str = "To compute an unknown function of the input"
input: int
system_message = """
You will make a call with the `compute` tool/function with
`input` the value I provide.
"""
def task_gen(i: int) -> Task:
cfg = ChatAgentConfig(
vecdb=None,
llm=OpenAIGPTConfig(),
use_functions_api=fn_api,
use_tools=not fn_api,
use_tools_api=tools_api,
)
agent = ChatAgent(cfg)
agent.enable_message(ComputeTool)
if use_done_tool:
agent.enable_message(DoneTool)
task = Task(
agent,
name=f"Test-{i}",
system_message=system_message,
interactive=False,
)
def handle(m: ComputeTool) -> str | DoneTool:
if i != 1:
return (
DoneTool(content="success") if use_done_tool else f"{DONE} success"
)
else:
raise RuntimeError("disaster")
setattr(agent, "compute", handle)
return task
# run the generated tasks on these inputs
questions = list(range(3))
# batch run
try:
answers = run_batch_task_gen(
task_gen,
questions,
sequential=sequential,
handle_exceptions=handle_exceptions,
batch_size=batch_size,
)
error_encountered = False
for i in [0, 2]:
a = answers[i]
assert a is not None
assert "success" in a.content.lower()
assert answers[1] is None
except RuntimeError as e:
error_encountered = True
assert "disaster" in str(e)
assert error_encountered != handle_exceptions