Pi Agent: Prompt Templates
最終更新:2026-08-31
--- title: "プロンプトテンプレート" description: "Pi Agentのプロンプトテンプレートシステムをマスターし、再利用可能でパラメータ化可能なプロンプトを作成してAgentの出力品質を向上させます。" order: 10 lang: ja
良いプロンプトは良いレシピのようなもの——従えば、毎回うまくいく。
1. なぜプロンプトテンプレートか
プロンプトを直接書く場合の問題:
- 似たタスクで似たプロンプトを繰り返す
- プロンプトの品質にばらつきがある
- チーム間でベストプラクティスを共有しにくい
プロンプトテンプレートが解決します:
| 特徴 | 説明 |
|---|---|
| 再利用可能 | 一度定義すれば何度でも呼び出し可能 |
| パラメータ化可能 | 異なるシナリオで変数置換 |
| 共有可能 | チームテンプレートライブラリ |
| テスト可能 | テンプレートを独立してテスト・最適化 |
2. 組み込みテンプレート
| テンプレート | 用途 |
|---|---|
| code_review | コードレビュー |
| bug_fix | バグ修正 |
| test_gen | テスト生成 |
| doc_gen | ドキュメント生成 |
| translate | 翻訳 |
| summarize | 要約 |
| refactor | リファクタリング |
PYTHON
from pi_agent import Agent
agent = Agent(name="reviewer")
result = agent.run(template="code_review", code="def add(a,b): return a+b")
3. カスタムテンプレート
(1) YAMLテンプレートファイル
~/.pi-agent/templates/code_explain.yamlを作成:
YAML
name: code_explain
description: "Explain code functionality and principles"
system: |
You are a code explanation expert. Explain code in simple language,
including: 1) Function overview 2) Key logic 3) Potential issues
template: |
Please explain the following {{ language }} code:
```{{ language }}
{{ code }}
Requirements:
- Explain functionality in plain language
- Mark key logic points
- Point out potential issues (if any) variables:
- name: language type: string required: true description: "Programming language name"
- name: code type: string required: true description: "Code to explain"
### (2) Pythonテンプレート定義
```python
from pi_agent import PromptTemplate
explain_template = PromptTemplate(
name="code_explain",
description="Explain code functionality and principles",
system="You are a code explanation expert",
template="Please explain the following {language} code:\n{code}",
variables=["language", "code"]
)
agent = Agent(name="explainer")
agent.add_template(explain_template)
result = agent.run(
template="code_explain",
language="python",
code="def fib(n): return n if n < 2 else fib(n-1) + fib(n-2)"
)
4. テンプレート変数と条件分岐
(1) 基本変数
YAML
template: |
Translate the following text from {{ source_lang }} to {{ target_lang }}:
{{ text }}
(2) 条件付きレンダリング
YAML
template: |
Please review the following code:
{{ code }}
{% if focus_area %}
Focus on {{ focus_area }} aspects.
{% endif %}
{% if severity == "strict" %}
Please review strictly, list all issues.
{% else %}
Only list critical issues.
{% endif %}
(3) ループ
YAML
template: |
Check if the following files conform to coding standards:
{% for file in files %}
- {{ file }}
{% endfor %}
5. テンプレートの合成
例1:マルチテンプレートワークフロー(難易度:⭐⭐)
PYTHON
from pi_agent import Agent, PromptTemplate
review_template = PromptTemplate(
name="review",
template="Review code: {code}\nFocus: {focus}"
)
fix_template = PromptTemplate(
name="fix",
template="Fix code based on review: {review_result}\nOriginal: {code}"
)
agent = Agent(name="pipeline")
code = "def add(a, b): return a + b"
review_result = agent.run(template="review", code=code, focus="security")
fix_result = agent.run(template="fix", review_result=review_result, code=code)
6. テンプレートのテストと最適化
(1) A/Bテスト
PYTHON
from pi_agent import Agent
template_a = "Review this code: {code}"
template_b = "As a senior engineer, strictly review this code for security, performance, and readability: {code}"
agent = Agent(name="ab_test")
result_a = agent.run(template=template_a, code=sample_code)
result_b = agent.run(template=template_b, code=sample_code)
❓ よくある質問
Q テンプレート変数とPythonのf-stringsの違いは?
A テンプレート変数は
{{ var }}(Jinja2スタイル)を使用し、実行時に条件分岐やループ付きでレンダリングされます。f-stringsはPython構文で、設定ファイルに保存できません。Q 多数のテンプレートを管理するには?
A ディレクトリで整理:
templates/coding/、templates/writing/、templates/review/。Pi Agentは全サブディレクトリを再帰的にスキャンします。Q チームとテンプレートを共有する最適な方法は?
A テンプレートディレクトリをGitリポジトリに置き、各メンバーが
~/.pi-agent/templates/にクローン。またはPi Agentパッケージとして公開。📖 まとめ
- プロンプトテンプレートは再利用性、一貫性、共有の問題を解決
- YAMLファイルとPython定義の両方をサポート
- 変数は条件付きレンダリングとループをサポート
- 複数テンプレートでワークフローを合成可能
- A/Bテストとスコアリングでテンプレート品質を継続的に改善
📝 練習問題
- 基礎(難易度:⭐): source_langとtarget_langパラメータを持つシンプルな翻訳テンプレートを作成してください。
- 中級(難易度:⭐⭐): severityに基づく条件付きレンダリングを持つコードレビューテンプレートを作成してください。
- 上級(難易度:⭐⭐⭐): 完全な開発テンプレートセット(review → fix → test)を設計し、その有効性をテストしてください。