What is Rhai?
Rhai is a simple, embedded scripting language designed for Rust applications. It has a JavaScript-like syntax and is used by the CLI to translate between the standard Circuit Breaker Labs message format and your custom API’s format.Why Rhai for Custom Providers?
Sandboxed Execution
Simple Syntax
Type Safety
Fast Performance
How Custom Providers Work
Custom providers act as translators between the CLI and your API:CLI Prepares Messages
build_request() Transforms
build_request() function converts these messages into your API’s request format.CLI Posts Request
parse_response() Extracts
parse_response() function extracts the assistant’s message from the API response.Evaluation Continues
Script Structure
Every custom provider script must implement two functions:Complete Examples from Source
The CLI repository includes working examples for common API formats:OpenAI Chat Completions API
examples/providers/openai_completions.rhai
examples/providers/openai_completions.rhai
OpenAI Responses API
examples/providers/openai_responses.rhai
examples/providers/openai_responses.rhai
Ollama Chat API
examples/providers/ollama_chat.rhai
examples/providers/ollama_chat.rhai
Ollama Completions API
examples/providers/ollama_completions.rhai
examples/providers/ollama_completions.rhai
Creating Your Own Provider
Identify Your API Format
Create Rhai Script
.rhai file with build_request() and parse_response() functions:Test the Script
Iterate and Refine
Advanced Examples
Adding Custom Parameters
Handling Different Message Formats
If your API expects a different message structure:Concatenating Messages for Completion APIs
Some APIs expect a single prompt instead of structured messages:Handling Nested Response Structures
Adding Debug Logging
Rhai Quick Reference
Data Types
Control Flow
Common Operations
Functions
Authentication and Headers
Authentication is typically handled via HTTP headers passed from environment variables:Troubleshooting
Error: 'build_request' function not found
Error: 'build_request' function not found
build_request function. Ensure it’s defined:Error: 'parse_response' function not found
Error: 'parse_response' function not found
parse_response function. Ensure it’s defined:Error: Type mismatch or cannot parse response
Error: Type mismatch or cannot parse response
Error: HTTP 400/401 from API
Error: HTTP 400/401 from API
- Verify your API URL is correct
- Check authentication headers are set properly
- Ensure request format matches what your API expects
- Test with curl to confirm API access
Empty or Invalid Responses
Empty or Invalid Responses
parse_response logic might be extracting the wrong field. Log the full response:Testing Your Custom Provider
Test with Single Variation
Verify Request Format
print() statements in build_request() if needed.Verify Response Parsing
print() in parse_response() to debug.Scale Up Testing
Best Practices
Start with Existing Examples
Start with Existing Examples
Add Debug Logging
Add Debug Logging
print() liberally during development to see request/response structures.Test with curl First
Test with curl First
Handle Errors Gracefully
Handle Errors Gracefully
Keep Scripts Simple
Keep Scripts Simple