The official Python library for the cerebras API
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Model listing and information retrieval for discovering available models and their capabilities. Provides access to model metadata, supported features, and configuration options for selecting the appropriate model for specific use cases.
Retrieve a list of all available models with their basic information and capabilities.
def list(
self,
*,
cf_ray: str | NotGiven = NOT_GIVEN,
x_amz_cf_id: str | NotGiven = NOT_GIVEN,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> ModelListResponse:
"""
List available models.
Returns information about all models available through the Cerebras Cloud API,
including model IDs, capabilities, and basic metadata.
Parameters:
- cf_ray: CloudFlare Ray ID for request tracing
- x_amz_cf_id: Amazon CloudFront ID for request tracing
- extra_headers: Additional headers to include with the request
- extra_query: Additional query parameters
- extra_body: Additional request body data
- timeout: Request timeout override
Returns:
ModelListResponse containing list of available models
"""Get detailed information about a specific model including its capabilities, parameters, and configuration options.
def retrieve(
self,
model_id: str,
*,
cf_ray: str | NotGiven = NOT_GIVEN,
x_amz_cf_id: str | NotGiven = NOT_GIVEN,
# Use the following arguments if you need to pass additional parameters to the API
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> ModelRetrieveResponse:
"""
Retrieve detailed information about a specific model.
Parameters:
- model_id: The ID of the model to retrieve (e.g., "llama3.1-70b")
- cf_ray: CloudFlare Ray ID for request tracing
- x_amz_cf_id: Amazon CloudFront ID for request tracing
- extra_headers: Additional headers to include with the request
- extra_query: Additional query parameters
- extra_body: Additional request body data
- timeout: Request timeout override
Returns:
ModelRetrieveResponse with detailed model information
"""Synchronous and asynchronous resource classes that provide the models API methods.
class ModelsResource(SyncAPIResource):
"""Synchronous models resource."""
@cached_property
def with_raw_response(self) -> ModelsResourceWithRawResponse: ...
@cached_property
def with_streaming_response(self) -> ModelsResourceWithStreamingResponse: ...
class AsyncModelsResource(AsyncAPIResource):
"""Asynchronous models resource."""
@cached_property
def with_raw_response(self) -> AsyncModelsResourceWithRawResponse: ...
@cached_property
def with_streaming_response(self) -> AsyncModelsResourceWithStreamingResponse: ...class ModelListResponse(BaseModel):
"""Response containing list of available models."""
data: List[Model]
object: Literal["list"]
class Model(BaseModel):
"""Individual model information in list response."""
id: str
created: int
object: Literal["model"]
owned_by: strclass ModelRetrieveResponse(BaseModel):
"""Detailed information about a specific model."""
id: str
created: int
object: Literal["model"]
owned_by: str
# Additional model-specific metadata may be includedfrom cerebras.cloud.sdk import Cerebras
client = Cerebras()
# Get list of all available models
models = client.models.list()
print("Available models:")
for model in models.data:
print(f"- {model.id} (owned by: {model.owned_by})")from cerebras.cloud.sdk import Cerebras
client = Cerebras()
# Get detailed information about a specific model
model_info = client.models.retrieve("llama3.1-70b")
print(f"Model ID: {model_info.id}")
print(f"Created: {model_info.created}")
print(f"Owned by: {model_info.owned_by}")from cerebras.cloud.sdk import Cerebras
def select_best_model(client: Cerebras, task_type: str) -> str:
"""
Select the most appropriate model for a given task type.
Args:
client: Cerebras client instance
task_type: Type of task ('chat', 'completion', 'code', etc.)
Returns:
Model ID string
"""
models = client.models.list()
# Simple model selection logic based on model ID patterns
model_ids = [model.id for model in models.data]
if task_type == "chat":
# Prefer larger models for chat tasks
for model_id in model_ids:
if "70b" in model_id.lower():
return model_id
elif task_type == "code":
# Look for code-specific models
for model_id in model_ids:
if "code" in model_id.lower():
return model_id
# Default to first available model
return model_ids[0] if model_ids else "llama3.1-70b"
# Usage
client = Cerebras()
best_model = select_best_model(client, "chat")
print(f"Selected model: {best_model}")
# Use the selected model
response = client.chat.completions.create(
model=best_model,
messages=[{"role": "user", "content": "Hello!"}]
)import asyncio
from cerebras.cloud.sdk import AsyncCerebras
async def get_model_info():
client = AsyncCerebras()
# List models asynchronously
models = await client.models.list()
print(f"Found {len(models.data)} models")
# Get details for the first model
if models.data:
first_model = models.data[0]
details = await client.models.retrieve(first_model.id)
print(f"First model: {details.id}")
await client.aclose()
asyncio.run(get_model_info())from cerebras.cloud.sdk import Cerebras, NotFoundError, APIError
client = Cerebras()
try:
# Try to retrieve a model that might not exist
model = client.models.retrieve("nonexistent-model")
print(f"Model found: {model.id}")
except NotFoundError:
print("Model not found")
# Fallback to listing available models
models = client.models.list()
print("Available models:")
for model in models.data:
print(f"- {model.id}")
except APIError as e:
print(f"API error: {e}")from cerebras.cloud.sdk import Cerebras
from datetime import datetime
client = Cerebras()
def analyze_models():
"""Analyze available models and their metadata."""
models = client.models.list()
print(f"Total models: {len(models.data)}")
# Group by owner
owners = {}
for model in models.data:
owner = model.owned_by
if owner not in owners:
owners[owner] = []
owners[owner].append(model.id)
print("\nModels by owner:")
for owner, model_ids in owners.items():
print(f" {owner}: {len(model_ids)} models")
for model_id in sorted(model_ids):
print(f" - {model_id}")
# Show creation dates
print("\nModel creation dates:")
for model in sorted(models.data, key=lambda m: m.created):
created_date = datetime.fromtimestamp(model.created)
print(f" {model.id}: {created_date.strftime('%Y-%m-%d %H:%M:%S')}")
analyze_models()Install with Tessl CLI
npx tessl i tessl/pypi-cerebras-cloud-sdk