Pi实战 02:提示词与技能篇——把重复工作固化成命令
来源:Pi 官方 Prompt Templates / Skills 文档,以及社区热门技能(impeccable、mattpocock/skills)。
如果你每天都在重复同样的指令——“先写测试”“按这个模板建 API”“帮我 review 这个 PR”——那就别每次手打。Pi 有两种固化方式:
- Prompt Templates(提示词模板):把一段提示词变成
/xxx斜杠命令,支持参数。适合"一句话触发一段固定流程"。 - Skills(技能):自包含的能力包(
SKILL.md+ 脚本 + 资源),按需加载。适合"一整套带脚本的工作流"。
一句话区别(composio.dev 总结得很准):Skills teach the agent(技能教会 Agent),Extensions extend the agent(扩展扩展 Agent)。
一、Prompt Templates:自定义/xxx命令
1. 创建一个模板
mkdir-p~/.pi/agent/promptsvim~/.pi/agent/prompts/review.md文件内容(frontmatter + 正文):
--- description: Review the last change --- Review the most recent changes in this repo. Focus on: 1. Correctness and edge cases 2. Security issues (injection, auth, secrets) 3. Readability Point out problems only — don't rewrite unless I ask.用法:在输入框敲/review,自动补全会显示描述,回车即展开。
2. 带参数的模板
Pi 的位置参数语法和 shell 很像:
| 语法 | 含义 |
|---|---|
$1$2 | 第 1、2 个位置参数 |
$@ | 所有参数(整段剩余输入) |
$ARGUMENTS | 同$@ |
${1:-default} | 带默认值的参数 |
${3:+text} | 仅当第 3 个参数存在时才插入text |
${@:N}/${@:N:L} | 从第 N 个起 / 取 N 开始长度为 L |
官方示例 A:调试命令/debug
--- description: Debug an issue --- Debug this issue: $@ Steps: 1. Identify the root cause 2. Explain why it happens 3. Propose a fix 4. Consider edge cases 5. Suggest prevention strategies用法:/debug "Login fails when username contains spaces"
官方示例 B:建组件/component
--- description: Create a component --- Create a React component named $1 with features: $@用法:/component Button "onClick handler" "disabled support"
官方示例 C:条件参数${3:+...}
--- description: Create API endpoint --- Create a $1 API endpoint at $2${3:+ with authentication}. Implement: - Request validation - Error handling - Response formatting ${3:+- Authentication checks}${3:+ with authentication}只有在提供了第 3 个参数时才会插入 “with authentication”。
官方示例 D:默认值${1:-7}
--- description: Summarize state --- Summarize the current state in ${1:-7} bullet points.不传参数就用 7 条;传了就覆盖。
3. 用完后校验
/validate-prompts这个命令会重新加载项目和全局提示词目录,校验 frontmatter、include 路径、循环引用、保留命令名、以及技能引用能否解析。改完模板跑一下,避免拼错导致命令失效。
4. 懒得手写?让 Pi 自己造
在会话里直接说:
“Create a custom prompt for code review. Make it detailed and very thorough.”
Pi 会读它自己的 prompt-template 文档,然后生成一个符合规范的.md文件。生成后再按需改参数即可。
二、Skills:按需加载的能力包
1. Skills 怎么工作
- 启动时 Pi 扫描技能目录,提取每个技能的
name和description。 - 这些元信息以 XML 形式塞进系统提示词。
- 当任务匹配某个技能时,Agent 用
read(或bash)加载完整SKILL.md来执行。- ⚠️ 模型不一定主动读
SKILL.md。两种办法强制它读:在提示词里明确要求,或直接/skill:name。
- ⚠️ 模型不一定主动读
2. 命名规则(容易踩坑)
- 1–64 字符,仅小写字母、数字、连字符
- - 不能以
-开头或结尾 - 不能连续
-
✅ pdf-processing >3. 一个 SKILL.md 长什么样--- name: my-skill description: What this skill does and when to use it. Be specific. --- # My Skill ## Setup Run once before first use: ```bash cd /path/to/skill && npm install
Usage
./scripts/process.sh<input>
See the reference guide for details.
> 关键:**用相对路径引用脚本和资源**,这样技能目录挪位置也能跑。 ### 4. 社区热门技能(可直接装) **impeccable** —— 给 Pi 一套真正的前端设计系统,而不是空泛的"让它好看点"。做前端的必装。 **mattpocock/skills** —— 引入正规 TDD 工作流、鼓励质疑弱假设、先推理再实现。安装: ```bash npx skills@latest add mattpocock/skills -a pi -g
(-a pi指定给 Pi,-g全局安装)
让 Pi 自己建技能:直接说"为我的场景建个技能",它连SKILL.md、脚本、文档一起生成。
三、实战组合:把"建 API"变成一条龙
假设你天天建 REST 端点。配一个/endpoint模板:
--- description: Scaffold a REST endpoint --- Scaffold a $1 endpoint at $2. 1. Add route + handler 2. Validate input with Zod 3. Write one happy-path and one error test 4. Run: npm run typecheck && npm test
用法:/endpoint "GET" "/users/:id/handler"—— 一次触发"写代码 + 校验 + 测试 + 跑检查"整条链,不用分四次说。
四、避坑小结
- 模板名 = 命令名,别和内置命令撞名(
/validate-prompts会报 reserved command names)。 - 技能描述要具体写"什么时候用",否则 Agent 匹配不到。
- 技能默认不主动加载全文,记得用
/skill:name或提示词强制读取。 - 模板/技能改完,
/reload或/validate-prompts确认生效。
上一篇:01 · 配置篇 | 下一篇:03 · 扩展篇