热点dev.to #ai新闻
利用Claude Skill运行脚本以确保数据汇总准确
针对大模型在长列表算术计算上的不可靠性,介绍了一种使用Claude Skill捆绑脚本的解决方案。通过SKILL.md定义技能,将脚本与指令绑定并预批准执行,让代码负责计算、模型负责解释,从而保证结果准确性。这是一种实用的工程实践模式。
语言模型在处理长列表的算术运算时并不可靠。通过 Claude 技能(Skill),可以将脚本打包在一起,让计算结果由代码生成,而 Claude 仅负责解释这些数据。
非官方产品。与 Anthropic 无关,也未获其认可。
模式
一个技能是一个包含 SKILL.md 的文件夹。将脚本放在旁边,预先批准确切的命令,并指示 Claude 运行它:
---
name: invoice-aging-report
description: "Build an invoice aging report from a CSV of invoices. Use when the user wants to see who owes them money."
allowed-tools: Bash(python3 ${CLAUDE_SKILL_DIR}/scripts/aging.py *) Read
---
1. The CSV needs columns invoice, customer, due, amount, paid.
2. Run `python3 ${CLAUDE_SKILL_DIR}/scripts/aging.py <file.csv> --as-of YYYY-MM-DD`.
3. Show the bucket totals, then draft a polite chase message for each overdue customer.${CLAUDE_SKILL_DIR} 会扩展为技能的文件夹路径,因此无论技能位于 ~/.claude/skills/ 还是插件中,相同的文本都能正常工作。
脚本的核心
from datetime import datetime
from decimal import Decimal
def bucket(days):
return "Current" if days <= 0 else "1-30" if days <= 30 else "31-60" if days <= 60 else "61-90" if days <= 90 else "90+"
def age(rows, as_of):
totals = {}
for r in rows:
owed = Decimal(r["amount"]) - Decimal(r["paid"] or "0")
if owed <= 0:
continue
days = (as_of - datetime.strptime(r["due"], "%Y-%m-%d").date()).days
totals[bucket(days)] = totals.get(bucket(days), Decimal("0")) + owed
return totals测试边界情况,而不仅仅是正常路径
账龄报告出错的地方往往在于逾期正好 30 天或 31 天的发票。请测试这些情况:0 天属于“当前”,1 天和 30 天属于“1-30”,31 天和 60 天属于“31-60”,91 天属于“90+”。还要测试部分支付的发票(只计算余额)和无法读取的行(报告错误,但继续处理)。
为什么要这么做
模型决定何时调用脚本以及如何措辞催款邮件;脚本则保证数字的准确性。同样的模式也适用于将银行 CSV 文件分类到不同类别中。
这个经过测试的完整版本(包括账龄报告、费用分类器以及营销、招聘和房地产领域的 11 个其他技能)可在《小型企业 Claude 技能包》中找到。相关资源:我们商店里的《Claude Code 技能包》。
由 CyberMax 自动发布。上述所有数据均来自链接的公开数据;没有任何估算内容。