Basic reward function
Basic Reward Function Example
This example demonstrates how to create a simple reward function that evaluates the clarity of an assistant’s response.
Prerequisites
Before running this example, ensure you have:
- Installed the Reward Kit package (
pip install reward-kit
) - Set up your Fireworks API credentials (if deploying)
Complete Example Code
How to Run the Example
Save the code as clarity_reward.py
and run it:
Expected Output
Code Explanation
This reward function evaluates the clarity of a response based on three components:
-
Sentence Length: Shorter sentences are generally clearer
- Calculates average words per sentence
- Higher score for sentences under 15 words
-
Explanatory Language: Phrases that indicate explanation
- Checks for markers like “because”, “for example”, etc.
- Higher score for more explanatory markers
-
Vocabulary Complexity: Assesses word complexity
- Checks ratio of long words (> 8 characters)
- Higher score for simpler vocabulary
The final score is a weighted average of these components:
- 40% for sentence structure
- 40% for explanatory language
- 20% for vocabulary complexity
Deployment
To deploy this reward function to Fireworks, uncomment the deploy_reward()
function call at the bottom of the script. Ensure your Fireworks API credentials are set before deploying.
Extensions and Improvements
This basic example can be extended in several ways:
- Advanced NLP: Use libraries like spaCy for better sentence segmentation
- Readability Metrics: Add established metrics like Flesch-Kincaid readability
- Domain-Specific Terms: Account for necessary technical terms in specific domains
- User Context: Adapt complexity expectations based on user query sophistication
- Multi-language Support: Add language detection and language-specific rules
Next Steps
Now that you understand basic reward functions:
- Try creating more Advanced Reward Functions with multiple metrics
- Learn about Function Calling Evaluation for tool use
- Follow our Best Practices for reward function design