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

View file

@ -22,6 +22,7 @@ classifiers = [
]
dependencies = [
"aiohttp",
"anthropic>=0.58.2",
"fastapi>=0.115.0,<1.0", # server
"fire", # for MCP in LLS client
"httpx",

View file

@ -10,8 +10,11 @@ aiosqlite==0.21.0
# via llama-stack
annotated-types==0.7.0
# via pydantic
anthropic==0.58.2
# via llama-stack
anyio==4.8.0
# via
# anthropic
# httpx
# llama-api-client
# llama-stack-client
@ -50,6 +53,7 @@ deprecated==1.2.18
# opentelemetry-semantic-conventions
distro==1.9.0
# via
# anthropic
# llama-api-client
# llama-stack-client
# openai
@ -82,6 +86,7 @@ httpcore==1.0.9
# via httpx
httpx==0.28.1
# via
# anthropic
# llama-api-client
# llama-stack
# llama-stack-client
@ -99,7 +104,9 @@ importlib-metadata==8.5.0
jinja2==3.1.6
# via llama-stack
jiter==0.8.2
# via openai
# via
# anthropic
# openai
jsonschema==4.23.0
# via llama-stack
jsonschema-specifications==2024.10.1
@ -169,6 +176,7 @@ pycparser==2.22 ; platform_python_implementation != 'PyPy'
# via cffi
pydantic==2.10.6
# via
# anthropic
# fastapi
# llama-api-client
# llama-stack
@ -220,6 +228,7 @@ six==1.17.0
# python-dateutil
sniffio==1.3.1
# via
# anthropic
# anyio
# llama-api-client
# llama-stack-client
@ -243,6 +252,7 @@ tqdm==4.67.1
typing-extensions==4.12.2
# via
# aiosqlite
# anthropic
# anyio
# fastapi
# huggingface-hub

3060
uv.lock generated

File diff suppressed because it is too large Load diff