-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.py
More file actions
205 lines (182 loc) · 6.58 KB
/
Copy pathcli.py
File metadata and controls
205 lines (182 loc) · 6.58 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import argparse
import json
from llama_index.core.chat_engine.types import AgentChatResponse
from termcolor import colored
from Orcar import OrcarAgent, TraceAnalysisAgent
from Orcar.environment.benchmark import BenchmarkEnv
from Orcar.environment.utils import (
ContainerBash,
generate_container_name,
get_container,
pause_persistent_container,
)
from Orcar.gen_config import Config, get_llm
from Orcar.load_cache_dataset import load_filter_hf_dataset
from Orcar.log_utils import get_logger
from Orcar.types import TraceAnalysisOutput
logger = get_logger(__name__)
def green(text, attrs=None):
return colored(text, "green", attrs=attrs)
def exit_with_help_message(parser):
print(green("To execute a prompt with a specified execution type", ["bold"]))
# retrieve subparsers from parser
subparsers_actions = [
action
for action in parser._actions
if isinstance(action, argparse._SubParsersAction)
]
# there will probably only be one subparser_action,
# but better save than sorry
for subparsers_action in subparsers_actions:
# get all subparsers and print help
for _, subparser in subparsers_action.choices.items():
print(subparser.format_help())
print(green("To perform other Orcar operations", ["bold"]))
parser.print_help()
parser.exit()
class ArgumentParser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs):
super(ArgumentParser, self).__init__(*args, **kwargs)
self.error = self.error
self.exit = self.exit
def error(self, message):
exit_with_help_message(self)
def main():
parser = ArgumentParser(add_help=False)
subparsers = parser.add_subparsers(dest="command", description="valid commands")
parser_execute = subparsers.add_parser(
"execute", help="Execute a prompt with a specified execution type"
)
default_model = "gpt-4o"
default_docker_image = "sweagent/swe-agent:latest"
parser_execute.add_argument(
"--model",
default=default_model,
help=f"The LLM model (only support OpenAI now) (default: {default_model})",
)
parser_execute.add_argument(
"--enable_jit",
action="store_true",
help=f"Should JIT be used to parallelly call function tools",
)
parser_execute.add_argument(
"-d",
"--docker",
action="store_true",
help=f"Is the prompt executed in local env or docker",
)
parser_execute.add_argument(
"--image",
default=default_docker_image,
help=f"The base docker image (default: {default_docker_image})",
)
parser_execute.add_argument(
"-p",
"--persistent",
action="store_true",
help=f"Is the prompt executed in local env or docker",
)
parser_execute.add_argument(
"-c",
"--container_name",
help=f"The name of container, will be generated from image name if not given",
)
parser_execute.add_argument("prompt", type=str, help="The prompt to execute")
parser_execute = subparsers.add_parser(
"benchmark", help="Run a given huggingface benchmark following swe-bench format"
)
default_dataset = "princeton-nlp/SWE-bench_Lite"
parser_execute.add_argument(
"--model",
default=default_model,
help=f"The LLM model (only support OpenAI now) (default: {default_model})",
)
parser_execute.add_argument(
"--image",
default=default_docker_image,
help=f"The base docker image (default: {default_docker_image})",
)
parser_execute.add_argument(
"--dataset",
default=default_dataset,
help=f"The target dataset (default: {default_dataset})",
)
parser_execute.add_argument(
"-p",
"--persistent",
action="store_true",
help=f"Is the prompt executed in local env or docker",
)
parser_execute.add_argument(
"-c",
"--container_name",
help=f"The name of container, will be generated from image name if not given",
)
parser_execute.add_argument(
"-s",
"--split",
default="test",
help=f"The split you care about, e.g. dev or test",
)
parser_execute.add_argument(
"-f",
"--filter_instance",
default=".*",
help=f"Filter the instances you care about with RegEx",
)
args = parser.parse_args()
cfg = Config("./key.cfg")
if args.command == "execute":
if args.docker:
ctr_name = args.container_name
if ctr_name is None:
ctr_name = generate_container_name(args.image)
docker_ctr_subprocess = get_container(
ctr_name=ctr_name, image_name=args.image, persistent=args.persistent
)[0]
ctr_bash = ContainerBash(
ctr_subprocess=docker_ctr_subprocess, ctr_name=ctr_name
)
orcar_agent = OrcarAgent(args, cfg, args.enable_jit, ctr_bash)
response = orcar_agent.chat(args.prompt)
logger.debug(response)
ctr_bash.ctr_subprocess.stdin.close()
if args.persistent:
pause_persistent_container(ctr_bash)
else:
orcar_agent = OrcarAgent(args, cfg, args.enable_jit)
response = orcar_agent.chat(args.prompt)
logger.debug(response)
elif args.command == "benchmark":
ctr_name = args.container_name
if ctr_name is None:
ctr_name = generate_container_name(args.image)
docker_ctr_subprocess = get_container(
ctr_name=ctr_name, image_name=args.image, persistent=args.persistent
)[0]
ctr_bash = ContainerBash(
ctr_subprocess=docker_ctr_subprocess, ctr_name=ctr_name
)
ds = load_filter_hf_dataset(args)
llm = get_llm(
model=args.model,
api_key=cfg["OPENAI_API_KEY"],
)
benchmark_env = BenchmarkEnv(args, ctr_bash)
trace_analysis_agent = TraceAnalysisAgent(llm=llm, env=benchmark_env)
for inst in ds:
benchmark_env.setup(inst)
agent_chat_response: AgentChatResponse = trace_analysis_agent.chat(
json.dumps(dict(inst))
)
trace_analysis_output = TraceAnalysisOutput.model_validate_json(
agent_chat_response.response
)
logger.debug(trace_analysis_output)
# Run Test on Benchmark
# TBD
ctr_bash.ctr_subprocess.stdin.close()
if args.persistent:
pause_persistent_container(ctr_bash)
else:
exit_with_help_message(parser)