第 2 讲

AI 编程基础

📅 2025 年 9 月 26 日 👨‍🏫 Mihail Eric 🏫 斯坦福大学

核心主题

Prompting 是 AI 时代的编程接口

"Prompts are the lingua franca of LLMs." — Andrej Karpathy

Prompting 技术分层

基础范式(3 类)

类型 说明 适用场景
Zero-shot 无示例直问 简单任务、通用知识
K-shot 提供 k 个示例 代码风格对齐、领域术语
Chain-of-Thought 显式要求推理链 数学证明、算法设计

高级技术(4 类)

类型 说明 适用场景
Self-consistency 多路径采样 + 投票聚合 调试根因分析
Tool Use 调用真实工具(如 pytest) CI/CD、自动化测试
RAG 检索增强生成 安全关键功能
Reflexion 多轮自省修正 复杂错误修复

工程化最佳实践

1. 结构化输入

<log>
Error: list index out of range
File "main.py", line 42
</log>

<code_snippet>
for i in range(len(users)):
    print(users[i])
</code_snippet>

2. 显式约束

  • 使用 Python 3.10+
  • 只用标准库
  • 遵循 PEP 8 规范
  • 添加类型注解
  • 包含单元测试

3. 角色扮演(Role Prompting)

You are a helpful assistant specialized in software development
at the level of a senior software developer.
You write clean, efficient, and well-documented code.

4. 清晰性原则

"Give to a human with minimal context — if they're confused, LLM will be too."

💡 对 Vibe Coding 学员的启示

  • Prompting 是核心技能:每天练习 Prompting 技巧,建立自己的 Prompt 库
  • 技术栈对应:第 1 课(Zero-shot)、第 2 课(K-shot)、第 3 课(CoT)、第 5 课(Tool Use)
  • Vibe Coding 培养:从"写代码"转变为"设计提示",培养"好品味"(Good Taste)