11import json
22import sqlparse
3- import logging
43import traceback
54import warnings
65from typing import Any , List , Optional , Union , Dict
3534from apps .system .schemas .system_schema import AssistantOutDsSchema
3635from common .core .config import settings
3736from common .core .deps import CurrentAssistant , SessionDep , CurrentUser
38- from common .utils .utils import extract_nested_json
37+ from common .utils .utils import SQLBotLogUtil , extract_nested_json
3938
4039warnings .filterwarnings ("ignore" )
4140
@@ -223,7 +222,7 @@ def generate_analysis(self):
223222 res = self .llm .stream (analysis_msg )
224223 token_usage = {}
225224 for chunk in res :
226- print (chunk )
225+ SQLBotLogUtil . info (chunk )
227226 reasoning_content_chunk = ''
228227 if 'reasoning_content' in chunk .additional_kwargs :
229228 reasoning_content_chunk = chunk .additional_kwargs .get ('reasoning_content' , '' )
@@ -273,7 +272,7 @@ def generate_predict(self):
273272 res = self .llm .stream (predict_msg )
274273 token_usage = {}
275274 for chunk in res :
276- print (chunk )
275+ SQLBotLogUtil . info (chunk )
277276 reasoning_content_chunk = ''
278277 if 'reasoning_content' in chunk .additional_kwargs :
279278 reasoning_content_chunk = chunk .additional_kwargs .get ('reasoning_content' , '' )
@@ -323,7 +322,7 @@ def generate_recommend_questions_task(self):
323322 token_usage = {}
324323 res = self .llm .stream (guess_msg )
325324 for chunk in res :
326- print (chunk )
325+ SQLBotLogUtil . info (chunk )
327326 reasoning_content_chunk = ''
328327 if 'reasoning_content' in chunk .additional_kwargs :
329328 reasoning_content_chunk = chunk .additional_kwargs .get ('reasoning_content' , '' )
@@ -390,7 +389,7 @@ def select_datasource(self):
390389 token_usage = {}
391390 res = self .llm .stream (datasource_msg )
392391 for chunk in res :
393- print (chunk )
392+ SQLBotLogUtil . info (chunk )
394393 reasoning_content_chunk = ''
395394 if 'reasoning_content' in chunk .additional_kwargs :
396395 reasoning_content_chunk = chunk .additional_kwargs .get ('reasoning_content' , '' )
@@ -474,7 +473,7 @@ def generate_sql(self):
474473 token_usage = {}
475474 res = self .llm .stream (self .sql_message )
476475 for chunk in res :
477- print (chunk )
476+ SQLBotLogUtil . info (chunk )
478477 reasoning_content_chunk = ''
479478 if 'reasoning_content' in chunk .additional_kwargs :
480479 reasoning_content_chunk = chunk .additional_kwargs .get ('reasoning_content' , '' )
@@ -543,7 +542,7 @@ def generate_filter(self, sql: str, tables: List):
543542 res = self .llm .stream (msg )
544543 token_usage = {}
545544 for chunk in res :
546- print (chunk )
545+ SQLBotLogUtil . info (chunk )
547546 reasoning_content_chunk = ''
548547 if 'reasoning_content' in chunk .additional_kwargs :
549548 reasoning_content_chunk = chunk .additional_kwargs .get ('reasoning_content' , '' )
@@ -567,7 +566,7 @@ def generate_filter(self, sql: str, tables: List):
567566 # 'content': msg.content} for msg
568567 # in
569568 # analysis_msg]).decode())
570- print (full_filter_text )
569+ SQLBotLogUtil . info (full_filter_text )
571570 return full_filter_text
572571
573572 def generate_chart (self ):
@@ -582,7 +581,7 @@ def generate_chart(self):
582581 token_usage = {}
583582 res = self .llm .stream (self .chart_message )
584583 for chunk in res :
585- print (chunk )
584+ SQLBotLogUtil . info (chunk )
586585 reasoning_content_chunk = ''
587586 if 'reasoning_content' in chunk .additional_kwargs :
588587 reasoning_content_chunk = chunk .additional_kwargs .get ('reasoning_content' , '' )
@@ -691,7 +690,7 @@ def execute_sql(self, sql: str):
691690 Returns:
692691 Query results
693692 """
694- print (f"Executing SQL on ds_id { self .ds .id } : { sql } " )
693+ SQLBotLogUtil . info (f"Executing SQL on ds_id { self .ds .id } : { sql } " )
695694 return exec_sql (self .ds , sql )
696695
697696
@@ -717,7 +716,7 @@ def execute_sql_with_db(db: SQLDatabase, sql: str) -> str:
717716
718717 except Exception as e :
719718 error_msg = f"SQL execution failed: { str (e )} "
720- logging . error (error_msg )
719+ SQLBotLogUtil . exception (error_msg )
721720 raise RuntimeError (error_msg )
722721
723722
@@ -741,7 +740,7 @@ def run_task(llm_service: LLMService, in_chat: bool = True):
741740 ds_res = llm_service .select_datasource ()
742741
743742 for chunk in ds_res :
744- print (chunk )
743+ SQLBotLogUtil . info (chunk )
745744 if in_chat :
746745 yield orjson .dumps (
747746 {'content' : chunk .get ('content' ), 'reasoning_content' : chunk .get ('reasoning_content' ),
@@ -765,7 +764,7 @@ def run_task(llm_service: LLMService, in_chat: bool = True):
765764 yield orjson .dumps ({'type' : 'info' , 'msg' : 'sql generated' }).decode () + '\n \n '
766765
767766 # filter sql
768- print (full_sql_text )
767+ SQLBotLogUtil . info (full_sql_text )
769768
770769 # todo row permission
771770 sql_json_str = extract_nested_json (full_sql_text )
@@ -785,11 +784,11 @@ def run_task(llm_service: LLMService, in_chat: bool = True):
785784 raise Exception ("SQL query is empty" )
786785
787786 sql_result = llm_service .generate_filter (data .get ('sql' ), data .get ('tables' )) # maybe no sql and tables
788- print (sql_result )
787+ SQLBotLogUtil . info (sql_result )
789788 sql = llm_service .check_save_sql (res = sql_result )
790789 # sql = llm_service.check_save_sql(res=full_sql_text)
791790
792- print (sql )
791+ SQLBotLogUtil . info (sql )
793792 format_sql = sqlparse .format (sql , reindent = True )
794793 if in_chat :
795794 yield orjson .dumps ({'content' : format_sql , 'type' : 'sql' }).decode () + '\n \n '
@@ -815,9 +814,9 @@ def run_task(llm_service: LLMService, in_chat: bool = True):
815814 yield orjson .dumps ({'type' : 'info' , 'msg' : 'chart generated' }).decode () + '\n \n '
816815
817816 # filter chart
818- print (full_chart_text )
817+ SQLBotLogUtil . info (full_chart_text )
819818 chart = llm_service .check_save_chart (res = full_chart_text )
820- print (chart )
819+ SQLBotLogUtil . info (chart )
821820 if in_chat :
822821 yield orjson .dumps ({'content' : orjson .dumps (chart ).decode (), 'type' : 'chart' }).decode () + '\n \n '
823822 else :
@@ -856,7 +855,7 @@ def run_task(llm_service: LLMService, in_chat: bool = True):
856855 if chart ['type' ] != 'table' :
857856 yield '### generated chart picture\n \n '
858857 image_url = request_picture (llm_service .record .chat_id , llm_service .record .id , chart , result )
859- print (image_url )
858+ SQLBotLogUtil . info (image_url )
860859 yield f'![{ chart ["type" ]} ]({ image_url } )'
861860 except Exception as e :
862861 traceback .print_exc ()
0 commit comments