-
Notifications
You must be signed in to change notification settings - Fork 1
/
inference.py
197 lines (169 loc) · 6.5 KB
/
inference.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
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
import json
import os
import traceback
import fire
import matplotlib
#from langchain_experimental.tools.python.tool import PythonAstREPLTool
from loguru import logger
from yaml import safe_load
from llms import build_llm
from llms.utils import message2dict
from utils.assistant import GPT
from utils.output_parser import parse_code_action
from utils.save_notebook import generate_notebook, save_as_ipynb
matplotlib.use("Agg")
import nbformat
from nbclient import NotebookClient
import traceback
import time
import ruamel.yaml
def execute_code(code_str: str, Kernel, nb):
nb.cells.append(nbformat.v4.new_code_cell(code_str))
total_cells = len(nb.cells)
cell = nb.cells[-1]
client = NotebookClient(nb, allow_errors=True)
client.kc = Kernel
print(cell)
try:
client.reset_execution_trackers()
client.execute_cell(cell=cell, cell_index=-1)
except Exception as e:
traceback.print_exc()
error_message = str(e)
return error_message
outputs = nb.cells[-1]['outputs']
result = ""
for output in outputs:
if output.output_type == "stream": # 如果输出是标准输出或标准错误
result += output.text
elif output.output_type == "execute_result": # 如果输出是执行结果
result += str(output['data']['text/plain'])
elif output.output_type == "error": # 如果输出是错误
result += "There are some errors in the code. All variables in this cell should be redefined. Please Debug:\n"
result += "Error: " + str(output.ename) + "\n"
result += str(output.evalue) + "\n"
return result
import os
import json
import threading
import traceback
from concurrent.futures import ThreadPoolExecutor
import fire
from ruamel.yaml import safe_load
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
from nbclient import NotebookClient
import logging
logger = logging.getLogger(__name__)
# 全局线程锁
lock = threading.Lock()
def process_task(task, config, llm, system_prompt_template, max_turns, output_path, processed_ids):
index = task["index"]
cell_path="Cells"
if index in processed_ids:
return
file_path = ",".join(task["file_paths"])
user_query = task["user"]
nb = nbformat.v4.new_notebook()
client = NotebookClient(nb, allow_errors=True)
client.km = client.create_kernel_manager()
client.start_new_kernel()
client.start_new_kernel_client()
Kernel = client.kc
logger.info("==" * 80)
logger.info(f"Task index: {index}")
logger.info(f"Task:\n{user_query}")
logger.info(f"File path: {file_path}")
messages = [
{"role": "system", "content": system_prompt_template},
{"role": "user", "content": f"[INFO]The data is uploaded to {file_path}"},
{"role": "user", "content": user_query},
]
cells = [
{"role": "system", "text": system_prompt_template},
{"role": "user", "text": f"[INFO]The data is uploaded to {file_path}"},
{"role": "user", "text": user_query},
]
try:
for count in range(max_turns): # max 5 turn interaction
logger.info("--" * 10 + f"Round: {count}" + "--" * 10)
#logger.info(f"input messages: {messages}")
try:
out_msg, debug_info = llm.generate(messages)
except:
break
#logger.info(f"output msg: {message2dict(out_msg)}")
reasoning, code_script = parse_code_action(
out_msg.content,
mode=config["mode"],
code_start_token=config["code_start_token"],
code_end_token=config["code_end_token"],
tool_call_token=config["tool_call_token"],
)
#logger.info(f"Reasoning: {reasoning}")
#logger.info(f"Code script: {code_script}")
if code_script is None or code_script.strip() == "":
messages.append({"role": "assistant", "content": reasoning})
cells.append({"role": "assistant", "text": reasoning})
else:
messages.append(
{
"role": "assistant",
"content": out_msg.content,
}
)
cells.append({"role": "assistant", "text": reasoning})
if code_script is None or code_script.strip() == "":
break
code_response = execute_code(code_script, Kernel, nb)
#logger.info(f"Code response:\n{code_response}")
messages.append({"role": "user", "content": "[INFO]This is a Code Interpreter Message:\n" + code_response})
cells.append(
{
"role": "assistant",
"code": code_script,
"result": code_response,
}
)
save_as_ipynb(generate_notebook(cells), f"{cell_path}/{index}.ipynb")
item = {"messages": messages}
item.update(task)
except Exception:
logger.error(traceback.format_exc())
save_as_ipynb(generate_notebook(cells), f"{cell_path}/{index}.ipynb")
item = {"messages": messages}
item.update(task)
finally:
client._cleanup_kernel()
with open(output_path, 'a') as f:
f.write(json.dumps(item, ensure_ascii=False) + '\n')
print("======Write Json=======")
def main(config_path: str, task_path: str, output_path: str):
os.makedirs("Check", exist_ok=True)
os.makedirs("output", exist_ok=True)
logger.info("started")
yaml = ruamel.yaml.YAML()
config=yaml.load(open(config_path, "r"))
logger.info("config loaded")
llm = build_llm(config["llm"]["type"], config["llm"]["args"])
logger.info("llm built")
system_prompt_template = config["system_prompt_template"]
max_turns = config["max_turns"]
test_data = json.load(open(task_path, "r"))
logger.info(f"total tasks: {len(test_data)}")
if os.path.exists(output_path):
processed_ids = set(
[json.loads(line)["index"] for line in open(output_path, "r")]
)
else:
processed_ids = set()
with ThreadPoolExecutor(max_workers=16) as executor:
futures = [
executor.submit(process_task, task, config, llm, system_prompt_template, max_turns, output_path, processed_ids)
for task in test_data
]
for future in futures:
future.result() # 等待所有任务完成
logger.info("finished")
if __name__ == "__main__":
fire.Fire(main)