SPARC/tests/test_llm.py
0xWheatyz d7cf80f02f feat: add LLM integration for patent analysis
Implemented LLMAnalyzer class using Anthropic's Claude API for:
- Single patent content analysis
- Portfolio-wide analysis across multiple patents
- Configurable API key management via environment variables

Key features:
- Uses Claude 3.5 Sonnet for high-quality analysis
- Structured prompts for innovation assessment
- Token limits optimized per use case (1024 for single, 2048 for portfolio)
- Analyzes: innovation quality, market potential, strategic direction

Updated config.py to support ANTHROPIC_API_KEY environment variable.

Added comprehensive test suite (6 tests) covering:
- Initialization from config and direct API key
- Single patent analysis
- Portfolio analysis
- Token limit validation

All 19 tests passing.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-19 18:55:35 -05:00

125 lines
4.8 KiB
Python

"""Tests for LLM analysis functionality."""
import pytest
from unittest.mock import Mock, MagicMock
from SPARC.llm import LLMAnalyzer
class TestLLMAnalyzer:
"""Test LLM analyzer initialization and API interaction."""
def test_analyzer_initialization_with_api_key(self, mocker):
"""Test that analyzer initializes with provided API key."""
mock_anthropic = mocker.patch("SPARC.llm.Anthropic")
analyzer = LLMAnalyzer(api_key="test-key-123")
mock_anthropic.assert_called_once_with(api_key="test-key-123")
assert analyzer.model == "claude-3-5-sonnet-20241022"
def test_analyzer_initialization_from_config(self, mocker):
"""Test that analyzer loads API key from config when not provided."""
mock_anthropic = mocker.patch("SPARC.llm.Anthropic")
mock_config = mocker.patch("SPARC.llm.config")
mock_config.anthropic_api_key = "config-key-456"
analyzer = LLMAnalyzer()
mock_anthropic.assert_called_once_with(api_key="config-key-456")
def test_analyze_patent_content(self, mocker):
"""Test single patent content analysis."""
mock_anthropic = mocker.patch("SPARC.llm.Anthropic")
mock_client = Mock()
mock_anthropic.return_value = mock_client
# Mock the API response
mock_response = Mock()
mock_response.content = [Mock(text="Innovative GPU architecture.")]
mock_client.messages.create.return_value = mock_response
analyzer = LLMAnalyzer(api_key="test-key")
result = analyzer.analyze_patent_content(
patent_content="ABSTRACT: GPU with new cache design...",
company_name="NVIDIA",
)
assert result == "Innovative GPU architecture."
mock_client.messages.create.assert_called_once()
# Verify the prompt includes company name and content
call_args = mock_client.messages.create.call_args
prompt_text = call_args[1]["messages"][0]["content"]
assert "NVIDIA" in prompt_text
assert "GPU with new cache design" in prompt_text
def test_analyze_patent_portfolio(self, mocker):
"""Test portfolio analysis with multiple patents."""
mock_anthropic = mocker.patch("SPARC.llm.Anthropic")
mock_client = Mock()
mock_anthropic.return_value = mock_client
# Mock the API response
mock_response = Mock()
mock_response.content = [
Mock(text="Strong portfolio in AI and graphics.")
]
mock_client.messages.create.return_value = mock_response
analyzer = LLMAnalyzer(api_key="test-key")
patents_data = [
{"patent_id": "US123", "content": "AI acceleration patent"},
{"patent_id": "US456", "content": "Graphics rendering patent"},
]
result = analyzer.analyze_patent_portfolio(
patents_data=patents_data, company_name="NVIDIA"
)
assert result == "Strong portfolio in AI and graphics."
mock_client.messages.create.assert_called_once()
# Verify the prompt includes all patents
call_args = mock_client.messages.create.call_args
prompt_text = call_args[1]["messages"][0]["content"]
assert "US123" in prompt_text
assert "US456" in prompt_text
assert "AI acceleration patent" in prompt_text
assert "Graphics rendering patent" in prompt_text
def test_analyze_patent_portfolio_with_correct_token_limit(self, mocker):
"""Test that portfolio analysis uses higher token limit."""
mock_anthropic = mocker.patch("SPARC.llm.Anthropic")
mock_client = Mock()
mock_anthropic.return_value = mock_client
mock_response = Mock()
mock_response.content = [Mock(text="Analysis result.")]
mock_client.messages.create.return_value = mock_response
analyzer = LLMAnalyzer(api_key="test-key")
patents_data = [{"patent_id": "US123", "content": "Test content"}]
analyzer.analyze_patent_portfolio(patents_data, "TestCo")
call_args = mock_client.messages.create.call_args
# Portfolio analysis should use 2048 tokens
assert call_args[1]["max_tokens"] == 2048
def test_analyze_single_patent_with_correct_token_limit(self, mocker):
"""Test that single patent analysis uses lower token limit."""
mock_anthropic = mocker.patch("SPARC.llm.Anthropic")
mock_client = Mock()
mock_anthropic.return_value = mock_client
mock_response = Mock()
mock_response.content = [Mock(text="Analysis result.")]
mock_client.messages.create.return_value = mock_response
analyzer = LLMAnalyzer(api_key="test-key")
analyzer.analyze_patent_content("Test content", "TestCo")
call_args = mock_client.messages.create.call_args
# Single patent should use 1024 tokens
assert call_args[1]["max_tokens"] == 1024