构建您自己的本地 AI 代理(第 3 部分):代码考古学家 🔦
发布: (2026年1月7日 GMT+8 20:00)
3 min read
原文: Dev.to
Source: Dev.to
概览
在第 2 部分我们查询了数据。现在我们要为代码添加文档。
你可能有一个像 legacy_math.py 的文件,里面没有注释,变量名也很通用(x、y、z)。直接修改它会让人觉得风险很大。
这时 私有代码考古学家 登场——它会读取你的代码,理解它,并在本地为其添加专业的 Google 风格的 docstring。
代理的工作流程
- 读取目标文件。
- 找出没有文档的函数。
- 生成描述参数和返回值的 Google 风格 docstring。
- 用新内容覆盖文件 且不改变任何逻辑。
为什么要在本地运行?
你可能不想把专有算法粘贴到像 ChatGPT 这样的托管服务中。通过使用 Goose + Ollama,所有处理都在你的 SSD 上完成,能够保持知识产权的私密性。
代理指令(前置元数据)
title: Code Archaeologist
instructions: |
1. Read the file.
2. For every function, write a DocString explaining Args & Returns.
3. Overwrite the file with the new content.
4. Do NOT change the logic.
我们在此任务中使用了 gpt-oss:20b 模型。它成功识别出隐藏在通用变量名中的 Haversine 公式,并添加了清晰的说明。
示例
前
def f2(lat1, lon1...):
# implementation omitted
...
后(代理生成)
def f2(lat1, lon1, lat2, lon2):
"""
Compute the great‑circle distance between two points on the Earth.
Args:
lat1 (float): Latitude of the first point in degrees.
lon1 (float): Longitude of the first point in degrees.
lat2 (float): Latitude of the second point in degrees.
lon2 (float): Longitude of the second point in degrees.
Returns:
float: Distance between the two points in meters, calculated using the
haversine formula.
"""
# implementation unchanged
...
接下来
盛大的收官——第 4 部分:处理敏感的 PII 数据。