|
| 1 | +"""Anthropic Claude provider for extraction-time JSON output. |
| 2 | +
|
| 3 | +Uses the ``anthropic`` Python SDK directly (no MAF dependency) to keep |
| 4 | +this package lightweight. Targets Haiku by default for cost: extraction |
| 5 | +is a high-volume, narrow-schema task — Haiku 4.5 is the right tool. |
| 6 | +
|
| 7 | +The provider forces JSON output by appending a brief reinforcement to |
| 8 | +the system prompt and stripping any code fences from the response. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from anthropic import AsyncAnthropic |
| 14 | + |
| 15 | +from config.settings import settings |
| 16 | +from pipeline.llm.base import LLMProvider |
| 17 | + |
| 18 | +_JSON_REINFORCEMENT = ( |
| 19 | + "\n\nIMPORTANT: respond with raw JSON only. No prose, no code fences, " |
| 20 | + "no preamble — just the JSON object starting with { and ending with }." |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _strip_code_fences(text: str) -> str: |
| 25 | + """Remove ```json … ``` wrappers that Claude sometimes emits anyway.""" |
| 26 | + |
| 27 | + stripped = text.strip() |
| 28 | + if stripped.startswith("```"): |
| 29 | + # drop the opening fence (with or without language tag) |
| 30 | + first_newline = stripped.find("\n") |
| 31 | + if first_newline != -1: |
| 32 | + stripped = stripped[first_newline + 1 :] |
| 33 | + # drop the closing fence |
| 34 | + if stripped.rstrip().endswith("```"): |
| 35 | + stripped = stripped.rstrip()[:-3] |
| 36 | + return stripped.strip() |
| 37 | + |
| 38 | + |
| 39 | +class AnthropicProvider(LLMProvider): |
| 40 | + """Calls Claude via the Anthropic Messages API and returns raw JSON text.""" |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + *, |
| 45 | + api_key: str | None = None, |
| 46 | + model: str | None = None, |
| 47 | + max_tokens: int = 4096, |
| 48 | + ) -> None: |
| 49 | + resolved_key = api_key or settings.ANTHROPIC_API_KEY |
| 50 | + if not resolved_key: |
| 51 | + raise RuntimeError( |
| 52 | + "ANTHROPIC_API_KEY is required when KG_LLM_PROVIDER=anthropic" |
| 53 | + ) |
| 54 | + self._client = AsyncAnthropic(api_key=resolved_key) |
| 55 | + self._model = model or settings.ANTHROPIC_EXTRACTION_MODEL |
| 56 | + self._max_tokens = max_tokens |
| 57 | + |
| 58 | + async def chat_json(self, system: str, user: str) -> str: |
| 59 | + response = await self._client.messages.create( |
| 60 | + model=self._model, |
| 61 | + max_tokens=self._max_tokens, |
| 62 | + system=system + _JSON_REINFORCEMENT, |
| 63 | + messages=[{"role": "user", "content": user}], |
| 64 | + ) |
| 65 | + # The Anthropic SDK returns a list of content blocks; for plain |
| 66 | + # text completions there is exactly one TextBlock. |
| 67 | + parts: list[str] = [] |
| 68 | + for block in response.content: |
| 69 | + text = getattr(block, "text", None) |
| 70 | + if isinstance(text, str): |
| 71 | + parts.append(text) |
| 72 | + return _strip_code_fences("".join(parts)) |
0 commit comments