124 lines
No EOL
4.4 KiB
Python
124 lines
No EOL
4.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to verify chat history parsing with actual talestorm-ai format
|
|
"""
|
|
|
|
from src.services.chat_service import ChatService
|
|
from src.api.v1.models import HistoryItem
|
|
|
|
# Sample data from talestorm-ai
|
|
sample_history = [
|
|
{
|
|
"id": "45d3e5f0-21cf-47b3-8c24-0d273107ab7f",
|
|
"chat_session_id": "f250bb60-d619-4802-914f-0594d2a52bfa",
|
|
"content": [
|
|
{
|
|
"parts": [
|
|
{
|
|
"content": "hi",
|
|
"timestamp": "2025-07-25T09:49:29.412466Z",
|
|
"part_kind": "user-prompt"
|
|
}
|
|
],
|
|
"instructions": "You are an expert insurance assistant. You may only answer questions about insurance plans, coverage, benefits, and related terms. If asked anything outside that domain, reply: 'I'm sorry, I can only answer insurance-related questions.'",
|
|
"kind": "request"
|
|
},
|
|
{
|
|
"parts": [
|
|
{
|
|
"content": "Hello! How can I assist you with your insurance questions today?",
|
|
"part_kind": "text"
|
|
}
|
|
],
|
|
"usage": {
|
|
"requests": 1,
|
|
"request_tokens": 137,
|
|
"response_tokens": 14,
|
|
"total_tokens": 151,
|
|
"details": {
|
|
"accepted_prediction_tokens": 0,
|
|
"audio_tokens": 0,
|
|
"reasoning_tokens": 0,
|
|
"rejected_prediction_tokens": 0,
|
|
"cached_tokens": 0
|
|
}
|
|
},
|
|
"model_name": "gpt-4.1-mini-2025-04-14",
|
|
"timestamp": "2025-07-25T09:49:30Z",
|
|
"kind": "response",
|
|
"vendor_details": None,
|
|
"vendor_id": "chatcmpl-Bx9LOZPgBbHOblSUd6LcuV3qIYEwz"
|
|
}
|
|
],
|
|
"created_at": "2025-07-25T09:40:20.797430"
|
|
}
|
|
]
|
|
|
|
def parse_history_manually(messages):
|
|
"""Manually parse the history to test our logic"""
|
|
history = []
|
|
|
|
for message in messages:
|
|
content = message.get("content", [])
|
|
|
|
for item in content:
|
|
if not isinstance(item, dict):
|
|
continue
|
|
|
|
kind = item.get("kind", "")
|
|
|
|
# Handle request messages (user input)
|
|
if kind == "request":
|
|
parts = item.get("parts", [])
|
|
for part in parts:
|
|
if part.get("part_kind") == "user-prompt":
|
|
history.append(HistoryItem(
|
|
role="user",
|
|
message=part.get("content", "")
|
|
))
|
|
|
|
# Handle response messages (assistant output)
|
|
elif kind == "response":
|
|
parts = item.get("parts", [])
|
|
for part in parts:
|
|
if part.get("part_kind") == "text":
|
|
history.append(HistoryItem(
|
|
role="assistant",
|
|
message=part.get("content", "")
|
|
))
|
|
|
|
return history
|
|
|
|
def test_history_parsing():
|
|
"""Test the history parsing logic"""
|
|
print("Testing History Parsing...")
|
|
print("=" * 50)
|
|
|
|
# Parse the sample history
|
|
history = parse_history_manually(sample_history)
|
|
|
|
print(f"Found {len(history)} history items:")
|
|
for i, item in enumerate(history, 1):
|
|
print(f" {i}. {item.role}: {item.message}")
|
|
|
|
# Verify the expected results
|
|
expected_user_message = "hi"
|
|
expected_assistant_message = "Hello! How can I assist you with your insurance questions today?"
|
|
|
|
if len(history) == 2:
|
|
if history[0].role == "user" and history[0].message == expected_user_message:
|
|
print("✅ User message parsed correctly")
|
|
else:
|
|
print("❌ User message parsing failed")
|
|
|
|
if history[1].role == "assistant" and history[1].message == expected_assistant_message:
|
|
print("✅ Assistant message parsed correctly")
|
|
else:
|
|
print("❌ Assistant message parsing failed")
|
|
else:
|
|
print(f"❌ Expected 2 history items, got {len(history)}")
|
|
|
|
print("\n✅ History parsing test completed!")
|
|
|
|
if __name__ == "__main__":
|
|
test_history_parsing() |