cli.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import argparse
  2. import asyncio
  3. import logging
  4. from typing import Type
  5. from termcolor import colored
  6. import agenthub # noqa F401 (we import this to get the agents registered)
  7. from openhands import __version__
  8. from openhands.controller import AgentController
  9. from openhands.controller.agent import Agent
  10. from openhands.core.config import (
  11. load_app_config,
  12. )
  13. from openhands.core.logger import openhands_logger as logger
  14. from openhands.core.schema import AgentState
  15. from openhands.events import EventSource, EventStream, EventStreamSubscriber
  16. from openhands.events.action import (
  17. Action,
  18. ChangeAgentStateAction,
  19. CmdRunAction,
  20. MessageAction,
  21. )
  22. from openhands.events.event import Event
  23. from openhands.events.observation import (
  24. AgentStateChangedObservation,
  25. CmdOutputObservation,
  26. )
  27. from openhands.llm.llm import LLM
  28. from openhands.runtime import get_runtime_cls
  29. from openhands.runtime.runtime import Runtime
  30. from openhands.storage import get_file_store
  31. def display_message(message: str):
  32. print(colored('🤖 ' + message + '\n', 'yellow'))
  33. def display_command(command: str):
  34. print('❯ ' + colored(command + '\n', 'green'))
  35. def display_command_output(output: str):
  36. lines = output.split('\n')
  37. for line in lines:
  38. if line.startswith('[Python Interpreter') or line.startswith('openhands@'):
  39. # TODO: clean this up once we clean up terminal output
  40. continue
  41. print(colored(line, 'blue'))
  42. print('\n')
  43. def display_event(event: Event):
  44. if isinstance(event, Action):
  45. if hasattr(event, 'thought'):
  46. display_message(event.thought)
  47. if isinstance(event, MessageAction):
  48. if event.source != EventSource.USER:
  49. display_message(event.content)
  50. if isinstance(event, CmdRunAction):
  51. display_command(event.command)
  52. if isinstance(event, CmdOutputObservation):
  53. display_command_output(event.content)
  54. def get_parser() -> argparse.ArgumentParser:
  55. """Get the parser for the command line arguments."""
  56. parser = argparse.ArgumentParser(description='Run an agent with a specific task')
  57. # Add the version argument
  58. parser.add_argument(
  59. '-v',
  60. '--version',
  61. action='version',
  62. version=f'{__version__}',
  63. help='Show the version number and exit',
  64. default=None,
  65. )
  66. return parser
  67. async def main():
  68. """Runs the agent in CLI mode"""
  69. parser = get_parser()
  70. args = parser.parse_args()
  71. if args.version:
  72. print(f'OpenHands version: {__version__}')
  73. return
  74. logger.setLevel(logging.WARNING)
  75. config = load_app_config(config_file=args.config_file)
  76. sid = 'cli'
  77. agent_cls: Type[Agent] = Agent.get_cls(config.default_agent)
  78. agent_config = config.get_agent_config(config.default_agent)
  79. llm_config = config.get_llm_config_from_agent(config.default_agent)
  80. agent = agent_cls(
  81. llm=LLM(config=llm_config),
  82. config=agent_config,
  83. )
  84. file_store = get_file_store(config.file_store, config.file_store_path)
  85. event_stream = EventStream(sid, file_store)
  86. runtime_cls = get_runtime_cls(config.runtime)
  87. runtime: Runtime = runtime_cls( # noqa: F841
  88. config=config,
  89. event_stream=event_stream,
  90. sid=sid,
  91. plugins=agent_cls.sandbox_plugins,
  92. )
  93. controller = AgentController(
  94. agent=agent,
  95. max_iterations=config.max_iterations,
  96. max_budget_per_task=config.max_budget_per_task,
  97. agent_to_llm_config=config.get_agent_to_llm_config_map(),
  98. event_stream=event_stream,
  99. )
  100. if controller is not None:
  101. controller.agent_task = asyncio.create_task(controller.start_step_loop())
  102. async def prompt_for_next_task():
  103. next_message = input('How can I help? >> ')
  104. if next_message == 'exit':
  105. event_stream.add_event(
  106. ChangeAgentStateAction(AgentState.STOPPED), EventSource.USER
  107. )
  108. return
  109. action = MessageAction(content=next_message)
  110. event_stream.add_event(action, EventSource.USER)
  111. async def on_event(event: Event):
  112. display_event(event)
  113. if isinstance(event, AgentStateChangedObservation):
  114. if event.agent_state == AgentState.ERROR:
  115. print('An error occurred. Please try again.')
  116. if event.agent_state in [
  117. AgentState.AWAITING_USER_INPUT,
  118. AgentState.FINISHED,
  119. AgentState.ERROR,
  120. ]:
  121. await prompt_for_next_task()
  122. event_stream.subscribe(EventStreamSubscriber.MAIN, on_event)
  123. await prompt_for_next_task()
  124. while controller.state.agent_state not in [
  125. AgentState.STOPPED,
  126. ]:
  127. await asyncio.sleep(1) # Give back control for a tick, so the agent can run
  128. print('Exiting...')
  129. await controller.close()
  130. if __name__ == '__main__':
  131. loop = asyncio.get_event_loop()
  132. try:
  133. loop.run_until_complete(main())
  134. finally:
  135. pass