Skip to content

Commit 2efad70

Browse files
perf: Ignore select ds if only one ds
1 parent e575551 commit 2efad70

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

backend/apps/chat/task/llm.py

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -386,51 +386,56 @@ def select_datasource(self):
386386
]
387387
""" _ds_list = self.session.exec(select(CoreDatasource).options(
388388
load_only(CoreDatasource.id, CoreDatasource.name, CoreDatasource.description))).all() """
389-
_ds_list_dict = []
390-
for _ds in _ds_list:
391-
_ds_list_dict.append(_ds)
392-
datasource_msg.append(
393-
HumanMessage(self.chat_question.datasource_user_question(orjson.dumps(_ds_list_dict).decode())))
394-
395-
history_msg = []
396-
if self.record.full_select_datasource_message and self.record.full_select_datasource_message.strip() != '':
397-
history_msg = orjson.loads(self.record.full_select_datasource_message)
398-
399-
self.record = save_full_select_datasource_message_and_answer(session=self.session, record_id=self.record.id,
400-
answer='',
401-
full_message=orjson.dumps(history_msg +
402-
[{'type': msg.type,
403-
'content': msg.content}
404-
for msg
405-
in
406-
datasource_msg]).decode())
407-
full_thinking_text = ''
408-
full_text = ''
409-
token_usage = {}
410-
res = self.llm.stream(datasource_msg)
411-
for chunk in res:
412-
SQLBotLogUtil.info(chunk)
413-
reasoning_content_chunk = ''
414-
if 'reasoning_content' in chunk.additional_kwargs:
415-
reasoning_content_chunk = chunk.additional_kwargs.get('reasoning_content', '')
416-
# else:
417-
# reasoning_content_chunk = chunk.get('reasoning_content')
418-
if reasoning_content_chunk is None:
389+
390+
ignore_auto_select = _ds_list and len(_ds_list) == 1
391+
# ignore auto select ds
392+
393+
if not ignore_auto_select:
394+
_ds_list_dict = []
395+
for _ds in _ds_list:
396+
_ds_list_dict.append(_ds)
397+
datasource_msg.append(
398+
HumanMessage(self.chat_question.datasource_user_question(orjson.dumps(_ds_list_dict).decode())))
399+
400+
history_msg = []
401+
if self.record.full_select_datasource_message and self.record.full_select_datasource_message.strip() != '':
402+
history_msg = orjson.loads(self.record.full_select_datasource_message)
403+
404+
self.record = save_full_select_datasource_message_and_answer(session=self.session, record_id=self.record.id,
405+
answer='',
406+
full_message=orjson.dumps(history_msg +
407+
[{'type': msg.type,
408+
'content': msg.content}
409+
for msg
410+
in
411+
datasource_msg]).decode())
412+
full_thinking_text = ''
413+
full_text = ''
414+
token_usage = {}
415+
res = self.llm.stream(datasource_msg)
416+
for chunk in res:
417+
SQLBotLogUtil.info(chunk)
419418
reasoning_content_chunk = ''
420-
full_thinking_text += reasoning_content_chunk
419+
if 'reasoning_content' in chunk.additional_kwargs:
420+
reasoning_content_chunk = chunk.additional_kwargs.get('reasoning_content', '')
421+
# else:
422+
# reasoning_content_chunk = chunk.get('reasoning_content')
423+
if reasoning_content_chunk is None:
424+
reasoning_content_chunk = ''
425+
full_thinking_text += reasoning_content_chunk
421426

422-
full_text += chunk.content
423-
yield {'content': chunk.content, 'reasoning_content': reasoning_content_chunk}
424-
get_token_usage(chunk, token_usage)
425-
datasource_msg.append(AIMessage(full_text))
427+
full_text += chunk.content
428+
yield {'content': chunk.content, 'reasoning_content': reasoning_content_chunk}
429+
get_token_usage(chunk, token_usage)
430+
datasource_msg.append(AIMessage(full_text))
426431

427-
json_str = extract_nested_json(full_text)
432+
json_str = extract_nested_json(full_text)
428433

429434
_error: Exception | None = None
430435
_datasource: int | None = None
431436
_engine_type: str | None = None
432437
try:
433-
data: dict = orjson.loads(json_str)
438+
data: dict = _ds_list[0] if ignore_auto_select else orjson.loads(json_str)
434439

435440
if data.get('id') and data.get('id') != 0:
436441
_datasource = data['id']
@@ -465,17 +470,18 @@ def select_datasource(self):
465470
except Exception as e:
466471
_error = e
467472

468-
self.record = save_full_select_datasource_message_and_answer(session=self.session, record_id=self.record.id,
469-
answer=orjson.dumps({'content': full_text,
470-
'reasoning_content': full_thinking_text}).decode(),
471-
datasource=_datasource,
472-
engine_type=_engine_type,
473-
full_message=orjson.dumps(history_msg +
474-
[{'type': msg.type,
475-
'content': msg.content}
476-
for msg
477-
in
478-
datasource_msg]).decode())
473+
if not ignore_auto_select:
474+
self.record = save_full_select_datasource_message_and_answer(session=self.session, record_id=self.record.id,
475+
answer=orjson.dumps({'content': full_text,
476+
'reasoning_content': full_thinking_text}).decode(),
477+
datasource=_datasource,
478+
engine_type=_engine_type,
479+
full_message=orjson.dumps(history_msg +
480+
[{'type': msg.type,
481+
'content': msg.content}
482+
for msg
483+
in
484+
datasource_msg]).decode())
479485
self.init_messages()
480486

481487
if _error:
@@ -815,7 +821,7 @@ def run_task(self, in_chat: bool = True):
815821
SQLBotLogUtil.info(full_sql_text)
816822

817823
# todo row permission
818-
if is_normal_user(self.current_user) or (self.current_assistant and self.current_assistant.type == 1):
824+
if (not self.current_assistant and is_normal_user(self.current_user)) or (self.current_assistant and self.current_assistant.type == 1):
819825
sql_json_str = extract_nested_json(full_sql_text)
820826
data = orjson.loads(sql_json_str)
821827

0 commit comments

Comments
 (0)