Cerebras/cerebras-cloud-sdk-python
Python
Captured source
source ↗Cerebras/cerebras-cloud-sdk-python
Language: Python
License: Apache-2.0
Stars: 135
Forks: 18
Open issues: 0
Created: 2024-07-31T18:26:42Z
Pushed: 2026-01-29T23:31:29Z
Default branch: main
Fork: no
Archived: no
README:
Cerebras Python API library
The Cerebras Python library provides convenient access to the Cerebras REST API from any Python 3.9+ application. The library includes type definitions for all request params and response fields, and offers both synchronous and asynchronous clients powered by httpx.
It is generated with Stainless.
About Cerebras
At Cerebras, we've developed the world's largest and fastest AI processor, the Wafer-Scale Engine-3 (WSE-3). The Cerebras CS-3 system, powered by the WSE-3, represents a new class of AI supercomputer that sets the standard for generative AI training and inference with unparalleled performance and scalability.
With Cerebras as your inference provider, you can:
- Achieve unprecedented speed for AI inference workloads
- Build commercially with high throughput
- Effortlessly scale your AI workloads with our seamless clustering technology
Our CS-3 systems can be quickly and easily clustered to create the largest AI supercomputers in the world, making it simple to place and run the largest models. Leading corporations, research institutions, and governments are already using Cerebras solutions to develop proprietary models and train popular open-source models.
Want to experience the power of Cerebras? Check out our website for more resources and explore options for accessing our technology through the Cerebras Cloud or on-premise deployments!
> [!NOTE] > This SDK has a mechanism that sends a few requests to /v1/tcp_warming upon construction to reduce the TTFT. If this behaviour is not desired, set warm_tcp_connection=False in the constructor. > > If you are repeatedly reconstructing the SDK instance it will lead to poor performance. It is recommended that you construct the SDK once and reuse the instance if possible.
Documentation
The REST API documentation can be found on inference-docs.cerebras.ai. The full API of this library can be found in [api.md](api.md).
Installation
pip install cerebras_cloud_sdk
API Key
Get an API Key from cloud.cerebras.ai and add it to your environment variables:
export CEREBRAS_API_KEY="your-api-key-here"
Usage
The full API of this library can be found in [api.md](api.md).
Chat Completion
import os
from cerebras.cloud.sdk import Cerebras
client = Cerebras(
api_key=os.environ.get("CEREBRAS_API_KEY"), # This is the default and can be omitted
)
chat_completion = client.chat.completions.create(
model="llama3.1-8b",
messages=[
{
"role": "user",
"content": "Why is fast inference important?",
}
],
)
print(chat_completion)Text Completion
import os
from cerebras.cloud.sdk import Cerebras
client = Cerebras(
api_key=os.environ.get("CEREBRAS_API_KEY"), # This is the default and can be omitted
)
completion = client.completions.create(
prompt="It was a dark and stormy ",
max_tokens=100,
model="llama3.1-8b",
)
print(completion)While you can provide an api_key keyword argument, we recommend using python-dotenv to add CEREBRAS_API_KEY="My API Key" to your .env file so that your API Key is not stored in source control.
Async usage
Simply import AsyncCerebras instead of Cerebras and use await with each API call:
import os
import asyncio
from cerebras.cloud.sdk import AsyncCerebras
client = AsyncCerebras(
api_key=os.environ.get("CEREBRAS_API_KEY"), # This is the default and can be omitted
)
async def main() -> None:
chat_completion = await client.chat.completions.create(
model="llama3.1-8b",
messages=[
{
"role": "user",
"content": "Why is fast inference important?",
}
],
)
print(chat_completion)
asyncio.run(main())Functionality between the synchronous and asynchronous clients is otherwise identical.
With aiohttp
By default, the async client uses httpx for HTTP requests. However, for improved concurrency performance you may also use aiohttp as the HTTP backend.
You can enable this by installing aiohttp:
pip install 'cerebras_cloud_sdk[aiohttp]'
Then you can enable it by instantiating the client with http_client=DefaultAioHttpClient():
import os
import asyncio
from cerebras.cloud.sdk import DefaultAioHttpClient
from cerebras.cloud.sdk import AsyncCerebras
async def main() -> None:
async with AsyncCerebras(
api_key=os.environ.get("CEREBRAS_API_KEY"), # This is the default and can be omitted
http_client=DefaultAioHttpClient(),
) as client:
chat_completion = await client.chat.completions.create(
model="llama3.1-8b",
messages=[
{
"role": "user",
"content": "Why is fast inference important?",
}
],
)
asyncio.run(main())Streaming responses
We provide support for streaming responses using Server Side Events (SSE).
Note that when streaming, usage and time_info will be information will only be included in the final chunk.
Chat Completion
import os
from cerebras.cloud.sdk import Cerebras
client = Cerebras(
# This is the default and can be omitted
api_key=os.environ.get("CEREBRAS_API_KEY"),
)
stream = client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Why is fast inference important?",
}
],
model="llama3.1-8b",
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")The async client uses the exact same interface.
import os
import asyncio
from cerebras.cloud.sdk import AsyncCerebras
client = AsyncCerebras(
# This is the default and can be omitted
api_key=os.environ.get("CEREBRAS_API_KEY"),
)
async def main() -> None:
stream = await client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Why is fast inference important?",
}
],
model="llama3.1-8b",
stream=True,
)
async for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
asyncio.run(main())Text Completion
import os
from cerebras.cloud.sdk import Cerebras
client = Cerebras(
# This is the default and can be omitted
api_key=os.environ.get("CEREBRAS_API_KEY"),
)
stream = client.completions.create(
prompt="It was a dark and stormy ",…Excerpt shown — open the source for the full document.
Notability
notability 5.0/10Solid new repo, moderate traction