-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
136 lines (108 loc) · 3.52 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
"""
CLI entry point for AgentHub
Usage: python run.py --agent-type <agent_type> --tools [tool1,tool2] --input "your input text"
"""
import argparse
import asyncio
from typing import List, Optional, Type
from app.agent import *
from app.flow.base import FlowType
from app.loop import loop
from app.tool import *
def get_tool_class(tool_name: str) -> Type[BaseTool]:
"""Get tool class by name"""
# Create a mapping of tool names to tool classes
tool_classes = {
"bash": Bash,
"code_review": CodeReview,
"file_localizer": FileLocalizer,
"create_tool": CreateTool,
"file_navigator": FileNavigator,
"filemap": Filemap,
"finish": Finish,
"terminate": Terminate,
"list_files": ListFiles,
"search_file": SearchFile,
"str_replace_editor": StrReplaceEditor,
"terminal": Terminal,
"python_execute": PythonExecute,
"create_chat_completion": CreateChatCompletion,
"attempt_completion_client_request": AttemptCompletion,
}
tool_class = tool_classes.get(tool_name.lower())
if not tool_class:
raise ValueError(
f"Unknown tool: {tool_name}. Available tools: {list(tool_classes.keys())}"
)
return tool_class
def create_tools(tools_str: str) -> Optional[List[BaseTool]]:
"""Create list of tool instances from comma-separated tool names"""
if not tools_str:
return None
tool_names = [name.strip() for name in tools_str.split(",")]
tools = []
for tool_name in tool_names:
tool_class = get_tool_class(tool_name)
tools.append(tool_class())
return tools
def get_agent(agent_type: str):
"""Factory function to create agent based on type"""
agent_types = {
"toolcall": ToolCallAgent,
"codeact": CodeActAgent,
"midwit": MidwitAgent,
"swe": SWEAgent,
"tao": TaoAgent,
}
if agent_type not in agent_types:
raise ValueError(
f"Unknown agent type: {agent_type}. Available types: {list(agent_types.keys())}"
)
return agent_types[agent_type]()
async def main(args):
"""Main async function to run the agent flow"""
try:
agent = get_agent(args.agent_type)
tools = create_tools(args.tools)
result = await loop(
agent=agent,
tools=tools,
flow_type=FlowType[args.flow_type.upper()],
input_text=args.input,
)
print(result)
except Exception as e:
print(f"Error: {str(e)}")
raise
def setup_argparse():
"""Setup command line argument parsing"""
parser = argparse.ArgumentParser(
description="Run AgentHub with specified configuration"
)
parser.add_argument(
"--agent-type",
type=str,
default="toolcall",
choices=["toolcall", "codeact", "midwit", "swe", "tao"],
help="Type of agent to use (default: toolcall)",
)
parser.add_argument(
"--tools",
type=str,
help='Comma-separated list of tools to use (e.g., "execute_command, terminate")',
)
parser.add_argument(
"--flow-type",
type=str,
default="basic",
choices=["basic", "mcts", "aflow"],
help="Type of flow to use (default: basic)",
)
parser.add_argument(
"--input", type=str, required=True, help="Input text for the agent to process"
)
return parser
if __name__ == "__main__":
parser = setup_argparse()
asyncio.run(main(parser.parse_args()))