main.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.agent import Agent
  8. from opendevin.controller import AgentController
  9. from opendevin.llm.llm import LLM
  10. def read_task_from_file(file_path: str) -> str:
  11. """Read task from the specified file."""
  12. with open(file_path, "r", encoding="utf-8") as file:
  13. return file.read()
  14. def read_task_from_stdin() -> str:
  15. """Read task from stdin."""
  16. return sys.stdin.read()
  17. def parse_arguments():
  18. """Parse command-line arguments."""
  19. parser = argparse.ArgumentParser(description="Run an agent with a specific task")
  20. parser.add_argument(
  21. "-d",
  22. "--directory",
  23. required=True,
  24. type=str,
  25. help="The working directory for the agent",
  26. )
  27. parser.add_argument(
  28. "-t", "--task", type=str, default="", help="The task for the agent to perform"
  29. )
  30. parser.add_argument(
  31. "-f",
  32. "--file",
  33. type=str,
  34. help="Path to a file containing the task. Overrides -t if both are provided.",
  35. )
  36. parser.add_argument(
  37. "-c",
  38. "--agent-cls",
  39. default="MonologueAgent",
  40. type=str,
  41. help="The agent class to use",
  42. )
  43. parser.add_argument(
  44. "-m",
  45. "--model-name",
  46. default=config.get_or_default("LLM_MODEL", "gpt-3.5-turbo-1106"),
  47. type=str,
  48. help="The (litellm) model name to use",
  49. )
  50. parser.add_argument(
  51. "-i",
  52. "--max-iterations",
  53. default=100,
  54. type=int,
  55. help="The maximum number of iterations to run the agent",
  56. )
  57. return parser.parse_args()
  58. async def main():
  59. """Main coroutine to run the agent controller with task input flexibility."""
  60. args = parse_arguments()
  61. # Determine the task source
  62. if args.file:
  63. task = read_task_from_file(args.file)
  64. elif not sys.stdin.isatty():
  65. task = read_task_from_stdin()
  66. else:
  67. task = args.task
  68. if not task:
  69. raise ValueError("No task provided. Please specify a task through -t, -f.")
  70. print(
  71. f'Running agent {args.agent_cls} (model: {args.model_name}, directory: {args.directory}) with task: "{task}"'
  72. )
  73. llm = LLM(args.model_name)
  74. AgentCls: Type[Agent] = Agent.get_cls(args.agent_cls)
  75. agent = AgentCls(llm=llm)
  76. controller = AgentController(
  77. agent=agent, workdir=args.directory, max_iterations=args.max_iterations
  78. )
  79. await controller.start_loop(task)
  80. if __name__ == "__main__":
  81. asyncio.run(main())