> ## Documentation Index
> Fetch the complete documentation index at: https://docs.baseten.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Build and deploy an LLM

> Package and deploy an LLM with Truss, from model setup to inference.

> [card] [View example on GitHub](https://github.com/basetenlabs/truss-examples/tree/main/02-llm)

This guide walks through deploying Mistral-7B, a powerful large language model (LLM), using Truss. You'll configure the model, set up inference, allocate resources, and deploy it as an API endpoint.

# Set up your model

Start by importing the necessary libraries:

```python model/model.py theme={"system"}
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
```

Specify the Hugging Face model checkpoint:

```python model/model.py theme={"system"}
CHECKPOINT = "mistralai/Mistral-7B-v0.1"

```

# Define the model class

Create a `Model` class that loads Mistral-7B and its tokenizer when the server starts:

```python model/model.py theme={"system"}
class Model:
    def __init__(self, **kwargs) -> None:
        self.tokenizer = None
        self.model = None

    def load(self):
        self.model = AutoModelForCausalLM.from_pretrained(
            CHECKPOINT, dtype=torch.float16, device_map="auto"
        )
        self.tokenizer = AutoTokenizer.from_pretrained(CHECKPOINT)
```

# Implement inference

The `predict` function handles inference by tokenizing input, generating text, and decoding the output.

```python model/model.py theme={"system"}
    def predict(self, request: dict):
        prompt = request.pop("prompt")
        generate_args = {
            "max_new_tokens": request.get("max_new_tokens", 128),
            "temperature": request.get("temperature", 1.0),
            "top_p": request.get("top_p", 0.95),
            "top_k": request.get("top_k", 50),
            "repetition_penalty": 1.0,
            "use_cache": True,
            "do_sample": True,
            "eos_token_id": self.tokenizer.eos_token_id,
            "pad_token_id": self.tokenizer.pad_token_id,
        }

        input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids.cuda()

        with torch.no_grad():
            output = self.model.generate(input_ids=input_ids, **generate_args)
            return self.tokenizer.decode(output[0])
```

# Configure your deployment

## Define dependencies

Specify the necessary Python packages in `config.yaml`:

```yaml config.yaml theme={"system"}
model_name: Mistral 7B
python_version: py311
requirements:
  - transformers==5.17.0
  - sentencepiece==0.2.2
  - accelerate==1.15.0
  - torch==2.14.0
  - numpy==2.4.6
```

## Allocate compute resources

Mistral-7B requires an NVIDIA A10G GPU for efficient inference:

```yaml config.yaml theme={"system"}
resources:
  accelerator: A10G
  use_gpu: true
```

# Deploy the model

Install the Baseten CLI and sign in:

**Install the Baseten CLI**

> [tab] macOS or Linux
>
> ```bash Terminal theme={"system"}
> brew tap basetenlabs/baseten
> brew trust --formula basetenlabs/baseten/baseten
> brew install baseten
> ```

> [tab] Windows
>
> Download and extract the Windows archive for the latest Baseten CLI release:
>
> ```powershell Terminal theme={"system"}
> $release = Invoke-RestMethod https://api.github.com/repos/basetenlabs/baseten-cli/releases/latest
> $asset = $release.assets | Where-Object { $_.name -like "*_windows_amd64.zip" }
> Invoke-WebRequest $asset.browser_download_url -OutFile baseten.zip
> Expand-Archive -Force baseten.zip .
> ```
>
> Move `baseten.exe` to a directory on your `PATH`.

For other platforms or a specific version, see the [Baseten CLI install reference](https://docs.baseten.co/reference/cli/baseten/overview.md#install).

**Sign in**

```sh theme={"system"}
baseten auth login
```

From the directory that contains `config.yaml`, push your model to Baseten:

```sh theme={"system"}
baseten model push
```

You should see output like:

```output theme={"system"}
Pushing model "Mistral 7B"...
Uploading model...
Uploaded model in 0s
✨ Model Mistral 7B was successfully pushed ✨

  Model:       Mistral 7B (abc1d2ef)
  Deployment:  xyz123
  Environment: production

🪵 View logs:
   deployment:   baseten model deployment logs --model-id abc1d2ef --deployment-id xyz123
   environment:  baseten model environment logs --model-id abc1d2ef --environment production  (once deployed)
   app:          https://app.baseten.co/models/abc1d2ef/logs/xyz123

🚀 Invoke your model:
   URL:  https://model-abc1d2ef.api.baseten.co/deployment/xyz123/predict
   CLI:  baseten model predict --model-id abc1d2ef
```

Copy the model ID from the `Model:` line (`abc1d2ef` in the example). The first deploy takes several minutes while Baseten builds the image and the model loads its weights from Hugging Face.

Once the deployment is active, call the model with the Baseten CLI:

```sh theme={"system"}
baseten model predict --model-id abc1d2ef --data '{"prompt": "What is a large language model?"}'
```

To call the model's endpoint from Python instead, create a personal key in [API keys](https://app.baseten.co/settings/api_keys) and set it in the terminal where you'll run the request:

```bash macOS/Linux theme={"system"}
export BASETEN_API_KEY="YOUR_API_KEY"
```

```powershell Windows theme={"system"}
$env:BASETEN_API_KEY = "YOUR_API_KEY"
```

A personal API key works for these steps on your own machine. For applications, CI, and shared automation, use a [team API key](https://docs.baseten.co/organization/api-keys.md#choose-a-credential) scoped to the models, environments, or Model APIs that workload needs.

Then send the request:

```python call_model.py theme={"system"}
import os
import requests

model_id = "abc1d2ef"

response = requests.post(
    f"https://model-{model_id}.api.baseten.co/production/predict",
    headers={"Authorization": f"Bearer {os.environ['BASETEN_API_KEY']}"},
    json={"prompt": "What is a large language model?"},
)

print(response.json())
```

Replace `abc1d2ef` with your model ID.

To use the Truss CLI instead, sign in with `truss login`, deploy with `truss push`, and call the model with `truss predict --published -d '{"prompt": "What is a large language model?"}'`.

# Check for optimized engine support

For faster inference, check whether your model runs on a Baseten engine such as [Engine-Builder-LLM](https://docs.baseten.co/engines/engine-builder-llm/overview.md), which builds TensorRT-LLM engines, or on an open-source server such as [vLLM](https://docs.baseten.co/examples/vllm.md) or [SGLang](https://docs.baseten.co/examples/sglang.md). For an overview, see [Engines](https://docs.baseten.co/engines.md).
