十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

PandasAI v3 移除 custom_whitelisted_dependencies 后如何用 Docker 沙箱替代

PandasAI v3 移除 custom_whitelisted_dependencies 后如何用 Docker 沙箱替代 PandasAI v3 移除 custom_whitelisted_dependencies 后如何用 Docker 沙箱替代【免费下载链接】pandas-aiChat with your database or your datalake (SQL, CSV, parquet). PandasAI makes data analysis conversational using LLMs and RAG.项目地址: https://gitcode.com/GitHub_Trending/pa/pandas-ai如果你在 v2 里用custom_whitelisted_dependencies给 PandasAI 的 LLM 代码执行加了自定义模块升级到 v3 后会发现这个配置项已经被删除——v3 明确用沙箱环境sandbox environment来处理代码执行安全。这篇文章面向正在做 v2 到 v3 迁移的开发者任务是把依赖白名单配置的旧应用改造成 v3 写法并让 LLM 生成的代码在 Docker 沙箱中运行。适用前提系统上已安装并在运行 Docker项目里已配置好 LLM文中示例使用pandasai-litellm扩展。custom_whitelisted_dependencies 在 v2 与 v3 中的差别v2 的默认白名单模块为pandas、numpy、matplotlib、seaborn、datetime、json、base64实例化Agent时可通过custom_whitelisted_dependencies参数追加模块模块必须已安装在 PandasAI 运行的环境中见 v2 自定义白名单文档。v2 文档同时给出了关键限制PandasAI cannot sandbox arbitrary code execution for custom libraries that are whitelisted.也就是说被加入白名单的库等于完全不受沙箱约束可以执行任意代码。这正是 v3 移除该配置项的原因。v3 的 迁移指南 在“Breaking Changes / Configuration”一节列出了被删除的配置项save_charts, enable_cache, security, custom_whitelisted_dependencies, save_charts_path, custom_head并说明了替代方向“Security handled through sandbox environment”。迁移步骤的 Step 7 也要求把custom_whitelisted_dependencies、security等旧配置从代码中删掉只保留 v3 的全局配置pai.config.set({ llm: llm, save_logs: True, verbose: False, max_retries: 3 })所以迁移路径是固定的两步删掉旧白名单配置改用 Docker 沙箱承载 LLM 生成代码的执行。准备条件按 隐私与安全文档 和 Agent 文档 的要求使用前需要本机安装 Docker 并确保它正在运行文档明确要求Make sure you have Docker running on your system before using the sandbox environment。安装 PandasAI 本体和 LLM 扩展pip install pandasai pandasai-litellm安装沙箱扩展pip install pandasai-docker沙箱扩展的 README 也给出 poetry 安装方式poetry add pandasai-docker。配置全局设置并启用沙箱v3 的配置是全局的pai.config.set()对所有 DataFrame 生效不再有 per-dataframe 的config参数。完整的最小示例来自 隐私与安全文档import pandasai as pai from pandasai_docker import DockerSandbox from pandasai_litellm.litellm import LiteLLM # Initialize LiteLLM with your OpenAI model llm LiteLLM(modelgpt-4.1-mini, api_keyYOUR_OPENAI_API_KEY) # Configure PandasAI to use this LLM pai.config.set({ llm: llm }) # initialize the sandbox sandbox DockerSandbox() sandbox.start() # read a csv as df df pai.read_csv(./data/heart.csv) # pass the df and the sandbox result pai.chat(plot total heart patients by gender, df, sandboxsandbox) # display the chart result.show() # stop the sandbox (docker container) sandbox.stop()代码中需要替换的只有两处api_key换成你自己的 LLM 密钥./data/heart.csv换成你要分析的本地 CSV 相对路径仓库 examples/data/heart.csv 有一份同名样例数据可参考。各步骤的判断依据pai.config.set()是 v3 唯一的全局配置入口若配置不生效迁移排查文档 指出应确认改用pai.config.set(config)而不是 v2 的SmartDataframe(data, configconfig)写法pai.chat(..., sandboxsandbox)传入沙箱后LLM 生成的代码在隔离的 Docker 容器内执行而不是在宿主机上直接跑result.show()能正常渲染出图表说明这次带沙箱的问答走通了用完后必须调用sandbox.stop()停掉容器否则会遗留一个常驻的 sandbox 容器。文档列出的沙箱特性包括代码在完全隔离的 Docker 容器内执行Isolated Execution、沙箱全程离线运行、阻断任何外部网络请求Offline Operation、严格的系统资源控制Resource Limitations、文件系统隔离File System Isolation。离线这一点要注意沙箱内的代码无法访问外网如果你的分析代码原本依赖下载资源这条路不成立。文档建议在这些场景下使用沙箱构建面向公众的应用、处理不可信的用户输入、生产环境部署、处理敏感数据、多租户环境。在 Agent 中替代 v2 的白名单配置如果你的 v2 代码是通过Agent(df, config{custom_whitelisted_dependencies: [...]})挂白名单的v3 对应写法是把沙箱传给Agent示例来自 Agent 文档from pandasai import Agent from pandasai_docker import DockerSandbox # Initialize the sandbox sandbox DockerSandbox() sandbox.start() # Create an agent with the sandbox df pai.read_csv(data.csv) agent Agent([df], sandboxsandbox) # Chat with the agent - code will run in the sandbox response agent.chat(Calculate the average sales) # Dont forget to stop the sandbox when done sandbox.stop()Agent本身在 v3 中保持不变只是移除了clarification_questions()、rephrase_query()、explain()三个方法多轮场景用follow_up()代替见 迁移指南。如果默认沙箱镜像不满足需求Agent 文档给出了自定义入口指定自定义镜像名和自定义 Dockerfilesandbox DockerSandbox( custom-sandbox-name, /path/to/custom/Dockerfile )第二个参数需要你替换为自己机器上某个 Dockerfile 的路径文档没有提供具体的自定义 Dockerfile 内容只能给出签名。默认镜像里装了什么可以看扩展自带的 Dockerfile基础镜像为python:3.9只安装了pandas、numpy、matplotlib工作目录/app容器命令为sleep infinity保持存活。对照 v2 的默认白名单含seaborn、datetime、json、base64可以据此判断你的分析代码能否直接在默认沙箱里跑依赖pandas/numpy/matplotlib的没问题依赖其他第三方库的要么改代码要么按上一节的方式换成自建 Dockerfile。另外沙箱代码中执行 SQL 时查询结果会被转成 CSV 文件传入容器处理见 docker_sandbox.py 中transfer_file相关实现SQL 查询本身在宿主侧完成这一点与“容器完全离线”并不矛盾。验证迁移结果迁移指南给出了迁移后的验证用例可直接作为沙箱改造后的回归测试Basic Chat Testimport pandasai as pai import pandas as pd df pd.DataFrame({x: [1, 2, 3], y: [4, 5, 6]}) df pai.DataFrame(df) response df.chat(What is the sum of x?) print(response)判断标准代码里不再出现custom_whitelisted_dependencies、security、enable_cache等 v2 配置项且用pai.config.set()完成全局配置后程序无异常带sandboxsandbox的pai.chat/agent.chat能返回结果result.show()能展示图表结束时sandbox.stop()成功停掉容器无遗留的 sandbox 容器。若出现ModuleNotFoundError: No module named pandasai.llm这是 v2 导入路径残留安装pandasai-litellm并改用 LLM 扩展 的导入方式即可具体对照见 迁移排查文档。限制沙箱是 v3 的替代方案但它不等同于 v2 的“白名单 信任库”模式默认镜像只带pandas、numpy、matplotlibv2 白名单里的seaborn等模块不会自动出现在沙箱中。沙箱容器离线运行容器内代码不能发起外部网络请求。使用沙箱前 Docker 必须处于运行状态文档未提供更细的错误排查顺序遇到容器级报错时先确认 Docker 服务状态与pandasai-docker是否安装成功。生产环境若需要自定义安全策略、高级资源管理、增强监控等能力文档将其归为企业版Enterprise license提供的沙箱选项见 隐私与安全文档 的 Enterprise Sandbox Options 一节。【免费下载链接】pandas-aiChat with your database or your datalake (SQL, CSV, parquet). PandasAI makes data analysis conversational using LLMs and RAG.项目地址: https://gitcode.com/GitHub_Trending/pa/pandas-ai创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表