feat: create dynamic model registration for Anthropic remote inference provider

This commit is contained in:
r3v5 2025-07-23 18:45:46 +01:00
parent e1ed152779
commit 2adc228762
No known key found for this signature in database
GPG key ID: 7758B9F272DE67D9
4 changed files with 1585 additions and 1520 deletions

View file

@ -4,11 +4,17 @@
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.
import logging
from anthropic import AsyncAnthropic, NotFoundError
from llama_stack.providers.utils.inference.litellm_openai_mixin import LiteLLMOpenAIMixin
from .config import AnthropicConfig
from .models import MODEL_ENTRIES
logger = logging.getLogger(__name__)
class AnthropicInferenceAdapter(LiteLLMOpenAIMixin):
def __init__(self, config: AnthropicConfig) -> None:
@ -19,9 +25,35 @@ class AnthropicInferenceAdapter(LiteLLMOpenAIMixin):
provider_data_api_key_field="anthropic_api_key",
)
self.config = config
self._client: AsyncAnthropic | None = None
async def initialize(self) -> None:
await super().initialize()
async def shutdown(self) -> None:
# Clean up the client connection pool
if self._client:
await self._client.aclose()
self._client = None
await super().shutdown()
@property
def client(self) -> AsyncAnthropic:
if self._client is None:
api_key = self.config.api_key if self.config.api_key else "no-key"
self._client = AsyncAnthropic(api_key=api_key)
return self._client
async def check_model_availability(self, model: str) -> bool:
try:
retrieved_model = await self.client.models.retrieve(model)
logger.info(f"Model {retrieved_model.id} is available on Anthropic")
return True
except NotFoundError:
logger.info(f"Model {model} was not found on Anthropic")
except Exception as e:
logger.error(f"Failed to check model availability for {model} on Anthropic: {e}")
return False