Skip to content

Commit 3c948d7

Browse files
authored
fix: Optimize the internationalization prompt of the assistant (#546)
1 parent 60f2d1b commit 3c948d7

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

backend/apps/chat/curd/chat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
from idlelib.pyparse import trans
23
from typing import List
34
from sqlalchemy import desc, func
45

@@ -398,7 +399,7 @@ def create_chat(session: SessionDep, current_user: CurrentUser, create_chat_obj:
398399
ds = session.get(CoreDatasource, create_chat_obj.datasource)
399400

400401
if not ds:
401-
raise Exception(f"Datasource with id {create_chat_obj.datasource} not found")
402+
raise Exception(trans('i18n_data_training.datasource_id_not_found', key=create_chat_obj.datasource))
402403

403404
chat.engine_type = ds.type_name
404405
else:

backend/apps/system/crud/assistant.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import re
33
import urllib
4+
from idlelib.pyparse import trans
45
from typing import Optional
56

67
import requests
@@ -116,7 +117,8 @@ def get_ds_from_api(self):
116117
endpoint: str = config['endpoint']
117118
endpoint = self.get_complete_endpoint(endpoint=endpoint)
118119
if not endpoint:
119-
raise Exception(f"Failed to get datasource list from {config['endpoint']}, error: [Assistant domain or endpoint miss]")
120+
raise Exception(
121+
f"Failed to get datasource list from {config['endpoint']}, error: [Assistant domain or endpoint miss]")
120122
certificateList: list[any] = json.loads(self.certificate)
121123
header = {}
122124
cookies = {}
@@ -143,23 +145,25 @@ def get_ds_from_api(self):
143145
raise Exception(f"Failed to get datasource list from {endpoint}, error: {result_json.get('message')}")
144146
else:
145147
raise Exception(f"Failed to get datasource list from {endpoint}, status code: {res.status_code}")
146-
148+
147149
def get_first_element(self, text: str):
148150
parts = re.split(r'[,;]', text.strip())
149151
first_domain = parts[0].strip()
150152
return first_domain
151-
153+
152154
def get_complete_endpoint(self, endpoint: str) -> str | None:
153155
if endpoint.startswith("http://") or endpoint.startswith("https://"):
154156
return endpoint
155157
domain_text = self.assistant.domain
156158
if not domain_text:
157159
return None
158160
if ',' in domain_text or ';' in domain_text:
159-
return (self.request_origin.strip('/') if self.request_origin else self.get_first_element(domain_text).strip('/')) + endpoint
161+
return (
162+
self.request_origin.strip('/') if self.request_origin else self.get_first_element(domain_text).strip(
163+
'/')) + endpoint
160164
else:
161-
return f"{domain_text}{endpoint}"
162-
165+
return f"{domain_text}{endpoint}"
166+
163167
def get_simple_ds_list(self):
164168
if self.ds_list:
165169
return [{'id': ds.id, 'name': ds.name, 'description': ds.comment} for ds in self.ds_list]
@@ -211,8 +215,8 @@ def get_ds(self, ds_id: int):
211215
if ds.id == ds_id:
212216
return ds
213217
else:
214-
raise Exception("Datasource list is not found.")
215-
raise Exception(f"Datasource with id {ds_id} not found.")
218+
raise Exception(trans('i18n_data_training.datasource_list_is_not_found'))
219+
raise Exception(trans('i18n_data_training.datasource_id_not_found', key=ds_id))
216220

217221
def convert2schema(self, ds_dict: dict, config: dict[any]) -> AssistantOutDsSchema:
218222
id_marker: str = ''
@@ -275,17 +279,17 @@ def get_ds_engine(ds: AssistantOutDsSchema) -> Engine:
275279
return engine
276280

277281

278-
def get_out_ds_conf(ds: AssistantOutDsSchema, timeout:int=30) -> str:
282+
def get_out_ds_conf(ds: AssistantOutDsSchema, timeout: int = 30) -> str:
279283
conf = {
280-
"host":ds.host or '',
281-
"port":ds.port or 0,
282-
"username":ds.user or '',
283-
"password":ds.password or '',
284-
"database":ds.dataBase or '',
285-
"driver":'',
286-
"extraJdbc":ds.extraParams or '',
287-
"dbSchema":ds.db_schema or '',
288-
"timeout":timeout or 30
284+
"host": ds.host or '',
285+
"port": ds.port or 0,
286+
"username": ds.user or '',
287+
"password": ds.password or '',
288+
"database": ds.dataBase or '',
289+
"driver": '',
290+
"extraJdbc": ds.extraParams or '',
291+
"dbSchema": ds.db_schema or '',
292+
"timeout": timeout or 30
289293
}
290294
conf["extraJdbc"] = ''
291295
return aes_encrypt(json.dumps(conf))

backend/locales/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
"datasource_not_found": "Datasource not found"
6666
},
6767
"i18n_data_training": {
68+
"datasource_list_is_not_found": "Data source list not found",
69+
"datasource_id_not_found": "Data source ID: {key} not found",
6870
"datasource_cannot_be_none": "Datasource cannot be empty",
6971
"datasource_assistant_cannot_be_none": "Datasource or advanced application cannot both be empty",
7072
"data_training_not_exists": "This example does not exist",

backend/locales/ko-KR.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
"invalid_origin": "도메인 이름 검증 실패 【{origin}】"
4040
},
4141
"i18n_terminology": {
42+
"datasource_list_is_not_found": "데이터 소스 목록을 찾을 수 없습니다",
43+
"datasource_id_not_found": "데이터 소스 ID: {key}를 찾을 수 없습니다",
4244
"terminology_not_exists": "이 용어가 존재하지 않습니다",
4345
"datasource_cannot_be_none": "데이터 소스는 비울 수 없습니다",
4446
"cannot_be_repeated": "용어 이름과 동의어는 반복될 수 없습니다",

backend/locales/zh-CN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
"datasource_not_found": "找不到数据源"
6666
},
6767
"i18n_data_training": {
68+
"datasource_list_is_not_found": "数据源列表未找到",
69+
"datasource_id_not_found": "数据源ID: {key} 未找到",
6870
"datasource_cannot_be_none": "数据源不能为空",
6971
"datasource_assistant_cannot_be_none": "数据源或高级应用不能都为空",
7072
"data_training_not_exists": "该示例不存在",

0 commit comments

Comments
 (0)