Skip to content

Commit 8784c35

Browse files
authored
feat: read quota from settings instead of db
1 parent a122503 commit 8784c35

File tree

9 files changed

+51
-48
lines changed

9 files changed

+51
-48
lines changed

config/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@
282282
FEISHU_APP_SECRET = env.str("FEISHU_APP_SECRET", default="")
283283
FEISHU_ENCRYPT_KEY = env.str("FEISHU_ENCRYPT_KEY", default="")
284284

285+
MAX_BOT_COUNT = env.str("MAX_BOT_COUNT", default=10)
286+
MAX_COLLECTION_COUNT = env.str("MAX_COLLECTION_COUNT", default=50)
287+
MAX_DOCUMENT_COUNT = env.str("MAX_DOCUMENT_COUNT", default=1000)
288+
MAX_CONVERSATION_COUNT = env.str("MAX_CONVERSATION_COUNT", default=100)
289+
285290
ENABLE_QA_GENERATOR = env.bool("ENABLE_QA_GENERATOR", default=False)
286291

287292
CHAT_CONSUMER_IMPLEMENTATION = env.str("CHAT_CONSUMER_IMPLEMENTATION", default="document-qa")

deploy/kubechat/templates/django-deployment.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ spec:
220220
configMapKeyRef:
221221
key: FEISHU_ENCRYPT_KEY
222222
name: kubechat-config
223+
- name: MAX_BOT_COUNT
224+
valueFrom:
225+
configMapKeyRef:
226+
key: MAX_BOT_COUNT
227+
name: kubechat-config
228+
- name: MAX_COLLECTION_COUNT
229+
valueFrom:
230+
configMapKeyRef:
231+
key: MAX_COLLECTION_COUNT
232+
name: kubechat-config
233+
- name: MAX_DOCUMENT_COUNT
234+
valueFrom:
235+
configMapKeyRef:
236+
key: MAX_DOCUMENT_COUNT
237+
name: kubechat-config
238+
- name: MAX_CONVERSATION_COUNT
239+
valueFrom:
240+
configMapKeyRef:
241+
key: MAX_CONVERSATION_COUNT
242+
name: kubechat-config
223243
- name: EMBEDDING_MODEL
224244
valueFrom:
225245
configMapKeyRef:

deploy/kubechat/templates/kubechat-config-configmap.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ data:
2323
FEISHU_APP_ID: "{{ .Values.django.feishu.appID }}"
2424
FEISHU_APP_SECRET: "{{ .Values.django.feishu.appSecret }}"
2525
FEISHU_ENCRYPT_KEY: "{{ .Values.django.feishu.encryptKey }}"
26+
MAX_BOT_COUNT: "{{ .Values.django.quota.maxBotCount }}"
27+
MAX_COLLECTION_COUNT: "{{ .Values.django.quota.maxCollectionCount }}"
28+
MAX_DOCUMENT_COUNT: "{{ .Values.django.quota.maxDocumentCount }}"
29+
MAX_CONVERSATION_COUNT: "{{ .Values.django.quota.maxConversationCount }}"
2630
EMBEDDING_MODEL: "{{ .Values.django.embeddingModel }}"
2731
EMBEDDING_DEVICE: "{{ .Values.django.embeddingDevice }}"
2832
ENABLE_QA_GENERATOR: "{{ .Values.django.enableQAGenerator }}"

deploy/kubechat/values.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ django:
9090
appID: ""
9191
appSecret: ""
9292
encryptKey: ""
93+
quota:
94+
maxBotCount: 10
95+
maxCollectionCount: 50
96+
maxDocumentCount: 1000
97+
maxConversationCount: 100
9398
resources:
9499
# We usually recommend not to specify default resources and to leave this as a conscious
95100
# choice for the user. This also increases chances charts run on environments with little

kubechat/apps.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
1-
import sys
2-
31
from django.apps import AppConfig
4-
import asyncio
5-
6-
from django.db import transaction
7-
from asgiref.sync import sync_to_async
82

93

104
class KubechatConfig(AppConfig):
115
default_auto_field = "django.db.models.BigAutoField"
126
name = "kubechat"
137

