التحكم في سير عمل AI مع Flows
تمثل CrewAI Flows المستوى التالي في تنسيق AI - الجمع بين القوة التعاونية لفرق Agents AI مع دقة ومرونة البرمجة الإجرائية. بينما تتفوق Crews في تعاون الـ Agents، تمنحك Flows تحكمًا دقيقًا في كيفية ووقت تفاعل المكونات المختلفة لنظام AI.
في هذا الدليل، سنمشي عبر إنشاء CrewAI Flow قوي ينشئ دليلًا تعليميًا شاملاً حول أي موضوع.
ما يجعل Flows قوية
تمكّنك Flows من:
- الجمع بين أنماط تفاعل AI مختلفة - استخدام Crews للمهام التعاونية المعقدة واستدعاءات LLM المباشرة للعمليات الأبسط والكود العادي للمنطق الإجرائي
- بناء أنظمة قائمة على الأحداث - تحديد كيفية استجابة المكونات لأحداث وتغييرات بيانات محددة
- الحفاظ على الحالة عبر المكونات - مشاركة وتحويل البيانات بين أجزاء مختلفة من تطبيقك
- التكامل مع الأنظمة الخارجية - ربط سير عمل AI بسلاسة مع قواعد البيانات وواجهات API وواجهات المستخدم
- إنشاء مسارات تنفيذ معقدة - تصميم فروع شرطية ومعالجة متوازية وسير عمل ديناميكية
المتطلبات المسبقة
قبل البدء، تأكد من:
- تثبيت CrewAI باتباع دليل التثبيت
- إعداد مفتاح API لنموذج LLM في بيئتك، باتباع دليل إعداد LLM
- فهم أساسي لـ Python
الخطوة 1: إنشاء مشروع CrewAI Flow جديد
crewai create flow guide_creator_flow
cd guide_creator_flow
الخطوة 2: فهم هيكل المشروع
guide_creator_flow/
├── .gitignore
├── pyproject.toml
├── README.md
├── .env
├── main.py
├── crews/
│ └── poem_crew/
│ ├── config/
│ │ ├── agents.yaml
│ │ └── tasks.yaml
│ └── poem_crew.py
└── tools/
└── custom_tool.py
يوفر هذا الهيكل فصلاً واضحًا بين مكونات Flow المختلفة. سنعدّل هذا الهيكل لإنشاء Flow منشئ الدليل.
الخطوة 3: إضافة Crew كتابة المحتوى
crewai flow add-crew content-crew
الخطوة 4: تهيئة Crew كتابة المحتوى
- حدّث ملف تهيئة الـ Agents. تذكر تعيين
llm للمزود الذي تستخدمه.
# src/guide_creator_flow/crews/content_crew/config/agents.yaml
content_writer:
role: >
Educational Content Writer
goal: >
Create engaging, informative content that thoroughly explains the assigned topic
and provides valuable insights to the reader
backstory: >
You are a talented educational writer with expertise in creating clear, engaging
content. You have a gift for explaining complex concepts in accessible language
and organizing information in a way that helps readers build their understanding.
llm: provider/model-id
content_reviewer:
role: >
Educational Content Reviewer and Editor
goal: >
Ensure content is accurate, comprehensive, well-structured, and maintains
consistency with previously written sections
backstory: >
You are a meticulous editor with years of experience reviewing educational
content. You have an eye for detail, clarity, and coherence.
llm: provider/model-id
- حدّث ملف تهيئة المهام:
# src/guide_creator_flow/crews/content_crew/config/tasks.yaml
write_section_task:
description: >
Write a comprehensive section on the topic: "{section_title}"
Section description: {section_description}
Target audience: {audience_level} level learners
Your content should:
1. Begin with a brief introduction to the section topic
2. Explain all key concepts clearly with examples
3. Include practical applications or exercises where appropriate
4. End with a summary of key points
5. Be approximately 500-800 words in length
Format your content in Markdown with appropriate headings, lists, and emphasis.
Previously written sections:
{previous_sections}
expected_output: >
A well-structured, comprehensive section in Markdown format that thoroughly
explains the topic and is appropriate for the target audience.
agent: content_writer
review_section_task:
description: >
Review and improve the following section on "{section_title}":
{draft_content}
Target audience: {audience_level} level learners
Previously written sections:
{previous_sections}
Your review should:
1. Fix any grammatical or spelling errors
2. Improve clarity and readability
3. Ensure content is comprehensive and accurate
4. Verify consistency with previously written sections
5. Enhance the structure and flow
6. Add any missing key information
expected_output: >
An improved, polished version of the section that maintains the original
structure but enhances clarity, accuracy, and consistency.
agent: content_reviewer
context:
- write_section_task
- حدّث ملف تنفيذ Crew:
# src/guide_creator_flow/crews/content_crew/content_crew.py
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
from crewai.agents.agent_builder.base_agent import BaseAgent
from typing import List
@CrewBase
class ContentCrew():
"""Content writing crew"""
agents: List[BaseAgent]
tasks: List[Task]
@agent
def content_writer(self) -> Agent:
return Agent(
config=self.agents_config['content_writer'], # type: ignore[index]
verbose=True
)
@agent
def content_reviewer(self) -> Agent:
return Agent(
config=self.agents_config['content_reviewer'], # type: ignore[index]
verbose=True
)
@task
def write_section_task(self) -> Task:
return Task(
config=self.tasks_config['write_section_task'] # type: ignore[index]
)
@task
def review_section_task(self) -> Task:
return Task(
config=self.tasks_config['review_section_task'], # type: ignore[index]
context=[self.write_section_task()]
)
@crew
def crew(self) -> Crew:
"""Creates the content writing crew"""
return Crew(
agents=self.agents,
tasks=self.tasks,
process=Process.sequential,
verbose=True,
)
الخطوة 5: إنشاء Flow
الآن الجزء المثير - إنشاء Flow الذي سينسّق عملية إنشاء الدليل بالكامل. راجع الملف الإنجليزي الأصلي للكود الكامل لـ main.py حيث أن الكود يبقى كما هو.
الخطوة 6: إعداد متغيرات البيئة
أنشئ ملف .env في جذر مشروعك بمفاتيح API. راجع دليل إعداد LLM لتفاصيل تهيئة المزود.
OPENAI_API_KEY=your_openai_api_key
# or
GEMINI_API_KEY=your_gemini_api_key
# or
ANTHROPIC_API_KEY=your_anthropic_api_key
الخطوة 7: تثبيت التبعيات
الخطوة 8: تشغيل Flow
عند تشغيل هذا الأمر، ستشاهد Flow يعمل:
- سيطلب منك موضوعًا ومستوى الجمهور
- سينشئ مخططًا منظمًا لدليلك
- سيعالج كل قسم مع تعاون الكاتب والمراجع
- أخيرًا سيجمع كل شيء في دليل شامل
الخطوة 9: تصوير Flow
سينشئ ملف HTML يوضح هيكل Flow بما في ذلك العلاقات بين الخطوات المختلفة.
الخطوة 10: مراجعة المخرجات
بمجرد اكتمال Flow، ستجد ملفين في مجلد output:
guide_outline.json: يحتوي على المخطط المنظم للدليل
complete_guide.md: الدليل الشامل بجميع الأقسام
الميزات الرئيسية الموضّحة
يوضح Flow منشئ الدليل عدة ميزات قوية لـ CrewAI:
- تفاعل المستخدم: يجمع Flow مدخلات مباشرة من المستخدم
- استدعاءات LLM المباشرة: يستخدم فئة LLM لتفاعلات AI فعّالة وأحادية الغرض
- بيانات منظمة مع Pydantic: يستخدم نماذج Pydantic لضمان سلامة الأنواع
- معالجة تسلسلية مع سياق: يكتب الأقسام بالترتيب ويوفر الأقسام السابقة كسياق
- Crews متعددة الـ Agents: يستفيد من Agents متخصصة (كاتب ومراجع) لإنشاء المحتوى
- إدارة الحالة: يحافظ على الحالة عبر خطوات العملية المختلفة
- بنية قائمة على الأحداث: يستخدم مزخرف
@listen للاستجابة للأحداث
الخطوات التالية
- جرّب هياكل Flow أكثر تعقيدًا وأنماطًا
- جرّب استخدام
@router() لإنشاء فروع شرطية
- استكشف دوال
and_ وor_ لتنفيذ متوازٍ أكثر تعقيدًا
- اربط Flow بواجهات API خارجية وقواعد بيانات وواجهات مستخدم
- ادمج عدة Crews متخصصة في Flow واحد
تهانينا! لقد بنيت بنجاح أول CrewAI Flow يجمع بين الكود العادي واستدعاءات LLM المباشرة ومعالجة Crew لإنشاء دليل شامل. هذه المهارات الأساسية تمكّنك من إنشاء تطبيقات AI متطورة بشكل متزايد.