MCPcopy Index your code
hub / github.com/dataease/SQLBot / select_datasource

Method select_datasource

backend/apps/chat/task/llm.py:583–726  ·  view source on GitHub ↗
(self, _session: Session)

Source from the content-addressed store, hash-verified

581 yield {'recommended_question': self.record.recommended_question}
582
583 def select_datasource(self, _session: Session):
584 datasource_msg: List[Union[BaseMessage, dict[str, Any]]] = []
585 datasource_msg.append(SystemPromptMessage(self.chat_question.datasource_sys_question()))
586 if self.current_assistant and self.current_assistant.type != 4:
587 _ds_list = get_assistant_ds(session=_session, llm_service=self)
588 else:
589 stmt = select(CoreDatasource.id, CoreDatasource.name, CoreDatasource.description).where(
590 and_(CoreDatasource.oid == self.current_user.oid))
591 _ds_list = [
592 {
593 "id": ds.id,
594 "name": ds.name,
595 "description": ds.description
596 }
597 for ds in _session.exec(stmt)
598 ]
599 if not _ds_list:
600 raise SingleMessageError('No available datasource configuration found')
601 ignore_auto_select = _ds_list and len(_ds_list) == 1
602 # ignore auto select ds
603
604 full_thinking_text = ''
605 full_text = ''
606 if not ignore_auto_select:
607 if settings.TABLE_EMBEDDING_ENABLED and (
608 not self.current_assistant or (self.current_assistant and self.current_assistant.type != 1)):
609 _ds_list = get_ds_embedding(_session, self.current_user, _ds_list, self.out_ds_instance,
610 self.chat_question.question, self.current_assistant)
611 # yield {'content': '{"id":' + str(ds.get('id')) + '}'}
612
613 _ds_list_dict = []
614 for _ds in _ds_list:
615 _ds_list_dict.append(_ds)
616 datasource_msg.append(
617 HumanMessage(self.chat_question.datasource_user_question(orjson.dumps(_ds_list_dict).decode())))
618
619 self.current_logs[OperationEnum.CHOOSE_DATASOURCE] = start_log(session=_session,
620 ai_modal_id=self.chat_question.ai_modal_id,
621 ai_modal_name=self.chat_question.ai_modal_name,
622 operate=OperationEnum.CHOOSE_DATASOURCE,
623 record_id=self.record.id,
624 full_message=[{'type': msg.type,
625 'sqlbot_system': getattr(msg,
626 'sqlbot_system',
627 False) is True,
628 'content': msg.content}
629 for
630 msg in datasource_msg])
631
632 token_usage = {}
633 res = process_stream(self.llm.stream(datasource_msg), token_usage)
634 for chunk in res:
635 if chunk.get('content'):
636 full_text += chunk.get('content')
637 if chunk.get('reasoning_content'):
638 full_thinking_text += chunk.get('reasoning_content')
639 yield chunk
640 datasource_msg.append(AIMessage(full_text))

Callers 1

run_taskMethod · 0.95

Calls 15

filter_custom_promptsMethod · 0.95
init_messagesMethod · 0.95
SystemPromptMessageClass · 0.90
get_assistant_dsFunction · 0.90
SingleMessageErrorClass · 0.90
get_ds_embeddingFunction · 0.90
start_logFunction · 0.90
end_logFunction · 0.90
extract_nested_jsonFunction · 0.90
get_versionFunction · 0.90

Tested by

no test coverage detected