跳转到内容

提示词模板

pi 可以创建提示词模板。请让它为你的工作流构建一个。

提示词模板(prompt template)是展开为完整提示词的 Markdown 片段。在编辑器中输入 /name 即可调用某个模板,其中 name 是文件名去掉 .md 后的部分。

Pi 从以下位置加载提示词模板:

  • 全局:~/.pi/agent/prompts/*.md
  • 项目内:.pi/prompts/*.md(仅在项目受信任后)
  • 包:prompts/ 目录或 package.json 中的 pi.prompts 条目
  • 设置:prompts 数组,可包含文件或目录
  • CLI:--prompt-template <path>(可重复)

使用 --no-prompt-templates 禁用模板发现。

---
description: Review staged git changes
---
Review the staged changes (`git diff --cached`). Focus on:
- Bugs and logic errors
- Security issues
- Error handling gaps
  • 文件名会成为命令名。review.md 变成 /review
  • description 为可选项。缺省时使用第一个非空行。
  • argument-hint 为可选项。设置后,该提示会显示在自动补全下拉列表中描述的前面。

使用 frontmatter 中的 argument-hint 在自动补全中显示期望的参数。必填参数使用 <尖括号>,可选参数使用 [方括号]

---
description: Review PRs from URLs with structured issue and code analysis
argument-hint: "<PR-URL>"
---

在自动补全下拉列表中的渲染效果如下:

→ pr <PR-URL> — Review PRs from URLs with structured issue and code analysis
is <issue> — Analyze GitHub issues (bugs or feature requests)
wr [instructions] — Finish the current task end-to-end
cl — Audit changelog entries before release

在编辑器中输入 / 后跟模板名。自动补全会显示可用的模板及其描述。

/review # 展开 review.md
/component Button # 带参数展开
/component Button "click handler" # 多个参数

模板支持位置参数、默认值和简单的切片:

  • $1$2、……位置参数
  • $@$ARGUMENTS:所有参数拼接
  • ${1:-default}:当 $1 存在或非空时使用,否则使用 default
  • ${@:-default}${ARGUMENTS:-default}:当所有参数存在或非空时使用全部参数,否则使用 default
  • ${@:N}:从第 N 个位置开始的参数(从 1 开始计数)
  • ${@:N:L}:从 N 开始的 L 个参数

示例:

---
description: Create a component
---
Create a React component named $1 with features: $@

可选参数使用默认值很方便:

Summarize the current state in ${1:-7} bullet points.

用法:/component Button "onClick handler" "disabled support"

  • prompts/ 中的模板发现是非递归的。
  • 如果要在子目录中使用模板,请通过 prompts 设置或包清单显式添加。