# Score answers with an LLM as a judge

> **About this page**
>
> - Kind: Example
> - Summary: Answers a set of prompts with one model, has a stronger judge model score every answer from 1 to 10, and summarizes the scores.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/llm-as-a-judge
> - 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/llm-as-a-judge`](https://github.com/swaleio/examples/tree/main/workflows/llm-as-a-judge) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/llm-as-a-judge/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/llm-as-a-judge/workflow.yaml).

Scores a model's answers with a stronger model. The model under test answers
every prompt; the judge reads each prompt and answer and rates the answer from
1 to 10; a last step prints the mean, the median and a histogram of the scores.
Run it on two models to compare them, or before and after a fine-tune.

| Step | Task definition | Compute |
|------|-----------------|---------|
| Fetch the model, and the judge, in parallel | [`swaleio/hf-download`](https://hub.swale.io/swaleio/hf-download) | `cpu-4` |
| Write one answer request per prompt | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Answer | [`swaleio/vllm-batch`](https://hub.swale.io/swaleio/vllm-batch) | `a100-1` |
| Join answers to prompts, write one judge request per answer | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Judge | [`swaleio/vllm-batch`](https://hub.swale.io/swaleio/vllm-batch) | `a100-1` |
| Summarize the scores | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `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. vLLM's structured outputs hold the judge to a
number from 1 to 10, and its thinking is turned off, so it answers at once.

## Inputs

| Input | Default | Description |
|-------|---------|-------------|
| `model` | `Qwen/Qwen2.5-1.5B-Instruct` | Model whose answers are judged. |
| `judge` | `Qwen/Qwen3-8B` | Judge model. |
| `prompts` | the `test_prefs` split of `HuggingFaceH4/ultrafeedback_binarized` | Parquet file or glob, as a DuckDB path. |
| `prompt_column` | `prompt` | Column holding each prompt. |
| `limit` | `300` | Number of prompts. |
| `max_tokens` | `1024` | Longest answer. |

The downloads read the secret `hf_token`.

## Output

`scored.parquet` on the workspace: each prompt, answer and score. The summary
is in the last task's log.

## 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: Score answers with an LLM as a judge
description: Answers a set of prompts with one model, has a stronger judge model score every answer from 1 to 10, and summarizes the scores.
compute_type: cpu-4

inputs:
  model:
    description: Hugging Face repo id of the model whose answers are judged.
    default: Qwen/Qwen2.5-1.5B-Instruct
  judge:
    description: Hugging Face repo id of the judge model.
    default: Qwen/Qwen3-8B
  prompts:
    description: Parquet file or glob holding the prompts, as a DuckDB path.
    default: hf://datasets/HuggingFaceH4/ultrafeedback_binarized/data/test_prefs-*.parquet
  prompt_column:
    description: Column holding each prompt.
    default: prompt
  limit:
    description: Number of prompts.
    default: "300"
  max_tokens:
    description: Longest answer, in tokens.
    default: "1024"

entry_point: main

