django框架的联创simonwillison近日分享了他用AI编程的实践经验,包括用函数签名精准的描述需求、及时测试AI编写的代码、vibe coding学习新的框架、让AI总结代码库为架构文档等。

用函数签名描述函数接口
函数签名包含函数名称、参数类型、返回类型等,对于AI编程,开发者先设计函数签名,之后描述函数的内容(步骤以及每步的异常处理),可以更有效的引导LLM去按预期实现具体的功能,例如:
Write a Python function that uses asyncio httpx with this signature:
async def download_db(url, max_size_bytes=5 * 1025 * 1025): -> pathlib.Path
Given a URL, this downloads the database to a temp directory and returns a path to it.
BUT it checks the content length header at the start of streaming back that data and, if it’s more than the limit, raises an error. \
When the download finishes it uses sqlite3.connect(...) and then runs a PRAGMA quick_check to confirm the SQLite data is valid—raising an error if not.
Finally, if the content length header lies to us— if it says 2MB but we download 3MB—we get an error raised as soon as we notice that problem
养成手工测试的习惯
大模型由于幻觉的存在,写的代码不一定正确,所以每次让AI完成代码后,最好手动编写单元测试进行质检,测试代码是否真的能正常工作。
用氛围编程学习
Andrej Karpathy在2月份提出的vibe coding(氛围编程):
There’s a new kind of coding I call “vibe coding”, where you fully give in to the vibes, embrace exponentials, and forget that the code even exists. […] I ask for the dumbest things like “decrease the padding on the sidebar by half” because I’m too lazy to find it. I “Accept All” always, I don’t read the diffs anymore. When I get error messages I just copy paste them in with no comment, usually that fixes it.
在周末让AI编写一些完整的小项目,从中了解新的编程语言和框架,是一个好的学习方式。
让AI总结代码库
Simon Willison开发了一个让AI总结某个项目codebase的工具files-to-prompt
,利用gemini-2.0-pro-exp-02-05
,免费的API,让Gemini总结输入项目的源代码架构,输入的提示词为:architectural overview as markdown
。
cd /tmp
git clone https://github.com/Y2Z/monolith
cd monolith
files-to-prompt . -c | llm -m gemini-2.0-pro-exp-02-05 \
-s 'architectural overview as markdown'
AI输出的文档总结了项目的架构、数据流转过程、各个模块之间的依赖关系、如何部署、项目的优势和潜在提升项,借此可以快速了解一个代码仓库的原理。
例如让AI总结Monolith工具的codebase,输出的文档介绍了High-Level Structure、Data Flow、Key Dependencies、Build and Deployment、Strengths和Potential Improvements:
files-to-prompt: codebase转为提示词
上文提到的总结代码库的工具 files-to-prompt
大部分的代码也是simonwillison让claude3.5写的。该工具的主要原理是把代码库下的所有文件拼接成一个提示词,提供给LLM总结。同时还配置了文件拼接时只包含特定扩展名的文件、包含隐藏文件、忽略特定模式的文件等选项。
simonwillison与claude3.5交互的流程是:
- 明确需求,构建初始工具。在对话开始时明确说明工具的目标和功能,帮助模型快速理解任务。
cookiecutter gh:simonw/click-app
[1/6] app_name (): files-to-prompt
[2/6] description (): Concatenate a directory full of files into a single prompt for use with LLMs
[3/6] hyphenated (files-to-prompt):
[4/6] underscored (files_to_prompt):
[5/6] github_username (): simonw
[6/6] author_name (): Simon Willison
- 提供具体示例。在必要时,输入代码片段或具体案例,让模型进行补全或参考。帮助模型更准确地调整输出。
例如输入代码片段,让模型补全代码:
import click
@click.command()
@click.argument(
"path",
type=click.Path
)
@click.option(
"--include-hidden",
is_flag=True,
help="Include files and folders starting with .",
)
@click.version_option()
def cli(path, include_hidden):
"""
Takes a path to a folder and outputs every file in that folder,
recursively, each one preceeded with its filename like this:
path/to/file.py
----
Contents of file.py goes here
---
path/to/file2.py
---
...
"""
finish this code for me
- 逐步迭代。通过多次对话和反馈,逐步完善代码。
- 检查与验证。仔细审查模型生成的代码,发现问题后及时反馈,确保最终代码的正确性。
ref
https://simonwillison.net/2025/Mar/11/using-llms-for-code/
https://simonwillison.net/2024/Apr/8/files-to-prompt/
https://gist.github.com/simonw/2c80749935ae3339d6f7175dc7cf325b
https://gist.github.com/simonw/3492bc9a6ff639ff7fbaa081fa508131
https://gist.github.com/simonw/5b7ce53bf85ba1403af81e3be83ca32f