Skip to main content

Generate responses over a dataset

Source on GitHub: the workflows/generate-responses folder, with its README and its workflow file.

Answers every prompt in a dataset with a chat model, through vLLM's offline batch inference, and saves each prompt beside its answer as Parquet. Point prompts at any Parquet file on the Hugging Face Hub and prompt_column at the column that holds the prompts.

StepTask definitionCompute
Fetch the modelswaleio/hf-downloadcpu-4
Write the requests: number the prompts and turn each into an OpenAI batch requestswaleio/duckdbcpu-4
Generate the answersswaleio/vllm-batcha100-1
Join answers to prompts, count how each answer ended, write Parquetswaleio/duckdbcpu-4
Upload to the Hugging Face Hub, when hub_repo is setswaleio/hf-uploadcpu-4

The Compute column gives each step's compute type, the compute its tasks are allocated.

Every step is its own task; only generation holds a GPU.

Inputs​

InputDefaultDescription
modelQwen/Qwen2.5-1.5B-InstructChat model on the Hugging Face Hub.
promptshf://datasets/openai/gsm8k/main/test-*.parquetParquet file or glob with the prompts, as a DuckDB path.
prompt_columnquestionColumn that holds each prompt.
limit200Number of prompts to answer.
max_tokens512Longest answer, in tokens.
hub_repoemptyHugging Face dataset repo to upload responses.parquet to; empty keeps it on the workspace.

The model download and the upload read the secret hf_token; the upload needs a token with write access.

Output​

responses.parquet on the workspace: every column of the prompts file, a custom_id, the response and its finish_reason (stop, or length when the answer hit max_tokens).

Run it​

You need the CLI, logged in to your account, and a project to add the workflow to; Getting started sets them up.

  1. Save the file under The workflow file as workflow.yaml, and add it to a project:

    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 shows.

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

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

    Each input you leave out takes its default from Inputs, and --input <name>=<value> gives it another value.

Create and manage workflows covers the other ways to add a workflow, Start and manage workflow runs how to follow a run, and View and stream task logs how to read its tasks' logs.

The workflow file​

Workflow definition syntax describes each key in it.

workflow.yaml
name: Generate responses over a dataset
description: Answers every prompt in a Hugging Face dataset with a chat model through vLLM batch inference and saves prompts and answers as Parquet.
compute_type: cpu-4

inputs:
model:
description: Hugging Face repo id of the chat model that answers.
default: Qwen/Qwen2.5-1.5B-Instruct
prompts:
description: Parquet file or glob holding the prompts, as a DuckDB path such as hf://datasets/<owner>/<dataset>/<file>.
default: hf://datasets/openai/gsm8k/main/test-*.parquet
prompt_column:
description: Column of the prompts file that holds each prompt.
default: question
limit:
description: Number of prompts to answer.
default: "200"
max_tokens:
description: Longest answer, in tokens.
default: "512"
hub_repo:
description: Hugging Face dataset repo to upload the answers to, as namespace/name. Leave empty to keep them on the workspace only.
default: ""

entry_point: main

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

requests:
name: Write requests
uses: swaleio/duckdb
args:
sql: |
CREATE TABLE prompts AS
SELECT 'row-' || lpad((row_number() OVER () - 1)::VARCHAR, 6, '0') AS custom_id, *
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': "${{inputs.prompt_column}}"}],
'max_tokens': ${{inputs.max_tokens}}} AS body
FROM prompts
) TO '${{env.WORKFLOW_STORAGE}}/requests.jsonl' (FORMAT json);
SELECT count(*) AS requests FROM prompts;

generate:
name: Generate
uses: swaleio/vllm-batch
compute_type: a100-1
start_on:
- model
- requests
args:
model: ${{env.WORKFLOW_STORAGE}}/model
input_file: ${{env.WORKFLOW_STORAGE}}/requests.jsonl
output_file: ${{env.WORKFLOW_STORAGE}}/results.jsonl

collect:
name: Collect answers
uses: swaleio/duckdb
start_on:
- generate
args:
sql: |
CREATE TABLE answers AS
SELECT p.*,
r.response.body.choices[1].message.content AS response,
r.response.body.choices[1].finish_reason AS finish_reason
FROM '${{env.WORKFLOW_STORAGE}}/prompts.parquet' p
JOIN read_json('${{env.WORKFLOW_STORAGE}}/results.jsonl') r USING (custom_id)
ORDER BY custom_id;
SELECT finish_reason, count(*) AS answers FROM answers GROUP BY ALL;
COPY answers TO '${{env.WORKFLOW_STORAGE}}/responses.parquet' (FORMAT parquet);

publish:
name: Upload when asked
start_on:
- collect
if:
condition: ${{inputs.hub_repo}}
true: upload

upload:
tasks:
upload_answers:
name: Upload to the Hugging Face Hub
uses: swaleio/hf-upload
args:
repo: ${{inputs.hub_repo}}
repo_type: dataset
source: ${{env.WORKFLOW_STORAGE}}/responses.parquet
path_in_repo: data/responses.parquet
token: ${{secrets.hf_token}}

Licenses of the defaults​