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_source=taotoken_aicg_blog_end&utm_medium=csdn&utm_campaign=rewrite&utm_content= ,API 入口是 https://taotoken.net/api 。
具体操作路径:
- 先到控制台创建 API Key:https://taotoken.net/console?utm_source=taotoken_aicg_blog_end&utm_content=console&utm_campaign=rewrite
- Key 管理页面在:https://taotoken.net/api-keys?utm_source=taotoken_aicg_blog_end&utm_content=api-keys&utm_campaign=rewrite
- 想先验证模型通不通,可以用模型对话页:https://taotoken.net/models?utm_source=taotoken_aicg_blog_end&utm_content=models&utm_campaign=rewrite
- 接入文档参考:https://taotoken.net/doc?utm_source=taotoken_aicg_blog_end&utm_content=doc&utm_campaign=rewrite
注意:Key 只存在本地 config.toml 或环境变量里,不要提交到 Git 仓库。AiPy 脚本本身是本地执行的,配置文件的权限建议设成 600。
如果你后面要做长期编码任务或者 Agent 类自动化,可以了解 Coding Plan:https://taotoken.net/coding-plan?utm_source=taotoken_aicg_blog_end&utm_content=coding-plan&utm_campaign=rewrite 。Claude Code 相关接入参考:https://taotoken.net/claude-code?utm_source=taotoken_aicg_blog_end&utm_content=claudecode&utm_campaign=rewrite 。
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": f"Bearer {self.api_key}", "Content-Type": "application/json", } payload = { "model": self.model, "messages": [ {"role": "system", "content": system}, {"role": "user", "content": prompt}, ], } resp = requests.post(url, json=payload, headers=headers, timeout=self.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(parents=True, exist_ok=True) moved = [] for f in source.iterdir(): if f.is_file(): ext_dir = target / f.suffix.lstrip(".").lower() or "no_ext" ext_dir.mkdir(exist_ok=True) 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_source=taotoken_aicg_blog_end&utm_content=models&utm_campaign=rewrite 。
报错四:文件移动后源目录没清空。检查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_source=taotoken_aicg_blog_end&utm_content=api-keys&utm_campaign=rewrite 。同时把 config.toml 加入.gitignore。
6. 接入文档与后续扩展
这套骨架跑通之后,扩展方向很明确:文件整理可以加“按内容语义分类”,数据抓取可以加“LLM 自动解析非结构化页面”,批量处理可以加“多模型对比输出”。所有新增能力都只需要在scripts/下加一个任务文件,复用config_loader和LLMClient,不用重复配 Key。
接入细节和参数说明以官方文档为准:https://taotoken.net/doc?utm_source=taotoken_aicg_blog_end&utm_content=doc&utm_campaign=rewrite 。如果你要长期跑编码类 Agent 任务,Coding Plan 的通道更适合持续调用:https://taotoken.net/coding-plan?utm_source=taotoken_aicg_blog_end&utm_content=coding-plan&utm_campaign=rewrite 。
最后提醒一句:AiPy 生成代码后,先在小样本目录上跑一遍,确认移动和删除逻辑符合预期,再放到正式数据上。自动化脚本最怕的不是跑不通,而是跑通了但把不该动的文件动了。