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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575 | class InstructReActAgentWorker(ReActAgentWorker):
def __init__(
self,
tools: Sequence[BaseTool],
llm: LLM,
user_id_list: List[str],
chat_group_id_list: List[str],
max_iterations: int = 10,
react_chat_formatter: Optional[ReActChatFormatter] = None,
output_parser: Optional[ReActOutputParser] = None,
callback_manager: Optional[CallbackManager] = None,
verbose: bool = False,
tool_retriever: Optional[ObjectRetriever[BaseTool]] = None,
handle_reasoning_failure_fn: Optional[Callable[[CallbackManager, Exception], ToolOutput]] = None,
enable_instruct: bool = False,
):
self._enable_instruct = enable_instruct
self._instruct_chat_formatter = InstructChatFormatter()
self.user_id_list = user_id_list
self.chat_group_id_list = chat_group_id_list
super().__init__(
tools=tools,
llm=llm,
max_iterations=max_iterations,
react_chat_formatter=react_chat_formatter,
output_parser=output_parser,
callback_manager=callback_manager,
verbose=verbose,
tool_retriever=tool_retriever,
handle_reasoning_failure_fn=handle_reasoning_failure_fn,
)
def set_enable_instruct(self, enable: bool):
self._enable_instruct = enable
@property
def enable_instruct(self):
r""" Enable user's instruction in reasoning phase. """
return self._enable_instruct
@classmethod
def from_tools(
cls,
tools: Optional[Sequence[BaseTool]] = None,
tool_retriever: Optional[ObjectRetriever[BaseTool]] = None,
llm: Optional[LLM] = None,
user_id_list: List[str] = None,
chat_group_id_list: List[str] = None,
max_iterations: int = 10,
react_chat_formatter: Optional[ReActChatFormatter] = None,
output_parser: Optional[ReActOutputParser] = None,
callback_manager: Optional[CallbackManager] = None,
verbose: bool = False,
handle_reasoning_failure_fn: Optional[Callable[[CallbackManager, Exception], ToolOutput]] = None,
enable_instruct: bool = False,
**kwargs: Any,
) -> "InstructReActAgentWorker":
"""
Convenience constructor method from set of BaseTools (Optional).
NOTE: kwargs should have been exhausted by this point. In other words
the various upstream components such as BaseSynthesizer (response synthesizer)
or BaseRetriever should have picked up off their respective kwargs in their
constructions.
Returns:
ReActAgentWorker
"""
llm = llm or Settings.llm
if callback_manager is not None:
llm.callback_manager = callback_manager
return cls(
tools=tools or [],
tool_retriever=tool_retriever,
user_id_list=user_id_list or AccountManager().get_users(),
chat_group_id_list=chat_group_id_list or AccountManager().get_chat_groups(),
llm=llm,
max_iterations=max_iterations,
react_chat_formatter=react_chat_formatter,
output_parser=output_parser,
callback_manager=callback_manager,
verbose=verbose,
handle_reasoning_failure_fn=handle_reasoning_failure_fn,
enable_instruct=enable_instruct,
)
def initialize_step(self, task: Task, **kwargs: Any) -> TaskStep:
"""Initialize step from task."""
sources: List[ToolOutput] = []
current_reasoning: List[BaseReasoningStep] = []
# temporary memory for new messages
new_memory = ChatMemoryBuffer.from_defaults()
# the tool log list with ToolLogs.
tool_log = []
# initialize task state
task_state = {
"sources": sources,
"current_reasoning": current_reasoning,
"new_memory": new_memory,
"tool_log": tool_log,
}
task.extra_state.update(task_state)
return TaskStep(
task_id=task.task_id,
step_id=str(uuid.uuid4()),
input=task.input,
step_state={"is_first": True, "system_msg": task.extra_state["system_msg"]},
)
def _run_step(self, step: TaskStep, task: Task, ) -> TaskStepOutput:
"""Run step."""
user_id = task.extra_state["user_id"]
if step.input is not None:
step.step_state["user_id"] = user_id
add_user_step_to_reasoning(
step,
task.extra_state["new_memory"],
task.extra_state["current_reasoning"],
verbose=self._verbose,
)
tools = self.get_tools(task.input)
input_chat = self._react_chat_formatter.format(
tools,
chat_history=task.memory.get(input=task.input) + task.extra_state["new_memory"].get_all(),
current_reasoning=task.extra_state["current_reasoning"],
)
# send prompt
chat_response = self._llm.chat(input_chat)
if task.extra_state["enable_instruct"]:
# TODO: interface: Send the action to the user
print_text(f">>> Initial reasoning: \n{chat_response.message.content}", color="pink", end="\n")
# TODO: interface: Get the user's suggestion
packed_msgs = ChatBuffer.test_get_user_text(
user_id=user_id,
enable_instruct=False,
enable_comment=False,
)
user_advice = packed_msgs.user_msg
# update enable_instruct and enable_comment
update_intervene_status(
task=task,
enable_instruct=ChatBuffer.config_buffer[user_id].enable_instruct,
enable_comment=ChatBuffer.config_buffer[user_id].enable_comment,
reply_in_speech=ChatBuffer.config_buffer[user_id].reply_in_speech,
)
print_text(f">>> User's suggestion: \n{user_advice}", color="blue", end="\n")
reasoning_step = ObservationReasoningStep(observation=f"User's suggestion: {user_advice}")
task.extra_state["current_reasoning"].append(reasoning_step)
instruct_chat = self._instruct_chat_formatter.format(
tools,
chat_history=task.memory.get(input=task.input) + task.extra_state["new_memory"].get_all(),
current_reasoning=task.extra_state["current_reasoning"],
prev_response=chat_response.message.content,
suggestion=f"User's suggestion: {user_advice}",
)
chat_response = self._llm.chat(instruct_chat)
print_text(f">>> Modified reasoning: \n{chat_response.message.content}", color="green", end="\n")
# given react prompt outputs, call tools or return response
reasoning_steps, is_done = self._process_actions(task, tools, output=chat_response)
task.extra_state["current_reasoning"].extend(reasoning_steps)
agent_response = self._get_response(task.extra_state["current_reasoning"], task.extra_state["sources"])
if is_done:
date, h_m_s = get_time()
additional_kwargs = {
LOG_DATE_NAME: date,
LOG_TIME_NAME: h_m_s,
}
task.extra_state["new_memory"].put(
ChatMessage(
content=agent_response.response,
role=MessageRole.ASSISTANT,
additional_kwargs=additional_kwargs,
)
)
return self._get_task_step_response(agent_response, step, is_done)
async def _arun_step(self, step: TaskStep, task: Task, ) -> TaskStepOutput:
"""Run step."""
user_id = task.extra_state["user_id"]
if step.input is not None:
step.step_state["user_id"] = user_id
add_user_step_to_reasoning(
step,
task.extra_state["new_memory"],
task.extra_state["current_reasoning"],
verbose=self._verbose,
)
tools = self.get_tools(task.input)
input_chat = self._react_chat_formatter.format(
tools,
chat_history=task.memory.get(input=task.input) + task.extra_state["new_memory"].get_all(),
current_reasoning=task.extra_state["current_reasoning"],
)
# send prompt
chat_response = await self._llm.achat(input_chat)
if task.extra_state["enable_instruct"]:
# TODO: interface: Send the action to the user
init_reasoning = (
f"**当前Thought**:\n"
f"{chat_response.message.content}\n"
f"请您参与到我的Reasoning过程中,给予指导。我将参考您的建议对我的决策做出调整:"
)
ChatBuffer.put_agent_reply(
user_id=user_id,
reply_str=init_reasoning,
inner_chat=True,
)
# TODO: interface: Get the user's suggestion
packed_msgs = await ChatBuffer.get_user_msg(
user_id=user_id,
)
user_advice = packed_msgs.user_msg
system_msg = packed_msgs.system_msg
# Update the enable_instruct and enable_comment
update_intervene_status(
task=task,
enable_instruct=ChatBuffer.config_buffer[user_id].enable_instruct,
enable_comment=ChatBuffer.config_buffer[user_id].enable_comment,
reply_in_speech=ChatBuffer.config_buffer[user_id].reply_in_speech,
)
# Put the user's instruction into reasoning.
system_step = ObservationReasoningStep(observation=f"<system>:{system_msg}")
reasoning_step = ObservationReasoningStep(observation=f"User's suggestion: {user_advice}")
task.extra_state["current_reasoning"].extend([system_step, reasoning_step])
instruct_chat = self._instruct_chat_formatter.format(
tools,
chat_history=task.memory.get(input=task.input) + task.extra_state["new_memory"].get_all(),
current_reasoning=task.extra_state["current_reasoning"],
prev_response=chat_response.message.content,
suggestion=f"User's suggestion: {user_advice}",
)
chat_response = await self._llm.achat(instruct_chat)
# modified_reasoning = (
# f"**参考您建议后的Thought**:\n"
# f"{chat_response.message.content}\n\n"
# f"我将根据这个Thought行动。"
# )
#
# ChatBuffer.put_agent_reply(
# user_id=step.step_state["user_id"],
# reply_str=modified_reasoning,
# inner_chat=True,
# )
# given react prompt outputs, call tools or return response
reasoning_steps, is_done = await self._aprocess_actions(task, tools, output=chat_response)
task.extra_state["current_reasoning"].extend(reasoning_steps)
agent_response = self._get_response(task.extra_state["current_reasoning"], task.extra_state["sources"])
if is_done:
date, h_m_s = get_time()
additional_kwargs = {
LOG_DATE_NAME: date,
LOG_TIME_NAME: h_m_s,
}
task.extra_state["new_memory"].put(
ChatMessage(
content=agent_response.response,
role=MessageRole.ASSISTANT,
additional_kwargs=additional_kwargs,
)
)
return self._get_task_step_response(agent_response, step, is_done)
def _process_actions(
self,
task: Task,
tools: Sequence[AsyncBaseTool],
output: ChatResponse,
is_streaming: bool = False,
) -> Tuple[List[BaseReasoningStep], bool]:
tools_dict: Dict[str, AsyncBaseTool] = {tool.metadata.get_name(): tool for tool in tools}
tool = None
try:
_, current_reasoning, is_done = self._extract_reasoning_step(output, is_streaming)
except ValueError as exp:
current_reasoning = []
tool_output = self._handle_reasoning_failure_fn(self.callback_manager, exp)
else:
if is_done:
return current_reasoning, True
# call tool with input
reasoning_step = cast(ActionReasoningStep, current_reasoning[-1])
if reasoning_step.action in tools_dict:
tool = tools_dict[reasoning_step.action]
with self.callback_manager.event(
CBEventType.FUNCTION_CALL,
payload={EventPayload.FUNCTION_CALL: reasoning_step.action_input,
EventPayload.TOOL: tool.metadata,
},
) as event:
try:
tool_output = tool.call(**reasoning_step.action_input)
except Exception as e:
tool_output = ToolOutput(
content=f"Error: {e!s}",
tool_name=tool.metadata.name,
raw_input={"kwargs": reasoning_step.action_input},
raw_output=e,
is_error=True,
)
event.on_end(payload={EventPayload.FUNCTION_OUTPUT: str(tool_output)})
else:
tool_output = self._handle_nonexistent_tool_name(reasoning_step)
task.extra_state["sources"].append(tool_output)
tool_output_str, tool_log_str = unpack_tool_output(tool_out_json=tool_output.content)
if tool is not None and tool.metadata.return_direct:
observation = tool_output_str
else:
observation = f"Tool output:\n{tool_output_str}\nTool logs:\n{tool_log_str}"
# record the tool log.
if tool_log_str:
tool_log = ToolLog.loads(log_str=tool_log_str)
task.extra_state["tool_log"].append(tool_log)
task.extra_state["new_memory"].put(
ChatMessage(
content=tool_log_str,
role=MessageRole.TOOL,
)
)
observation_step = ObservationReasoningStep(
observation=observation,
return_direct=(tool.metadata.return_direct and not tool_output.is_error if tool else False),
)
current_reasoning.append(observation_step)
if self._verbose:
print_text(f"{observation_step.get_content()}\n", color="blue")
return (
current_reasoning,
tool.metadata.return_direct and not tool_output.is_error if tool else False,
)
async def _aprocess_actions(
self,
task: Task,
tools: Sequence[AsyncBaseTool],
output: ChatResponse,
is_streaming: bool = False,
) -> Tuple[List[BaseReasoningStep], bool]:
tools_dict = {tool.metadata.name: tool for tool in tools}
tool = None
try:
_, current_reasoning, is_done = self._extract_reasoning_step(output, is_streaming)
except ValueError as exp:
current_reasoning = []
tool_output = self._handle_reasoning_failure_fn(self.callback_manager, exp)
else:
if is_done:
return current_reasoning, True
# call tool with input
reasoning_step = cast(ActionReasoningStep, current_reasoning[-1])
if reasoning_step.action in tools_dict:
tool = tools_dict[reasoning_step.action]
with self.callback_manager.event(CBEventType.FUNCTION_CALL,
payload={EventPayload.FUNCTION_CALL: reasoning_step.action_input,
EventPayload.TOOL: tool.metadata, }, ) as event:
try:
tool_output = await tool.acall(**reasoning_step.action_input)
except Exception as e:
tool_output = ToolOutput(content=f"Error: {e!s}", tool_name=tool.metadata.name,
raw_input={"kwargs": reasoning_step.action_input}, raw_output=e, is_error=True, )
event.on_end(payload={EventPayload.FUNCTION_OUTPUT: str(tool_output)})
else:
tool_output = self._handle_nonexistent_tool_name(reasoning_step)
task.extra_state["sources"].append(tool_output)
tool_output_str, tool_log_str = unpack_tool_output(tool_out_json=tool_output.content)
if tool is not None and tool.metadata.return_direct:
observation = tool_output_str
else:
observation = f"Tool output:\n{tool_output_str}\nTool logs:\n{tool_log_str}"
# record the tool log.
if tool_log_str:
tool_log = ToolLog.loads(log_str=tool_log_str)
task.extra_state["tool_log"].append(tool_log)
task.extra_state["new_memory"].put(
ChatMessage(
content=tool_log_str,
role=MessageRole.TOOL,
)
)
observation_step = ObservationReasoningStep(observation=observation,
return_direct=(tool.metadata.return_direct and not tool_output.is_error if tool else False), )
current_reasoning.append(observation_step)
if self._verbose:
print_text(f"{observation_step.get_content()}\n", color="blue")
return (
current_reasoning, tool.metadata.return_direct and not tool_output.is_error if tool else False,
)
def finalize_task(self, task: Task, **kwargs: Any) -> None:
"""Finalize task, after all the steps are completed."""
user_id = task.extra_state.get("user_id", None)
chat_group_id = task.extra_state.get("chat_group_id", None)
if chat_group_id is not None:
if chat_group_id in self.chat_group_id_list:
update_chat_memory(
memory_id=user_id,
chat_messages=task.extra_state["new_memory"].get_all(),
)
else:
if self._verbose:
print_text(f"The chat group {chat_group_id} is not registered.", color="cyan", end="\n")
else:
if user_id in self.user_id_list:
update_chat_memory(
memory_id=user_id,
chat_messages=task.extra_state["new_memory"].get_all(),
)
else:
if self._verbose and user_id is not None:
print_text(f"{user_id} is not registered as a user.", color="cyan", end="\n")
# add new messages to memory
task.memory.set(task.memory.get_all() + task.extra_state["new_memory"].get_all())
# reset new memory
task.extra_state["new_memory"].reset()
|