Reward function decorator
reward_function Decorator Reference
The @reward_function
decorator transforms a regular Python function into a reward function with standardized inputs/outputs and deployment capabilities.
Overview
The decorator serves several key purposes:
- Ensures consistent input and output formats
- Adds error handling and validation
- Provides a
.deploy()
method for deploying the function to Fireworks
Import
Usage
Parameter Requirements
Functions decorated with @reward_function
should accept the following parameters:
-
messages
(List[Dict[str, str]]
): Required. List of conversation messages, with the last message typically being the one evaluated. -
original_messages
(Optional[List[Dict[str, str]]]
): Optional. The conversation context, without the message being evaluated. -
**kwargs
: Optional. Additional parameters (like metadata) that can be passed to the function.
Return Value Requirements
Functions must return a RewardOutput
object or a compatible tuple format:
Added Methods
.deploy()
The decorator adds a .deploy()
method to the function, allowing it to be deployed to Fireworks.
Parameters
-
name
(str
): Required. ID for the deployed evaluator. -
description
(str
): Optional. Human-readable description of the evaluator. -
account_id
(Optional[str]
): Optional. Fireworks account ID. If not provided, will be read from config or environment. -
auth_token
(Optional[str]
): Optional. Authentication token. If not provided, will be read from config or environment. -
force
(bool
): Optional. Whether to overwrite an existing evaluator with the same name. Default is False. -
providers
(Optional[List[Dict[str, str]]]
): Optional. List of provider configurations. If not provided, uses a default provider.
Returns
str
: The evaluation ID that can be used in RL training.
Exceptions
ValueError
: Raised if authentication fails or required parameters are missing.requests.exceptions.HTTPError
: Raised if the API request fails.
Implementation Details
Validation Logic
The decorator performs the following validations:
- Ensures the decorated function has the expected parameters
- Validates that the return value is a
RewardOutput
or a compatible tuple - Handles exceptions that occur during function execution
Backward Compatibility
For backward compatibility, the decorator supports the legacy tuple return format:
This gets automatically converted to a RewardOutput
object.
Deployment Process
When .deploy()
is called, the decorator:
- Extracts the function’s source code
- Creates a wrapper that handles the Fireworks evaluation format
- Creates a temporary directory with the wrapped function
- Uploads and registers the function with the Fireworks API