@@ -74,24 +74,25 @@ def list_records(session: SessionDep, chart_id: int, current_user: CurrentUser)
7474 return record_list
7575
7676
77- def create_chat (session : SessionDep , current_user : CurrentUser , create_chat_obj : CreateChat ) -> ChatInfo :
78- if not create_chat_obj .datasource :
77+ def create_chat (session : SessionDep , current_user : CurrentUser , create_chat_obj : CreateChat , require_datasource : bool = True ) -> ChatInfo :
78+ if not create_chat_obj .datasource and require_datasource :
7979 raise Exception ("Datasource cannot be None" )
8080
8181 if not create_chat_obj .question or create_chat_obj .question .strip () == '' :
8282 create_chat_obj .question = datetime .datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
8383
8484 chat = Chat (create_time = datetime .datetime .now (),
8585 create_by = current_user .id ,
86- brief = create_chat_obj .question .strip ()[:20 ],
87- datasource = create_chat_obj .datasource )
88-
89- ds = session .query (CoreDatasource ).filter (CoreDatasource .id == create_chat_obj .datasource ).first ()
86+ brief = create_chat_obj .question .strip ()[:20 ])
87+ ds : CoreDatasource = None
88+ if create_chat_obj .datasource :
89+ chat .datasource = create_chat_obj .datasource
90+ ds = session .query (CoreDatasource ).filter (CoreDatasource .id == create_chat_obj .datasource ).first ()
9091
91- if not ds :
92- raise Exception (f"Datasource with id { create_chat_obj .datasource } not found" )
92+ if not ds :
93+ raise Exception (f"Datasource with id { create_chat_obj .datasource } not found" )
9394
94- chat .engine_type = ds .type_name
95+ chat .engine_type = ds .type_name
9596
9697 chat_info = ChatInfo (** chat .model_dump ())
9798
@@ -101,6 +102,13 @@ def create_chat(session: SessionDep, current_user: CurrentUser, create_chat_obj:
101102 chat_info .id = chat .id
102103 session .commit ()
103104
105+ if not create_chat_obj .datasource :
106+ # use AI to get ds
107+
108+ if not ds :
109+ raise Exception (f"Datasource with id { create_chat_obj .datasource } not found" )
110+
111+
104112 chat_info .datasource_exists = True
105113 chat_info .datasource_name = ds .name
106114
0 commit comments