File size: 8,984 Bytes
2a26d3b |
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# -*- coding: utf-8 -*-
import ast
import json
import random
import re
import signal
import threading
from contextlib import contextmanager
from typing import Any, Tuple
import pandas as pd
from langchain_experimental.tools.python.tool import PythonAstREPLTool
from evaluate_code_correction.pytool import (
extract_last_df, format_result)
def read_jsonl(file_path):
data = []
with open(file_path, "r", encoding="utf-8") as f:
for line in f:
data.append(json.loads(line.strip()))
return data
def load_json(data_path):
"""
# 加载 json 文件
"""
with open(data_path, "r", encoding="utf-8") as f:
data = json.load(f)
return data
def save_json(data_path, data_list):
"""
# 保存 json 文件
"""
with open(data_path, "w", encoding="utf-8") as f:
json.dump(data_list, f, ensure_ascii=False)
def get_dfs_info(table_paths):
"""将所有csv文件对应的df-info拼装到一起"""
infos_list = []
if len(table_paths) == 1:
df_markdown_info = str(
pd.read_csv(table_paths[0], encoding="utf-8").head(5).to_string(index=False)
)
normalized_head = f"""/*\n"df.head()" as follows:\n{df_markdown_info}\n*/"""
infos_list.append(normalized_head)
else:
for i, path in enumerate(table_paths):
# normalized_name = normalize_table_name(path)
df_markdown_info = str(
pd.read_csv(path, encoding="utf-8").head(5).to_string(index=False)
)
normalized_head = (
f"""/*\n"df{i+1}.head()" as follows:\n{df_markdown_info}\n*/"""
)
# single_table_name = "\n".join([normalized_head, df_markdown_info])
infos_list.append(normalized_head)
return "\n".join(infos_list)
def sample_from_two_lists(list1, list2):
# 如果列表为空,则直接返回None或抛出异常,取决于你的需求
if not list1 or not list2:
return None # 或者你可以抛出异常
# 生成一个0到1之间的随机浮点数
rand_val = random.random()
# 如果随机数小于0.5,从第一个列表中采样
if rand_val < 0.5:
return random.choice(list1)
# 否则,从第二个列表中采样
else:
return random.choice(list2)
def recraft_query(query, locals):
last_df = extract_last_df(query, locals)
end_str = "\n" + format_result + "print(format_result({}))".format(last_df)
recraft_query = query + end_str
return recraft_query
def extract_code_without_comments(code):
"""
从Python代码中提取除注释行以外的代码。
:param code: str, 输入的Python代码
:return: str, 提取后的代码
"""
code = re.sub(r'"""[\s\S]*?"""', "", code)
code = re.sub(r"'''[\s\S]*?'''", "", code)
# 移除单行注释
lines = code.split("\n")
cleaned_lines = []
for line in lines:
# 移除以 # 开始的注释,但保留字符串中的 #
cleaned_line = re.sub(r'(?<!["\'"])#.*$', "", line)
cleaned_lines.append(cleaned_line.rstrip()) # rstrip() 移除行尾空白
# 重新组合代码,保留空行以维持原始结构
return "\n".join(cleaned_lines)
def is_python_code(line: str) -> bool:
"""Tool function to check if a line of text is Python code"""
try:
tree = ast.parse(line)
# Check if the parsed tree has at least one node that represents executable code
for node in ast.walk(tree):
if isinstance(
node,
(
ast.Expr,
ast.Assign,
ast.FunctionDef,
ast.ClassDef,
ast.Import,
ast.ImportFrom,
ast.For,
ast.While,
ast.If,
ast.With,
ast.Raise,
ast.Try,
),
):
return True
return False
except SyntaxError:
return False
def extract_text_before_code(text: str) -> str:
"""Tool function for extract text content"""
lines = text.split("\n")
text_before_code = []
for line in lines:
if is_python_code(line):
break
text_before_code.append(line)
return "\n".join(text_before_code)
def extract_python_code(text: str) -> str:
"""Tool function for extract python code"""
lines = text.split("\n")
python_code = []
for line in lines:
if is_python_code(line):
python_code.append(line)
return "\n".join(python_code)
def fix_indents(text: str) -> str:
return text.replace("\t", " ")
def filter_cot(completion: str):
"""
Filter the COT steps before python code
:param completion: llm output contents
:return filtered COT content
"""
try:
# 如果输出较为规范,可以使用这种方式提取cot部分的内容
pattern = r"Thought:\s*(.*?)\s*(?=Python Code:)"
match = re.search(pattern, completion, re.DOTALL)
if match:
thought_content = match.group(1)
else:
# 如果输出内容相对杂乱
thought_content = extract_text_before_code(completion)
return thought_content
except:
return ""
def filter_code(completion: str) -> Tuple[str, str]:
"""
Filter python code from the llm output completion
:param completion: llm output contents
:return filtered python code and execute code
"""
try:
# 输出形式符合prompt
regex = r"```python\s(.*?)```"
action_match = re.search(regex, completion, re.DOTALL)
if action_match:
action_input = action_match.group(1)
action_input = action_input.strip(" ")
action_input = action_input.strip('"')
code = action_input.strip(" ")
else:
# 输出形式随意
code = extract_python_code(completion)
code = code.strip(" ")
pure_code = extract_code_without_comments(code)
return code, pure_code
except:
return "", ""
def get_tool(df: Any, df_names=None):
"""
Define python code execute tool
:param df: List[pd.DataFrame] or pd.DataFrame
:return Runnable
"""
tool = PythonAstREPLTool()
if df_names == None:
if isinstance(df, pd.DataFrame):
locals = {"df": df}
else:
locals = {}
for i, dataframe in enumerate(df):
locals[f"df{i + 1}"] = dataframe
else:
locals = {}
for i, dataframe in enumerate(df):
locals[df_names[i]] = dataframe
tool.locals = locals
tool.globals = tool.locals
return tool
def get_table_infos(table_paths):
"""将所有csv文件对应的df-info拼装到一起"""
infos_list = []
if len(table_paths) == 1:
df_markdown_info = str(
pd.read_csv(table_paths[0], encoding="utf-8")
.head(3)
.to_markdown(index=False)
)
normalized_head = f"""/*\n"df.head()" as follows:\n{df_markdown_info}\n*/"""
infos_list.append(normalized_head)
else:
for i, path in enumerate(table_paths):
# normalized_name = normalize_table_name(path)
df_markdown_info = str(
pd.read_csv(path, encoding="utf-8").head(3).to_markdown(index=False)
)
normalized_head = (
f"""/*\n"df{i+1}.head()" as follows:\n{df_markdown_info}\n*/"""
)
# single_table_name = "\n".join([normalized_head, df_markdown_info])
infos_list.append(normalized_head)
return "\n".join(infos_list)
# 定义一个异常类,用于超时处理
class TimeoutException(Exception):
pass
# 创建一个上下文管理器来处理超时
@contextmanager
def timeout(time):
# 定义信号处理函数
def raise_timeout(signum, frame):
raise TimeoutException(f"Timeout error, running time exceed {time}")
# 设置信号定时器
signal.signal(signal.SIGALRM, raise_timeout)
signal.alarm(time)
try:
yield
finally:
# 取消信号定时器
signal.alarm(0)
def run_code(code, result, tool):
try:
# 在子线程中运行代码
result.append(tool.run(code))
except Exception as e:
result.append(e)
def execute_with_timeout(code, timeout_seconds, tool):
result = []
thread = threading.Thread(target=run_code, args=(code, result, tool))
thread.start()
thread.join(timeout_seconds)
if thread.is_alive():
thread._stop() # 终止子线程
raise TimeoutException(
f"Timeout error, running time exceed {timeout_seconds} seconds"
)
else:
if isinstance(result[0], Exception):
raise result[0]
return result[0]
|