Python Provider
The Python provider enables you to create custom evaluation logic using Python scripts. This allows you to integrate Promptfoo with any Python-based model, API, or custom logic.
Common use cases:
- Integrating proprietary or local models
- Adding custom preprocessing/postprocessing logic
- Implementing complex evaluation workflows
- Using Python-specific ML libraries
- Creating mock providers for testing
Prerequisites
Before using the Python provider, ensure you have:
- Python 3.7 or higher installed
- Basic familiarity with Promptfoo configuration
- Understanding of Python dictionaries and JSON
Quick Start
Let's create a simple Python provider that echoes back the input with a prefix.
Step 1: Create your Python script
# echo_provider.py
def call_api(prompt, options, context):
"""Simple provider that echoes the prompt with a prefix."""
config = options.get('config', {})
prefix = config.get('prefix', 'Tell me about: ')
return {
"output": f"{prefix}{prompt}"
}
Step 2: Configure Promptfoo
# promptfooconfig.yaml
providers:
- id: 'file://echo_provider.py'
prompts:
- 'Tell me a joke'
- 'What is 2+2?'
Step 3: Run the evaluation
npx promptfoo@latest eval
That's it! You've created your first custom Python provider.
How It Works
When Promptfoo evaluates a test case with a Python provider:
- Promptfoo prepares the prompt based on your configuration
- Python Script is called with three parameters:
prompt
: The final prompt stringoptions
: Provider configuration from your YAMLcontext
: Variables and metadata for the current test
- Your Code processes the prompt and returns a response
- Promptfoo validates the response and continues evaluation
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ Promptfoo │────▶│ Your Python │────▶│ Your Logic │
│ Evaluation │ │ Provider │ │ (API/Model) │
└─────────────┘ └──────────────┘ └─────────────┘
▲ │
│ ▼
│ ┌──────────────┐
└────────────│ Response │
└──────────────┘