examples/config-extension-api/README.md
You can run this example with:
npx promptfoo@latest init --example config-extension-api
cd config-extension-api
This example demonstrates how to leverage promptfoo's powerful extensions API to implement custom setup and teardown hooks for individual tests. These extensions can be defined in either Python or JavaScript, providing flexibility for your preferred programming environment.
promptfoo supports four types of extension hooks, each triggered at a specific point in the evaluation lifecycle:
beforeAll: Executed once before the entire test suite beginsafterAll: Executed once after the entire test suite completesbeforeEach: Executed before each individual testafterEach: Executed after each individual testEach hook receives a hookName parameter and a context object containing relevant data for that specific hook type.
The beforeAll and beforeEach hooks should return the context object, while the afterAll and afterEach hooks should not return anything.
For comprehensive information on implementing and using these hooks, refer to the Extension Hooks section in the official documentation.
Specify your extensions in the promptfooconfig.yaml file:
extensions:
- file://path/to/your/extension.py:extension_hook
- file://path/to/your/extension.js:extensionHook
Note: You must include the function name after the file path, separated by a colon (:).
Extensions in promptfoo empower you to:
These extensions function similarly to "before" and "after" hooks in popular unit testing frameworks, allowing you to set up and tear down test environments with precision and control.
To execute this example, use the following command in your terminal:
LOG_LEVEL=debug promptfoo eval
Then
promptfoo view