react_agent.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from .base_agent import BaseAgent
  2. import time
  3. from ..utils.chat_template import Query
  4. import json
  5. from aios.hooks.request import send_request
  6. class ReactAgent(BaseAgent):
  7. def __init__(self,
  8. agent_name,
  9. task_input,
  10. # agent_process_factory,
  11. log_mode: str
  12. ):
  13. BaseAgent.__init__(
  14. self,
  15. agent_name,
  16. task_input,
  17. # agent_process_factory,
  18. log_mode
  19. )
  20. self.plan_max_fail_times = 3
  21. self.tool_call_max_fail_times = 3
  22. def build_system_instruction(self):
  23. prefix = "".join(
  24. [
  25. "".join(self.config["description"])
  26. ]
  27. )
  28. plan_instruction = "".join(
  29. [
  30. f'You are given the available tools from the tool list: {json.dumps(self.tool_info)} to help you solve problems. ',
  31. 'Generate a plan with comprehensive yet minimal steps to fulfill the task. ',
  32. 'The plan must follow the json format as below: ',
  33. '[',
  34. '{"message": "message_value1","tool_use": [tool_name1, tool_name2,...]}',
  35. '{"message": "message_value2", "tool_use": [tool_name1, tool_name2,...]}',
  36. '...',
  37. ']',
  38. 'In each step of the planned plan, identify tools to use and recognize no tool is necessary. ',
  39. 'Followings are some plan examples. ',
  40. '['
  41. '[',
  42. '{"message": "gather information from arxiv. ", "tool_use": ["arxiv"]},',
  43. '{"message", "write a summarization based on the gathered information. ", "tool_use": []}',
  44. '];',
  45. '[',
  46. '{"message": "gather information from arxiv. ", "tool_use": ["arxiv"]},',
  47. '{"message", "understand the current methods and propose ideas that can improve ", "tool_use": []}',
  48. ']',
  49. ']'
  50. ]
  51. )
  52. if self.workflow_mode == "manual":
  53. self.messages.append(
  54. {"role": "system", "content": prefix}
  55. )
  56. else:
  57. assert self.workflow_mode == "automatic"
  58. self.messages.append(
  59. {"role": "system", "content": prefix}
  60. )
  61. self.messages.append(
  62. {"role": "user", "content": plan_instruction}
  63. )
  64. def automatic_workflow(self):
  65. return super().automatic_workflow()
  66. def manual_workflow(self):
  67. pass
  68. def run(self):
  69. super().run()