Conditionals
Documentation for working with conditional feature interventions
Conditionals allow you to define dynamic feature interventions that are applied based on the activation patterns of other features during model inference. This enables creating more sophisticated steering behaviors that respond to the content being generated.
Before using the Conditionals API, you’ll need to find the features you want to intervene on, and a model variant
Examples
Basic Conditional Intervention
Apply pirate-themed features only when whale-related content is detected:
Aborting Generation
Stop generation if certain content is detected:
Auto-Generated Conditionals
Use natural language to automatically generate conditional statements:
Creating Conditionals
Comparison Operators
You can create conditionals by comparing features or feature groups with numeric values or other features using standard comparison operators. This creates a Conditional object that can be used in steering behaviors.
Supported operators:
==
(equal)!=
(not equal)<
(less than)<=
(less than or equal)>
(greater than)>=
(greater than or equal)
Logical Operators
Multiple conditions can be combined using logical operators to create a ConditionalGroup:
Using Conditionals
set_when()
Apply feature interventions when a condition is met.
Parameters:
The ConditionalGroup that triggers the intervention
Feature edits to apply when condition is met
Returns: None
Example:
abort_when()
Abort inference when a condition is met by raising an InferenceAbortedException.
Parameters:
The ConditionalGroup that triggers the abort
Returns: None
Example:
handle_when()
Register a custom handler function to be called when a condition is met.
Parameters:
The ConditionalGroup that triggers the handler
Function that takes an InferenceContext and returns None
Returns: None
Example:
AutoConditional
The AutoConditional utility helps automatically generate conditional statements based on natural language descriptions.
Parameters:
Natural language description of the desired condition
Goodfire client instance
Model to use for generating conditions
Returns:
Generated ConditionalGroup
Example:
Best Practices
- Use conditional interventions to create context-aware steering behaviors
- Combine multiple conditions with logical operators for more precise control
- Handle aborted inferences gracefully in your application
- Test conditions thoroughly to ensure desired behavior
- Consider using AutoConditional for quick prototyping
Classes
ConditionalGroup
A group of conditions combined with logical operators.
Conditional
A single conditional expression comparing features.
InferenceContext
Context object containing information about the current inference state.