1+ import sys
2+ import logging
3+ import json
4+ from pathlib import Path
5+
6+ # Add parent directory to path to import modules
7+ sys .path .append (str (Path (__file__ ).parent .parent ))
8+
9+ from gradio_app import chat
10+ from store import VectorStore
11+ from local_rag_agent import LocalRAGAgent
12+
13+ # Configure logging
14+ logging .basicConfig (
15+ level = logging .DEBUG ,
16+ format = '%(asctime)s | %(name)s | %(levelname)s | %(message)s' ,
17+ handlers = [
18+ logging .StreamHandler (sys .stdout )
19+ ]
20+ )
21+
22+ logger = logging .getLogger (__name__ )
23+
24+ def debug_response_structure (response , prefix = "" ):
25+ """Helper function to debug response structure"""
26+ logger .debug (f"{ prefix } Response type: { type (response )} " )
27+ if isinstance (response , dict ):
28+ logger .debug (f"{ prefix } Response keys: { list (response .keys ())} " )
29+ for key , value in response .items ():
30+ logger .debug (f"{ prefix } Key '{ key } ' type: { type (value )} " )
31+ if isinstance (value , list ):
32+ logger .debug (f"{ prefix } List length: { len (value )} " )
33+ if value and isinstance (value [0 ], dict ):
34+ logger .debug (f"{ prefix } First item keys: { list (value [0 ].keys ())} " )
35+ elif isinstance (response , str ):
36+ logger .debug (f"{ prefix } String length: { len (response )} " )
37+ logger .debug (f"{ prefix } First 100 chars: { response [:100 ]} " )
38+
39+ def test_cot_chat ():
40+ """Test the CoT chat interface with detailed logging"""
41+ try :
42+ # Initialize components
43+ logger .info ("Initializing vector store..." )
44+ vector_store = VectorStore ()
45+
46+ logger .info ("Initializing local agent..." )
47+ agent = LocalRAGAgent (vector_store , model_name = "ollama:phi3" , use_cot = True )
48+
49+ # Test message
50+ test_message = "What is self-instruct in AI?"
51+ logger .info (f"Test message: { test_message } " )
52+
53+ # Initialize empty chat history
54+ history = []
55+
56+ # Log initial state
57+ logger .info ("Initial state:" )
58+ logger .info (f"History type: { type (history )} " )
59+ logger .info (f"History length: { len (history )} " )
60+
61+ # Process the chat
62+ logger .info ("Processing chat..." )
63+ try :
64+ # Get raw response from agent
65+ logger .info ("Getting raw response from agent..." )
66+ raw_response = agent .process_query (test_message )
67+ logger .info ("Raw response received" )
68+ debug_response_structure (raw_response , "Raw response: " )
69+
70+ # Verify response structure
71+ if not isinstance (raw_response , dict ):
72+ logger .error (f"Unexpected response type: { type (raw_response )} " )
73+ raise TypeError (f"Expected dict response, got { type (raw_response )} " )
74+
75+ required_keys = ["answer" , "reasoning_steps" , "context" ]
76+ missing_keys = [key for key in required_keys if key not in raw_response ]
77+ if missing_keys :
78+ logger .error (f"Missing required keys in response: { missing_keys } " )
79+ raise KeyError (f"Response missing required keys: { missing_keys } " )
80+
81+ # Process through chat function
82+ logger .info ("Processing through chat function..." )
83+ result = chat (
84+ message = test_message ,
85+ history = history ,
86+ agent_type = "ollama:phi3" ,
87+ use_cot = True ,
88+ collection = "PDF Collection"
89+ )
90+ logger .info ("Chat processing completed" )
91+ debug_response_structure (result , "Final result: " )
92+
93+ except Exception as e :
94+ logger .error (f"Error during processing: { str (e )} " , exc_info = True )
95+ raise
96+
97+ # Log final state
98+ logger .info ("Final state:" )
99+ logger .info (f"Result type: { type (result )} " )
100+ logger .info (f"Result length: { len (result )} " )
101+
102+ # Save debug information to file
103+ debug_info = {
104+ "test_message" : test_message ,
105+ "raw_response" : {
106+ "type" : str (type (raw_response )),
107+ "keys" : list (raw_response .keys ()) if isinstance (raw_response , dict ) else None ,
108+ "content" : str (raw_response )
109+ },
110+ "final_result" : {
111+ "type" : str (type (result )),
112+ "length" : len (result ) if isinstance (result , list ) else None ,
113+ "content" : str (result )
114+ },
115+ "history" : {
116+ "type" : str (type (history )),
117+ "length" : len (history ),
118+ "content" : str (history )
119+ }
120+ }
121+
122+ with open ("cot_chat_debug.json" , "w" ) as f :
123+ json .dump (debug_info , f , indent = 2 )
124+
125+ logger .info ("Debug information saved to cot_chat_debug.json" )
126+
127+ except Exception as e :
128+ logger .error (f"Test failed: { str (e )} " , exc_info = True )
129+ raise
130+
131+ if __name__ == "__main__" :
132+ test_cot_chat ()
0 commit comments