1515 sys .path .insert (0 , str (_PROJECT_ROOT ))
1616
1717import config
18- from main_chat .data_ingestion .utils .log_util import log_debug , log_info , log_error , log_success , log_warning
1918
2019# Optional LangSmith tracing
2120try :
@@ -73,7 +72,7 @@ def _get_db_connection():
7372 autocommit = True ,
7473 )
7574 except Exception as exc :
76- log_debug (f"MySQL connection failed: { exc } " , file = sys .stderr )
75+ print (f"MySQL connection failed: { exc } " , file = sys .stderr )
7776 sys .exit (1 )
7877
7978 return conn
@@ -130,7 +129,7 @@ def _get_unique_values(table_name: str, column_name: str, schema: str = "public"
130129 return [row [0 ] for row in rows ]
131130 except Exception as exc :
132131 # If query times out or fails, return empty list
133- log_debug (f"[Warning] Could not fetch unique values for { table_name } .{ column_name } : { exc } " , file = sys .stderr )
132+ print (f"[Warning] Could not fetch unique values for { table_name } .{ column_name } : { exc } " , file = sys .stderr )
134133 return []
135134 finally :
136135 conn .close ()
@@ -263,7 +262,7 @@ def _read_metadata_text() -> str:
263262 data = json .load (f )
264263 return json .dumps (data , ensure_ascii = False , indent = 2 )
265264 except Exception as exc :
266- log_debug (f"Warning: could not read metadata JSON: { exc } " , file = sys .stderr )
265+ print (f"Warning: could not read metadata JSON: { exc } " , file = sys .stderr )
267266 return ""
268267
269268
@@ -411,14 +410,7 @@ def _llm_generate_sql(question: str, schema: str, default_model: str, metadata:
411410 )
412411
413412 if metadata :
414- user_prompt = (
415- "Schema:\n " + schema + "\n \n "
416- "Additional metadata (JSON):\n " + metadata + "\n \n "
417- "Instruction: Write a single MySQL SELECT to answer the question. "
418- "Always wrap table and column identifiers in backticks. "
419- "If the question is ambiguous, choose a reasonable interpretation.\n \n "
420- f"Question: { question } "
421- )
413+ user_prompt = "Schema:\n " + schema + "\n \n " "Additional metadata (JSON):\n " + metadata + "\n \n " "Instruction: Write a single MySQL SELECT to answer the question. " "Always wrap table and column identifiers in backticks. " "If the question is ambiguous, choose a reasonable interpretation.\n \n " f"Question: { question } "
422414 else :
423415 user_prompt = "Schema:\n " + schema + "\n \n " "Instruction: Write a single MySQL SELECT to answer the question. " "Always wrap table and column identifiers in backticks. " "If the question is ambiguous, choose a reasonable interpretation.\n \n " f"Question: { question } "
424416
@@ -571,15 +563,15 @@ def _execute_with_retries(
571563 for attempt_idx in range (1 , max_attempts + 1 ):
572564 try :
573565 if attempt_idx == 1 :
574- log_debug ("\n [SQL]\n " + sql + "\n " )
566+ print ("\n [SQL]\n " + sql + "\n " )
575567 else :
576- log_debug (f"\n [SQL Retry { attempt_idx - 1 } ]\n " + sql + "\n " )
568+ print (f"\n [SQL Retry { attempt_idx - 1 } ]\n " + sql + "\n " )
577569
578570 # Normalize SQL for comparison (remove extra whitespace)
579571 sql_normalized = " " .join (sql .split ())
580572 if sql_normalized in previous_sqls :
581573 # Same SQL as before - avoid infinite loop, return early
582- log_debug ("\n [Warning] SQL same as previous attempt, stopping to avoid infinite loop\n " )
574+ print ("\n [Warning] SQL same as previous attempt, stopping to avoid infinite loop\n " )
583575 result = {"columns" : [], "rows" : []}
584576 return {"result" : result , "sql" : sql }
585577
@@ -664,7 +656,7 @@ def _execute_with_retries(
664656
665657 # On second attempt with no rows, return early to avoid expensive retries
666658 if attempt_idx >= 2 :
667- log_debug (f"\n [Info] After { attempt_idx } attempts with no rows, stopping to avoid delays\n " )
659+ print (f"\n [Info] After { attempt_idx } attempts with no rows, stopping to avoid delays\n " )
668660 return {"result" : result , "sql" : sql }
669661
670662 # Build error text with unique values info (only on first retry)
@@ -710,10 +702,10 @@ def _execute_with_retries(
710702 uvals = col_info .get ("unique_values" , [])
711703 if uvals and len (uvals ) <= 150 :
712704 error_parts .append (f"\n \n Metadata unique_values for `{ col_name } ` (first 20): { ', ' .join (str (v )[:50 ] for v in uvals [:20 ])} " )
713- log_debug (f"[Debug] Injected { len (uvals )} unique_values for column `{ col_name } `" , file = sys .stderr )
705+ print (f"[Debug] Injected { len (uvals )} unique_values for column `{ col_name } `" , file = sys .stderr )
714706 break
715707 except Exception as e :
716- log_debug (f"[Debug] Exception extracting metadata unique_values: { e } " , file = sys .stderr )
708+ print (f"[Debug] Exception extracting metadata unique_values: { e } " , file = sys .stderr )
717709
718710 sql = _llm_refine_sql (
719711 question = question ,
@@ -728,7 +720,7 @@ def _execute_with_retries(
728720 err_text = str (exc )
729721 if attempt_idx == max_attempts :
730722 # On final failure, return a structured error result instead of raising
731- log_debug (f"\n [Error] SQL failed after { attempt_idx } attempts: { err_text } \n " , file = sys .stderr )
723+ print (f"\n [Error] SQL failed after { attempt_idx } attempts: { err_text } \n " , file = sys .stderr )
732724 error_result = {"columns" : [], "rows" : [], "error" : err_text }
733725 return {"result" : error_result , "sql" : sql }
734726
@@ -738,7 +730,7 @@ def _execute_with_retries(
738730
739731 # If we've seen this exact error 2+ times, stop to avoid infinite loop
740732 if error_count [error_key ] >= 2 :
741- log_debug (f"\n [Warning] Same error repeated { error_count [error_key ]} times, stopping to avoid infinite loop\n " )
733+ print (f"\n [Warning] Same error repeated { error_count [error_key ]} times, stopping to avoid infinite loop\n " )
742734 result = {"columns" : [], "rows" : []}
743735 return {"result" : result , "sql" : sql }
744736
@@ -763,7 +755,7 @@ def _execute_with_retries(
763755 # If refinement itself fails, stop after 2 attempts and return error
764756 if attempt_idx >= 2 and last_err is not None :
765757 err_text = str (last_err )
766- log_debug (f"\n [Error] SQL refinement failed after { attempt_idx } attempts: { err_text } \n " , file = sys .stderr )
758+ print (f"\n [Error] SQL refinement failed after { attempt_idx } attempts: { err_text } \n " , file = sys .stderr )
767759 error_result = {"columns" : [], "rows" : [], "error" : err_text }
768760 return {"result" : error_result , "sql" : sql }
769761 continue
@@ -856,31 +848,31 @@ def _llm_generate_answer(question: str, sql: str, result: Dict[str, Any], defaul
856848 return "\n " .join (lines )
857849
858850
859- def _log_debug_schema (database : str ) -> None :
860- log_debug ("=== Database schema (tables/columns) ===" )
861- log_debug (_fetch_schema_snapshot (database ))
851+ def _print_schema (database : str ) -> None :
852+ print ("=== Database schema (tables/columns) ===" )
853+ print (_fetch_schema_snapshot (database ))
862854
863855
864- # Pretty-log_debug a sample of the SQL result rows
865- def _log_debug_result (result : Dict [str , Any ]) -> None :
856+ # Pretty-print a sample of the SQL result rows
857+ def _print_result (result : Dict [str , Any ]) -> None :
866858 try :
867859 cols = result .get ("columns" , []) if isinstance (result , dict ) else []
868860 rows = result .get ("rows" , []) if isinstance (result , dict ) else []
869- log_debug ("[Result] rows=" , len (rows ))
861+ print ("[Result] rows=" , len (rows ))
870862 if not cols or not rows :
871863 return
872864 header = " | " .join (str (c ) for c in cols )
873- log_debug (header )
874- log_debug ("-" * len (header ))
865+ print (header )
866+ print ("-" * len (header ))
875867 max_rows = 30
876868 for r in rows [:max_rows ]:
877869 line = " | " .join (str (r .get (c , "" )) for c in cols )
878- log_debug (line )
870+ print (line )
879871 if len (rows ) > max_rows :
880- log_debug (f"... ({ len (rows ) - max_rows } more rows)" )
872+ print (f"... ({ len (rows ) - max_rows } more rows)" )
881873 except Exception :
882874 try :
883- log_debug (json .dumps (result , ensure_ascii = False , default = str )[:4000 ])
875+ print (json .dumps (result , ensure_ascii = False , default = str )[:4000 ])
884876 except Exception :
885877 pass
886878
@@ -911,7 +903,7 @@ def exec(self, prep_res):
911903
912904 def post (self , shared , prep_res , exec_res ):
913905 shared ["sql" ] = exec_res
914- log_debug ("\n [SQL]\n " + exec_res + "\n " )
906+ print ("\n [SQL]\n " + exec_res + "\n " )
915907 return "default"
916908
917909
@@ -936,7 +928,7 @@ def post(self, shared, prep_res, exec_res):
936928 shared ["result" ] = exec_res ["result" ]
937929 # Ensure shared SQL reflects the possibly refined SQL
938930 shared ["sql" ] = exec_res .get ("sql" , prep_res .get ("sql" ))
939- _log_debug_result (shared ["result" ]) # show SQL call return
931+ _print_result (shared ["result" ]) # show SQL call return
940932 return "default"
941933
942934
@@ -953,7 +945,7 @@ def exec(self, prep_res):
953945
954946 def post (self , shared , prep_res , exec_res ):
955947 shared ["answer" ] = exec_res
956- log_debug ("[Answer]\n " + exec_res + "\n " )
948+ print ("[Answer]\n " + exec_res + "\n " )
957949 return None
958950
959951
@@ -975,19 +967,19 @@ def _run_pipeline_fallback(shared: Dict[str, Any]) -> None:
975967 )
976968 shared ["sql" ] = exec_out ["sql" ]
977969 shared ["result" ] = exec_out ["result" ]
978- _log_debug_result (shared ["result" ]) # show SQL call return (fallback)
970+ _print_result (shared ["result" ]) # show SQL call return (fallback)
979971 answer = _llm_generate_answer (question , shared ["sql" ], shared ["result" ], config .GEMINI_SUMMARY_MODEL )
980972 shared ["answer" ] = answer
981- log_debug ("[Answer]\n " + answer + "\n " , flush = True )
973+ print ("[Answer]\n " + answer + "\n " , flush = True )
982974
983975
984976def _interactive_loop () -> None :
985977 if not (config .GEMINI_API_KEY ):
986- log_debug ("GEMINI_API_KEY not configured" , file = sys .stderr )
978+ print ("GEMINI_API_KEY not configured" , file = sys .stderr )
987979 sys .exit (1 )
988980
989981 database = config .PGSCHEMA
990- _log_debug_schema (database )
982+ _print_schema (database )
991983
992984 get_schema = GetSchemaNode ()
993985 gen_sql = GenerateSQLNode ()
@@ -997,12 +989,12 @@ def _interactive_loop() -> None:
997989 flow = Flow ().start (get_schema )
998990 get_schema >> gen_sql >> run_sql >> summarize
999991
1000- log_debug ("\n Type a question to query the database (or 'exit' to quit).\n " )
992+ print ("\n Type a question to query the database (or 'exit' to quit).\n " )
1001993 while True :
1002994 try :
1003995 prompt = input ("Question> " ).strip ()
1004996 except (EOFError , KeyboardInterrupt ):
1005- log_debug ()
997+ print ()
1006998 break
1007999 if not prompt :
10081000 continue
@@ -1019,7 +1011,7 @@ def _interactive_loop() -> None:
10191011 else :
10201012 flow .run (shared )
10211013 except Exception as exc :
1022- log_debug (f"Error while running flow: { exc } " , file = sys .stderr )
1014+ print (f"Error while running flow: { exc } " , file = sys .stderr )
10231015 # Fallback if no answer was produced
10241016 if not shared .get ("answer" ):
10251017 _run_pipeline_fallback (shared )
@@ -1029,7 +1021,7 @@ def main() -> None:
10291021 if len (sys .argv ) > 1 :
10301022 question = " " .join (sys .argv [1 :])
10311023 if not (config .GEMINI_API_KEY ):
1032- log_debug ("GEMINI_API_KEY not configured" , file = sys .stderr )
1024+ print ("GEMINI_API_KEY not configured" , file = sys .stderr )
10331025 sys .exit (1 )
10341026
10351027 database = config .PGSCHEMA
@@ -1051,7 +1043,7 @@ def main() -> None:
10511043 else :
10521044 flow .run (shared )
10531045 except Exception as exc :
1054- log_debug (f"Error while running flow: { exc } " , file = sys .stderr )
1046+ print (f"Error while running flow: { exc } " , file = sys .stderr )
10551047 if not shared .get ("answer" ):
10561048 _run_pipeline_fallback (shared )
10571049 else :
0 commit comments