feat: Implement async AI analysis for email threads
- Added `get_latest_email_date()` function in `database.py` to retrieve the most recent email date for a given account and folder. - Enhanced `fetch_folder_emails()` in `zoho_client.py` to intelligently determine the start date for fetching emails based on the latest email date in the database. - Introduced `analyze_and_update_threads_async()` for asynchronous analysis of email threads, allowing concurrent processing. - Created a synchronous wrapper `analyze_and_update_threads()` for easier integration. - Updated `fetch_emails()` to support database session and account email parameters. - Added comprehensive documentation in `AI_ANALYSIS_GUIDE.md` detailing the new AI analysis functionality. - Implemented tests for the new features, including `test_fetch_with_db.py`, `test_ai_analysis.py`, and `test_single_analysis.py`. - Added error handling and logging improvements throughout the codebase.
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to validate Groq API configuration and diagnose issues.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Add the src directory to the path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
|
||||
|
||||
from ai import analyze_thread
|
||||
|
||||
|
||||
def test_groq_api():
|
||||
"""Test the Groq API with a simple request."""
|
||||
|
||||
print("Testing Groq API Configuration")
|
||||
print("=" * 40)
|
||||
|
||||
# Check API key
|
||||
api_key = os.getenv("GROQ_API_KEY")
|
||||
if not api_key:
|
||||
print("❌ GROQ_API_KEY environment variable not set")
|
||||
print(" Please set your Groq API key: export GROQ_API_KEY='your-key-here'")
|
||||
return False
|
||||
|
||||
print(f"✓ GROQ_API_KEY found (length: {len(api_key)})")
|
||||
|
||||
# Check model
|
||||
model = os.getenv("GROQ_MODEL", "llama-3.1-70b-versatile")
|
||||
print(f"✓ Using model: {model}")
|
||||
|
||||
# Test with simple data
|
||||
print("\nTesting simple analysis...")
|
||||
|
||||
simple_messages = [
|
||||
{
|
||||
"date_sent": "2025-08-11 10:00:00",
|
||||
"is_incoming": True,
|
||||
"subject": "Test Question",
|
||||
"from_email": "test@example.com",
|
||||
"to_email": "user@example.com",
|
||||
"body": "Can you help me with this issue? Please let me know.",
|
||||
}
|
||||
]
|
||||
|
||||
try:
|
||||
result = analyze_thread("Test Question", simple_messages)
|
||||
print("✓ Analysis successful!")
|
||||
print(f" Model used: {result.get('model', 'unknown')}")
|
||||
print(f" Actionable: {result.get('actionable', False)}")
|
||||
print(f" Confidence: {result.get('confidence', 0)}")
|
||||
print(f" Summary: {result.get('summary', 'No summary')[:100]}...")
|
||||
|
||||
if result.get("model") == "heuristic":
|
||||
print("\n⚠️ Note: Fell back to heuristic analysis")
|
||||
print(" This might indicate an API issue")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Analysis failed: {e}")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
def test_rate_limiting():
|
||||
"""Test rate limiting by making multiple quick requests."""
|
||||
|
||||
print("\nTesting Rate Limiting")
|
||||
print("=" * 40)
|
||||
|
||||
import time
|
||||
|
||||
simple_messages = [
|
||||
{
|
||||
"date_sent": "2025-08-11 10:00:00",
|
||||
"is_incoming": True,
|
||||
"subject": "Quick test",
|
||||
"from_email": "test@example.com",
|
||||
"to_email": "user@example.com",
|
||||
"body": "Quick test message.",
|
||||
}
|
||||
]
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
for i in range(3):
|
||||
print(f"Request {i + 1}...")
|
||||
result = analyze_thread(f"Test {i + 1}", simple_messages)
|
||||
print(f" Result: {result.get('model', 'unknown')} analysis")
|
||||
|
||||
total_time = time.time() - start_time
|
||||
print(f"\nTotal time for 3 requests: {total_time:.2f} seconds")
|
||||
|
||||
if total_time < 2.5:
|
||||
print("⚠️ Requests completed very quickly - rate limiting may not be working")
|
||||
else:
|
||||
print("✓ Rate limiting appears to be working")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = test_groq_api()
|
||||
|
||||
if success:
|
||||
test_rate_limiting()
|
||||
|
||||
print("\nTroubleshooting Tips:")
|
||||
print("- If getting 400 errors: Check message content for special characters")
|
||||
print("- If getting 401 errors: Verify GROQ_API_KEY is correct")
|
||||
print(
|
||||
"- If getting 429 errors: Reduce max_concurrent in analyze_and_update_threads()"
|
||||
)
|
||||
print("- If getting 503 errors: Groq service may be temporarily unavailable")
|
||||
Reference in New Issue
Block a user