Builds PaperTrail from the base skeleton into a full evidence-verification platform for the "Built with Claude: Life Sciences" hackathon (Builder Track). New named-user features (Build Beyond the Bench): - Lab Notebook Companion — rough bench notes -> structured, searchable, grounded experiment records (lib/labNotebook, /console/lab-notebook, migration 0059) - Clinical Trial Matcher — de-identified patient notes -> ranked eligible ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning (lib/trialMatcher, /console/trial-matcher, migrations 0060) Platform: - Deterministic verification moat (biostats recompute, exact-span grounding) - 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar, PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier) - Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise governance (RBAC, /api/v1 gateway, metering, audit chain, retention) - 17 OSS projects assimilated into native TypeScript on our stack (backend/engines retained + credited in NOTICE) - Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site - Next.js 16 upgrade, benchmark harness (honest smoke-test caveat) Licensing: Apache-2.0 (LICENSE + NOTICE).
221 lines
8.0 KiB
Python
221 lines
8.0 KiB
Python
from typing import List, Tuple, Optional
|
|
from app.models.claim import Claim
|
|
from app.services.llm.gateway import LLMTask
|
|
from app.services.llm.types import empty_usage
|
|
from textwrap import dedent
|
|
import logging
|
|
import re
|
|
from app.services.llm.validators import validate_paper_analysis_payload
|
|
from app.services.prompt_store import load_prompt, render_prompt
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class PaperAnalyzer:
|
|
|
|
async def analyze_relevance_and_extract(
|
|
self,
|
|
paper_content: str,
|
|
claim_text: str,
|
|
ai_service,
|
|
batch_id: Optional[str] = None,
|
|
claim_id: Optional[str] = None,
|
|
paper_id: Optional[str] = None,
|
|
model_override: Optional[str] = None,
|
|
) -> Tuple[float, List[str], List[str], Optional[str], List[int]]:
|
|
"""
|
|
Analyze paper content for relevance to the claim and extract supporting, contradicting, or generally relevant evidence.
|
|
|
|
Returns:
|
|
- relevance score (0-1)
|
|
- list of relevant excerpts
|
|
- list of explanations for each excerpt
|
|
- explanation if paper is not relevant
|
|
- list of page numbers for excerpts
|
|
- usage stats
|
|
"""
|
|
|
|
# Prepare the analysis prompt
|
|
system_prompt = load_prompt("paper_analysis_system")
|
|
|
|
cleaned_content, truncation = self._clean_content_with_budget(
|
|
content=paper_content,
|
|
claim_text=claim_text,
|
|
system_prompt=system_prompt,
|
|
ai_service=ai_service,
|
|
model_hint=model_override,
|
|
reserved_output_tokens=1200,
|
|
)
|
|
|
|
if truncation.get("truncated"):
|
|
await ai_service.add_issue(
|
|
batch_id=batch_id,
|
|
claim_id=claim_id,
|
|
severity="WARN",
|
|
stage=LLMTask.PAPER_ANALYSIS,
|
|
message="Paper content truncation applied to fit context window.",
|
|
details={
|
|
"paper_id": paper_id,
|
|
"original_tokens": truncation.get("original_tokens"),
|
|
"kept_tokens": truncation.get("kept_tokens"),
|
|
},
|
|
)
|
|
|
|
user_prompt = render_prompt(
|
|
"paper_analysis_user",
|
|
claim_text=claim_text,
|
|
cleaned_content=cleaned_content,
|
|
)
|
|
|
|
try:
|
|
# Use the async version of generate_json
|
|
result = await ai_service.chat_json(
|
|
user_prompt=user_prompt,
|
|
system_prompt=system_prompt,
|
|
task=LLMTask.PAPER_ANALYSIS,
|
|
batch_id=batch_id,
|
|
claim_id=claim_id,
|
|
paper_id=paper_id,
|
|
model_override=model_override,
|
|
)
|
|
response = validate_paper_analysis_payload(result['content'])
|
|
usage = result['usage']
|
|
|
|
# Log the analysis results
|
|
logger.info(f"Paper analysis results:")
|
|
logger.info(f"- Relevance: {response.get('relevance', 0)}")
|
|
logger.info(f"- Number of excerpts: {len(response.get('excerpts', []))}")
|
|
|
|
if response.get('relevance', 0) < 0.1:
|
|
logger.info(f"- Not relevant: {response.get('non_relevant_explanation')}")
|
|
|
|
return (
|
|
response.get('relevance', 0),
|
|
response.get('excerpts', []),
|
|
response.get('explanations', []),
|
|
response.get('non_relevant_explanation'),
|
|
response.get('excerpt_pages', []),
|
|
usage
|
|
)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error analyzing paper content: {str(e)}")
|
|
# Return empty usage stats in error case
|
|
return 0, [], [], "Error analyzing paper content", [], empty_usage(is_estimated=True)
|
|
|
|
def _clean_content(self, content: str) -> str:
|
|
"""Clean and format paper content for analysis."""
|
|
if not content:
|
|
return ""
|
|
|
|
# Remove multiple newlines
|
|
content = re.sub(r'\n{3,}', '\n\n', content)
|
|
|
|
# Remove excessive whitespace
|
|
content = re.sub(r'\s+', ' ', content)
|
|
|
|
# Clean up common OCR artifacts
|
|
content = re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\xFF]', '', content)
|
|
|
|
# Remove references section if present
|
|
if 'References' in content:
|
|
content = content.split('References')[0]
|
|
elif 'REFERENCES' in content:
|
|
content = content.split('REFERENCES')[0]
|
|
|
|
return content.strip()
|
|
|
|
def _clean_content_with_budget(
|
|
self,
|
|
*,
|
|
content: str,
|
|
claim_text: str,
|
|
system_prompt: str,
|
|
ai_service,
|
|
model_hint: Optional[str],
|
|
reserved_output_tokens: int,
|
|
) -> Tuple[str, dict]:
|
|
cleaned = self._clean_content(content)
|
|
model_name = model_hint or getattr(ai_service, "default_model", "gpt-4o")
|
|
estimator = getattr(ai_service, "token_estimator", None)
|
|
if estimator is None:
|
|
return cleaned, {"truncated": False, "original_tokens": 0, "kept_tokens": 0}
|
|
|
|
base_prompt = dedent(f"""
|
|
Analyze this paper content for both direct and mechanistic evidence related to the following claim:
|
|
|
|
Claim: {claim_text}
|
|
|
|
Paper content:
|
|
""").strip()
|
|
|
|
base_messages = [
|
|
{"role": "system", "content": system_prompt},
|
|
{"role": "user", "content": base_prompt},
|
|
]
|
|
base_tokens = estimator.estimate_chat_tokens(base_messages, model_name)
|
|
|
|
registry = getattr(ai_service, "model_registry", None)
|
|
context_window = 128000
|
|
if registry is not None:
|
|
context_window = registry.context_window(model_name)
|
|
|
|
safety_margin = int(getattr(ai_service, "context_safety_margin_tokens", 256))
|
|
available_for_content = max(0, context_window - base_tokens - reserved_output_tokens - safety_margin)
|
|
|
|
original_tokens = estimator.estimate_text_tokens(cleaned, model_name)
|
|
if original_tokens <= available_for_content:
|
|
return cleaned, {
|
|
"truncated": False,
|
|
"original_tokens": original_tokens,
|
|
"kept_tokens": original_tokens,
|
|
}
|
|
|
|
if available_for_content <= 0:
|
|
return "", {
|
|
"truncated": True,
|
|
"original_tokens": original_tokens,
|
|
"kept_tokens": 0,
|
|
}
|
|
|
|
# Approximate truncation by token ratio to avoid expensive iterative slicing.
|
|
ratio = available_for_content / max(1, original_tokens)
|
|
keep_chars = max(200, int(len(cleaned) * ratio))
|
|
truncated = cleaned[:keep_chars].rstrip() + "...[truncated]"
|
|
kept_tokens = estimator.estimate_text_tokens(truncated, model_name)
|
|
return truncated, {
|
|
"truncated": True,
|
|
"original_tokens": original_tokens,
|
|
"kept_tokens": kept_tokens,
|
|
}
|
|
|
|
def extract_page_numbers(self, content: str, excerpt: str) -> Optional[int]:
|
|
"""
|
|
Attempt to extract page numbers for excerpts.
|
|
Returns None if page number cannot be determined.
|
|
"""
|
|
try:
|
|
# Look for page markers in the content
|
|
page_markers = re.finditer(r'(?i)page\s*(\d+)|pg\.\s*(\d+)|\[(\d+)\]', content)
|
|
excerpt_pos = content.find(excerpt)
|
|
|
|
if excerpt_pos == -1:
|
|
return None
|
|
|
|
# Find the closest page marker before the excerpt
|
|
closest_page = None
|
|
closest_distance = float('inf')
|
|
|
|
for match in page_markers:
|
|
page_num = next(num for num in match.groups() if num is not None)
|
|
distance = excerpt_pos - match.start()
|
|
|
|
if 0 <= distance < closest_distance:
|
|
closest_distance = distance
|
|
closest_page = int(page_num)
|
|
|
|
return closest_page
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error extracting page number: {str(e)}")
|
|
return None
|