
1. 为什么我又把 AiPy 捡起来了AiPy 是一个把 LLM 和 Python 生态缝在一起的自动化工具平台你可以用自然语言描述任务它帮你生成并执行 Python 代码适合经常处理重复性工作的开发者、数据分析师和办公自动化玩家。我第一次接触它是因为每周要整理几十份日报手动合并到怀疑人生。后来发现它本质上就是一个“自然语言驱动的 Python 脚本骨架生成器”你给它一句话它给你一段能跑的代码本地执行、结果落地。但用久了会碰到一个很现实的问题AiPy 本身负责“生成代码”和“调度任务”真正干活的那段 Python 脚本里只要涉及调用大模型——比如让 LLM 帮你做文本摘要、分类、信息抽取——你就得自己配 Key、配 Base URL、处理不同模型供应商的接口差异。这时候如果每个脚本都硬编码一套 Key维护起来就是灾难。所以这篇不讲 AiPy 怎么安装、怎么点按钮而是聚焦一件事用 AiPy 搭一套自动化脚本骨架把 LLM 调用统一走一个 Key 通道。文件整理、数据抓取、批量处理这三类日常重复任务我都会给出可复制的 config.toml 配置和 Python 调用示例最后附一条本地运行验证命令确认脚本能跑通。2. TaoToken 前置统一 Key 与 API 通道AiPy 生成的脚本里LLM 调用部分如果直接写死某个厂商的地址和 Key换模型、换账号、多脚本共用时就非常痛苦。我试过在每个脚本里单独维护配置结果就是 Key 散落在十几个文件里改一次要全局搜索替换。TaoToken 在这里的角色是统一 API 通道你只需要一个 Key、一个 Base URL就能在 AiPy 生成的 Python 脚本里调用不同的大模型。官网地址是 https://taotoken.net/?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_content API 入口是 https://taotoken.net/api 。具体操作路径先到控制台创建 API Keyhttps://taotoken.net/console?utm_sourcetaotoken_aicg_blog_endutm_contentconsoleutm_campaignrewriteKey 管理页面在https://taotoken.net/api-keys?utm_sourcetaotoken_aicg_blog_endutm_contentapi-keysutm_campaignrewrite想先验证模型通不通可以用模型对话页https://taotoken.net/models?utm_sourcetaotoken_aicg_blog_endutm_contentmodelsutm_campaignrewrite接入文档参考https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_contentdocutm_campaignrewrite注意Key 只存在本地 config.toml 或环境变量里不要提交到 Git 仓库。AiPy 脚本本身是本地执行的配置文件的权限建议设成 600。如果你后面要做长期编码任务或者 Agent 类自动化可以了解 Coding Planhttps://taotoken.net/coding-plan?utm_sourcetaotoken_aicg_blog_endutm_contentcoding-planutm_campaignrewrite 。Claude Code 相关接入参考https://taotoken.net/claude-code?utm_sourcetaotoken_aicg_blog_endutm_contentclaudecodeutm_campaignrewrite 。3. 可复制配置config.toml 骨架AiPy 的脚本骨架我习惯拆成三层配置层、LLM 调用层、任务执行层。配置层用 config.toml 统一管理这样 AiPy 生成新脚本时只要复用这个配置读取逻辑就不用每次重新填 Key。先建目录结构mkdir -p aipy-auto/{config,scripts,data,logs} cd aipy-auto然后写config/config.toml# aipy-auto/config/config.toml [llm] base_url https://taotoken.net/api api_key sk-你的TaoToken密钥 model gpt-4o-mini timeout 60 max_retries 3 [task.file_organize] source_dir ./data/inbox target_dir ./data/sorted rules [by_ext, by_date] [task.data_fetch] urls [https://example.com/api/list] output ./data/fetched.json [task.batch_process] input_dir ./data/sorted output_dir ./data/processed batch_size 20 [log] level INFO path ./logs/aipy.log对应的配置读取模块scripts/config_loader.py# scripts/config_loader.py import tomllib from pathlib import Path CONFIG_PATH Path(__file__).parent.parent / config / config.toml def load_config(): with open(CONFIG_PATH, rb) as f: return tomllib.load(f) if __name__ __main__: cfg load_config() print(base_url:, cfg[llm][base_url]) print(model:, cfg[llm][model])Python 3.11 以上自带 tomllib如果是 3.10 及以下把tomllib换成tomli并pip install tomli即可。LLM 调用层scripts/llm_client.py这里统一走 TaoToken 的 API 通道# scripts/llm_client.py import requests from config_loader import load_config class LLMClient: def __init__(self): cfg load_config()[llm] self.base_url cfg[base_url].rstrip(/) self.api_key cfg[api_key] self.model cfg[model] self.timeout cfg[timeout] def chat(self, prompt, system你是一个自动化任务助手): url f{self.base_url}/v1/chat/completions headers { Authorization: fBearer {self.api_key}, Content-Type: application/json, } payload { model: self.model, messages: [ {role: system, content: system}, {role: user, content: prompt}, ], } resp requests.post(url, jsonpayload, headersheaders, timeoutself.timeout) resp.raise_for_status() return resp.json()[choices][0][message][content] if __name__ __main__: client LLMClient() print(client.chat(用一句话说明什么是文件自动整理))任务执行层以文件整理为例scripts/organize.py# scripts/organize.py import shutil from pathlib import Path from config_loader import load_config from llm_client import LLMClient def organize_by_ext(source, target): source, target Path(source), Path(target) target.mkdir(parentsTrue, exist_okTrue) moved [] for f in source.iterdir(): if f.is_file(): ext_dir target / f.suffix.lstrip(.).lower() or no_ext ext_dir.mkdir(exist_okTrue) shutil.move(str(f), str(ext_dir / f.name)) moved.append(f.name) return moved def summarize(moved): client LLMClient() prompt f以下文件已按扩展名整理请用三句话总结整理结果{moved[:20]} return client.chat(prompt) if __name__ __main__: cfg load_config()[task][file_organize] moved organize_by_ext(cfg[source_dir], cfg[target_dir]) print(f已移动 {len(moved)} 个文件) print(summarize(moved))数据抓取和批量处理的骨架同理把organize_by_ext换成requests.get抓取逻辑或者遍历目录调用LLMClient.chat做批量文本处理即可。AiPy 生成代码时你只要把这三层结构作为提示词模板丢给它生成的脚本就能直接复用配置。4. 验证请求一条命令确认跑通配置写完后先验证 LLM 通道是否通。在scripts目录下执行cd aipy-auto/scripts python llm_client.py预期输出类似文件自动整理是指通过程序按照预设规则如扩展名、日期将文件移动到对应目录的过程。如果这一步返回了正常文本说明 TaoToken 的 Key、Base URL、模型名三者匹配API 通道没问题。接着验证文件整理任务mkdir -p ../data/inbox touch ../data/inbox/{a.txt,b.pdf,c.jpg} python organize.py预期输出已移动 3 个文件 本次整理将 3 个文件按扩展名分类txt、pdf、jpg 分别归入独立目录源目录已清空。到这里一条完整的“配置读取 → LLM 调用 → 任务执行 → 结果回传”链路就跑通了。AiPy 负责生成和调度TaoToken 负责统一模型通道Python 负责实际执行三者各司其职。5. 本篇常见错排查报错一tomllib找不到。Python 3.10 及以下没有内置 tomllib报ModuleNotFoundError: No module named tomllib。解决pip install tomli然后把import tomllib改成import tomli as tomllib。报错二401 Unauthorized。大概率是 config.toml 里的 api_key 填错或者复制时带了空格。检查api_key sk-...这行确认没有多余引号嵌套。另外确认 base_url 是https://taotoken.net/api不要多加/v1因为代码里已经拼了/v1/chat/completions。报错三404 Not Found。通常是 base_url 写成了https://taotoken.net少了/api或者模型名拼错。可以到模型对话页确认当前可用的模型标识https://taotoken.net/models?utm_sourcetaotoken_aicg_blog_endutm_contentmodelsutm_campaignrewrite 。报错四文件移动后源目录没清空。检查source_dir路径是否写成了相对路径但执行目录不对。config.toml 里的路径是相对于脚本执行目录的建议统一用绝对路径或在脚本里用Path(__file__).parent.parent拼接。报错五批量处理时超时。如果一次处理几百个文件每个都调一次 LLM很容易触发 timeout。把batch_size调小或者在LLMClient里加指数退避重试。config.toml 里的max_retries目前只是占位需要在代码里实现重试逻辑。报错六Key 泄露风险。如果误把 config.toml 提交到了 Git立刻到 API Keys 页面吊销旧 Key 并重新生成https://taotoken.net/api-keys?utm_sourcetaotoken_aicg_blog_endutm_contentapi-keysutm_campaignrewrite 。同时把 config.toml 加入.gitignore。6. 接入文档与后续扩展这套骨架跑通之后扩展方向很明确文件整理可以加“按内容语义分类”数据抓取可以加“LLM 自动解析非结构化页面”批量处理可以加“多模型对比输出”。所有新增能力都只需要在scripts/下加一个任务文件复用config_loader和LLMClient不用重复配 Key。接入细节和参数说明以官方文档为准https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_contentdocutm_campaignrewrite 。如果你要长期跑编码类 Agent 任务Coding Plan 的通道更适合持续调用https://taotoken.net/coding-plan?utm_sourcetaotoken_aicg_blog_endutm_contentcoding-planutm_campaignrewrite 。最后提醒一句AiPy 生成代码后先在小样本目录上跑一遍确认移动和删除逻辑符合预期再放到正式数据上。自动化脚本最怕的不是跑不通而是跑通了但把不该动的文件动了。