mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-12-22 22:42:25 +00:00
feat: create dynamic model registration for Anthropic remote inference provider
This commit is contained in:
parent
e1ed152779
commit
2adc228762
4 changed files with 1585 additions and 1520 deletions
|
|
@ -4,11 +4,17 @@
|
||||||
# This source code is licensed under the terms described in the LICENSE file in
|
# This source code is licensed under the terms described in the LICENSE file in
|
||||||
# the root directory of this source tree.
|
# 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 llama_stack.providers.utils.inference.litellm_openai_mixin import LiteLLMOpenAIMixin
|
||||||
|
|
||||||
from .config import AnthropicConfig
|
from .config import AnthropicConfig
|
||||||
from .models import MODEL_ENTRIES
|
from .models import MODEL_ENTRIES
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class AnthropicInferenceAdapter(LiteLLMOpenAIMixin):
|
class AnthropicInferenceAdapter(LiteLLMOpenAIMixin):
|
||||||
def __init__(self, config: AnthropicConfig) -> None:
|
def __init__(self, config: AnthropicConfig) -> None:
|
||||||
|
|
@ -19,9 +25,35 @@ class AnthropicInferenceAdapter(LiteLLMOpenAIMixin):
|
||||||
provider_data_api_key_field="anthropic_api_key",
|
provider_data_api_key_field="anthropic_api_key",
|
||||||
)
|
)
|
||||||
self.config = config
|
self.config = config
|
||||||
|
self._client: AsyncAnthropic | None = None
|
||||||
|
|
||||||
async def initialize(self) -> None:
|
async def initialize(self) -> None:
|
||||||
await super().initialize()
|
await super().initialize()
|
||||||
|
|
||||||
async def shutdown(self) -> None:
|
async def shutdown(self) -> None:
|
||||||
|
# Clean up the client connection pool
|
||||||
|
if self._client:
|
||||||
|
await self._client.aclose()
|
||||||
|
self._client = None
|
||||||
await super().shutdown()
|
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
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ classifiers = [
|
||||||
]
|
]
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aiohttp",
|
"aiohttp",
|
||||||
|
"anthropic>=0.58.2",
|
||||||
"fastapi>=0.115.0,<1.0", # server
|
"fastapi>=0.115.0,<1.0", # server
|
||||||
"fire", # for MCP in LLS client
|
"fire", # for MCP in LLS client
|
||||||
"httpx",
|
"httpx",
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,11 @@ aiosqlite==0.21.0
|
||||||
# via llama-stack
|
# via llama-stack
|
||||||
annotated-types==0.7.0
|
annotated-types==0.7.0
|
||||||
# via pydantic
|
# via pydantic
|
||||||
|
anthropic==0.58.2
|
||||||
|
# via llama-stack
|
||||||
anyio==4.8.0
|
anyio==4.8.0
|
||||||
# via
|
# via
|
||||||
|
# anthropic
|
||||||
# httpx
|
# httpx
|
||||||
# llama-api-client
|
# llama-api-client
|
||||||
# llama-stack-client
|
# llama-stack-client
|
||||||
|
|
@ -50,6 +53,7 @@ deprecated==1.2.18
|
||||||
# opentelemetry-semantic-conventions
|
# opentelemetry-semantic-conventions
|
||||||
distro==1.9.0
|
distro==1.9.0
|
||||||
# via
|
# via
|
||||||
|
# anthropic
|
||||||
# llama-api-client
|
# llama-api-client
|
||||||
# llama-stack-client
|
# llama-stack-client
|
||||||
# openai
|
# openai
|
||||||
|
|
@ -82,6 +86,7 @@ httpcore==1.0.9
|
||||||
# via httpx
|
# via httpx
|
||||||
httpx==0.28.1
|
httpx==0.28.1
|
||||||
# via
|
# via
|
||||||
|
# anthropic
|
||||||
# llama-api-client
|
# llama-api-client
|
||||||
# llama-stack
|
# llama-stack
|
||||||
# llama-stack-client
|
# llama-stack-client
|
||||||
|
|
@ -99,7 +104,9 @@ importlib-metadata==8.5.0
|
||||||
jinja2==3.1.6
|
jinja2==3.1.6
|
||||||
# via llama-stack
|
# via llama-stack
|
||||||
jiter==0.8.2
|
jiter==0.8.2
|
||||||
# via openai
|
# via
|
||||||
|
# anthropic
|
||||||
|
# openai
|
||||||
jsonschema==4.23.0
|
jsonschema==4.23.0
|
||||||
# via llama-stack
|
# via llama-stack
|
||||||
jsonschema-specifications==2024.10.1
|
jsonschema-specifications==2024.10.1
|
||||||
|
|
@ -169,6 +176,7 @@ pycparser==2.22 ; platform_python_implementation != 'PyPy'
|
||||||
# via cffi
|
# via cffi
|
||||||
pydantic==2.10.6
|
pydantic==2.10.6
|
||||||
# via
|
# via
|
||||||
|
# anthropic
|
||||||
# fastapi
|
# fastapi
|
||||||
# llama-api-client
|
# llama-api-client
|
||||||
# llama-stack
|
# llama-stack
|
||||||
|
|
@ -220,6 +228,7 @@ six==1.17.0
|
||||||
# python-dateutil
|
# python-dateutil
|
||||||
sniffio==1.3.1
|
sniffio==1.3.1
|
||||||
# via
|
# via
|
||||||
|
# anthropic
|
||||||
# anyio
|
# anyio
|
||||||
# llama-api-client
|
# llama-api-client
|
||||||
# llama-stack-client
|
# llama-stack-client
|
||||||
|
|
@ -243,6 +252,7 @@ tqdm==4.67.1
|
||||||
typing-extensions==4.12.2
|
typing-extensions==4.12.2
|
||||||
# via
|
# via
|
||||||
# aiosqlite
|
# aiosqlite
|
||||||
|
# anthropic
|
||||||
# anyio
|
# anyio
|
||||||
# fastapi
|
# fastapi
|
||||||
# huggingface-hub
|
# huggingface-hub
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue