Skip to content

Commit 661b007

Browse files
authored
refactor: update assistant i18n info (#580)
1 parent 34bad0e commit 661b007

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

backend/apps/chat/api/chat.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ async def chats(session: SessionDep, current_user: CurrentUser):
2626

2727

2828
@router.get("/{chart_id}")
29-
async def get_chat(session: SessionDep, current_user: CurrentUser, chart_id: int, current_assistant: CurrentAssistant):
29+
async def get_chat(session: SessionDep, current_user: CurrentUser, chart_id: int, current_assistant: CurrentAssistant,
30+
trans: Trans):
3031
def inner():
3132
return get_chat_with_records(chart_id=chart_id, session=session, current_user=current_user,
32-
current_assistant=current_assistant)
33+
current_assistant=current_assistant, trans=trans)
3334

3435
return await asyncio.to_thread(inner)
3536

@@ -108,7 +109,7 @@ async def start_chat(session: SessionDep, current_user: CurrentUser):
108109

109110
@router.post("/recommend_questions/{chat_record_id}")
110111
async def recommend_questions(session: SessionDep, current_user: CurrentUser, chat_record_id: int,
111-
current_assistant: CurrentAssistant, articles_number: Optional[int] = 4):
112+
current_assistant: CurrentAssistant, articles_number: Optional[int] = 4):
112113
def _return_empty():
113114
yield 'data:' + orjson.dumps({'content': '[]', 'type': 'recommended_question'}).decode() + '\n\n'
114115

@@ -134,6 +135,7 @@ def _err(_e: Exception):
134135

135136
return StreamingResponse(llm_service.await_result(), media_type="text/event-stream")
136137

138+
137139
@router.get("/recent_questions/{datasource_id}")
138140
async def recommend_questions(session: SessionDep, current_user: CurrentUser, datasource_id: int):
139141
return list_recent_questions(session=session, current_user=current_user, datasource_id=datasource_id)

backend/apps/chat/curd/chat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from apps.datasource.crud.recommended_problem import get_datasource_recommended, get_datasource_recommended_chart
1313
from apps.datasource.models.datasource import CoreDatasource, DsRecommendedProblem
1414
from apps.system.crud.assistant import AssistantOutDsFactory
15-
from common.core.deps import CurrentAssistant, SessionDep, CurrentUser
15+
from common.core.deps import CurrentAssistant, SessionDep, CurrentUser, Trans
1616
from common.utils.utils import extract_nested_json
1717

1818

@@ -191,7 +191,7 @@ def get_chat_with_records_with_data(session: SessionDep, chart_id: int, current_
191191

192192

193193
def get_chat_with_records(session: SessionDep, chart_id: int, current_user: CurrentUser,
194-
current_assistant: CurrentAssistant, with_data: bool = False) -> ChatInfo:
194+
current_assistant: CurrentAssistant, with_data: bool = False,trans: Trans = None) -> ChatInfo:
195195
chat = session.get(Chat, chart_id)
196196
if not chat:
197197
raise Exception(f"Chat with id {chart_id} not found")
@@ -200,7 +200,7 @@ def get_chat_with_records(session: SessionDep, chart_id: int, current_user: Curr
200200

201201
if current_assistant and current_assistant.type in dynamic_ds_types:
202202
out_ds_instance = AssistantOutDsFactory.get_instance(current_assistant)
203-
ds = out_ds_instance.get_ds(chat.datasource)
203+
ds = out_ds_instance.get_ds(chat.datasource,trans)
204204
else:
205205
ds = session.get(CoreDatasource, chat.datasource) if chat.datasource else None
206206

backend/apps/system/crud/assistant.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from common.core.sqlbot_cache import cache
2121
from common.utils.aes_crypto import simple_aes_decrypt
2222
from common.utils.utils import equals_ignore_case, string_to_numeric_hash
23+
from common.core.deps import Trans
2324

2425

2526
@cache(namespace=CacheNamespace.EMBEDDED_INFO, cacheName=CacheName.ASSISTANT_INFO, keyExpression="assistant_id")
@@ -143,12 +144,12 @@ def get_ds_from_api(self):
143144
raise Exception(f"Failed to get datasource list from {endpoint}, error: {result_json.get('message')}")
144145
else:
145146
raise Exception(f"Failed to get datasource list from {endpoint}, status code: {res.status_code}")
146-
147+
147148
def get_first_element(self, text: str):
148149
parts = re.split(r'[,;]', text.strip())
149150
first_domain = parts[0].strip()
150151
return first_domain
151-
152+
152153
def get_complete_endpoint(self, endpoint: str) -> str | None:
153154
if endpoint.startswith("http://") or endpoint.startswith("https://"):
154155
return endpoint
@@ -158,8 +159,8 @@ def get_complete_endpoint(self, endpoint: str) -> str | None:
158159
if ',' in domain_text or ';' in domain_text:
159160
return (self.request_origin.strip('/') if self.request_origin else self.get_first_element(domain_text).strip('/')) + endpoint
160161
else:
161-
return f"{domain_text}{endpoint}"
162-
162+
return f"{domain_text}{endpoint}"
163+
163164
def get_simple_ds_list(self):
164165
if self.ds_list:
165166
return [{'id': ds.id, 'name': ds.name, 'description': ds.comment} for ds in self.ds_list]
@@ -205,14 +206,14 @@ def get_db_schema(self, ds_id: int, question: str, embedding: bool = True) -> st
205206

206207
return schema_str
207208

208-
def get_ds(self, ds_id: int):
209+
def get_ds(self, ds_id: int,trans: Trans = None):
209210
if self.ds_list:
210211
for ds in self.ds_list:
211212
if ds.id == ds_id:
212213
return ds
213214
else:
214215
raise Exception("Datasource list is not found.")
215-
raise Exception(f"Datasource with id {ds_id} not found.")
216+
raise Exception(f"Datasource id {ds_id} is not found." if trans is None else trans('i18n_data_training.datasource_id_not_found', key=ds_id))
216217

217218
def convert2schema(self, ds_dict: dict, config: dict[any]) -> AssistantOutDsSchema:
218219
id_marker: str = ''

0 commit comments

Comments
 (0)