Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/any_llm/providers/cerebras/cerebras.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import AsyncIterator, Iterator
from collections.abc import AsyncIterator, Iterator, Sequence
from typing import Any, cast

from pydantic import BaseModel
Expand All @@ -15,10 +15,12 @@
from any_llm.exceptions import UnsupportedParameterError
from any_llm.provider import ApiConfig, Provider
from any_llm.providers.cerebras.utils import (
_convert_models_list,
_convert_response,
_create_openai_chunk_from_cerebras_chunk,
)
from any_llm.types.completion import ChatCompletion, ChatCompletionChunk, CompletionParams
from any_llm.types.model import Model
from any_llm.utils.instructor import _convert_instructor_response


Expand All @@ -34,7 +36,7 @@ class CerebrasProvider(Provider):
SUPPORTS_RESPONSES = False
SUPPORTS_COMPLETION_REASONING = False
SUPPORTS_EMBEDDING = False
SUPPORTS_LIST_MODELS = False
SUPPORTS_LIST_MODELS = True

PACKAGES_INSTALLED = PACKAGES_INSTALLED

Expand Down Expand Up @@ -194,3 +196,10 @@ def completion(
raise ValueError(msg)

return _convert_response(response_data)

def list_models(self, **kwargs: Any) -> Sequence[Model]:
"""
Fetch available models from the /v1/models endpoint.
"""
models_list = self.client.models.list(**kwargs)
return _convert_models_list(models_list)
9 changes: 9 additions & 0 deletions src/any_llm/providers/cerebras/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any

try:
from cerebras.cloud.sdk.types import ModelListResponse as CerebrasModelListResponse
from cerebras.cloud.sdk.types.chat.chat_completion import ChatChunkResponse
except ImportError as exc:
msg = "cerebras is not installed. Please install it with `pip install any-llm-sdk[cerebras]`"
Expand All @@ -16,6 +17,7 @@
CompletionUsage,
Function,
)
from any_llm.types.model import Model


def _create_openai_chunk_from_cerebras_chunk(chunk: ChatChunkResponse) -> ChatCompletionChunk:
Expand Down Expand Up @@ -154,3 +156,10 @@ def _convert_response(response_data: dict[str, Any]) -> ChatCompletion:
choices=choices_out,
usage=usage,
)


def _convert_models_list(models_list: CerebrasModelListResponse) -> list[Model]:
return [
Model(id=model.id, object="model", created=model.created or 0, owned_by="cerebras")
for model in models_list.data
]