-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_agent.py
More file actions
131 lines (114 loc) · 5.56 KB
/
python_agent.py
File metadata and controls
131 lines (114 loc) · 5.56 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
import os
from dotenv import load_dotenv
load_dotenv(override=True)
from langchain_deepseek import ChatDeepSeek
from langgraph.prebuilt import create_react_agent
from langchain_core.tools import tool
from pydantic import BaseModel, Field
import matplotlib
import json
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import pymysql
from langchain_tavily import TavilySearch
from config.load_key import load_key
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent
model = ChatOpenAI(
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
api_key=load_key("aliyun-bailian"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
model="qwen-plus", # 阿里云百炼模型
)
# ✅创建Python代码执行工具
# Python代码执行工具结构化参数说明
class PythonCodeInput(BaseModel):
py_code: str = Field(description="一段合法的 Python 代码字符串,例如 '2 + 2' 或 'x = 3\\ny = x * 2'")
@tool(args_schema=PythonCodeInput)
def python_inter(py_code):
"""
当用户需要编写Python程序并执行时,请调用该函数。
该函数可以执行一段Python代码并返回最终结果,需要注意,本函数只能执行非绘图类的代码,若是绘图相关代码,则需要调用fig_inter函数运行。
"""
g = globals()
try:
# 尝试如果是表达式,则返回表达式运行结果
return str(eval(py_code, g))
# 若报错,则先测试是否是对相同变量重复赋值
except Exception as e:
global_vars_before = set(g.keys())
try:
exec(py_code, g)
except Exception as e:
return f"代码执行时报错{e}"
global_vars_after = set(g.keys())
new_vars = global_vars_after - global_vars_before
# 若存在新变量
if new_vars:
result = {var: g[var] for var in new_vars}
# print("代码已顺利执行,正在进行结果梳理...")
return str(result)
else:
# print("代码已顺利执行,正在进行结果梳理...")
return "已经顺利执行代码"
# ✅ 创建绘图工具
# 绘图工具结构化参数说明
class FigCodeInput(BaseModel):
py_code: str = Field(description="要执行的 Python 绘图代码,必须使用 matplotlib/seaborn 创建图像并赋值给变量")
fname: str = Field(description="图像对象的变量名,例如 'fig',用于从代码中提取并保存为图片")
@tool(args_schema=FigCodeInput)
def fig_inter(py_code: str, fname: str) -> str:
"""
当用户需要使用 Python 进行可视化绘图任务时,请调用该函数。
注意:
1. 所有绘图代码必须创建一个图像对象,并将其赋值为指定变量名(例如 `fig`)。
2. 必须使用 `fig = plt.figure()` 或 `fig = plt.subplots()`。
3. 不要使用 `plt.show()`。
4. 请确保代码最后调用 `fig.tight_layout()`。
5. 所有绘图代码中,坐标轴标签(xlabel、ylabel)、标题(title)、图例(legend)等文本内容,必须使用英文描述。
示例代码:
fig = plt.figure(figsize=(10,6))
plt.plot([1,2,3], [4,5,6])
fig.tight_layout()
"""
# print("正在调用fig_inter工具运行Python代码...")
current_backend = matplotlib.get_backend()
matplotlib.use('Agg')
local_vars = {"plt": plt, "pd": pd, "sns": sns}
# ✅ 设置图像保存路径(你自己的绝对路径)
base_dir = r"C:\Users\Administrator\Desktop\data_agent\agent-chat-ui\agent-chat-ui-main\public"
images_dir = os.path.join(base_dir, "images")
os.makedirs(images_dir, exist_ok=True) # ✅ 自动创建 images 文件夹(如不存在)
try:
g = globals()
exec(py_code, g, local_vars)
g.update(local_vars)
fig = local_vars.get(fname, None)
if fig:
image_filename = f"{fname}.png"
abs_path = os.path.join(images_dir, image_filename) # ✅ 绝对路径
rel_path = os.path.join("images", image_filename) # ✅ 返回相对路径(给前端用)
fig.savefig(abs_path, bbox_inches='tight')
return f"✅ 图片已保存,路径为: {rel_path}"
else:
return "⚠️ 图像对象未找到,请确认变量名正确并为 matplotlib 图对象。"
except Exception as e:
return f"❌ 执行失败:{e}"
finally:
plt.close('all')
matplotlib.use(current_backend)
Python_Agent_prompt = """
你是一名经验丰富的智能数据分析助手,擅长帮助用户高效完成以下任务:
1. **非绘图类任务的Python代码执行:**
- 当用户需要执行Python脚本或进行数据处理、统计计算时,请调用`python_inter`工具。
- 仅限执行非绘图类代码,例如变量定义、数据分析等。
2. **绘图类Python代码执行:**
- 当用户需要进行可视化展示(如生成图表、绘制分布等)时,请调用`fig_inter`工具。
- 你可以直接读取数据并进行绘图,不需要借助`python_inter`工具读取图片。
- 你应根据用户需求编写绘图代码,并正确指定绘图对象变量名(如 `fig`)。
- 当你生成Python绘图代码时必须指明图像的名称,如fig = plt.figure()或fig = plt.subplots()创建图像对象,并赋值为fig。
- 不要调用plt.show(),否则图像将无法保存。
"""
python_tool = [python_inter, fig_inter]
python_agent = create_agent(model=model, tools=python_tool, system_prompt=Python_Agent_prompt)