blocks:
  main:
    tasks:
      fetch_model:
        name: Fetch model
        uses: swaleio/hf-download
        args:
          repo: ${{inputs.model}}
          dest: ${{env.WORKFLOW_STORAGE}}/model
          token: ${{secrets.hf_token}}

      fetch_judge:
        name: Fetch judge
        uses: swaleio/hf-download
        args:
          repo: ${{inputs.judge}}
          dest: ${{env.WORKFLOW_STORAGE}}/judge
          token: ${{secrets.hf_token}}

      requests:
        name: Write answer requests
        uses: swaleio/duckdb
        args:
          sql: |
            CREATE TABLE prompts AS
              SELECT 'p-' || lpad((row_number() OVER () - 1)::VARCHAR, 6, '0') AS custom_id,
                     "${{inputs.prompt_column}}" AS prompt
              FROM (SELECT * FROM '${{inputs.prompts}}' LIMIT ${{inputs.limit}});
            COPY prompts TO '${{env.WORKFLOW_STORAGE}}/prompts.parquet' (FORMAT parquet);
            COPY (
              SELECT custom_id, 'POST' AS method, '/v1/chat/completions' AS url,
                     {'model': '${{env.WORKFLOW_STORAGE}}/model',
                      'messages': [{'role': 'user', 'content': prompt}],
                      'max_tokens': ${{inputs.max_tokens}}} AS body
              FROM prompts
            ) TO '${{env.WORKFLOW_STORAGE}}/answer-requests.jsonl' (FORMAT json);

      answer:
        name: Answer
        uses: swaleio/vllm-batch
        compute_type: a100-1
        start_on:
          - fetch_model
          - requests
        args:
          model: ${{env.WORKFLOW_STORAGE}}/model
          input_file: ${{env.WORKFLOW_STORAGE}}/answer-requests.jsonl
          output_file: ${{env.WORKFLOW_STORAGE}}/answers.jsonl

      judge_requests:
        name: Write judge requests
        uses: swaleio/duckdb
        start_on:
          - answer
        args:
          sql: |
            CREATE TABLE answers AS
              SELECT p.*, r.response.body.choices[1].message.content AS answer
              FROM '${{env.WORKFLOW_STORAGE}}/prompts.parquet' p
              JOIN read_json('${{env.WORKFLOW_STORAGE}}/answers.jsonl') r USING (custom_id);
            COPY answers TO '${{env.WORKFLOW_STORAGE}}/answers.parquet' (FORMAT parquet);
            COPY (
              SELECT custom_id, 'POST' AS method, '/v1/chat/completions' AS url,
                     {'model': '${{env.WORKFLOW_STORAGE}}/judge',
                      'messages': [
                        {'role': 'system', 'content': 'You grade answers. Rate how helpful, correct and clear the answer is for the request, from 1 (useless) to 10 (excellent). Reply with the number only.'},
                        {'role': 'user', 'content': '[Request]\n' || prompt || '\n\n[Answer]\n' || answer}],
                      'max_tokens': 4, 'temperature': 0,
                      'chat_template_kwargs': {'enable_thinking': false},
                      'structured_outputs': {'choice': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']}} AS body
              FROM answers
            ) TO '${{env.WORKFLOW_STORAGE}}/judge-requests.jsonl' (FORMAT json);

      judge:
        name: Judge
        uses: swaleio/vllm-batch
        compute_type: a100-1
        start_on:
          - fetch_judge
          - judge_requests
        args:
          model: ${{env.WORKFLOW_STORAGE}}/judge
          input_file: ${{env.WORKFLOW_STORAGE}}/judge-requests.jsonl
          output_file: ${{env.WORKFLOW_STORAGE}}/scores.jsonl

      summarize:
        name: Summarize scores
        uses: swaleio/duckdb
        start_on:
          - judge
        args:
          sql: |
            CREATE TABLE scored AS
              SELECT a.*, trim(s.response.body.choices[1].message.content)::INT AS score
              FROM '${{env.WORKFLOW_STORAGE}}/answers.parquet' a
              JOIN read_json('${{env.WORKFLOW_STORAGE}}/scores.jsonl') s USING (custom_id);
            COPY scored TO '${{env.WORKFLOW_STORAGE}}/scored.parquet' (FORMAT parquet);
            SELECT count(*) AS answers, round(avg(score), 2) AS mean_score,
                   median(score) AS median_score, sum((score >= 8)::INT) AS scored_8_or_more
              FROM scored;
            SELECT score, count(*) AS answers, repeat('#', count(*)::INT) AS bar
              FROM scored GROUP BY score ORDER BY score;
```

## Licenses of the defaults

- Models: [Qwen/Qwen2.5-1.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct) and [Qwen/Qwen3-8B](https://huggingface.co/Qwen/Qwen3-8B), Apache-2.0.
- Dataset: [HuggingFaceH4/ultrafeedback_binarized](https://huggingface.co/datasets/HuggingFaceH4/ultrafeedback_binarized), MIT.
