Pi Agent: Prompt Templates
آخر تحديث: 2026-08-31
--- title: "قوالب الأوامر" description: "أتقن نظام قوالب أوامر Pi Agent، وأنشئ أوامر قابلة لإعادة الاستخدام وقابلة للمعلمات، وحسّن جودة مخرجات الوكيل." order: 10 lang: ar
الأوامر الجيدة مثل الوصفات الجيدة — اتبعها، وكل طبقة تخرج صحيحة.
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)
❓ أسئلة شائعة
س متغيرات القالب مقابل f-strings في Python؟
ج متغيرات القالب تستخدم
{{ var }} (أسلوب Jinja2)، تُعرض في وقت التشغيل مع شروط وحلقات. f-strings هي بناء Python ولا يمكن تخزينها في ملفات التكوين.س كيف أُدير العديد من القوالب؟
ج رتّب بالدليل:
templates/coding/، templates/writing/، templates/review/. Pi Agent يمسح جميع الأدلة الفرعية بشكل متكرر.س أفضل طريقة لمشاركة القوالب مع الفريق؟
ج ضع دليل القوالب في مستودع Git، كل شخص ينسخ إلى
~/.pi-agent/templates/. أو انشر كحزمة Pi Agent.📖 ملخص
- قوالب الأوامر تحل مشاكل إعادة الاستخدام والاتساق والمشاركة
- ملفات YAML وتعريفات Python كلاهما مدعوم
- المتغيرات تدعم العرض الشرطي والحلقات
- قوالب متعددة يمكن أن تُركّب سير عمل
- اختبار A/B والتسجيل يحسّنان جودة القالب باستمرار
📝 تمارين
- أساسي (الصعوبة: ⭐): أنشئ قالب ترجمة بسيط بمعلمات source_lang وtarget_lang.
- متوسط (الصعوبة: ⭐⭐): أنشئ قالب مراجعة كود بعرض شرطي بناءً على severity.
- متقدم (الصعوبة: ⭐⭐⭐): صمّم مجموعة قوالب تطوير كاملة (مراجعة ← إصلاح ← اختبار) واختبر فعاليتها.