main.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import asyncio
  2. import argparse
  3. import sys
  4. from typing import Type
  5. import agenthub # noqa F401 (we import this to get the agents registered)
  6. from opendevin import config
  7. from opendevin.schema import ConfigType
  8. from opendevin.agent import Agent
  9. from opendevin.controller import AgentController
  10. from opendevin.llm.llm import LLM
  11. def read_task_from_file(file_path: str) -> str:
  12. """Read task from the specified file."""
  13. with open(file_path, 'r', encoding='utf-8') as file:
  14. return file.read()
  15. def read_task_from_stdin() -> str:
  16. """Read task from stdin."""
  17. return sys.stdin.read()
  18. def parse_arguments():
  19. """Parse command-line arguments."""
  20. parser = argparse.ArgumentParser(
  21. description='Run an agent with a specific task')
  22. parser.add_argument(
  23. '-d',
  24. '--directory',
  25. type=str,
  26. help='The working directory for the agent',
  27. )
  28. parser.add_argument(
  29. '-t', '--task', type=str, default='', help='The task for the agent to perform'
  30. )
  31. parser.add_argument(
  32. '-f',
  33. '--file',
  34. type=str,
  35. help='Path to a file containing the task. Overrides -t if both are provided.',
  36. )
  37. parser.add_argument(
  38. '-c',
  39. '--agent-cls',
  40. default='MonologueAgent',
  41. type=str,
  42. help='The agent class to use',
  43. )
  44. parser.add_argument(
  45. '-m',
  46. '--model-name',
  47. default=config.get(ConfigType.LLM_MODEL),
  48. type=str,
  49. help='The (litellm) model name to use',
  50. )
  51. parser.add_argument(
  52. '-i',
  53. '--max-iterations',
  54. default=config.get(ConfigType.MAX_ITERATIONS),
  55. type=int,
  56. help='The maximum number of iterations to run the agent',
  57. )
  58. parser.add_argument(
  59. '-n',
  60. '--max-chars',
  61. default=config.get(ConfigType.MAX_CHARS),
  62. type=int,
  63. help='The maximum number of characters to send to and receive from LLM per task',
  64. )
  65. args, _ = parser.parse_known_args()
  66. return args
  67. async def main():
  68. """Main coroutine to run the agent controller with task input flexibility."""
  69. args = parse_arguments()
  70. # Determine the task source
  71. if args.file:
  72. task = read_task_from_file(args.file)
  73. elif args.task:
  74. task = args.task
  75. elif not sys.stdin.isatty():
  76. task = read_task_from_stdin()
  77. else:
  78. raise ValueError(
  79. 'No task provided. Please specify a task through -t, -f.')
  80. print(
  81. f'Running agent {args.agent_cls} (model: {args.model_name}, directory: {args.directory}) with task: "{task}"'
  82. )
  83. llm = LLM(args.model_name)
  84. AgentCls: Type[Agent] = Agent.get_cls(args.agent_cls)
  85. agent = AgentCls(llm=llm)
  86. controller = AgentController(
  87. agent=agent, max_iterations=args.max_iterations, max_chars=args.max_chars
  88. )
  89. await controller.start(task)
  90. if __name__ == '__main__':
  91. asyncio.run(main())