main.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import asyncio
  2. import sys
  3. from typing import Type
  4. import agenthub # noqa F401 (we import this to get the agents registered)
  5. from opendevin.config import args
  6. from opendevin.agent import Agent
  7. from opendevin.controller import AgentController
  8. from opendevin.llm.llm import LLM
  9. def read_task_from_file(file_path: str) -> str:
  10. """Read task from the specified file."""
  11. with open(file_path, 'r', encoding='utf-8') as file:
  12. return file.read()
  13. def read_task_from_stdin() -> str:
  14. """Read task from stdin."""
  15. return sys.stdin.read()
  16. async def main(task_str: str = ''):
  17. """Main coroutine to run the agent controller with task input flexibility."""
  18. # Determine the task source
  19. if task_str:
  20. task = task_str
  21. elif args.file:
  22. task = read_task_from_file(args.file)
  23. elif args.task:
  24. task = args.task
  25. elif not sys.stdin.isatty():
  26. task = read_task_from_stdin()
  27. else:
  28. raise ValueError(
  29. 'No task provided. Please specify a task through -t, -f.')
  30. print(
  31. f'Running agent {args.agent_cls} (model: {args.model_name}) with task: "{task}"'
  32. )
  33. llm = LLM(args.model_name)
  34. AgentCls: Type[Agent] = Agent.get_cls(args.agent_cls)
  35. agent = AgentCls(llm=llm)
  36. controller = AgentController(
  37. agent=agent, max_iterations=args.max_iterations, max_chars=args.max_chars
  38. )
  39. await controller.start(task)
  40. if __name__ == '__main__':
  41. asyncio.run(main())