Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel
Michael 76797692e5
feat(schema): add yaml schema validation to config files (#1990)
10 months ago
..
a6c2affdb5
docs: fix typo in hooks documentation
11 months ago
381d3eb98a
feat: extension hook api (#1249)
1 year ago
1df67ec49e
chore(cli): improve validation for extension hooks (#1827)
10 months ago
76797692e5
feat(schema): add yaml schema validation to config files (#1990)
10 months ago

README.md

You have to be logged in to leave a comment. Sign In

Custom Extensions Example for promptfoo

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.

Extension Hooks Overview

promptfoo supports four types of extension hooks, each triggered at a specific point in the evaluation lifecycle:

  1. beforeAll: Executed once before the entire test suite begins
  2. afterAll: Executed once after the entire test suite completes
  3. beforeEach: Executed before each individual test
  4. afterEach: Executed after each individual test

Each hook receives a hookName parameter and a context object containing relevant data for that specific hook type.

For comprehensive information on implementing and using these hooks, refer to the Extension Hooks section in the official documentation.

Configuring Extensions

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 (:).

Why Use Extensions?

Extensions in promptfoo empower you to:

  1. Enhance test functionality with custom pre- and post-test actions
  2. Dynamically prepare and clean up test environments
  3. Seamlessly integrate with external systems or services for comprehensive testing
  4. Implement custom logging or reporting mechanisms

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.

Practical Use Cases for Extensions

  1. Environment Setup/Teardown: Dynamically create and remove files or resources for each test.
  2. AI Agent Testing: Configure complex scenarios, such as deploying Kubernetes manifests before evaluation.
  3. Model Loading: Efficiently load ML models into memory before tests and unload them afterward.
  4. Local Server Management: Programmatically start and stop local language model servers (e.g., llama.cpp, ollama) for testing.
  5. Connection Management: Automate the opening and closing of connection tunnels or database connections.
  6. Credential Handling: Securely load and manage credentials or authentication tokens for testing environments.

Running the Example

To execute this example, use the following command in your terminal:

LOG_LEVEL=debug promptfoo eval

Then

promptfoo view
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...