Skip to content

Commit 7d2232d

Browse files
committed
feat: added tests for cot interface
1 parent 8a5d594 commit 7d2232d

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

agentic_rag/tests/test_cot_chat.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
# Process through chat function
71+
logger.info("Processing through chat function...")
72+
result = chat(
73+
message=test_message,
74+
history=history,
75+
agent_type="ollama:phi3",
76+
use_cot=True,
77+
collection="PDF Collection"
78+
)
79+
logger.info("Chat processing completed")
80+
debug_response_structure(result, "Final result: ")
81+
82+
except Exception as e:
83+
logger.error(f"Error during processing: {str(e)}", exc_info=True)
84+
raise
85+
86+
# Log final state
87+
logger.info("Final state:")
88+
logger.info(f"Result type: {type(result)}")
89+
logger.info(f"Result length: {len(result)}")
90+
91+
# Save debug information to file
92+
debug_info = {
93+
"test_message": test_message,
94+
"raw_response": str(raw_response),
95+
"final_result": str(result),
96+
"history": str(history)
97+
}
98+
99+
with open("cot_chat_debug.json", "w") as f:
100+
json.dump(debug_info, f, indent=2)
101+
102+
logger.info("Debug information saved to cot_chat_debug.json")
103+
104+
except Exception as e:
105+
logger.error(f"Test failed: {str(e)}", exc_info=True)
106+
raise
107+
108+
if __name__ == "__main__":
109+
test_cot_chat()

0 commit comments

Comments
 (0)