14-
def ready(self):
15-
# if manage.py is running, this might be a migration, so we don't need to load quotas
16-
if "manage.py" in sys.argv:
17-
return
18-
19-
if asyncio.get_event_loop().is_running():
20-
# call load_quotas() asynchronously in uvicorn
21-
asyncio.create_task(sync_to_async(load_quotas)())
22-
else:
23-
# call load_quotas() after the commit transaction in celery
24-
transaction.on_commit(load_quotas)
25-
26-
27-
class DefaultQuota:
28-
MAX_BOT_COUNT = None
29-
MAX_COLLECTION_COUNT = None
30-
MAX_DOCUMENT_COUNT = None
31-
MAX_CONVERSATION_COUNT = None
32-
338

349
class QuotaType:
3510
MAX_BOT_COUNT = 'max_bot_count'
@@ -38,10 +13,3 @@ class QuotaType:
3813
MAX_CONVERSATION_COUNT = 'max_conversation_count'
3914

4015

41-
def load_quotas():
42-
from kubechat.db.ops import query_quota
43-
44-
DefaultQuota.MAX_BOT_COUNT = query_quota(QuotaType.MAX_BOT_COUNT)
45-
DefaultQuota.MAX_COLLECTION_COUNT = query_quota(QuotaType.MAX_COLLECTION_COUNT)
46-
DefaultQuota.MAX_DOCUMENT_COUNT = query_quota(QuotaType.MAX_DOCUMENT_COUNT)
47-
DefaultQuota.MAX_CONVERSATION_COUNT = query_quota(QuotaType.MAX_CONVERSATION_COUNT)

kubechat/chat/websocket/base_consumer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import redis.asyncio as redis
1313

1414
import config.settings as settings
15-
from kubechat.apps import DefaultQuota, QuotaType
15+
from kubechat.apps import QuotaType
1616
from kubechat.auth.validator import DEFAULT_USER
1717
from kubechat.chat.utils import start_response, success_response, stop_response, fail_response, welcome_response
1818
from kubechat.pipeline.pipeline import KUBE_CHAT_DOC_QA_REFERENCES, KeywordPipeline,KUBE_CHAT_RELATED_QUESTIONS
@@ -58,7 +58,7 @@ async def connect(self):
5858
self.redis_client = redis.Redis.from_url(settings.MEMORY_REDIS_URL)
5959
self.conversation_limit = await query_user_quota(self.user, QuotaType.MAX_CONVERSATION_COUNT)
6060
if self.conversation_limit is None:
61-
self.conversation_limit = DefaultQuota.MAX_CONVERSATION_COUNT
61+
self.conversation_limit = settings.MAX_CONVERSATION_COUNT
6262

6363
# If using daphne, we need to put the token in the headers and include the headers in the subprotocol.
6464
# headers = {}

kubechat/tasks/sync_documents_task.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pydantic import BaseModel
1414

1515
from config.celery import app
16-
from kubechat.apps import DefaultQuota
16+
from config.settings import MAX_DOCUMENT_COUNT
1717
from kubechat.db.models import Document, DocumentStatus, Collection, CollectionStatus, CollectionSyncHistory, \
1818
CollectionSyncStatus
1919
from kubechat.source.base import get_source
@@ -63,7 +63,7 @@ def sync_documents(self, **kwargs):
6363

6464
document_limit = kwargs["document_user_quota"]
6565
if document_limit is None:
66-
document_limit = DefaultQuota.MAX_DOCUMENT_COUNT
66+
document_limit = MAX_DOCUMENT_COUNT
6767

6868
src_docs = {}
6969
tasks = []

kubechat/views/main.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import kubechat.chat.message
1717
from config import settings
1818
from config.celery import app
19-
from kubechat.apps import DefaultQuota, QuotaType
19+
from kubechat.apps import QuotaType
2020
from kubechat.chat.history.redis import RedisChatMessageHistory
2121
from kubechat.llm.base import Predictor
2222
from kubechat.llm.prompts import DEFAULT_MODEL_PROMPT_TEMPLATES, DEFAULT_CHINESE_PROMPT_TEMPLATE_V2, \
@@ -225,10 +225,10 @@ async def create_collection(request, collection: CollectionIn):
225225
return fail(HTTPStatus.BAD_REQUEST, error_msg)
226226

