Files
SPARC/test_database_mode.py
T
0xWheatyz 44456cb073 feat: add database mode for LLM message storage and analytics
Implements a database mode that stores LLM prompts and responses in PostgreSQL
instead of making API calls. This enables:
- Testing without consuming API credits
- Collecting analytics on usage patterns
- Development and debugging workflows

Changes:
- Added DatabaseClient class for PostgreSQL operations
- Modified LLMAnalyzer to support database/API mode toggle
- Added USE_DATABASE config flag to switch between modes
- Included Docker Compose setup for PostgreSQL
- Added utility scripts for database init and analytics viewing
- Comprehensive documentation in DATABASE_MODE.md

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-03-10 21:13:13 -04:00

88 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Test script to verify database mode functionality.
This script tests the LLMAnalyzer in database mode without requiring
actual API keys or patent downloads.
"""
from SPARC.llm import LLMAnalyzer
from SPARC.database import DatabaseClient
from SPARC import config
def test_database_mode():
"""Test that database mode stores messages correctly."""
print("Testing Database Mode")
print("=" * 70)
# Initialize analyzer in database mode
print("\n1. Initializing LLMAnalyzer in database mode...")
analyzer = LLMAnalyzer(use_database=True)
print(f" - use_database: {analyzer.use_database}")
print(f" - db_client: {analyzer.db_client is not None}")
# Test single patent analysis
print("\n2. Testing single patent analysis (database mode)...")
result = analyzer.analyze_patent_content(
patent_content="Test patent content about semiconductor innovation",
company_name="TestCorp"
)
print(f" Result: {result}")
# Test portfolio analysis
print("\n3. Testing portfolio analysis (database mode)...")
test_patents = [
{"patent_id": "US001", "content": "First test patent"},
{"patent_id": "US002", "content": "Second test patent"},
]
result = analyzer.analyze_patent_portfolio(
patents_data=test_patents,
company_name="TestCorp"
)
print(f" Result: {result}")
# Verify messages were stored
print("\n4. Verifying messages were stored...")
db_client = DatabaseClient(config.database_url)
messages = db_client.get_messages(company_name="TestCorp", limit=10)
print(f" Found {len(messages)} stored messages")
for msg in messages:
print(f" - ID: {msg['id']}, Type: {msg['analysis_type']}, Timestamp: {msg['timestamp']}")
# Get analytics
print("\n5. Getting analytics...")
analytics = db_client.get_analytics(days=1)
print(f" Total messages: {analytics['total_messages']}")
print(f" By company: {analytics['by_company']}")
print(f" By type: {analytics['by_type']}")
print("\n" + "=" * 70)
print("Database mode test completed successfully!")
def test_api_mode():
"""Test that API mode initializes correctly."""
print("\nTesting API Mode")
print("=" * 70)
print("\n1. Initializing LLMAnalyzer in API mode...")
analyzer = LLMAnalyzer(use_database=False, test_mode=True)
print(f" - use_database: {analyzer.use_database}")
print(f" - test_mode: {analyzer.test_mode}")
print("\n2. Testing single patent analysis (test mode)...")
result = analyzer.analyze_patent_content(
patent_content="Test patent content",
company_name="TestCorp2"
)
print(f" Result: {result}")
print("\n" + "=" * 70)
print("API mode test completed successfully!")
if __name__ == "__main__":
test_database_mode()
print("\n")
test_api_mode()