Skip to content

Commit ded2c11

Browse files
committed
feat: global enable chat type or model
1 parent 1b0829f commit ded2c11

File tree

8 files changed

+114
-53
lines changed

8 files changed

+114
-53
lines changed

backend/api/conf/config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111

1212
class CommonSetting(BaseModel):
13+
sync_conversations_on_startup: bool = True
14+
sync_conversations_regularly: bool = False
1315
print_sql: bool = False
1416
create_initial_admin_user: bool = True
1517
initial_admin_user_username: str = 'admin'
1618
initial_admin_user_password: str = 'password'
17-
sync_conversations_on_startup: bool = True
18-
sync_conversations_regularly: bool = True
1919

2020
@validator("initial_admin_user_password")
2121
def validate_password(cls, v):
@@ -52,6 +52,7 @@ class AuthSetting(BaseModel):
5252

5353

5454
class OpenaiWebChatGPTSetting(BaseModel):
55+
enabled: bool = True
5556
is_plus_account: bool = True
5657
chatgpt_base_url: Optional[str] = None
5758
proxy: Optional[str] = None
@@ -75,6 +76,7 @@ def chatgpt_base_url_end_with_slash(cls, v):
7576

7677

7778
class OpenaiApiSetting(BaseModel):
79+
enabled: bool = True
7880
openai_base_url: str = 'https://api.openai.com/v1/'
7981
proxy: Optional[str] = None
8082
connect_timeout: int = Field(10, ge=1)

backend/api/routers/chat.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ async def check_limits(user: UserReadAdmin, ask_request: AskRequest):
126126
if not source_setting.allow_to_use:
127127
raise WebsocketInvalidAskException(tip="errors.userNotAllowToUseChatType")
128128

129+
# 当前对话类型是否全局启用
130+
if ask_request.source == ChatSourceTypes.openai_web and not config.openai_web.enabled or \
131+
ask_request.source == ChatSourceTypes.openai_api and not config.openai_api.enabled:
132+
raise WebsocketInvalidAskException(tip="errors.chatTypeNotEnabled")
133+
129134
# 是否到期
130135
current_datetime = datetime.now().astimezone(tz=timezone.utc)
131136
if source_setting.valid_until is not None and current_datetime > source_setting.valid_until:
@@ -144,9 +149,13 @@ async def check_limits(user: UserReadAdmin, ask_request: AskRequest):
144149
# 判断是否能使用该模型
145150
if ask_request.source == ChatSourceTypes.openai_web and ask_request.model not in user.setting.openai_web.available_models or \
146151
ask_request.source == ChatSourceTypes.openai_api and ask_request.model not in user.setting.openai_api.available_models:
147-
# await websocket.close(1007, "errors.userNotAllowToUseModel")
148152
raise WebsocketInvalidAskException("errors.userNotAllowToUseModel")
149153

154+
# 模型是否全局启用
155+
if ask_request.source == ChatSourceTypes.openai_web and ask_request.model not in config.openai_web.enabled_models or \
156+
ask_request.source == ChatSourceTypes.openai_api and ask_request.model not in config.openai_api.enabled_models:
157+
raise WebsocketInvalidAskException("errors.modelNotEnabled")
158+
150159
# 对话次数判断
151160
model_ask_count = source_setting.per_model_ask_count.dict().get(ask_request.model, -1)
152161
total_ask_count = source_setting.total_ask_count

backend/api/sources/openai_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ async def ask(self, content: str, conversation_id: uuid.UUID = None,
6464
parent_id: uuid.UUID = None, model: OpenaiApiChatModels = None,
6565
context_message_count: int = -1, extra_args: Optional[dict] = None, **_kwargs):
6666

