Merge branch 'feat/chart-visualization' into feat/data_visualization_hackathon

This commit is contained in:
ZJU_czx
2025-03-25 10:06:00 +08:00
17 changed files with 9167 additions and 0 deletions
+3
View File
@@ -197,3 +197,6 @@ cython_debug/
# OSX
.DS_Store
# node
node_modules
+67
View File
@@ -0,0 +1,67 @@
from pydantic import Field
from app.agent.browser import BrowserAgent
from app.config import config
from app.prompt.browser import NEXT_STEP_PROMPT as BROWSER_NEXT_STEP_PROMPT
from app.prompt.visualization import NEXT_STEP_PROMPT, SYSTEM_PROMPT
from app.tool import Terminate, ToolCollection
from app.tool.browser_use_tool import BrowserUseTool
from app.tool.chart_visualization.chart_visualization import ChartVisualization
from app.tool.chart_visualization.normal_python_execute import NormalPythonExecute
from app.tool.chart_visualization.data_analysis_python import DataAnalysisPythonExecute
class DataAnalysis(BrowserAgent):
"""
A data analysis agent that uses planning to solve various data analysis tasks.
This agent extends BrowserAgent with a comprehensive set of tools and capabilities,
including Python execution, web browsing, chart visualization.
"""
name: str = "DataAnalysis"
description: str = (
"An analytical agent that utilizes multiple tools to solve diverse data tasks"
)
system_prompt: str = SYSTEM_PROMPT.format(directory=config.workspace_root)
next_step_prompt: str = NEXT_STEP_PROMPT
max_observe: int = 10000
max_steps: int = 20
# Add general-purpose tools to the tool collection
available_tools: ToolCollection = Field(
default_factory=lambda: ToolCollection(
NormalPythonExecute(),
DataAnalysisPythonExecute(),
ChartVisualization(),
BrowserUseTool(),
Terminate(),
)
)
async def think(self) -> bool:
"""Process current state and decide next actions with appropriate context."""
# Store original prompt
original_prompt = self.next_step_prompt
# Only check recent messages (last 3) for browser activity
recent_messages = self.memory.messages[-3:] if self.memory.messages else []
browser_in_use = any(
"browser_use" in msg.content.lower()
for msg in recent_messages
if hasattr(msg, "content") and isinstance(msg.content, str)
)
if browser_in_use:
# Override with browser-specific prompt temporarily to get browser context
self.next_step_prompt = BROWSER_NEXT_STEP_PROMPT
# Call parent's think method
result = await super().think()
# Restore original prompt
self.next_step_prompt = original_prompt
return result
+8
View File
@@ -0,0 +1,8 @@
SYSTEM_PROMPT = (
"You are an AI agent designed to data analysis and data visualization task. You have various tools at your disposal that you can call upon to efficiently complete complex requests."
"The initial directory is: {directory}"
)
NEXT_STEP_PROMPT = """
Based on user needs, proactively select the most appropriate tool or combination of tools. For complex tasks, you can break down the problem and use different tools step by step to solve it. After using each tool, clearly explain the execution results and suggest the next steps.
"""
+75
View File
@@ -0,0 +1,75 @@
# Chart Visualization Tool
The chart visualization tool generates data processing code through Python and ultimately invokes [@visactor/vmind](https://github.com/VisActor/VMind) to obtain chart specifications. Chart rendering is implemented using [@visactor/vchart](https://github.com/VisActor/VChart).
## Installation
1. Install Node.js >= 18
```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# After installation, restart the terminal and install the latest Node.js LTS version:
nvm install --lts
```
2. Install dependencies
```bash
cd app/tool/chart_visualization
npm install
```
## Tool Parameters
```typescript
{
// Generates Python code for data processing to produce a CSV file
code: string;
// Parses user intent to generate chart description
chart_description: string;
// Final output type (png/html). HTML supports VChart rendering and interaction
output_type: 'png' | 'html'
}
```
## Output
The final results will be saved locally in `png` or `html` format for subsequent use by agents.
## VMind Configuration
### LLM
VMind requires LLM invocation for intelligent chart generation. By default, it uses the `config.llm["default"]` configuration.
### Generation Settings
Main configurations include chart dimensions, theme, and generation method:
### Generation Method
Default: png. Currently supports automatic selection of `output_type` by LLM based on context.
### Dimensions
Default dimensions are unspecified. For HTML output, charts fill the entire page by default. For PNG output, defaults to `1000*1000`.
### Theme
Default theme: `'light'`. VChart supports multiple themes. See [Themes](https://www.visactor.io/vchart/guide/tutorial_docs/Theme/Theme_Extension).
## Testing
Two test tasks with different difficulty levels are provided:
### Basic Chart Generation Task
Generates charts from given data and specific requirements. Execute with:
```bash
python -m app.tool.chart_visualization.test.simple_chart
```
Results will be saved in `./data`, containing 9 different chart types.
### Simple Data Report Task
Processes raw data with basic analysis requirements. Execute with:
```bash
python -m app.tool.chart_visualization.test.simple_report
```
Results will also be saved in `./data`.
+74
View File
@@ -0,0 +1,74 @@
# 图表可视化工具
图表可视化工具,通过python生成数据处理代码,最终调用[@visactor/vmind](https://github.com/VisActor/VMind)得到图表的spec结果,图表渲染使用[@visactor/vchart](https://github.com/VisActor/VChart)
## 安装
1. 安装node >= 18
```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# 安装完成后重启终端,然后安装 Node 最新 LTS 版本:
nvm install --lts
```
2. 安装依赖
```bash
cd app/tool/chart_visualization
npm install
```
## 工具参数
```typescript
{
// 用于生产数据处理的python代码,最终得到csv文件
code: string;
// 解析用户意图,得到图表描述
chart_description: string;
// 最终产物png或者html;html下支持vchart渲染和交互
output_type: 'png' | 'html'
}
```
## 输出
最终以'png'或者'html'的形式保存在本地,供后续agent使用
## VMind配置
### LLM
VMind本身也需要通过调用大模型得到智能图表生成结果,目前默认会使用`config.llm["default"]`配置
### 生成配置
主要生成配置包括图表的宽高、主题以及生成方式;
### 生成方式
默认为png,目前支持大模型根据上下文自己选择`output_type`
### 宽高
目前默认不指定宽高,`html`下默认占满整个页面,'png'下默认为`1000 * 1000`
### 主题
目前默认主题为`'light'`VChart图表支持多种主题,详见[主题](https://www.visactor.io/vchart/guide/tutorial_docs/Theme/Theme_Extension)
## 测试
当前设置了两种不能难度的任务用于测试
### 简单图表生成任务
给予数据和具体的图表生成需求,测试结果,执行命令:
```bash
python -m app.tool.chart_visualization.test.simple_chart
```
结果应位于`./data`下,涉及到9种不同的图表结果
### 简单数据报表任务
给予简单原始数据可分析需求,需要对数据进行简单加工处理,执行命令:
```bash
python -m app.tool.chart_visualization.test.simple_report
```
结果同样位于`./data`
+5
View File
@@ -0,0 +1,5 @@
from app.tool.chart_visualization.chart_visualization import ChartVisualization
from app.tool.chart_visualization.data_analysis_python import DataAnalysisPythonExecute
from app.tool.chart_visualization.normal_python_execute import NormalPythonExecute
__all__ = ["ChartVisualization", "DataAnalysisPythonExecute", "NormalPythonExecute"]
@@ -0,0 +1,117 @@
import subprocess
import json
import base64
import pandas as pd
import aiofiles
import os
from typing import Any, Hashable
from pydantic import Field, model_validator
from app.llm import LLM
from app.tool.base import BaseTool
from app.logger import logger
class ChartVisualization(BaseTool):
name: str = "generate_data_visualization"
description: str = """Visualize a statistical chart using csv data and chart description. The tool accepts local csv data file path and description of the chart, and output a chart in png or html.
Note: Each tool call generates only one single chart.
"""
parameters: dict = {
"type": "object",
"properties": {
"csv_path": {
"type": "string",
"description": """file path of csv data with ".csv" in the end""",
},
"chart_description": {
"type": "string",
"description": "The chart title or description should be concise and clear. Examples: 'Product sales distribution', 'Monthly revenue trend'.",
},
"output_type": {
"description": "Rendering format (html=interactive)",
"type": "string",
"default": "html",
"enum": ["png", "html"],
},
},
"required": ["code", "chart_description"],
}
llm: LLM = Field(default_factory=LLM, description="Language model instance")
@model_validator(mode="after")
def initialize_llm(self):
"""Initialize llm with default settings if not provided."""
if self.llm is None or not isinstance(self.llm, LLM):
self.llm = LLM(config_name=self.name.lower())
return self
async def execute(
self, csv_path: str, chart_description: str, output_type: str
) -> str:
logger.info(
f"📈 Chart Generation with data and description: {chart_description} with {csv_path} "
)
try:
df = pd.read_csv(csv_path)
df = df.astype(object)
df = df.where(pd.notnull(df), None)
data_dict_list = df.to_json(orient="records", force_ascii=False)
result = await self.invoke_vmind(
data_dict_list, chart_description, output_type
)
if "error" in result:
return {
"observation": f"Error: {result["error"]}",
"success": False,
}
chart_file_path = csv_path.replace(".csv", f".{output_type}")
while os.path.exists(chart_file_path):
chart_file_path = chart_file_path.replace(
f".{output_type}", f"_new.{output_type}"
)
if output_type == "png":
byte_data = base64.b64decode(result["res"])
async with aiofiles.open(chart_file_path, "wb") as file:
await file.write(byte_data)
else:
async with aiofiles.open(
chart_file_path, "w", encoding="utf-8"
) as file:
await file.write(result["res"])
return {"observation": f"chart successfully saved to {chart_file_path}"}
except Exception as e:
return {
"observation": f"Error: {e}",
"success": False,
}
async def invoke_vmind(
self,
dict_data: list[dict[Hashable, Any]],
chart_description: str,
output_type: str,
):
llm_config = {
"base_url": self.llm.base_url,
"model": self.llm.model,
"api_key": self.llm.api_key,
}
vmind_params = {
"llm_config": llm_config,
"user_prompt": chart_description,
"dataset": dict_data,
"output_type": output_type,
}
process = subprocess.run(
["npx", "ts-node", "src/chartVisualize.ts"],
input=json.dumps(vmind_params),
capture_output=True,
text=True,
encoding="utf-8",
cwd=os.path.dirname(__file__),
)
if process.returncode == 0:
return json.loads(process.stdout)
else:
return {"error": f"Node.js Error: {process.stderr}"}
@@ -0,0 +1,42 @@
from app.tool.chart_visualization.normal_python_execute import NormalPythonExecute
class DataAnalysisPythonExecute(NormalPythonExecute):
"""A tool for executing Python code in data analysis task with timeout and safety restrictions."""
name: str = "data_analysis_python_execute"
description: str = (
"Executes Python code string in data analysis task, save data table in csv file. Note: Only print outputs are visible, function return values are not captured. Use print statements to see results."
)
parameters: dict = {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": """Python code template EXCLUSIVELY for data analysis. Must Contains:
1. Data loading logic (handle dataframe/dict/file/url/json/web crawler)
2. Data analysis (cleaning/transformation)
3. CSV saving with path print: print(csv_path)
""",
},
"analysis_content": {
"type": "string",
"description": "Your analysis of current task, ensure your analysis is concise, clear, and easy to understand.",
},
},
"required": ["code"],
}
async def execute(self, code: str, analysis_content: str, timeout=5):
"""
Executes the provided Python code with a timeout.
Args:
code (str): The Python code to execute.
analysis_content (str): The analysis content of current task.
timeout (int): Execution timeout in seconds.
Returns:
Dict: Contains 'output' with execution output or error message and 'success' status.
"""
return await super().execute(code, timeout)
@@ -0,0 +1,45 @@
import sys
from io import StringIO
from app.tool.python_execute import PythonExecute
from app.tool.chart_visualization.utils import (
extract_executable_code,
)
class NormalPythonExecute(PythonExecute):
"""A tool for executing Python code with timeout and safety restrictions."""
name: str = "common_python_execute"
description: str = (
"""Executes Python code strings. Note:
1. Only outputs from print() are visible; function return values are not captured. Use print() statements to display results
2. Applicable to scenarios **excluding data analysis and chart generation**"""
)
parameters: dict = {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "The Python code to execute.",
},
},
"required": ["code"],
}
def _run_code(self, code: str, result_dict: dict, safe_globals: dict) -> None:
original_stdout = sys.stdout
be_extracted_code = extract_executable_code(code) # ignore_security_alert RCE
try:
output_buffer = StringIO()
sys.stdout = output_buffer
exec( # ignore_security_alert RCE
be_extracted_code, safe_globals, safe_globals
) # ignore_security_alert RCE
result_dict["observation"] = output_buffer.getvalue()
result_dict["success"] = True
except Exception as e:
result_dict["observation"] = str(e)
result_dict["success"] = False
finally:
sys.stdout = original_stdout
File diff suppressed because it is too large Load Diff
+21
View File
@@ -0,0 +1,21 @@
{
"name": "chart_visualization",
"version": "1.0.0",
"main": "src/index.ts",
"devDependencies": {
"@types/node": "^22.10.1",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
},
"dependencies": {
"@visactor/vchart": "^1.13.7",
"@visactor/vmind": "^2.0.4",
"canvas": "^2.11.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": ""
}
@@ -0,0 +1,132 @@
import Canvas from "canvas";
import path from "path";
import { readFileSync } from "fs";
import VMind from "@visactor/vmind";
import VChart from "@visactor/vchart";
import { isString } from "@visactor/vutils";
const getBase64 = async (spec: any, width?: number, height?: number) => {
spec.animation = false;
width && (spec.width = width);
height && (spec.height = height);
const cs = new VChart(spec, {
mode: "node",
modeParams: Canvas,
animation: false,
dpr: 2,
});
await cs.renderAsync();
const buffer = await cs.getImageBuffer();
return Buffer.from(buffer, "utf8").toString("base64");
};
const serializeSpec = (spec: any) => {
return JSON.stringify(spec, (key, value) => {
if (typeof value === "function") {
const funcStr = value
.toString()
.replace(/(\r\n|\n|\r)/gm, "")
.replace(/\s+/g, " ");
return `__FUNCTION__${funcStr}`;
}
return value;
});
};
async function getHtmlVChart(spec: any, width: number, height: number) {
return `<!DOCTYPE html>
<html>
<head>
<title>VChart 示例</title>
<script src="${path.join(
__dirname,
"../node_modules/@visactor/vchart/build/index.min.js"
)}"></script>
</head>
<body>
<div id="chart-container" style="width: ${
width ? `${width}px` : "100%"
}; height: ${height ? `${height}px` : "100%"};"></div>
<script>
// parse spec with function
function parseSpec(stringSpec) {
return JSON.parse(stringSpec, (k, v) => {
if (typeof v === 'string' && v.startsWith('__FUNCTION__')) {
const funcBody = v.slice(12); // 移除标记
try {
return new Function('return (' + funcBody + ')')();
} catch(e) {
console.error('函数解析失败:', e);
return () => {};
}
}
return v;
});
}
const spec = parseSpec('${serializeSpec(spec)}');
const chart = new VChart.VChart(spec, {
dom: 'chart-container'
});
chart.renderSync();
</script>
</body>
</html>
`;
}
async function generateChart() {
const inputData = JSON.parse(readFileSync(process.stdin.fd, "utf-8"));
try {
const {
llm_config,
user_prompt: userPrompt,
dataset,
output_type: outputType = "png",
width,
height,
} = inputData;
const { base_url: baseUrl, model, api_key: apiKey } = llm_config;
const vmind = new VMind({
url: `${baseUrl}/chat/completions`,
model,
headers: {
"api-key": apiKey,
Authorization: `Bearer ${apiKey}`,
},
});
const jsonDataset = isString(dataset) ? JSON.parse(dataset) : dataset;
const { spec, error } = await vmind.generateChart(
userPrompt,
undefined,
jsonDataset,
{
enableDataQuery: false,
theme: "light",
}
);
if (error || !spec) {
console.log(
JSON.stringify({
error: error || "Spec of Chart was Empty!",
})
);
return;
}
if (outputType === "png") {
console.log(
JSON.stringify({ res: await getBase64(spec, width, height) })
);
} else {
console.log(
JSON.stringify({ res: await getHtmlVChart(spec, width, height) })
);
}
} catch (error) {
console.log(JSON.stringify({ error }));
}
}
generateChart();
@@ -0,0 +1,190 @@
import asyncio
from app.agent.data_analysis import DataAnalysis
from app.logger import logger
prefix = "帮我生成图表并保存在本地./data下,具体为:"
tasks = [
{
"prompt": "帮我展示不同区域各商品销售额",
"data": """商品名称,region,销售额
可乐,south,2350
可乐,east,1027
可乐,west,1027
可乐,north,1027
雪碧,south,215
雪碧,east,654
雪碧,west,159
雪碧,north,28
芬达,south,345
芬达,east,654
芬达,west,2100
芬达,north,1679
醒目,south,1476
醒目,east,830
醒目,west,532
醒目,north,498
""",
},
{
"prompt": "展示各品牌市场占有率",
"data": """品牌名称,市场份额,平均价格,净利润
Apple,0.5,7068,314531
Samsung,0.2,6059,362345
Vivo,0.05,3406,234512
Nokia,0.01,1064,-1345
Xiaomi,0.1,4087,131345""",
},
{
"prompt": "请帮我展示各产品的销售趋势",
"data": """date,type,value
2023-01-01,产品 A,52.9
2023-01-01,产品 B,63.6
2023-01-01,产品 C,11.2
2023-01-02,产品 A,45.7
2023-01-02,产品 B,89.1
2023-01-02,产品 C,21.4
2023-01-03,产品 A,67.2
2023-01-03,产品 B,82.4
2023-01-03,产品 C,31.7
2023-01-04,产品 A,80.7
2023-01-04,产品 B,55.1
2023-01-04,产品 C,21.1
2023-01-05,产品 A,65.6
2023-01-05,产品 B,78
2023-01-05,产品 C,31.3
2023-01-06,产品 A,75.6
2023-01-06,产品 B,89.1
2023-01-06,产品 C,63.5
2023-01-07,产品 A,67.3
2023-01-07,产品 B,77.2
2023-01-07,产品 C,43.7
2023-01-08,产品 A,96.1
2023-01-08,产品 B,97.6
2023-01-08,产品 C,59.9
2023-01-09,产品 A,96.1
2023-01-09,产品 B,100.6
2023-01-09,产品 C,66.8
2023-01-10,产品 A,101.6
2023-01-10,产品 B,108.3
2023-01-10,产品 C,56.9 """,
},
{
"prompt": "展示搜索关键词热度",
"data": """关键词,热度
热词,1000
燥了我们,800
娆贱货,400
我的心愿是世界和平,400
咻咻咻,400
神舟十一号,400
百鸟朝风,400
中国女排,400
我的关呐,400
腿咚,400
火锅英雄,400
宝宝心里苦,400
奥运会,400
厉害了我的哥,400
诗和远方,400
宋仲基,400
PPAP,400
蓝瘦香菇,400
雨露均沾,400
友谊的小船说翻就翻就翻,400
北京瘫,400
敬业,200
Apple,200
狗带,200
老司机,200
吃瓜群众,200
疯狂动物城,200
城会玩,200
套路,200
水逆,200
你咋不上天呢,200
蛇精男,200
你咋不上天呢,200
三星爆炸门,200
小李子奥斯卡,200
人丑就要多读书,200
男友力,200
一脸懵逼,200
太阳的后裔,200""",
},
{
"prompt": "帮我比较不同电动汽车品牌性能,使用散点图",
"data": """续航里程,充电时间,品牌名称,平均价格
2904,46,品牌1,2350
1231,146,品牌2,1027
5675,324,品牌3,1242
543,57,品牌4,6754
326,234,品牌5,215
1124,67,品牌6,654
3426,81,品牌7,159
2134,24,品牌8,28
1234,52,品牌9,345
2345,27,品牌10,654
526,145,品牌11,2100
234,93,品牌12,1679
567,94,品牌13,1476
789,45,品牌14,830
469,75,品牌15,532
5689,54,品牌16,498
""",
},
{
"prompt": "展示各个流程转化率",
"data": """流程,转化率,Month
Step1,100,1
Step2,80,1
Step3,60,1
Step4,40,1""",
},
{
"prompt": "展示男女早餐饭量不同",
"data": """时间,男-早餐,女-早餐
周一,15,22
周二,12,10
周三,15,20
周四,10,12
周五,13,15
周六,10,15
周日,12,14""",
},
{
"prompt": "帮我展示这个人在不同方面的绩效,他是否是六边形战士",
"data": """dimension,performance
Strength,5
Speed,5
Shooting,3
Endurance,5
Precision,5
Growth,5""",
},
{
"prompt": "展示数据流动",
"data": """始发地,终点站,value
Node A,Node 1,10
Node A,Node 2,5
Node B,Node 2,8
Node B,Node 3,2
Node C,Node 2,4
Node A,Node C,2
Node C,Node 1,2""",
},
]
async def main():
for index, item in enumerate(tasks):
logger.info(f"Begin task {index} / {len(tasks)}!")
agent = DataAnalysis()
await agent.run(
f"{prefix},chart_description:{item["prompt"]},Data:{item["data"]}"
)
logger.info(f"Finish with {item["prompt"]}")
if __name__ == "__main__":
asyncio.run(main())
@@ -0,0 +1,24 @@
import asyncio
from app.agent.data_analysis import DataAnalysis
# from app.agent.manus import Manus
async def main():
agent = DataAnalysis()
# agent = Manus()
await agent.run(
"""分析以下数据并生成一个图文数据报告在本地./data文件夹下,格式为html.Requriment:展示3个团队半年内的人效变化,并且将相邻两个月各团队的环比上升或者下降的比例体现出来
Data:月份 团队A 团队B 团队C
1月 1200小时 1350小时 1100小时
2月 1250小时 1400小时 1150小时
3月 1180小时 1300小时 1300小时
4月 1220小时 1280小时 1400小时
5月 1230小时 1320小时 1450小时
6月 1200小时 1250小时 1500小时"""
)
if __name__ == "__main__":
asyncio.run(main())
+109
View File
@@ -0,0 +1,109 @@
{
"include": [
"src/**/*.ts",
],
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "ES2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
"typeRoots": [
"./node_modules/@types",
"src/types"
], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
// "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "noUncheckedSideEffectImports": true, /* Check side effect imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
"checkJs": false, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
+34
View File
@@ -0,0 +1,34 @@
def extract_executable_code(code_str: str) -> str:
"""
Extract executable code from function call's parameters
Args:
code_str (string): The python code generated by llm.
Returns:
String: Python code can execute directly.
"""
lines = code_str.strip().splitlines()
start_idx = -1
end_idx = -1
# Find first occurrence of ```
for i, line in enumerate(lines):
if "```" in line.strip() or '"""' in line.strip():
start_idx = i
break
# Find last occurrence of ```
for i in reversed(range(len(lines))):
if "```" in line.strip() or '"""' in line.strip():
end_idx = i
break
if start_idx != -1 and end_idx != -1 and start_idx < end_idx:
lines = lines[start_idx + 1 : end_idx]
elif start_idx != -1:
lines = lines[start_idx + 1 :]
elif end_idx != -1:
lines = lines[:end_idx]
return "\n".join(lines)