fix: resolve CI linting and type errors
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
"""Anthropic provider implementation."""
|
||||
|
||||
import time
|
||||
from typing import Any, AsyncIterator, Dict, Optional
|
||||
from typing import AsyncIterator, Optional
|
||||
|
||||
from anthropic import Anthropic, APIError, RateLimitError
|
||||
|
||||
@@ -9,6 +10,8 @@ from ..core.exceptions import ProviderError
|
||||
|
||||
|
||||
class AnthropicProvider(ProviderBase):
|
||||
"""Anthropic Claude models provider."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
api_key: Optional[str] = None,
|
||||
@@ -16,6 +19,7 @@ class AnthropicProvider(ProviderBase):
|
||||
temperature: float = 0.7,
|
||||
**kwargs,
|
||||
):
|
||||
"""Initialize Anthropic provider."""
|
||||
super().__init__(api_key, model, temperature, **kwargs)
|
||||
self._client: Optional[Anthropic] = None
|
||||
|
||||
@@ -24,6 +28,7 @@ class AnthropicProvider(ProviderBase):
|
||||
return "anthropic"
|
||||
|
||||
def _get_client(self) -> Anthropic:
|
||||
"""Get or create Anthropic client."""
|
||||
if self._client is None:
|
||||
api_key = self.api_key or self._get_api_key_from_env()
|
||||
if not api_key:
|
||||
@@ -45,25 +50,37 @@ class AnthropicProvider(ProviderBase):
|
||||
max_tokens: Optional[int] = None,
|
||||
**kwargs,
|
||||
) -> ProviderResponse:
|
||||
"""Send completion request to Anthropic."""
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
client = self._get_client()
|
||||
|
||||
messages = [{"role": "user", "content": prompt}]
|
||||
if system_prompt:
|
||||
system_message = system_prompt
|
||||
user_message = prompt
|
||||
else:
|
||||
system_message = None
|
||||
user_message = prompt
|
||||
|
||||
response = client.messages.create(
|
||||
response = client.messages.create( # type: ignore[arg-type]
|
||||
model=self.model,
|
||||
messages=messages,
|
||||
temperature=self.temperature,
|
||||
max_tokens=max_tokens or 4096,
|
||||
temperature=self.temperature,
|
||||
system=system_message, # type: ignore[arg-type]
|
||||
messages=[{"role": "user", "content": user_message}],
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
latency_ms = (time.time() - start_time) * 1000
|
||||
|
||||
content = ""
|
||||
for block in response.content:
|
||||
if block.type == "text":
|
||||
content += block.text
|
||||
|
||||
return ProviderResponse(
|
||||
content=response.content[0].text,
|
||||
content=content,
|
||||
model=self.model,
|
||||
provider=self.name,
|
||||
usage={
|
||||
@@ -71,29 +88,39 @@ class AnthropicProvider(ProviderBase):
|
||||
"output_tokens": response.usage.output_tokens,
|
||||
},
|
||||
latency_ms=latency_ms,
|
||||
metadata={
|
||||
"stop_reason": response.stop_reason,
|
||||
},
|
||||
)
|
||||
except APIError as e:
|
||||
raise ProviderError(f"Anthropic API error: {e}")
|
||||
except RateLimitError as e:
|
||||
raise ProviderError(f"Anthropic rate limit exceeded: {e}")
|
||||
|
||||
async def stream_complete(
|
||||
async def stream_complete( # type: ignore[override]
|
||||
self,
|
||||
prompt: str,
|
||||
system_prompt: Optional[str] = None,
|
||||
max_tokens: Optional[int] = None,
|
||||
**kwargs,
|
||||
) -> AsyncIterator[str]:
|
||||
"""Stream completion from Anthropic."""
|
||||
try:
|
||||
client = self._get_client()
|
||||
|
||||
messages = [{"role": "user", "content": prompt}]
|
||||
if system_prompt:
|
||||
system_message = system_prompt
|
||||
user_message = prompt
|
||||
else:
|
||||
system_message = None
|
||||
user_message = prompt
|
||||
|
||||
with client.messages.stream(
|
||||
with client.messages.stream( # type: ignore[arg-type]
|
||||
model=self.model,
|
||||
messages=messages,
|
||||
temperature=self.temperature,
|
||||
max_tokens=max_tokens or 4096,
|
||||
temperature=self.temperature,
|
||||
system=system_message, # type: ignore[arg-type]
|
||||
messages=[{"role": "user", "content": user_message}],
|
||||
**kwargs,
|
||||
) as stream:
|
||||
for text in stream.text_stream:
|
||||
@@ -102,12 +129,24 @@ class AnthropicProvider(ProviderBase):
|
||||
raise ProviderError(f"Anthropic API error: {e}")
|
||||
|
||||
def validate_api_key(self) -> bool:
|
||||
"""Validate Anthropic API key."""
|
||||
try:
|
||||
import os
|
||||
api_key = self.api_key or os.environ.get("ANTHROPIC_API_KEY")
|
||||
if not api_key:
|
||||
return False
|
||||
client = Anthropic(api_key=api_key)
|
||||
_ = Anthropic(api_key=api_key)
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
return False
|
||||
|
||||
def list_models(self) -> list[str]:
|
||||
"""List available Anthropic models."""
|
||||
return [
|
||||
"claude-3-opus-20240229",
|
||||
"claude-3-sonnet-20240229",
|
||||
"claude-3-haiku-20240307",
|
||||
"claude-2.1",
|
||||
"claude-2.0",
|
||||
"claude-instant-1.2",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user