forked from 0xWheatyz/SPARC
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e2d750146c |
+17
-21
@@ -5,13 +5,10 @@ to provide company performance estimation based on patent portfolios.
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import logging
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from typing import Callable
|
||||
|
||||
from SPARC import config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from SPARC.database import DatabaseClient
|
||||
from SPARC.serp_api import SERP
|
||||
from SPARC.llm import LLMAnalyzer
|
||||
@@ -55,13 +52,13 @@ class CompanyAnalyzer:
|
||||
query_hash = hashlib.sha256(company_name.lower().encode()).hexdigest()
|
||||
cached_ids = self.db.get_cached_serp_query(query_hash)
|
||||
if cached_ids is not None:
|
||||
logger.info("Using cached SERP results for %s (%d patents)", company_name, len(cached_ids))
|
||||
print(f"Using cached SERP results for {company_name} ({len(cached_ids)} patents)")
|
||||
patents = Patents(patents=[
|
||||
Patent(patent_id=pid, pdf_link="")
|
||||
for pid in cached_ids
|
||||
])
|
||||
else:
|
||||
logger.info("Retrieving patents for %s...", company_name)
|
||||
print(f"Retrieving patents for {company_name}...")
|
||||
patents = SERP.query(company_name)
|
||||
# Cache the SERP results
|
||||
if patents.patents:
|
||||
@@ -69,13 +66,12 @@ class CompanyAnalyzer:
|
||||
company_name=company_name,
|
||||
query_hash=query_hash,
|
||||
patent_ids=[p.patent_id for p in patents.patents],
|
||||
ttl_hours=config.serp_cache_ttl_hours,
|
||||
)
|
||||
|
||||
if not patents.patents:
|
||||
return f"No patents found for {company_name}"
|
||||
|
||||
logger.info("Found %d patents. Processing...", len(patents.patents))
|
||||
print(f"Found {len(patents.patents)} patents. Processing...")
|
||||
|
||||
# Download, parse, and minimize patents in parallel
|
||||
processed_patents = []
|
||||
@@ -91,12 +87,12 @@ class CompanyAnalyzer:
|
||||
if result:
|
||||
processed_patents.append(result)
|
||||
except Exception as e:
|
||||
logger.warning("Failed to process %s: %s", patent.patent_id, e)
|
||||
print(f"Warning: Failed to process {patent.patent_id}: {e}")
|
||||
|
||||
if not processed_patents:
|
||||
return f"Failed to process any patents for {company_name}"
|
||||
|
||||
logger.info("Analyzing portfolio with LLM...")
|
||||
print(f"Analyzing portfolio with LLM...")
|
||||
|
||||
# Analyze the full portfolio with LLM
|
||||
analysis = self.llm_analyzer.analyze_patent_portfolio(
|
||||
@@ -119,7 +115,7 @@ class CompanyAnalyzer:
|
||||
"""
|
||||
# Note: This simplified version assumes the patent PDF is already downloaded
|
||||
# A more complete implementation would support direct patent ID lookup
|
||||
logger.info("Analyzing patent %s for %s...", patent_id, company_name)
|
||||
print(f"Analyzing patent {patent_id} for {company_name}...")
|
||||
|
||||
patent_path = f"patents/{patent_id}.pdf"
|
||||
|
||||
@@ -173,7 +169,7 @@ class CompanyAnalyzer:
|
||||
|
||||
return {"patent_id": patent.patent_id, "content": minimized_content}
|
||||
except Exception as e:
|
||||
logger.warning("Failed to process %s: %s", patent.patent_id, e)
|
||||
print(f"Warning: Failed to process {patent.patent_id}: {e}")
|
||||
return None
|
||||
|
||||
def _analyze_company_safe(self, company_name: str) -> CompanyAnalysisResult:
|
||||
@@ -244,7 +240,7 @@ class CompanyAnalyzer:
|
||||
results: list[CompanyAnalysisResult] = []
|
||||
total = len(companies)
|
||||
|
||||
logger.info("Starting batch analysis of %d companies...", total)
|
||||
print(f"Starting batch analysis of {total} companies...")
|
||||
|
||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||
future_to_company = {
|
||||
@@ -261,8 +257,8 @@ class CompanyAnalyzer:
|
||||
result = future.result()
|
||||
results.append(result)
|
||||
|
||||
status = "OK" if result.success else "FAIL"
|
||||
logger.info("[%d/%d] %s %s", completed, total, status, company)
|
||||
status = "✓" if result.success else "✗"
|
||||
print(f"[{completed}/{total}] {status} {company}")
|
||||
|
||||
if progress_callback:
|
||||
progress_callback(company, completed, total)
|
||||
@@ -277,12 +273,12 @@ class CompanyAnalyzer:
|
||||
error=str(e),
|
||||
)
|
||||
)
|
||||
logger.error("[%d/%d] FAIL %s: %s", completed, total, company, e)
|
||||
print(f"[{completed}/{total}] ✗ {company}: {e}")
|
||||
|
||||
successful = sum(1 for r in results if r.success)
|
||||
failed = total - successful
|
||||
|
||||
logger.info("Batch complete: %d succeeded, %d failed", successful, failed)
|
||||
print(f"\nBatch complete: {successful} succeeded, {failed} failed")
|
||||
|
||||
return BatchAnalysisResult(
|
||||
results=results,
|
||||
@@ -308,20 +304,20 @@ class CompanyAnalyzer:
|
||||
results: list[CompanyAnalysisResult] = []
|
||||
total = len(companies)
|
||||
|
||||
logger.info("Starting sequential analysis of %d companies...", total)
|
||||
print(f"Starting sequential analysis of {total} companies...")
|
||||
|
||||
for idx, company in enumerate(companies, 1):
|
||||
logger.info("[%d/%d] Analyzing %s...", idx, total, company)
|
||||
print(f"\n[{idx}/{total}] Analyzing {company}...")
|
||||
result = self._analyze_company_safe(company)
|
||||
results.append(result)
|
||||
|
||||
status = "OK" if result.success else "FAIL"
|
||||
logger.info("[%d/%d] %s %s", idx, total, status, company)
|
||||
status = "✓" if result.success else "✗"
|
||||
print(f"[{idx}/{total}] {status} {company}")
|
||||
|
||||
successful = sum(1 for r in results if r.success)
|
||||
failed = total - successful
|
||||
|
||||
logger.info("Batch complete: %d succeeded, %d failed", successful, failed)
|
||||
print(f"\nBatch complete: {successful} succeeded, {failed} failed")
|
||||
|
||||
return BatchAnalysisResult(
|
||||
results=results,
|
||||
|
||||
+28
-6
@@ -7,9 +7,13 @@ from contextlib import asynccontextmanager
|
||||
from datetime import datetime
|
||||
from typing import Annotated, List
|
||||
|
||||
from fastapi import BackgroundTasks, Depends, FastAPI, HTTPException, Query
|
||||
from fastapi import BackgroundTasks, Depends, FastAPI, HTTPException, Query, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
from slowapi import Limiter
|
||||
from slowapi.errors import RateLimitExceeded
|
||||
from slowapi.util import get_remote_address
|
||||
|
||||
from SPARC import config
|
||||
from SPARC.analyzer import CompanyAnalyzer
|
||||
@@ -164,6 +168,22 @@ app = FastAPI(
|
||||
root_path=config.root_path,
|
||||
)
|
||||
|
||||
# Rate limiter (in-memory storage, suitable for single-instance deployments)
|
||||
limiter = Limiter(key_func=get_remote_address)
|
||||
app.state.limiter = limiter
|
||||
|
||||
|
||||
@app.exception_handler(RateLimitExceeded)
|
||||
async def rate_limit_handler(request: Request, exc: RateLimitExceeded):
|
||||
"""Return 429 with Retry-After header when rate limit is exceeded."""
|
||||
retry_after = getattr(exc, "retry_after", 60)
|
||||
return JSONResponse(
|
||||
status_code=429,
|
||||
content={"detail": "Rate limit exceeded. Please try again later."},
|
||||
headers={"Retry-After": str(retry_after)},
|
||||
)
|
||||
|
||||
|
||||
# Add CORS middleware for React frontend
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
@@ -178,7 +198,8 @@ app.add_middleware(
|
||||
|
||||
|
||||
@app.post("/auth/register", response_model=UserResponse, tags=["Auth"])
|
||||
async def register(request: RegisterRequest):
|
||||
@limiter.limit("5/minute")
|
||||
async def register(request: Request, body: RegisterRequest):
|
||||
"""Register a new user.
|
||||
|
||||
The first registered user automatically becomes an admin.
|
||||
@@ -190,8 +211,8 @@ async def register(request: RegisterRequest):
|
||||
role = "admin" if user_count == 0 else "user"
|
||||
|
||||
user = db.create_user(
|
||||
email=request.email,
|
||||
password=request.password,
|
||||
email=body.email,
|
||||
password=body.password,
|
||||
role=role,
|
||||
)
|
||||
|
||||
@@ -210,11 +231,12 @@ async def register(request: RegisterRequest):
|
||||
|
||||
|
||||
@app.post("/auth/login", response_model=TokenResponse, tags=["Auth"])
|
||||
async def login(request: LoginRequest):
|
||||
@limiter.limit("10/minute")
|
||||
async def login(request: Request, body: LoginRequest):
|
||||
"""Authenticate user and return JWT tokens."""
|
||||
db = get_db_client()
|
||||
|
||||
user = db.authenticate_user(request.email, request.password)
|
||||
user = db.authenticate_user(body.email, body.password)
|
||||
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
|
||||
+1
-16
@@ -2,20 +2,11 @@
|
||||
|
||||
Loads environment variables from .env file for API keys and other secrets.
|
||||
"""
|
||||
import logging
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Logging configuration
|
||||
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||
logging.basicConfig(
|
||||
level=getattr(logging, log_level, logging.INFO),
|
||||
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||
)
|
||||
|
||||
# SerpAPI key for patent search
|
||||
api_key = os.getenv("API_KEY")
|
||||
|
||||
@@ -39,12 +30,6 @@ use_database = os.getenv("USE_DATABASE", "false").lower() in ("true", "1", "yes"
|
||||
patent_search_days = int(os.getenv("PATENT_SEARCH_DAYS", "90"))
|
||||
patent_thread_workers = int(os.getenv("PATENT_THREAD_WORKERS", "5"))
|
||||
|
||||
# LLM model to use via OpenRouter (e.g. "anthropic/claude-3.5-sonnet", "openai/gpt-4o")
|
||||
model = os.getenv("MODEL", "anthropic/claude-3.5-sonnet")
|
||||
|
||||
# SERP cache TTL in hours (how long cached search results are considered fresh)
|
||||
serp_cache_ttl_hours = int(os.getenv("SERP_CACHE_TTL_HOURS", "24"))
|
||||
|
||||
# Root path for running behind a reverse proxy (e.g., "/api" when served at /api/)
|
||||
# This ensures OpenAPI docs work correctly when accessed via the proxy
|
||||
root_path = os.getenv("ROOT_PATH", "")
|
||||
|
||||
+8
-9
@@ -1,14 +1,9 @@
|
||||
"""LLM integration for patent analysis using OpenRouter."""
|
||||
|
||||
import logging
|
||||
from typing import Dict
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
from SPARC import config
|
||||
from SPARC.database import DatabaseClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from typing import Dict
|
||||
|
||||
|
||||
class LLMAnalyzer:
|
||||
@@ -25,7 +20,7 @@ class LLMAnalyzer:
|
||||
"""
|
||||
self.test_mode = test_mode
|
||||
self.use_cache = use_cache if use_cache is not None else config.use_cache
|
||||
self.model = config.model
|
||||
self.model = "anthropic/claude-3.5-sonnet"
|
||||
|
||||
# Always initialize database client for storage and caching
|
||||
self.db_client = DatabaseClient(config.database_url)
|
||||
@@ -64,7 +59,11 @@ Patent Content:
|
||||
Provide a concise analysis (2-3 paragraphs) focusing on what this patent reveals about the company's technical direction and competitive advantage."""
|
||||
|
||||
if self.test_mode:
|
||||
logger.debug("TEST MODE - Prompt that would be sent to LLM:\n%s", prompt)
|
||||
print("=" * 80)
|
||||
print("TEST MODE - Prompt that would be sent to LLM:")
|
||||
print("=" * 80)
|
||||
print(prompt)
|
||||
print("=" * 80)
|
||||
return "[TEST MODE - No API call made]"
|
||||
|
||||
# Check cache first
|
||||
@@ -166,7 +165,7 @@ Patent Portfolio:
|
||||
Provide a comprehensive analysis (4-5 paragraphs) with a final verdict on the company's innovation strength and performance outlook."""
|
||||
|
||||
if self.test_mode:
|
||||
logger.debug("TEST MODE - Portfolio prompt:\n%s", prompt)
|
||||
print(prompt)
|
||||
return "[TEST MODE]"
|
||||
|
||||
metadata = {
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ from datetime import datetime
|
||||
|
||||
@dataclass
|
||||
class Patent:
|
||||
patent_id: str
|
||||
patent_id: int
|
||||
pdf_link: str
|
||||
pdf_path: str | None = None
|
||||
summary: dict | None = None
|
||||
|
||||
@@ -14,3 +14,4 @@ numpy
|
||||
pandas
|
||||
bcrypt
|
||||
PyJWT
|
||||
slowapi
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
"""Tests for rate limiting on auth endpoints."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import Mock, patch, MagicMock
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from SPARC.api import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
"""Create test client with rate limiter enabled."""
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_limiter():
|
||||
"""Reset rate limiter storage between tests."""
|
||||
from SPARC.api import limiter
|
||||
limiter.reset()
|
||||
yield
|
||||
|
||||
|
||||
class TestRateLimiting:
|
||||
"""Test rate limiting on login and register endpoints."""
|
||||
|
||||
@patch("SPARC.api.get_db_client")
|
||||
def test_login_allows_requests_under_limit(self, mock_db_client, client):
|
||||
"""Login endpoint allows requests under the rate limit."""
|
||||
mock_db = MagicMock()
|
||||
mock_db.authenticate_user.return_value = None
|
||||
mock_db_client.return_value = mock_db
|
||||
|
||||
# Should allow at least a few requests
|
||||
for _ in range(5):
|
||||
response = client.post(
|
||||
"/auth/login",
|
||||
json={"email": "test@example.com", "password": "password123"},
|
||||
)
|
||||
# 401 is expected (invalid credentials), not 429
|
||||
assert response.status_code == 401
|
||||
|
||||
@patch("SPARC.api.get_db_client")
|
||||
def test_login_rate_limited_after_threshold(self, mock_db_client, client):
|
||||
"""Login endpoint returns 429 after exceeding rate limit."""
|
||||
mock_db = MagicMock()
|
||||
mock_db.authenticate_user.return_value = None
|
||||
mock_db_client.return_value = mock_db
|
||||
|
||||
# Send more than the limit (10/minute)
|
||||
statuses = []
|
||||
for _ in range(15):
|
||||
response = client.post(
|
||||
"/auth/login",
|
||||
json={"email": "test@example.com", "password": "password123"},
|
||||
)
|
||||
statuses.append(response.status_code)
|
||||
|
||||
# At least one should be 429
|
||||
assert 429 in statuses, f"Expected 429 in statuses but got: {set(statuses)}"
|
||||
|
||||
@patch("SPARC.api.get_db_client")
|
||||
def test_register_rate_limited_after_threshold(self, mock_db_client, client):
|
||||
"""Register endpoint returns 429 after exceeding rate limit."""
|
||||
mock_db = MagicMock()
|
||||
mock_db.get_user_count.return_value = 1
|
||||
mock_db.create_user.return_value = None # triggers 400 (email exists)
|
||||
mock_db_client.return_value = mock_db
|
||||
|
||||
# Send more than the limit (5/minute)
|
||||
statuses = []
|
||||
for _ in range(10):
|
||||
response = client.post(
|
||||
"/auth/register",
|
||||
json={"email": "test@example.com", "password": "password123"},
|
||||
)
|
||||
statuses.append(response.status_code)
|
||||
|
||||
# At least one should be 429
|
||||
assert 429 in statuses, f"Expected 429 in statuses but got: {set(statuses)}"
|
||||
|
||||
@patch("SPARC.api.get_db_client")
|
||||
def test_rate_limit_returns_retry_after_header(self, mock_db_client, client):
|
||||
"""Rate limited responses include a Retry-After header."""
|
||||
mock_db = MagicMock()
|
||||
mock_db.authenticate_user.return_value = None
|
||||
mock_db_client.return_value = mock_db
|
||||
|
||||
# Exhaust the limit
|
||||
for _ in range(15):
|
||||
response = client.post(
|
||||
"/auth/login",
|
||||
json={"email": "test@example.com", "password": "password123"},
|
||||
)
|
||||
if response.status_code == 429:
|
||||
assert "Retry-After" in response.headers
|
||||
break
|
||||
Reference in New Issue
Block a user