67+
assert config.openai_api.enabled, "openai_api is not enabled"
68+
6769
now_time = datetime.now().astimezone(tz=timezone.utc)
6870
message_id = uuid.uuid4()
6971
new_message = OpenaiApiChatMessage(

backend/api/sources/openai_web.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ async def clear_conversations(self):
226226
async def ask(self, content: str, conversation_id: uuid.UUID = None, parent_id: uuid.UUID = None,
227227
model: OpenaiWebChatModels = None, plugin_ids: list[str] = None, **_kwargs):
228228

229+
assert config.openai_web.enabled, "OpenAI Web is not enabled"
230+
229231
model = model or OpenaiWebChatModels.gpt_3_5
230232

231233
if conversation_id or parent_id:

frontend/src/types/json/config_schema.json

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"openai_web": {
66
"title": "Openai Web",
77
"default": {
8+
"enabled": true,
89
"is_plus_account": true,
910
"chatgpt_base_url": null,
1011
"proxy": null,
@@ -34,6 +35,7 @@
3435
"openai_api": {
3536
"title": "Openai Api",
3637
"default": {
38+
"enabled": true,
3739
"openai_base_url": "https://api.openai.com/v1/",
3840
"proxy": null,
3941
"connect_timeout": 10,
@@ -56,12 +58,12 @@
5658
"common": {
5759
"title": "Common",
5860
"default": {
61+
"sync_conversations_on_startup": true,
62+
"sync_conversations_regularly": false,
5963
"print_sql": false,
6064
"create_initial_admin_user": true,
6165
"initial_admin_user_username": "admin",
62-
"initial_admin_user_password": "password",
63-
"sync_conversations_on_startup": true,
64-
"sync_conversations_regularly": true
66+
"initial_admin_user_password": "password"
6567
},
6668
"allOf": [
6769
{
@@ -159,6 +161,11 @@
159161
"title": "OpenaiWebChatGPTSetting",
160162
"type": "object",
161163
"properties": {
164+
"enabled": {
165+
"title": "Enabled",
166+
"default": true,
167+
"type": "boolean"
168+
},
162169
"is_plus_account": {
163170
"title": "Is Plus Account",
164171
"default": true,
@@ -226,6 +233,11 @@
226233
"title": "OpenaiApiSetting",
227234
"type": "object",
228235
"properties": {
236+
"enabled": {
237+
"title": "Enabled",
238+
"default": true,
239+
"type": "boolean"
240+
},
229241
"openai_base_url": {
230242
"title": "Openai Base Url",
231243
"default": "https://api.openai.com/v1/",
@@ -274,6 +286,16 @@
274286
"title": "CommonSetting",
275287
"type": "object",
276288
"properties": {
289+
"sync_conversations_on_startup": {
290+
"title": "Sync Conversations On Startup",
291+
"default": true,
292+
"type": "boolean"
293+
},
294+
"sync_conversations_regularly": {
295+
"title": "Sync Conversations Regularly",
296+
"default": false,
297+
"type": "boolean"
298+
},
277299
"print_sql": {
278300
"title": "Print Sql",
279301
"default": false,
@@ -293,16 +315,6 @@
293315
"title": "Initial Admin User Password",
294316
"default": "password",
295317
"type": "string"
296-
},
297-
"sync_conversations_on_startup": {
298-
"title": "Sync Conversations On Startup",
299-
"default": true,
300-
"type": "boolean"
301-
},
302-
"sync_conversations_regularly": {
303-
"title": "Sync Conversations Regularly",
304-
"default": true,
305-
"type": "boolean"
306318
}
307319
}
308320
},

frontend/src/types/json/openapi.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

frontend/src/types/json/schemas.json

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,16 @@
22562256
"title": "CommonSetting",
22572257
"type": "object",
22582258
"properties": {
2259+
"sync_conversations_on_startup": {
2260+
"title": "Sync Conversations On Startup",
2261+
"type": "boolean",
2262+
"default": true
2263+
},
2264+
"sync_conversations_regularly": {
2265+
"title": "Sync Conversations Regularly",
2266+
"type": "boolean",
2267+
"default": false
2268+
},
22592269
"print_sql": {
22602270
"title": "Print Sql",
22612271
"type": "boolean",
@@ -2275,16 +2285,6 @@
22752285
"title": "Initial Admin User Password",
22762286
"type": "string",
22772287
"default": "password"
2278-
},
2279-
"sync_conversations_on_startup": {
2280-
"title": "Sync Conversations On Startup",
2281-
"type": "boolean",
2282-
"default": true
2283-
},
2284-
"sync_conversations_regularly": {
2285-
"title": "Sync Conversations Regularly",
2286-
"type": "boolean",
2287-
"default": true
22882288
}
22892289
}
22902290
},
@@ -2329,6 +2329,11 @@
23292329
"title": "OpenaiWebChatGPTSetting",
23302330
"type": "object",
23312331
"properties": {
2332+
"enabled": {
2333+
"title": "Enabled",
2334+
"type": "boolean",
2335+
"default": true
2336+
},
23322337
"is_plus_account": {
23332338
"title": "Is Plus Account",
23342339
"type": "boolean",
@@ -2395,6 +2400,7 @@
23952400
}
23962401
],
23972402
"default": {
2403+
"enabled": true,
23982404
"is_plus_account": true,
23992405
"common_timeout": 10,
24002406
"ask_timeout": 600,
@@ -2421,6 +2427,11 @@
24212427
"title": "OpenaiApiSetting",
24222428
"type": "object",
24232429
"properties": {
2430+
"enabled": {
2431+
"title": "Enabled",
2432+
"type": "boolean",
2433+
"default": true
2434+
},
24242435
"openai_base_url": {
24252436
"title": "Openai Base Url",
24262437
"type": "string",
@@ -2473,6 +2484,7 @@
24732484
}
24742485
],
24752486
"default": {
2487+
"enabled": true,
24762488
"openai_base_url": "https://api.openai.com/v1/",
24772489
"connect_timeout": 10,
24782490
"read_timeout": 20,
@@ -2493,6 +2505,16 @@
24932505
"title": "CommonSetting",
24942506
"type": "object",
24952507
"properties": {
2508+
"sync_conversations_on_startup": {
2509+
"title": "Sync Conversations On Startup",
2510+
"type": "boolean",
2511+
"default": true
2512+
},
2513+
"sync_conversations_regularly": {
2514+
"title": "Sync Conversations Regularly",
2515+
"type": "boolean",
2516+
"default": false
2517+
},
24962518
"print_sql": {
24972519
"title": "Print Sql",
24982520
"type": "boolean",
@@ -2512,27 +2534,17 @@
25122534
"title": "Initial Admin User Password",
25132535
"type": "string",
25142536
"default": "password"
2515-
},
2516-
"sync_conversations_on_startup": {
2517-
"title": "Sync Conversations On Startup",
2518-
"type": "boolean",
2519-
"default": true
2520-
},
2521-
"sync_conversations_regularly": {
2522-
"title": "Sync Conversations Regularly",
2523-
"type": "boolean",
2524-
"default": true
25252537
}
25262538
}
25272539
}
25282540
],
25292541
"default": {
2542+
"sync_conversations_on_startup": true,
2543+
"sync_conversations_regularly": false,
25302544
"print_sql": false,
25312545
"create_initial_admin_user": true,
25322546
"initial_admin_user_username": "admin",
2533-
"initial_admin_user_password": "password",
2534-
"sync_conversations_on_startup": true,
2535-
"sync_conversations_regularly": true
2547+
"initial_admin_user_password": "password"
25362548
}
25372549
},
25382550
"http": {
@@ -3805,6 +3817,11 @@
38053817
"title": "OpenaiApiSetting",
38063818
"type": "object",
38073819
"properties": {
3820+
"enabled": {
3821+
"title": "Enabled",
3822+
"type": "boolean",
3823+
"default": true
3824+
},
38083825
"openai_base_url": {
38093826
"title": "Openai Base Url",
38103827
"type": "string",
@@ -4224,6 +4241,11 @@
42244241
"title": "OpenaiWebChatGPTSetting",
42254242
"type": "object",
42264243
"properties": {
4244+
"enabled": {
4245+
"title": "Enabled",
4246+
"type": "boolean",
4247+
"default": true
4248+
},
42274249
"is_plus_account": {
42284250
"title": "Is Plus Account",
42294251
"type": "boolean",

0 commit comments

Comments
 (0)