الفصل 3: تطبيق RAG متعدد المستأجرين
نظرة عامة على المشروع
ابنِ منصة RAG بنموذج SaaS تخدم عدة مؤسسات:
- عزل كامل للمستأجرين
- تخصيص لكل مستأجر
- تتبّع الاستخدام والفوترة
- بنية تحتية قابلة للتوسيع
المعمارية
عزل المستأجرين
# tenant_service.py
class TenantService:
def __init__(self, vectorstore_factory, config_db):
self.vectorstore_factory = vectorstore_factory
self.config_db = config_db
def get_tenant_retriever(self, tenant_id):
"""Get retriever scoped to tenant"""
config = self.config_db.get(tenant_id)
# Option 1: Namespace isolation
return self.vectorstore.as_retriever(
search_kwargs={
"namespace": f"tenant_{tenant_id}",
"k": config.get("top_k", 5)
}
)
# Option 2: Metadata filter
# return retriever with filter={"tenant_id": tenant_id}
def get_tenant_llm(self, tenant_id):
"""Get LLM with tenant-specific config"""
config = self.config_db.get(tenant_id)
return ChatOpenAI(
model=config.get("model", "gpt-3.5-turbo"),
temperature=config.get("temperature", 0),
max_tokens=config.get("max_tokens", 500)
)
تتبّع الاستخدام
# usage_tracking.py
from datetime import datetime
class UsageTracker:
def __init__(self, db):
self.db = db
def track_query(self, tenant_id, query, tokens_used):
"""Track API usage"""
self.db.usage.insert({
"tenant_id": tenant_id,
"timestamp": datetime.utcnow(),
"type": "query",
"tokens": tokens_used
})
def track_indexing(self, tenant_id, documents_count, chunks_count):
"""Track indexing usage"""
self.db.usage.insert({
"tenant_id": tenant_id,
"timestamp": datetime.utcnow(),
"type": "indexing",
"documents": documents_count,
"chunks": chunks_count
})
def get_monthly_usage(self, tenant_id, month):
"""Get usage for billing"""
return self.db.usage.aggregate([
{"$match": {"tenant_id": tenant_id, "month": month}},
{"$group": {
"_id": "$type",
"total_tokens": {"$sum": "$tokens"},
"total_docs": {"$sum": "$documents"}
}}
])
تكامل الفوترة
# billing.py
class BillingService:
PRICING = {
"queries": 0.001, # per query
"tokens": 0.00001, # per token
"storage": 0.10, # per GB/month
}
def calculate_invoice(self, tenant_id, month):
usage = usage_tracker.get_monthly_usage(tenant_id, month)
invoice = {
"tenant_id": tenant_id,
"period": month,
"line_items": []
}
for item in usage:
if item["_id"] == "query":
cost = item["count"] * self.PRICING["queries"]
invoice["line_items"].append({
"description": f"{item['count']} queries",
"amount": cost
})
invoice["total"] = sum(i["amount"] for i in invoice["line_items"])
return invoice
تخصيص المستأجر
| الميزة | خيارات لكل مستأجر |
|---|---|
| النموذج | GPT-3.5، GPT-4، Claude |
| Temperature | 0-1 |
| Top-K | 3-10 |
| حجم الـ chunk | 500-2000 |
| prompts مخصّصة | نعم |
| الهوية البصرية | شعار، ألوان |
اعتبارات النشر
- Kubernetes للتوسيع
- قواعد بيانات منفصلة لكل مستوى
- تحديد المعدّل لكل مستأجر
- مراقبة الصحة لكل مستأجر
- نسخ احتياطية تلقائية