Codex: أفضل ممارسات Prompt في Codex
آخر تحديث: 2026-08-31
الـ Prompts هي جسرك إلى Codex. الـ Prompts الجيدة تجعل Codex يصيب من المرة الأولى؛ السيئة تتطلب تصحيحات متكررة.
📋 المتطلبات المسبقة: فهم العمليات الأساسية لـ Codex
1. ما ستتعلمه
- مبادئ Prompts الفعّالة الأساسية
- طرق الوصف المهيكل
- تقنيات توفير السياق
- قوالب Prompts الشائعة
2. المبادئ الأساسية
(1) مبدأ CLEAR
| المبدأ | الوصف | المثال |
|---|---|---|
| Context | وفر سياقًا | "اتبع أسلوب auth.py" |
| Limit | حدد النطاق | "عدّل فقط دليل src/api/" |
| Example | قدّم أمثلة | "صيغة الإخراج وفق types.ts" |
| Assert | ضمّن التحقق | "تأكد من نجاح npm test" |
| Reason | اشرح السبب | "لأن دعم Unicode مطلوب" |
(2) مقارنة بين الجيد والسيئ
TEXT
📖 للعرض فقط
# Bad Prompt
"Help me write a function"
# Good Prompt
"In src/utils/date.ts, create a formatDate function,
input: Date object and locale string,
output: localized date string,
follow the style of formatNumber in the same file,
include null handling and unit tests."
3. الوصف المهيكل
(1) الهيكل القياسي
TEXT
📖 للعرض فقط
[Goal]: What to accomplish
[Scope]: Which files/directories to modify
[Reference]: Which existing code to reference
[Constraints]: What rules to follow
[Verification]: How to verify completion
▶ مثال 1: Prompt المهيكل لأليس
TEXT
📖 للعرض فقط
[Goal] Add user password reset feature
[Scope] src/auth/reset.ts, src/api/reset.ts
[Reference] Code style of src/auth/login.ts
[Constraints] Use bcrypt hashing, token expires in 1 hour
[Verification] npm test -- --grep "reset" all pass
(2) تعليمات خطوة بخطوة
للمهام المعقدة، صِف على شكل خطوات:
TEXT
📖 للعرض فقط
Step 1: Create PasswordReset model with token, expires_at, user_id fields
Tell me when done, I'll confirm before continuing to step 2.
Step 2: Implement POST /api/auth/reset-request endpoint
Step 3: Implement POST /api/auth/reset-confirm endpoint
Step 4: Write integration tests for all endpoints
4. تقنيات توفير السياق
(1) إشارات دقيقة
TEXT
📖 للعرض فقط
# Good: reference specific files
> Follow the User type definition in src/models/user.ts, add Profile type
# Bad: vague description
> Follow existing models, add new ones
(2) إرفاق معلومات إضافية
TEXT
📖 للعرض فقط
# Attach images in the App
> Implement this page following the UI layout in the screenshot
# Reference error logs
> Output from cat error.log:
> TypeError: Cannot read property 'id' of undefined
> Fix this error
(3) وصف البيئة
TEXT
📖 للعرض فقط
> Project uses Next.js 14 + App Router + TypeScript
> Database uses Prisma ORM + PostgreSQL
> Test framework is Vitest
> Add CRUD functionality for the new page
5. تعليمات التحقق
(1) التحقق بالاختبار
TEXT
📖 للعرض فقط
# Require running tests
> Ensure npm test passes
> Run pytest, fix all failing cases
> Test coverage no less than 80%
(2) فحص الأنواع
TEXT
📖 للعرض فقط
> Ensure tsc --noEmit has no errors
> Add complete TypeScript type annotations
(3) فحص Lint
TEXT
📖 للعرض فقط
> Ensure eslint has no errors
> Fix all lint warnings
▶ مثال 2: التحقق الكامل لبوب
TEXT
📖 للعرض فقط
Add pagination to the orders API:
1. Modify GET /api/orders endpoint
2. Support page and pageSize query parameters
3. Return pagination metadata
4. Ensure tsc --noEmit has no errors
5. Ensure eslint has no errors
6. npm test all pass
7. New tests covering pagination logic
6. قوالب Prompts الشائعة
(1) إصلاح Bug
TEXT
📖 للعرض فقط
Fix <Bug-description> in <file>.
Error message: <paste-error-log>
Expected behavior: <correct-behavior>
Ensure <verify-command> passes.
(2) إضافة ميزة
TEXT
📖 للعرض فقط
Add <feature> to <module>.
Follow the code style of <existing-file>.
Include input validation and error handling.
Write unit tests, ensure <verify-command> passes.
(3) إعادة هيكلة الكود
TEXT
📖 للعرض فقط
Refactor <old-impl> to <new-impl>.
Maintain the external interface.
Ensure all existing tests pass.
Add new tests covering the new implementation.
(4) مراجعة الكود
TEXT
📖 للعرض فقط
Review <file/PR>, focusing on:
1. Security vulnerabilities (SQL injection, XSS, etc.)
2. Performance issues (N+1 queries, memory leaks, etc.)
3. Code style consistency
4. Error handling completeness
5. Test coverage
(5) توليد التوثيق
TEXT
📖 للعرض فقط
Generate documentation for <module/API>:
- API interface docs (Markdown format)
- Usage examples (including curl commands)
- Parameter description table
- Error code list
7. تقنيات متقدمة
(1) تعيين الدور
TEXT
📖 للعرض فقط
You are a senior security engineer specializing in Web security.
Review the following code for security vulnerabilities, categorized by OWASP Top 10.
(2) التحكم في صيغة الإخراج
TEXT
📖 للعرض فقط
Output in the following format:
1. One issue per line
2. Format: [Severity] file:line - Issue description
3. Severity: 🔴Critical 🟡Medium 🟢Low
4. Finally, provide a fix priority ranking
(3) شروط القيود
TEXT
📖 للعرض فقط
Only use dependencies already in the project, do not install new packages.
Do not modify .env files.
Do not delete existing tests.
All new functions must have TypeScript type annotations.
❓ أسئلة شائعة
س هل يجب أن تكون Prompts باللغة الإنجليزية؟
ج يدعم Codex لغات متعددة، لكن الإنجليزية عادة تعمل بشكل أفضل. المهام المعقدة يجب تستخدم الإنجليزية؛ المهام البسيطة يمكن استخدام لغتك المفضلة.
س هل تؤثر Prompts الطويلة على النتائج؟
ج لا. الأوصاف التفصيلية تعمل بشكل أفضل من المختصرة. فقط احذر من تجاوز نافذة السياق.
س هل يمكنني تقديم مهام متعددة دفعة واحدة؟
ج نعم، لكن رتب حسب الأولوية أو حدد تنفيذًا خطوة بخطوة بوضوح. مهام كثيرة دفعة واحدة قد تجعل Codex يفقد بعضها.
س كيف أجعل Codex يتبع أسلوب كود محدد؟
ج عرّف قواعد الأسلوب في AGENTS.md، أو أشر إلى ملفات الأسلوب في الـ Prompt. يمكنك أيضًا الإشارة إلى إعدادات ESLint/Prettier.
س هل يمكنني الإشارة إلى عناوين URL في Prompts؟
ج لا يمكن لـ Codex الوصول إلى عناوين URL مباشرة، لكن يمكنك نسخ المحتوى الرئيسي من عناوين URL إلى Prompt.
📖 ملخص
- مبدأ CLEAR: Context / Limit / Example / Assert / Reason
- الوصف المهيكل: هدف + نطاق + مرجع + قيود + تحقق
- توفير السياق: إشارات ملفات دقيقة + إرفاق لقطات شاشة/سجلات
- تعليمات التحقق: اختبارات + فحص أنواع + lint
- القوالب تعزز الكفاءة
📝 تمارين
- أساسي (⭐): اكتب 3 Prompts لسيناريوهات مختلفة باستخدام مبدأ CLEAR.
- متوسط (⭐⭐): أنشئ مكتبة قوالب Prompt خاصة بك تغطي 5 سيناريوهات شائعة.
- متقدم (⭐⭐⭐): قارن نفس المهمة باستخدام "Prompt جيد" مقابل "Prompt سيئ"، اكتب تقرير تحليل عن فروق التنفيذ.