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

Function create_chat

backend/apps/chat/curd/chat.py:701–773  ·  view source on GitHub ↗
(session: SessionDep, current_user: CurrentUser, create_chat_obj: CreateChat,
                require_datasource: bool = True, current_assistant: CurrentAssistant = None)

Source from the content-addressed store, hash-verified

699
700
701def create_chat(session: SessionDep, current_user: CurrentUser, create_chat_obj: CreateChat,
702 require_datasource: bool = True, current_assistant: CurrentAssistant = None) -> ChatInfo:
703 if not create_chat_obj.datasource and require_datasource:
704 raise Exception("Datasource cannot be None")
705
706 if not create_chat_obj.question or create_chat_obj.question.strip() == '':
707 create_chat_obj.question = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
708
709 chat = Chat(create_time=datetime.datetime.now(),
710 create_by=current_user.id,
711 oid=current_user.oid if current_user.oid is not None else 1,
712 brief=create_chat_obj.question.strip()[:20],
713 origin=create_chat_obj.origin if create_chat_obj.origin is not None else 0)
714 ds: CoreDatasource | AssistantOutDsSchema | None = None
715 if create_chat_obj.datasource:
716 chat.datasource = create_chat_obj.datasource
717 if current_assistant and current_assistant.type == 1:
718 out_ds_instance: AssistantOutDs = AssistantOutDsFactory.get_instance(current_assistant)
719 ds = out_ds_instance.get_ds(chat.datasource)
720 ds.type_name = DB.get_db(ds.type)
721 else:
722 ds = session.get(CoreDatasource, create_chat_obj.datasource)
723 if ds.oid != current_user.oid:
724 raise Exception(f"Datasource with id {create_chat_obj.datasource} does not belong to current workspace")
725
726 if not ds:
727 raise Exception(f"Datasource with id {create_chat_obj.datasource} not found")
728
729 chat.engine_type = ds.type_name
730 else:
731 chat.engine_type = ''
732
733 chat_info = ChatInfo(**chat.model_dump())
734
735 session.add(chat)
736 session.flush()
737 session.refresh(chat)
738 chat_info.id = chat.id
739 session.commit()
740
741 if ds:
742 chat_info.datasource_exists = True
743 chat_info.datasource_name = ds.name
744 chat_info.ds_type = ds.type
745
746 if require_datasource and ds:
747 # generate first empty record
748 record = ChatRecord()
749 record.chat_id = chat.id
750 record.datasource = ds.id
751 record.engine_type = ds.type_name
752 record.first_chat = True
753 record.finish = True
754 record.create_time = datetime.datetime.now()
755 record.create_by = current_user.id
756 if isinstance(ds, CoreDatasource) and ds.recommended_config == 2:
757 questions = get_datasource_recommended_chart(session, ds.id)
758 record.recommended_question = orjson.dumps(questions).decode()

Callers 3

start_chatFunction · 0.90
mcp_startFunction · 0.90
mcp_assistantFunction · 0.90

Calls 10

ChatClass · 0.90
ChatInfoClass · 0.90
ChatRecordClass · 0.90
get_instanceMethod · 0.80
get_dsMethod · 0.80
get_dbMethod · 0.80
getMethod · 0.65
addMethod · 0.65
appendMethod · 0.45

Tested by

no test coverage detected