227227
# there is quota limit on collection
228-
if DefaultQuota.MAX_COLLECTION_COUNT:
228+
if settings.MAX_COLLECTION_COUNT:
229229
collection_limit = await query_user_quota(user, QuotaType.MAX_COLLECTION_COUNT)
230230
if collection_limit is None:
231-
collection_limit = DefaultQuota.MAX_COLLECTION_COUNT
231+
collection_limit = settings.MAX_COLLECTION_COUNT
232232
if collection_limit and await query_collections_count(user) >= collection_limit:
233233
return fail(HTTPStatus.FORBIDDEN, f"collection number has reached the limit of {collection_limit}")
234234

@@ -353,10 +353,10 @@ async def create_document(request, collection_id, file: List[UploadedFile] = Fil
353353
return fail(HTTPStatus.NOT_FOUND, "Collection not found")
354354

355355
# there is quota limit on document
356-
if DefaultQuota.MAX_DOCUMENT_COUNT:
356+
if settings.MAX_DOCUMENT_COUNT:
357357
document_limit = await query_user_quota(user, QuotaType.MAX_DOCUMENT_COUNT)
358358
if document_limit is None:
359-
document_limit = DefaultQuota.MAX_DOCUMENT_COUNT
359+
document_limit = settings.MAX_DOCUMENT_COUNT
360360
if await query_documents_count(user, collection_id) >= document_limit:
361361
return fail(HTTPStatus.FORBIDDEN, f"document number has reached the limit of {document_limit}")
362362

@@ -399,10 +399,10 @@ async def create_url_document(request, collection_id):
399399
return fail(HTTPStatus.NOT_FOUND, "Collection not found")
400400

401401
# there is quota limit on document
402-
if DefaultQuota.MAX_DOCUMENT_COUNT:
402+
if settings.MAX_DOCUMENT_COUNT:
403403
document_limit = await query_user_quota(user, QuotaType.MAX_DOCUMENT_COUNT)
404404
if document_limit is None:
405-
document_limit = DefaultQuota.MAX_DOCUMENT_COUNT
405+
document_limit = settings.MAX_DOCUMENT_COUNT
406406
if await query_documents_count(user, collection_id) >= document_limit:
407407
return fail(HTTPStatus.FORBIDDEN, f"document number has reached the limit of {document_limit}")
408408

@@ -603,10 +603,10 @@ async def create_bot(request, bot_in: BotIn):
603603
user = get_user(request)
604604

605605
# there is quota limit on bot
606-
if DefaultQuota.MAX_BOT_COUNT:
606+
if settings.MAX_BOT_COUNT:
607607
bot_limit = await query_user_quota(user, QuotaType.MAX_BOT_COUNT)
608608
if bot_limit is None:
609-
bot_limit = DefaultQuota.MAX_BOT_COUNT
609+
bot_limit = settings.MAX_BOT_COUNT
610610
if await query_bots_count(user) >= bot_limit:
611611
return fail(HTTPStatus.FORBIDDEN, f"bot number has reached the limit of {bot_limit}")
612612

kubechat/views/weixin.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from config import settings
88
import kubechat.chat.message
99

10-
from kubechat.apps import QuotaType, DefaultQuota
10+
from kubechat.apps import QuotaType
11+
from config.settings import MAX_CONVERSATION_COUNT
1112
from kubechat.chat.history.redis import RedisChatMessageHistory
1213
from kubechat.pipeline.pipeline import KeywordPipeline
1314
from kubechat.db.ops import *
@@ -62,7 +63,7 @@ async def weixin_text_response(client, user, bot, query, msg_id):
6263

6364
conversation_limit = await query_user_quota(user, QuotaType.MAX_CONVERSATION_COUNT)
6465
if conversation_limit is None:
65-
conversation_limit = DefaultQuota.MAX_CONVERSATION_COUNT
66+
conversation_limit = MAX_CONVERSATION_COUNT
6667

6768
try:
6869
if use_default_token and conversation_limit:
@@ -103,7 +104,7 @@ async def weixin_card_response(client, user, bot, query, msg_id):
103104

104105
conversation_limit = await query_user_quota(user, QuotaType.MAX_CONVERSATION_COUNT)
105106
if conversation_limit is None:
106-
conversation_limit = DefaultQuota.MAX_CONVERSATION_COUNT
107+
conversation_limit = MAX_CONVERSATION_COUNT
107108

108109
try:
109110
if use_default_token and conversation_limit:

0 commit comments

Comments
 (0)