Skip to content

Run Outlines using Cerebrium

Cerebrium is a serverless AI infrastructure platform that makes it easier for companies to build and deploy AI based applications. They offer Serverless GPU's with low cold start times with over 12 varieties of GPU chips that auto scale and you only pay for the compute you use.

In this guide we will show you how you can use Cerebrium to run programs written with Outlines on GPUs in the cloud.

Setup Cerebrium

First, we install Cerebrium and login to get authenticated.

pip install cerebrium
cerebrium login

Then let us create our first project

cerebrium init outlines-project

Setup Environment and Hardware

You set up your environment and hardware in the cerebrium.toml file that was created using the init function above.

cpu = 2
memory = 14.0
gpu = "AMPERE A10"
gpu_count = 1
provider = "aws"
region = "us-east-1"

outline = "==0.0.37"
transformers = "==4.38.2"
datasets = "==2.18.0"
accelerate = "==0.27.2"

Setup inference

Running code in Cerebrium is like writing normal python with no special syntax. In a file specify the following:

import outlines

model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")

schema = """{
    "title": "Character",
    "type": "object",
    "properties": {
        "name": {
            "title": "Name",
            "maxLength": 10,
            "type": "string"
        "age": {
            "title": "Age",
            "type": "integer"
        "armor": {"$ref": "#/definitions/Armor"},
        "weapon": {"$ref": "#/definitions/Weapon"},
        "strength": {
            "title": "Strength",
            "type": "integer"
    "required": ["name", "age", "armor", "weapon", "strength"],
    "definitions": {
        "Armor": {
            "title": "Armor",
            "description": "An enumeration.",
            "enum": ["leather", "chainmail", "plate"],
            "type": "string"
        "Weapon": {
            "title": "Weapon",
            "description": "An enumeration.",
            "enum": ["sword", "axe", "mace", "spear", "bow", "crossbow"],
            "type": "string"

generator = outlines.generate.json(model, schema)

On first deploy, it will download the model and store it on disk therefore for subsequent calls it will load the model from disk.

Every function in Cerebrium is callable through an API endpoint. Code at the top most layer (ie: not in a function) is instantiated only when the container is spun up the first time so for subsequent calls, it will simply run the code defined in the function you call.

To deploy an API that creates a new character when called with a prompt you can add the following code to

def generate(
    prompt: str = "Amiri, a 53 year old warrior woman with a sword and leather armor.",

    character = generator(
        f"<s>[INST]Give me a character description. Describe {prompt}.[/INST]"

    return character

Run on the cloud

cerebrium deploy

You will see your application deploy, install pip packages and download the model. Once completed it will output a CURL request you can use to call your endpoint. Just remember to end the url with the function you would like to call - in this case /generate. You should see your response returned!