Environment
Base abstract class for creating RL environments to train and evaluate LLMs.Overview
TheEnvironment class provides the core infrastructure for:
- Managing datasets (training and evaluation)
- Running rollouts with LLM clients
- Scoring rollouts with rubrics
- Handling state lifecycle and cleanup
- Token usage tracking
rollout() method.
Inheritance Hierarchy
Constructor
Parameters
Dataset | DatasetBuilder | None
Training dataset or a callable that returns a dataset. Either
dataset or eval_dataset must be provided.Dataset | DatasetBuilder | None
Evaluation dataset or a callable that returns a dataset.
str | None
System prompt to prepend to all conversations.
Messages | None
Few-shot examples to include in prompts.
Parser | None
Parser for extracting structured data from completions. Defaults to
Parser().Rubric | None
Rubric for scoring rollouts. Defaults to
Rubric().SamplingArgs | None
Default sampling arguments for generation (temperature, top_p, etc.).
list[Tool] | None
Provider-agnostic tool definitions in
vf.Tool format.int
default:"512"
Maximum number of worker threads for synchronous execution.
str | None
Unique identifier for this environment.
dict | None
Additional environment-specific arguments.
dict
default:"{}"
Keyword arguments to pass to HuggingFace dataset
.map() operations.int | None
Maximum sequence length for tokenization and truncation.
bool
default:"True"
Whether to score rollouts using the rubric.
float
default:"0.5"
Reward threshold for considering a rollout as “passed”.
Core Methods
rollout
RolloutInput
Input data from the dataset containing prompt, answer, etc.
Client
LLM client for making API calls.
str
Model identifier (e.g., “gpt-4”, “claude-3-5-sonnet”).
SamplingArgs | None
Optional sampling arguments to override defaults.
State - Final state after rollout completion.
get_model_response
State
Current rollout state.
Messages | str
Prompt as messages or string.
Client | None
Client to use (defaults to
state["client"]).str | None
Model to use (defaults to
state["model"]).list[Tool] | None
Tools available for this request (defaults to
state["tool_defs"]).SamplingArgs | None
Sampling arguments (defaults to
state["sampling_args"]).Response - Model response with message, usage, etc.
init_state
RolloutInput
Input data from the dataset.
Client | ClientConfig
Client or client configuration.
str
Model identifier.
SamplingArgs | None
Sampling arguments.
State - Initialized state with input fields, client, model, etc.
Dataset Methods
build_dataset
Dataset | None - Built dataset or None if no source.
build_eval_dataset
Dataset | None - Built dataset or None if no source.
get_dataset
int
default:"-1"
Maximum number of examples to return. -1 returns all.
int | None
Random seed for shuffling.
Dataset - Training dataset.
get_eval_dataset
int
default:"-1"
Maximum number of examples to return. -1 returns all.
int | None
Random seed for shuffling.
Dataset - Evaluation dataset.
Generation & Evaluation
generate
Dataset | List[RolloutInput]
Input examples to generate rollouts for.
Client | ClientConfig
LLM client or client configuration.
str
Model identifier.
SamplingArgs | None
Sampling arguments to override defaults.
int
default:"-1"
Maximum concurrent rollouts. -1 for unlimited.
Path | None
Path to save/resume results.
list[str] | None
Additional state fields to include in outputs.
bool
default:"False"
Whether to save results to disk.
bool
default:"False"
Whether to push results to HuggingFace Hub.
str | None
Dataset name for HuggingFace Hub.
bool
default:"False"
Score rollouts independently vs. in groups.
int
default:"0"
Maximum retries for failed rollouts.
StartCallback | None
Callback when generation starts.
ProgressCallback | list[ProgressCallback] | None
Progress callback(s). None uses default tqdm progress bar.
LogCallback | None
Logging callback.
GenerateOutputs - Dictionary with outputs and metadata keys.
generate_sync
generate(). Handles event loop creation.
evaluate
Client | ClientConfig
LLM client or client configuration.
str
Model identifier.
int
default:"-1"
Number of examples to evaluate. -1 for all.
int
default:"1"
Number of rollouts to generate per example.
generate().
Returns: GenerateOutputs - Dictionary with outputs and metadata keys.
evaluate_sync
evaluate().
Token Usage Tracking
get_state_usage
State
Rollout state.
TokenUsage | None - Dictionary with input_tokens and output_tokens keys, or None.
increment_state_usage
increment_state_usage_from_response
State Lifecycle
is_completed
state["is_completed"] = True if any condition is met.
State
Current rollout state.
bool - True if any stop condition is met.
Configuration
set_kwargs
add_rubric
RubricGroup if a rubric already exists.
set_max_seq_len
set_score_rollouts
Server Methods
start_server
stop_server
Static Methods
make_dataset
verifiers.utils.save_utils.make_dataset for details.
Example Usage
See Also
- SingleTurnEnv - Single-turn Q&A environments
- MultiTurnEnv - Multi-turn interactive environments
- ToolEnv - Tool-calling environments
- EnvGroup - Mixture of multiple environments