Are you sure you want to delete this access key?
This example demonstrates how to create a custom Python provider for promptfoo that integrates with the OpenAI API.
You can run this example with:
npx promptfoo@latest init --example python-provider
The Python provider allows you to use Python code as a provider in promptfoo evaluations. This is useful when you need to:
This example requires the following environment variable:
OPENAI_API_KEY
- Your OpenAI API keyYou can set this in a .env
file or directly in your environment.
pip install openai
)provider.py
- The Python provider implementation that calls OpenAI's APIpromptfooconfig.yaml
- Configuration for promptfoo evaluation with proper YAML schema referenceconfigs/
directory:
fileConfig.yaml
- YAML configuration for model settingsfileConfig.js
- JavaScript configuration for formatting optionsfileConfig.py
- Python configuration for additional parametersThe Python provider is defined in provider.py
and includes:
call_api
function that makes API calls to OpenAIBy default, the example is configured to use gpt-4.1-mini
model, but you can modify it to use other models as needed.
When you run this example, you'll see:
The example demonstrates how to load configuration values from external files using the file://
protocol directly in the promptfooconfig.yaml
file. It shows three main file types:
configs/fileConfig.yaml
): Contains model settings like temperature and max tokensconfigs/fileConfig.js
): Provides formatting options through a function exportconfigs/fileConfig.py
): Supplies additional parameters through a Python functionThe provider supports loading from:
.json
).yaml
, .yml
).js
, .mjs
, .ts
, .cjs
).py
).txt
, .md
)You can see how this works in the promptfooconfig.yaml
file:
providers:
- id: 'file://provider.py:call_api'
config:
# YAML
settings: 'file://configs/fileConfig.yaml'
# JavaScript file
formatting: 'file://configs/fileConfig.js:getFormatConfig'
nested: # Python file
parameters: 'file://configs/fileConfig.py:get_params'
Run the example with:
npx promptfoo@latest evaluate -c examples/python-provider/promptfooconfig.yaml
For more information on creating custom providers, see the promptfoo documentation.
Press p or to see the previous file or, n or to see the next file
Browsing data directories saved to S3 is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
promptfoo is now integrated with AWS S3!
Are you sure you want to delete this access key?
Browsing data directories saved to Google Cloud Storage is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
promptfoo is now integrated with Google Cloud Storage!
Are you sure you want to delete this access key?
Browsing data directories saved to Azure Cloud Storage is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
promptfoo is now integrated with Azure Cloud Storage!
Are you sure you want to delete this access key?
Browsing data directories saved to S3 compatible storage is possible with DAGsHub. Let's configure your repository to easily display your data in the context of any commit!
promptfoo is now integrated with your S3 compatible storage!
Are you sure you want to delete this access key?