@@ -121,8 +121,16 @@ def __init__(self, session: Session, current_user: CurrentUser, chat_question: C
121121 if not ds :
122122 raise SingleMessageError ("No available datasource configuration found" )
123123 chat_question .engine = (ds .type_name if ds .type != 'excel' else 'PostgreSQL' ) + get_version (ds )
124- chat_question .db_schema = get_table_schema (session = session , current_user = current_user , ds = ds ,
125- question = chat_question .question , embedding = embedding )
124+ # 延迟 get_table_schema 调用到 init_record 之后,以便记录 LLM 表选择日志
125+ self ._pending_schema_params = {
126+ 'session' : session ,
127+ 'current_user' : current_user ,
128+ 'ds' : ds ,
129+ 'question' : chat_question .question ,
130+ 'embedding' : embedding ,
131+ 'history_questions' : history_questions ,
132+ 'config' : config
133+ }
126134
127135 self .generate_sql_logs = list_generate_sql_logs (session = session , chart_id = chat_id )
128136 self .generate_chart_logs = list_generate_chart_logs (session = session , chart_id = chat_id )
@@ -230,6 +238,22 @@ def init_messages(self):
230238
231239 def init_record (self , session : Session ) -> ChatRecord :
232240 self .record = save_question (session = session , current_user = self .current_user , question = self .chat_question )
241+
242+ # 如果有延迟的 schema 获取,现在执行(此时 record 已存在,可以记录 LLM 表选择日志)
243+ if hasattr (self , '_pending_schema_params' ) and self ._pending_schema_params :
244+ params = self ._pending_schema_params
245+ self .chat_question .db_schema = get_table_schema (
246+ session = params ['session' ],
247+ current_user = params ['current_user' ],
248+ ds = params ['ds' ],
249+ question = params ['question' ],
250+ embedding = params ['embedding' ],
251+ history_questions = params ['history_questions' ],
252+ config = params ['config' ],
253+ record_id = self .record .id
254+ )
255+ self ._pending_schema_params = None
256+
233257 return self .record
234258
235259 def get_record (self ):
@@ -355,7 +379,9 @@ def generate_recommend_questions_task(self, _session: Session):
355379 session = _session ,
356380 current_user = self .current_user , ds = self .ds ,
357381 question = self .chat_question .question ,
358- embedding = False )
382+ embedding = False ,
383+ config = self .config ,
384+ record_id = self .record .id )
359385
360386 guess_msg : List [Union [BaseMessage , dict [str , Any ]]] = []
361387 guess_msg .append (SystemMessage (content = self .chat_question .guess_sys_question (self .articles_number )))
@@ -500,7 +526,9 @@ def select_datasource(self, _session: Session):
500526 self .ds )
501527 self .chat_question .db_schema = get_table_schema (session = _session ,
502528 current_user = self .current_user , ds = self .ds ,
503- question = self .chat_question .question )
529+ question = self .chat_question .question ,
530+ config = self .config ,
531+ record_id = self .record .id )
504532 _engine_type = self .chat_question .engine
505533 _chat .engine_type = _ds .type_name
506534 # save chat
@@ -1003,7 +1031,9 @@ def run_task(self, in_chat: bool = True, stream: bool = True,
10031031 session = _session ,
10041032 current_user = self .current_user ,
10051033 ds = self .ds ,
1006- question = self .chat_question .question )
1034+ question = self .chat_question .question ,
1035+ config = self .config ,
1036+ record_id = self .record .id )
10071037 else :
10081038 self .validate_history_ds (_session )
10091039
0 commit comments