| 192 | |
| 193 | |
| 194 | class AnthropicLLM: |
| 195 | name = "anthropic" |
| 196 | |
| 197 | def __init__(self, model: str, api_key: str, base_url: Optional[str] = None, timeout: float = 60.0) -> None: |
| 198 | from anthropic import Anthropic # local import keeps dependency optional |
| 199 | |
| 200 | self.model = model |
| 201 | self._client = Anthropic(api_key=api_key, base_url=base_url, timeout=timeout) |
| 202 | |
| 203 | def generate( |
| 204 | self, |
| 205 | prompt: str, |
| 206 | *, |
| 207 | system: Optional[str] = None, |
| 208 | temperature: float = 0.0, |
| 209 | max_tokens: int = 1024, |
| 210 | ) -> LLMResponse: |
| 211 | kwargs = { |
| 212 | "model": self.model, |
| 213 | "max_tokens": max_tokens, |
| 214 | "temperature": temperature, |
| 215 | "messages": [{"role": "user", "content": prompt}], |
| 216 | } |
| 217 | if system: |
| 218 | kwargs["system"] = system |
| 219 | |
| 220 | response = self._client.messages.create(**kwargs) |
| 221 | text = "".join( |
| 222 | block.text # type: ignore[attr-defined] |
| 223 | for block in response.content |
| 224 | if getattr(block, "type", None) == "text" |
| 225 | ) |
| 226 | return LLMResponse( |
| 227 | text=text, |
| 228 | model=self.model, |
| 229 | provider=self.name, |
| 230 | prompt_tokens=getattr(response.usage, "input_tokens", 0) or 0, |
| 231 | completion_tokens=getattr(response.usage, "output_tokens", 0) or 0, |
| 232 | ) |
| 233 | |
| 234 | def stream_generate( |
| 235 | self, |
| 236 | prompt: str, |
| 237 | *, |
| 238 | system: Optional[str] = None, |
| 239 | temperature: float = 0.0, |
| 240 | max_tokens: int = 1024, |
| 241 | ) -> Iterator[str]: |
| 242 | kwargs = { |
| 243 | "model": self.model, |
| 244 | "max_tokens": max_tokens, |
| 245 | "temperature": temperature, |
| 246 | "messages": [{"role": "user", "content": prompt}], |
| 247 | } |
| 248 | if system: |
| 249 | kwargs["system"] = system |
| 250 | |
| 251 | with self._client.messages.stream(**kwargs) as stream: |
no outgoing calls
no test coverage detected