الفصل 1: توسيع تطبيقات RAG
تحديات التوسيع
| التحدي | الأثر | الحل |
|---|---|---|
| ملايين الوثائق | فهرسة بطيئة | معالجة دفعية |
| معدل استعلامات مرتفع | مهلة زمنية | موازنة الحمل |
| متجهات كبيرة | حدود الذاكرة | Sharding |
قواعد بيانات متجهية موزّعة
# Milvus cluster configuration
from pymilvus import connections, Collection
# Connect to distributed cluster
connections.connect(
alias="default",
host="milvus-cluster",
port="19530"
)
# Data automatically distributed across nodes
المعالجة غير المتزامنة
import asyncio
from langchain.embeddings import OpenAIEmbeddings
async def batch_embed_async(texts, batch_size=100):
"""Embed texts in async batches"""
embeddings = OpenAIEmbeddings()
async def embed_batch(batch):
return await asyncio.to_thread(
embeddings.embed_documents, batch
)
tasks = []
for i in range(0, len(texts), batch_size):
batch = texts[i:i + batch_size]
tasks.append(embed_batch(batch))
results = await asyncio.gather(*tasks)
return [e for batch in results for e in batch]
الفهرسة القائمة على الطوابير
# Producer: Add to queue
import redis
r = redis.Redis()
r.lpush("index_queue", document_json)
# Consumer: Process queue
while True:
doc = r.brpop("index_queue")
process_and_index(doc)
توصيات النطاق
| الوثائق | المعمارية |
|---|---|
| <100K | نسخة واحدة |
| 100K-1M | قاعدة بيانات مكرّرة |
| 1M-10M | عنقود مجزّأ (sharded) |
| >10M | نظام موزّع |