Functions and Scaling Groups
Combine settings in the @chalkcompute.function decorator, inject secrets, mount volumes, and find the settings functions share with other workloads.
This page covers the configuration specific to functions. These settings work the same way across workloads and have their own pages:
The decorator accepts a wide set of arguments that control the compute environment a function runs in: image, CPU/memory/GPU, secrets, volumes, batching, retries, rate limits, concurrency, autoscaling, and tracing. A realistic decorator usually combines several of them:
import chalkcompute
@chalkcompute.function(
image=chalkcompute.Image.base("python:3.12-slim").run_commands(
"pip install sentence-transformers",
),
cpu="2",
memory="4Gi",
gpu="nvidia-l4",
secrets=[chalkcompute.Secret.from_chalk_env("HF_TOKEN")],
max_batching_size=25,
max_buffer_duration=2000,
retries=3,
)
def embed(texts: list[str]) -> list[list[float]]:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2")
return model.encode(texts).tolist()
Each argument is covered on its own page or in a section below: the shared
settings listed above, Batching,
Retries,
Rate and Concurrency Limits,
Secrets, and Volumes. For the full per-argument
reference, types, and defaults, see
@chalkcompute.function in the
API reference.
Inject secrets into a function using the secrets parameter. Secrets are
resolved at deploy time and made available as environment variables inside
the container:
import chalkcompute
from chalkcompute import Secret
@chalkcompute.function(
secrets=[
Secret.from_chalk_env("OPENAI_API_KEY"),
Secret.from_chalk_integration("prod_postgres"),
],
)
def call_api(prompt: str) -> str:
import os
openai_api_key = os.environ["OPENAI_API_KEY"]
database_url = os.environ["DATABASE_URL"]
...
See Secrets for the full Secret API.
Attach persistent storage to a function with the volumes parameter. Each
entry is a (name, mount_path) string tuple. Inside the function, read and
write the volume as an ordinary filesystem path:
import chalkcompute
@chalkcompute.function(
volumes=[("model-cache", "/models")],
gpu="nvidia-l4",
)
def predict(text: str) -> list[float]:
import os
import json
from sentence_transformers import SentenceTransformer
cache_path = "/models/all-MiniLM-L6-v2"
if os.path.exists(cache_path):
model = SentenceTransformer(cache_path)
else:
model = SentenceTransformer("all-MiniLM-L6-v2")
model.save(cache_path)
return model.encode(text).tolist()
Volumes persist across invocations: data written by one call is available to subsequent calls. This is useful for caching large model weights, storing intermediate results, or sharing data between functions that mount the same volume.