Spaces:
Sleeping
Sleeping
Update crew_manager.py
Browse files- crew_manager.py +18 -142
crew_manager.py
CHANGED
|
@@ -9,8 +9,7 @@ from crewai import Agent, Task, Crew, Process
|
|
| 9 |
from crewai_tools import (
|
| 10 |
FileReadTool,
|
| 11 |
DirectorySearchTool,
|
| 12 |
-
CodeDocsSearchTool
|
| 13 |
-
SerperDevTool
|
| 14 |
)
|
| 15 |
from langchain_openai import ChatOpenAI
|
| 16 |
import json
|
|
@@ -41,43 +40,28 @@ class ProjectManagerCrew:
|
|
| 41 |
root_dir=self.project_path,
|
| 42 |
description="البحث في توثيق الكود"
|
| 43 |
)
|
| 44 |
-
|
| 45 |
-
# أداة البحث على الإنترنت (تتطلب API key)
|
| 46 |
-
try:
|
| 47 |
-
self.web_search = SerperDevTool(
|
| 48 |
-
description="البحث على الإنترنت للحصول على أحدث المعلومات"
|
| 49 |
-
)
|
| 50 |
-
except:
|
| 51 |
-
self.web_search = None
|
| 52 |
-
print("⚠️ SerperDevTool غير متوفر، تأكد من إضافة SERPER_API_KEY في .env")
|
| 53 |
|
| 54 |
def setup_llm(self):
|
| 55 |
-
"""إعداد النماذج اللغوية"""
|
| 56 |
-
# استخدام Hugging Face models
|
| 57 |
hf_token = os.getenv("HF_TOKEN")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
temperature=0.7,
|
| 71 |
-
openai_api_key="sk-dummy"
|
| 72 |
-
)
|
| 73 |
|
| 74 |
def setup_agents(self):
|
| 75 |
"""إعداد فريق الوكلاء"""
|
| 76 |
|
| 77 |
# أدوات المدير
|
| 78 |
manager_tools = [self.project_analyzer, self.code_reader]
|
| 79 |
-
if self.web_search:
|
| 80 |
-
manager_tools.append(self.web_search)
|
| 81 |
|
| 82 |
# 1. الوكيل الرئيسي (المتحدث مع المستخدم)
|
| 83 |
self.project_manager = Agent(
|
|
@@ -136,26 +120,13 @@ class ProjectManagerCrew:
|
|
| 136 |
verbose=True,
|
| 137 |
llm=self.llm
|
| 138 |
)
|
| 139 |
-
|
| 140 |
-
# 5. وكيل التوثيق
|
| 141 |
-
self.documenter = Agent(
|
| 142 |
-
role="متخصص التوثيق",
|
| 143 |
-
goal="توثيق المشروع والتغييرات بشكل احترافي",
|
| 144 |
-
backstory="""
|
| 145 |
-
أنا متخصص في توثيق البرمجيات. مهمتي هي إنشاء توثيق واضح وشامل
|
| 146 |
-
للمشروع والتغييرات التي تم إجراؤها، مما يسهل فهمها وصيانتها.
|
| 147 |
-
""",
|
| 148 |
-
tools=[self.code_reader, self.docs_searcher],
|
| 149 |
-
verbose=True,
|
| 150 |
-
llm=self.llm
|
| 151 |
-
)
|
| 152 |
|
| 153 |
def add_to_history(self, role: str, content: str):
|
| 154 |
"""إضافة رسالة إلى سجل المحادثة"""
|
| 155 |
self.conversation_history.append({
|
| 156 |
"role": role,
|
| 157 |
"content": content,
|
| 158 |
-
"timestamp": json.dumps({"time": "now"})
|
| 159 |
})
|
| 160 |
|
| 161 |
def create_development_plan(self, user_request: str):
|
|
@@ -253,43 +224,19 @@ class ProjectManagerCrew:
|
|
| 253 |
context=[execution_task]
|
| 254 |
)
|
| 255 |
|
| 256 |
-
# المهمة 5: التوثيق
|
| 257 |
-
documentation_task = Task(
|
| 258 |
-
description="""
|
| 259 |
-
## المطلوب:
|
| 260 |
-
بناءً على جميع المهام السابقة، قم بـ:
|
| 261 |
-
1. توثيق التغييرات التي تم إجراؤها
|
| 262 |
-
2. تحديث وثائق المشروع الحالية
|
| 263 |
-
3. كتابة دليل للمستخدم إذا لزم الأمر
|
| 264 |
-
4. إنشاء دليل للمطور للصيانة المستقبلية
|
| 265 |
-
5. توثيق القرارات التقنية المهمة
|
| 266 |
-
|
| 267 |
-
## تعليمات:
|
| 268 |
-
- كن واضحاً وموجزاً
|
| 269 |
-
- استخدم أمثلة عملية
|
| 270 |
-
- نظم المعلومات بطريقة سهلة الفهم
|
| 271 |
-
- أضف رسوماً توضيحية إذا أمكن
|
| 272 |
-
""",
|
| 273 |
-
agent=self.documenter,
|
| 274 |
-
expected_output="توثيق كامل للمشروع والتغييرات",
|
| 275 |
-
context=[execution_task, review_task]
|
| 276 |
-
)
|
| 277 |
-
|
| 278 |
# إنشاء الفريق وتشغيله
|
| 279 |
crew = Crew(
|
| 280 |
agents=[
|
| 281 |
self.project_manager,
|
| 282 |
self.technical_planner,
|
| 283 |
self.coder,
|
| 284 |
-
self.reviewer
|
| 285 |
-
self.documenter
|
| 286 |
],
|
| 287 |
tasks=[
|
| 288 |
analysis_task,
|
| 289 |
planning_task,
|
| 290 |
execution_task,
|
| 291 |
-
review_task
|
| 292 |
-
documentation_task
|
| 293 |
],
|
| 294 |
process=Process.sequential,
|
| 295 |
verbose=True,
|
|
@@ -348,75 +295,4 @@ class ProjectManagerCrew:
|
|
| 348 |
# إضافة رد المدير إلى السجل
|
| 349 |
self.add_to_history("manager", str(response))
|
| 350 |
|
| 351 |
-
return response
|
| 352 |
-
|
| 353 |
-
def get_conversation_summary(self):
|
| 354 |
-
"""الحصول على ملخص للمحادثة"""
|
| 355 |
-
if not self.conversation_history:
|
| 356 |
-
return "لا توجد محادثة سابقة"
|
| 357 |
-
|
| 358 |
-
summary_task = Task(
|
| 359 |
-
description=f"""
|
| 360 |
-
## تاريخ المحادثة الكامل:
|
| 361 |
-
{json.dumps(self.conversation_history, ensure_ascii=False)}
|
| 362 |
-
|
| 363 |
-
## المطلوب:
|
| 364 |
-
قم بإنشاء ملخص واضح للمحادثة يشمل:
|
| 365 |
-
1. النقاط الرئيسية التي تمت مناقشتها
|
| 366 |
-
2. القرارات التي تم اتخاذها
|
| 367 |
-
3. المهام المطلوبة أو المكتملة
|
| 368 |
-
4. الخطوات التالية المقترحة
|
| 369 |
-
|
| 370 |
-
## تعليمات:
|
| 371 |
-
- كن موجزاً وواضحاً
|
| 372 |
-
- نظم المعلومات بطريقة منطقية
|
| 373 |
-
- استخدم لغة عربية سليمة
|
| 374 |
-
""",
|
| 375 |
-
agent=self.project_manager,
|
| 376 |
-
expected_output="ملخص محادثة منظم وواضح"
|
| 377 |
-
)
|
| 378 |
-
|
| 379 |
-
crew = Crew(
|
| 380 |
-
agents=[self.project_manager],
|
| 381 |
-
tasks=[summary_task],
|
| 382 |
-
process=Process.sequential,
|
| 383 |
-
verbose=False
|
| 384 |
-
)
|
| 385 |
-
|
| 386 |
-
return crew.kickoff()
|
| 387 |
-
|
| 388 |
-
# دالة مساعدة لإنشاء نموذج تجريبي
|
| 389 |
-
def create_demo_crew(project_path: str = "demo_project"):
|
| 390 |
-
"""إنشاء نموذج تجريبي للاختبار"""
|
| 391 |
-
|
| 392 |
-
# إنشاء مجرد تجريبي إذا لم يكن موجوداً
|
| 393 |
-
if not os.path.exists(project_path):
|
| 394 |
-
os.makedirs(project_path, exist_ok=True)
|
| 395 |
-
demo_files = {
|
| 396 |
-
"main.py": "# ملف رئيسي تجريبي\nprint('مرحباً بالعالم')",
|
| 397 |
-
"README.md": "# مشروع تجريبي\nهذا مشروع للاختبار",
|
| 398 |
-
"requirements.txt": "# المتطلبات\nflask==2.3.0\nrequests==2.31.0"
|
| 399 |
-
}
|
| 400 |
-
|
| 401 |
-
for filename, content in demo_files.items():
|
| 402 |
-
with open(os.path.join(project_path, filename), "w", encoding="utf-8") as f:
|
| 403 |
-
f.write(content)
|
| 404 |
-
|
| 405 |
-
return ProjectManagerCrew(project_path)
|
| 406 |
-
|
| 407 |
-
if __name__ == "__main__":
|
| 408 |
-
# مثال على الاستخدام
|
| 409 |
-
print("🚀 اختبار نظام مدير المشاريع الذكي...")
|
| 410 |
-
|
| 411 |
-
# استخدام مشروع تجريبي
|
| 412 |
-
crew = create_demo_crew()
|
| 413 |
-
|
| 414 |
-
# مثال على الدردشة
|
| 415 |
-
response = crew.chat_with_manager("مرحباً، أريد إضافة واجهة ويب للمشروع")
|
| 416 |
-
print(f"👤 أنت: أريد إضافة واجهة ويب للمشروع")
|
| 417 |
-
print(f"🤖 المدير: {response}")
|
| 418 |
-
|
| 419 |
-
# مثال على إنشاء خطة تطوير
|
| 420 |
-
print("\n📋 إنشاء خطة تطوير...")
|
| 421 |
-
plan = crew.create_development_plan("إضافة نظام مصادقة للمستخدمين")
|
| 422 |
-
print(f"📄 الخطة: {plan[:200]}...") # عرض أول 200 حرف فقط
|
|
|
|
| 9 |
from crewai_tools import (
|
| 10 |
FileReadTool,
|
| 11 |
DirectorySearchTool,
|
| 12 |
+
CodeDocsSearchTool
|
|
|
|
| 13 |
)
|
| 14 |
from langchain_openai import ChatOpenAI
|
| 15 |
import json
|
|
|
|
| 40 |
root_dir=self.project_path,
|
| 41 |
description="البحث في توثيق الكود"
|
| 42 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def setup_llm(self):
|
| 45 |
+
"""إعداد النماذج اللغوية باستخدام Hugging Face فقط"""
|
|
|
|
| 46 |
hf_token = os.getenv("HF_TOKEN")
|
| 47 |
+
|
| 48 |
+
if not hf_token:
|
| 49 |
+
raise ValueError("❌ HF_TOKEN غير موجود. يرجى إضافته في إعدادات السرية (Secrets).")
|
| 50 |
+
|
| 51 |
+
# نستخدم ChatOpenAI مع قاعدة Hugging Face
|
| 52 |
+
self.llm = ChatOpenAI(
|
| 53 |
+
model="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 54 |
+
temperature=0.7,
|
| 55 |
+
openai_api_key=hf_token,
|
| 56 |
+
openai_api_base="https://router.huggingface.co/hf-inference/v1",
|
| 57 |
+
max_tokens=2048,
|
| 58 |
+
)
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def setup_agents(self):
|
| 61 |
"""إعداد فريق الوكلاء"""
|
| 62 |
|
| 63 |
# أدوات المدير
|
| 64 |
manager_tools = [self.project_analyzer, self.code_reader]
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# 1. الوكيل الرئيسي (المتحدث مع المستخدم)
|
| 67 |
self.project_manager = Agent(
|
|
|
|
| 120 |
verbose=True,
|
| 121 |
llm=self.llm
|
| 122 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
def add_to_history(self, role: str, content: str):
|
| 125 |
"""إضافة رسالة إلى سجل المحادثة"""
|
| 126 |
self.conversation_history.append({
|
| 127 |
"role": role,
|
| 128 |
"content": content,
|
| 129 |
+
"timestamp": json.dumps({"time": "now"})
|
| 130 |
})
|
| 131 |
|
| 132 |
def create_development_plan(self, user_request: str):
|
|
|
|
| 224 |
context=[execution_task]
|
| 225 |
)
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
# إنشاء الفريق وتشغيله
|
| 228 |
crew = Crew(
|
| 229 |
agents=[
|
| 230 |
self.project_manager,
|
| 231 |
self.technical_planner,
|
| 232 |
self.coder,
|
| 233 |
+
self.reviewer
|
|
|
|
| 234 |
],
|
| 235 |
tasks=[
|
| 236 |
analysis_task,
|
| 237 |
planning_task,
|
| 238 |
execution_task,
|
| 239 |
+
review_task
|
|
|
|
| 240 |
],
|
| 241 |
process=Process.sequential,
|
| 242 |
verbose=True,
|
|
|
|
| 295 |
# إضافة رد المدير إلى السجل
|
| 296 |
self.add_to_history("manager", str(response))
|
| 297 |
|
| 298 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|