1yahoo commited on
Commit
eb981eb
·
verified ·
1 Parent(s): af068e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -19
app.py CHANGED
@@ -11,7 +11,7 @@ from chromadb.utils import embedding_functions
11
  # --- الإعدادات الفنية ---
12
  app = FastAPI(title="Neural RAG Engine")
13
 
14
- # تفعيل CORS للسماح لواجهات Next.js بالاتصال بالخادم
15
  app.add_middleware(
16
  CORSMiddleware,
17
  allow_origins=["*"],
@@ -19,6 +19,7 @@ app.add_middleware(
19
  allow_headers=["*"],
20
  )
21
 
 
22
  STORAGE_PATH = "/data/neural_memory" if os.path.exists("/data") else "./neural_memory"
23
  chroma_client = chromadb.PersistentClient(path=STORAGE_PATH)
24
  default_ef = embedding_functions.DefaultEmbeddingFunction()
@@ -29,62 +30,54 @@ client = OpenAI(
29
  api_key=os.getenv("HF_TOKEN")
30
  )
31
 
32
- # --- وظائف المعالجة ---
33
  def ingest_document(file_content, filename):
34
- # (نفس منطق التقطيع الاحترافي السابق مع الـ Overlap)
35
  text = file_content.decode("utf-8", errors="ignore")
36
- # إذا كان PDF يحتاج لمكتبة pypdf (يمكن دمجها هنا)
37
  chunk_size, overlap = 1000, 200
38
  chunks = [text[i : i + chunk_size] for i in range(0, len(text), chunk_size - overlap)]
39
  ids = [str(uuid.uuid4()) for _ in chunks]
40
  metadatas = [{"source": filename} for _ in chunks]
41
  collection.add(documents=chunks, ids=ids, metadatas=metadatas)
42
 
43
- # --- الـ Endpoints المتوافقة مع Next.js ---
44
 
45
  @app.get("/")
46
  def home():
47
- return {"status": "Neural Engine is Running", "docs_in_memory": collection.count()}
48
 
49
  @app.post("/upload")
50
  async def upload_file(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
51
  content = await file.read()
52
  background_tasks.add_task(ingest_document, content, file.filename)
53
- return {"message": f"Processing {file.filename} in background..."}
54
 
55
  @app.post("/v1/chat/completions")
 
56
  async def chat_endpoint(request: Request):
57
- """هذا المسار يجعل الخادم متوافقاً تماماً مع واجهات Next.js"""
58
  body = await request.json()
59
  messages = body.get("messages", [])
60
- temperature = body.get("temperature", 0.7)
61
 
62
- # البحث الدلالي (RAG) بناءً على آخر رسالة
63
  user_query = messages[-1]["content"] if messages else ""
64
  results = collection.query(query_texts=[user_query], n_results=3)
65
  knowledge = "\n".join(results['documents'][0]) if results['documents'] else ""
66
 
67
- # حقن المعرفة
68
- system_instruction = f"Context from Memory:\n{knowledge}\n\nAnswer based on this context."
69
  messages.insert(0, {"role": "system", "content": system_instruction})
70
 
71
  def stream_gen():
72
  response = client.chat.completions.create(
73
  model="huihui-ai/Qwen2.5-72B-Instruct-abliterated",
74
  messages=messages,
75
- temperature=temperature,
76
  stream=True
77
  )
78
  for chunk in response:
79
  content = chunk.choices[0].delta.content
80
  if content:
81
- # التنسيق المطلوب لـ Server-Sent Events (SSE)
82
  yield f"data: {content}\n\n"
83
  yield "data: [DONE]\n\n"
84
 
85
  return StreamingResponse(stream_gen(), media_type="text/event-stream")
86
-
87
- if __name__ == "__main__":
88
- import uvicorn
89
- # تشغيل الخادم
90
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
11
  # --- الإعدادات الفنية ---
12
  app = FastAPI(title="Neural RAG Engine")
13
 
14
+ # تفعيل CORS للسماح لواجهات Next.js بالاتصال
15
  app.add_middleware(
16
  CORSMiddleware,
17
  allow_origins=["*"],
 
19
  allow_headers=["*"],
20
  )
21
 
22
+ # إعداد الذاكرة الدائمية في مساحة التخزين
23
  STORAGE_PATH = "/data/neural_memory" if os.path.exists("/data") else "./neural_memory"
24
  chroma_client = chromadb.PersistentClient(path=STORAGE_PATH)
25
  default_ef = embedding_functions.DefaultEmbeddingFunction()
 
30
  api_key=os.getenv("HF_TOKEN")
31
  )
32
 
33
+ # --- وظائف معالجة المعرفة (RAG Logic) ---
34
  def ingest_document(file_content, filename):
35
+ """تقطيع وحقن المستندات مع Overlap لضمان سياق دلالي قوي."""
36
  text = file_content.decode("utf-8", errors="ignore")
 
37
  chunk_size, overlap = 1000, 200
38
  chunks = [text[i : i + chunk_size] for i in range(0, len(text), chunk_size - overlap)]
39
  ids = [str(uuid.uuid4()) for _ in chunks]
40
  metadatas = [{"source": filename} for _ in chunks]
41
  collection.add(documents=chunks, ids=ids, metadatas=metadatas)
42
 
43
+ # --- الـ Endpoints المتوافقة مع Chatbot UI ---
44
 
45
  @app.get("/")
46
  def home():
47
+ return {"status": "Neural Engine Active", "memory_count": collection.count()}
48
 
49
  @app.post("/upload")
50
  async def upload_file(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
51
  content = await file.read()
52
  background_tasks.add_task(ingest_document, content, file.filename)
53
+ return {"message": f"Processing {file.filename}..."}
54
 
55
  @app.post("/v1/chat/completions")
56
+ @app.post("/api/chat") # لدعم مسارات Next.js المختلفة
57
  async def chat_endpoint(request: Request):
58
+ """محرك الاستدلال الدلالي المتوافق مع معايير OpenAI."""
59
  body = await request.json()
60
  messages = body.get("messages", [])
 
61
 
62
+ # البحث في الذاكرة بناءً على آخر مدخل للمستخدم
63
  user_query = messages[-1]["content"] if messages else ""
64
  results = collection.query(query_texts=[user_query], n_results=3)
65
  knowledge = "\n".join(results['documents'][0]) if results['documents'] else ""
66
 
67
+ # حقن المعرفة المسترجعة في نظام التوجيه
68
+ system_instruction = f"Context from Knowledge Vault:\n{knowledge}\n\nAnswer using the context above."
69
  messages.insert(0, {"role": "system", "content": system_instruction})
70
 
71
  def stream_gen():
72
  response = client.chat.completions.create(
73
  model="huihui-ai/Qwen2.5-72B-Instruct-abliterated",
74
  messages=messages,
 
75
  stream=True
76
  )
77
  for chunk in response:
78
  content = chunk.choices[0].delta.content
79
  if content:
 
80
  yield f"data: {content}\n\n"
81
  yield "data: [DONE]\n\n"
82
 
83
  return StreamingResponse(stream_gen(), media_type="text/event-stream")