|
| 1 | +from collections.abc import Iterable |
| 2 | + |
| 3 | +from osa_tool.operations.analysis.paper_claims.claim_schemas import ClaimCandidateResponse |
| 4 | +from osa_tool.operations.analysis.paper_claims.models import ExtractedClaim, HeadingMeta, PaperSection |
| 5 | + |
| 6 | +DEFAULT_SECTION_TEXT = "The model uses BERT-base without fine-tuning." |
| 7 | + |
| 8 | + |
| 9 | +class FakeAsyncHandler: |
| 10 | + """Queued async LLM handler used by paper-claims unit tests.""" |
| 11 | + |
| 12 | + def __init__(self, responses: Iterable[str | BaseException]): |
| 13 | + self.responses = iter(responses) |
| 14 | + self.prompts: list[str] = [] |
| 15 | + |
| 16 | + async def async_request(self, prompt, system_message=None, retry_delay=1): |
| 17 | + self.prompts.append(prompt) |
| 18 | + response = next(self.responses) |
| 19 | + if isinstance(response, BaseException): |
| 20 | + raise response |
| 21 | + return response |
| 22 | + |
| 23 | + |
| 24 | +class ModelTrackingFakeHandler(FakeAsyncHandler): |
| 25 | + """Fake handler that records the model producing each successful response.""" |
| 26 | + |
| 27 | + def __init__(self, responses: Iterable[str | BaseException], models: Iterable[str]): |
| 28 | + super().__init__(responses) |
| 29 | + self.models = iter(models) |
| 30 | + self.last_successful_model: str | None = None |
| 31 | + |
| 32 | + async def async_request(self, prompt, system_message=None, retry_delay=1): |
| 33 | + response = await super().async_request(prompt, system_message, retry_delay) |
| 34 | + self.last_successful_model = next(self.models) |
| 35 | + return response |
| 36 | + |
| 37 | + |
| 38 | +def word_token_count(text: str, _encoder: str = "fake") -> int: |
| 39 | + return len(text.split()) |
| 40 | + |
| 41 | + |
| 42 | +def make_paper_section( |
| 43 | + text: str = DEFAULT_SECTION_TEXT, |
| 44 | + *, |
| 45 | + section_id: str = "s001", |
| 46 | + name: str = "Method", |
| 47 | + heading_raw: str = "2. Method", |
| 48 | + heading_level: int = 1, |
| 49 | + heading_numbering: str | None = "2", |
| 50 | +) -> PaperSection: |
| 51 | + return PaperSection( |
| 52 | + section_id=section_id, |
| 53 | + name=name, |
| 54 | + text=text, |
| 55 | + heading_meta=HeadingMeta(raw=heading_raw, level=heading_level, numbering=heading_numbering), |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def make_extracted_claim( |
| 60 | + claim_id: str, |
| 61 | + claim: str, |
| 62 | + *, |
| 63 | + original_text: str | None = None, |
| 64 | + category: str = "infrastructure", |
| 65 | + value: str | None = None, |
| 66 | + verifiability: str = "high", |
| 67 | + section_id: str = "s001", |
| 68 | + section_name: str = "Method", |
| 69 | + section_heading_raw: str | None = "2. Method", |
| 70 | + contradiction: bool = False, |
| 71 | +) -> ExtractedClaim: |
| 72 | + return ExtractedClaim( |
| 73 | + claim_id=claim_id, |
| 74 | + claim=claim, |
| 75 | + original_text=claim if original_text is None else original_text, |
| 76 | + category=category, |
| 77 | + value=value, |
| 78 | + verifiability=verifiability, |
| 79 | + section_id=section_id, |
| 80 | + section_name=section_name, |
| 81 | + section_heading_raw=section_heading_raw, |
| 82 | + contradiction=contradiction, |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def make_claim_candidate( |
| 87 | + *, |
| 88 | + claim: str, |
| 89 | + original_text: str, |
| 90 | + category: str = "model_architecture", |
| 91 | + value: str | None = None, |
| 92 | + verifiability: str = "high", |
| 93 | +) -> ClaimCandidateResponse: |
| 94 | + return ClaimCandidateResponse( |
| 95 | + claim=claim, |
| 96 | + original_text=original_text, |
| 97 | + category=category, |
| 98 | + value=value, |
| 99 | + verifiability=verifiability, |
| 100 | + ) |
0 commit comments