agent_repl.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import argparse
  2. from typing import List, Tuple
  3. from fastapi import Depends
  4. from aios.hooks.llm import useFactory, useKernel, useFIFOScheduler
  5. from aios.utils.utils import delete_directories, humanify_agent, parse_global_args
  6. from aios.utils.state import useGlobalState
  7. from pyopenagi.agents.interact import Interactor
  8. from pyopenagi.manager.manager import AgentManager
  9. from dotenv import load_dotenv
  10. import warnings
  11. # load the args
  12. warnings.filterwarnings("ignore")
  13. parser = parse_global_args()
  14. parser.add_argument("--no-color")
  15. parser.add_argument("--manager-url", default="https://my.aios.foundation")
  16. args = parser.parse_args()
  17. load_dotenv()
  18. # start the kernel
  19. llm = useKernel(
  20. llm_name=args.llm_name,
  21. max_gpu_memory=args.max_gpu_memory,
  22. eval_device=args.eval_device,
  23. max_new_tokens=args.max_new_tokens,
  24. log_mode=args.log_mode,
  25. use_backend=args.use_backend,
  26. )
  27. # boilerplate from "launch.py"
  28. # provides an interface for setting state variables
  29. startScheduler, stopScheduler = useFIFOScheduler(
  30. llm=llm, log_mode=args.log_mode, get_queue_message=None
  31. )
  32. submitAgent, awaitAgentExecution = useFactory(
  33. log_mode=args.log_mode, max_workers=500
  34. )
  35. startScheduler()
  36. getLLMState, setLLMState, setLLMCallback = useGlobalState()
  37. getFactory, setFactory, setFactoryCallback = useGlobalState()
  38. getManager, setManager, setManagerCallback = useGlobalState()
  39. setFactory({"submit": submitAgent, "execute": awaitAgentExecution})
  40. setManager(AgentManager(args.manager_url))
  41. WHITE='\033[1m'
  42. GREEN = '\033[32m'
  43. BLUE = '\033[34m'
  44. RED = '\033[31m'
  45. YELLOW = '\033[33m'
  46. RESET = '\033[0m'
  47. def clean_cache(root_directory):
  48. targets = {
  49. ".ipynb_checkpoints",
  50. "__pycache__",
  51. ".pytest_cache",
  52. "context_restoration",
  53. }
  54. delete_directories(root_directory, targets)
  55. def get_all_agents() -> List[str]:
  56. manager: AgentManager = getManager()
  57. agents = manager.list_available_agents()
  58. return [agent["agent"] for agent in agents]
  59. def display_agents(agents: List[str]):
  60. print("Available Agents:")
  61. for i, (name) in enumerate(agents, 1):
  62. print(f"{i}. {name}")
  63. def get_user_choice(agents: List[Tuple[str, str]]) -> Tuple[str, str]:
  64. while True:
  65. try:
  66. choice = int(input("Enter the number of the agent you want to use: "))
  67. if 1 <= choice <= len(agents):
  68. return agents[choice - 1]
  69. else:
  70. print("Invalid choice. Please try again.")
  71. except ValueError:
  72. print("Please enter a valid number.")
  73. def get_user_task(chosen_agent) -> str:
  74. return input(f"{BLUE}{chosen_agent}:{RESET} ")
  75. def add_agent(
  76. agent_name: str,
  77. task_input: str,
  78. ):
  79. try:
  80. submit_agent = getFactory()["submit"]
  81. process_id = submit_agent(agent_name=agent_name, task_input=task_input)
  82. return {"success": True, "agent": agent_name, "pid": process_id}
  83. except Exception as e:
  84. return {"success": False, "exception": f"{e}"}
  85. def execute_agent(
  86. pid: int,
  87. ):
  88. print(getFactory()["execute"])
  89. response = getFactory()["execute"](pid)
  90. print(response)
  91. return {"success": True, "response": response}
  92. print(
  93. """
  94. \033c
  95. Welcome to the Agent REPL.
  96. Please select an agent by pressing tab.
  97. To exit, press Ctrl+C.
  98. """
  99. )
  100. # check for getch
  101. getch = None
  102. try:
  103. from getch import getch
  104. except ImportError:
  105. print(
  106. """
  107. The terminal will NOT look pretty without getch. Please install it.
  108. pip install getch
  109. """
  110. )
  111. getch = input
  112. # shell loop
  113. try:
  114. while True:
  115. chosen_agent = ""
  116. agents = get_all_agents()
  117. # shell prompt
  118. print(f"{WHITE}[{args.llm_name}]>{RESET} ", end="")
  119. # check if the user put in a tab
  120. if getch() != "\t":
  121. continue
  122. try:
  123. from pyfzf.pyfzf import FzfPrompt
  124. fzf = FzfPrompt()
  125. selected = fzf.prompt(agents)
  126. if len(selected) == 0:
  127. print("No agent selected. Please try again.")
  128. continue
  129. chosen_agent = selected[0]
  130. except ImportError:
  131. print("pyfzf is not installed. Falling back to default reader.")
  132. display_agents(list(agents.keys()))
  133. chosen_agent, _ = "example/" + get_user_choice(agents)
  134. task = get_user_task(chosen_agent)
  135. try:
  136. agent_payload = add_agent(
  137. agent_name=chosen_agent,
  138. task_input=task,
  139. )
  140. if not agent_payload["success"]:
  141. print(f"An error occurred: {agent_payload['exception']}")
  142. continue
  143. agent_id = agent_payload["pid"]
  144. print(f"Agent {chosen_agent} started with PID {agent_id}.")
  145. agent_response = execute_agent(agent_id)
  146. if not agent_response["success"]:
  147. print(f"An error occurred: {agent_response['exception']}")
  148. continue
  149. print(agent_response["response"])
  150. except Exception as e:
  151. print(f"An error occurred: {str(e)}")
  152. except KeyboardInterrupt:
  153. stopScheduler()
  154. clean_cache(root_directory="./")