构建您自己的本地 AI 代理(第 3 部分):代码考古学家 🔦

发布: (2026年1月7日 GMT+8 20:00)
3 min read
原文: Dev.to

Source: Dev.to

概览

在第 2 部分我们查询了数据。现在我们要为代码添加文档。

你可能有一个像 legacy_math.py 的文件,里面没有注释,变量名也很通用(xyz)。直接修改它会让人觉得风险很大。

这时 私有代码考古学家 登场——它会读取你的代码,理解它,并在本地为其添加专业的 Google 风格的 docstring。

代理的工作流程

  1. 读取目标文件。
  2. 找出没有文档的函数。
  3. 生成描述参数和返回值的 Google 风格 docstring。
  4. 用新内容覆盖文件 且不改变任何逻辑

为什么要在本地运行?

你可能不想把专有算法粘贴到像 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 数据

Back to Blog

相关文章

阅读更多 »

重构你的简历

简历生成器 - FlowCV – 我个人最喜欢的。它免费、现代,而且布局非常不易被“破坏”。 - Standard Resume – 极简主义,高可读性布局……