# Fine-tune a model then serve it with vLLM

> **About this page**
>
> - Kind: Example
> - Summary: Fine-tunes a small chat model with TRL, serves the result through vLLM's OpenAI-compatible server, and checks it answers.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/train-then-serve
> - CLI reference for its commands: [`swale workflow create`](https://docs.swale.io/reference/cli-reference.md#workflow-create), [`swale workflow run start`](https://docs.swale.io/reference/cli-reference.md#workflow-run-start)
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.
>
> The steps on this page cost money or cannot be undone. Please check with the user before running them.

Source on GitHub: the [`workflows/train-then-serve`](https://github.com/swaleio/examples/tree/main/workflows/train-then-serve) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/train-then-serve/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/train-then-serve/workflow.yaml).

Fine-tunes a small chat model and serves it right away through vLLM's
OpenAI-compatible server, inside the same run. A smoke test waits for the
server, asks one question, and saves the answer; when it is done the platform
stops the server. Add your own consumers of the server, and list them in the
server's `terminate_on`.

| Step | Task definition | Compute |
|------|-----------------|---------|
| Write the training config | [`swaleio/bash`](https://hub.swale.io/swaleio/bash) | `cpu-4` |
| Fine-tune | [`swaleio/trl-sft`](https://hub.swale.io/swaleio/trl-sft) | `a100-1` |
| Write the smoke-test request | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Serve the fine-tuned model, as a runner | [`swaleio/vllm-serve`](https://hub.swale.io/swaleio/vllm-serve) | `a100-1` |
| Wait for the server, checking every 5 seconds | [`swaleio/http-request`](https://hub.swale.io/swaleio/http-request) | `cpu-4` |
| Ask the question | [`swaleio/http-request`](https://hub.swale.io/swaleio/http-request) | `cpu-4` |

The Compute column gives each step's [compute type](https://docs.swale.io/explanation/core-concepts.md#compute-types), the compute its tasks are allocated.

Every step is its own task.

## Inputs

| Input | Default | Description |
|-------|---------|-------------|
| `model` | `Qwen/Qwen2.5-0.5B-Instruct` | Model to fine-tune. |
| `dataset` | `HuggingFaceH4/ultrafeedback_binarized` | Hugging Face dataset with a `messages` column. |
| `split` | `train_sft[:5000]` | Split, or slice of one, to train on. |
| `question` | a question about fine-tuning | What the smoke test asks. |

Training reads the secret `hf_token`.

## Output

The fine-tuned model in `model/` on the workspace, and the server's answer in
`answer.json`.

## Run it

You need the CLI, logged in to your account, and a project to add the workflow to; [Getting started](https://docs.swale.io/getting-started.md) sets them up.

1. Save the file under [The workflow file](#the-workflow-file) as `workflow.yaml`, and add it to a project:

   ```bash
   swale workflow create --project-name my-project --file workflow.yaml
   ```

   The command prints `Workflow created:` followed by the workflow's ID.

2. The workflow reads the secret `hf_token`. Create it as a project secret, or as an account secret that allows the project, as [Use secrets in workflows](https://docs.swale.io/how-to-guides/use-secrets-in-workflows.md) shows.

3. Start a run with the workflow's ID:

   ```bash
   swale workflow run start --project-name my-project --workflow-id <workflow-id>
   ```

   Each input you leave out takes its default from [Inputs](#inputs), and `--input <name>=<value>` gives it another value.

[Create and manage workflows](https://docs.swale.io/how-to-guides/create-and-manage-workflows.md) covers the other ways to add a workflow, [Start and manage workflow runs](https://docs.swale.io/how-to-guides/start-and-manage-workflow-runs.md) how to follow a run, and [View and stream task logs](https://docs.swale.io/how-to-guides/view-and-stream-task-logs.md) how to read its tasks' logs.

## The workflow file

[Workflow definition syntax](https://docs.swale.io/reference/workflow-definition-syntax.md) describes each key in it.

```yaml title="workflow.yaml"
name: Fine-tune a model then serve it with vLLM
description: Fine-tunes a small chat model with TRL, serves the result through vLLM's OpenAI-compatible server, and checks it answers.
compute_type: cpu-4

inputs:
  model:
    description: Hugging Face repo id of the model to fine-tune.
    default: Qwen/Qwen2.5-0.5B-Instruct
  dataset:
    description: Hugging Face dataset with a messages column of chat turns.
    default: HuggingFaceH4/ultrafeedback_binarized
  split:
    description: Split of the dataset to train on.
    default: train_sft[:5000]
  question:
    description: The question the smoke test asks the served model.
    default: In one sentence, what is supervised fine-tuning?

entry_point: main

blocks:
  main:
    tasks:
      write_config:
        name: Write training config
        uses: swaleio/bash
        args:
          script: |
            cat > "$WORKFLOW_STORAGE/sft.yaml" <<EOF
            model_name_or_path: ${{inputs.model}}
            datasets:
              - path: ${{inputs.dataset}}
                split: ${{inputs.split}}
                columns: [messages]
            learning_rate: 1.0e-5
            num_train_epochs: 1
            per_device_train_batch_size: 8
            gradient_accumulation_steps: 2
            max_length: 1024
            bf16: true
            report_to: none
            save_strategy: "no"
            output_dir: $WORKFLOW_STORAGE/model
            EOF

      train:
        name: Fine-tune
        uses: swaleio/trl-sft
        compute_type: a100-1
        start_on:
          - write_config
        args:
          config: ${{env.WORKFLOW_STORAGE}}/sft.yaml
          token: ${{secrets.hf_token}}

      write_request:
        name: Write the smoke-test request
        uses: swaleio/duckdb
        args:
          sql: |
            COPY (SELECT '${{env.WORKFLOW_STORAGE}}/model' AS model,
                         [{'role': 'user', 'content': $$${{inputs.question}}$$}] AS messages,
                         128 AS max_tokens)
              TO '${{env.WORKFLOW_STORAGE}}/request.json' (FORMAT json);

      serve:
        name: Serve the fine-tuned model
        uses: swaleio/vllm-serve
        compute_type: a100-1
        start_on:
          - train
        terminate_on:
          - ask
        args:
          model: ${{env.WORKFLOW_STORAGE}}/model

      wait_ready:
        name: Wait for the server
        uses: swaleio/http-request
        start_on:
          - serve
        args:
          url: http://${{tasks.serve.ip-address}}:8000/health
          retry: "360"
          retry_delay: "5"
          expected_status: "200"

      ask:
        name: Ask a question
        uses: swaleio/http-request
        start_on:
          - wait_ready
          - write_request
        args:
          url: http://${{tasks.serve.ip-address}}:8000/v1/chat/completions
          method: POST
          headers: |
            Content-Type: application/json
          body_file: ${{env.WORKFLOW_STORAGE}}/request.json
          expected_status: "200"
          response_path: ${{env.WORKFLOW_STORAGE}}/answer.json
```

## Licenses of the defaults

- Model: [Qwen/Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct), Apache-2.0.
- Training data: [HuggingFaceH4/ultrafeedback_binarized](https://huggingface.co/datasets/HuggingFaceH4/ultrafeedback_binarized), MIT.
