Skip to main content

Testing prompts with Github Actions

This guide describes how to automatically run a before vs. after evaluation of edited prompts using the promptfoo Github Action.

On every pull request that modifies a prompt, the action will automatically run a full comparison:

Github Action comment on modified LLM prompt

The provided link opens the web viewer interface, which allows you to interactively explore the before vs. after:

promptfoo web viewer

Using the Github Action

Here's an example action that watches a PR for modifications. If any file in the prompts/ directory is modified, we automatically run the eval and post a link to the results using the typpo/promptfoo-action@v1:

name: 'Prompt Evaluation'

- 'prompts/**'

runs-on: ubuntu-latest
# This permission is used to post comments on Pull Requests
pull-requests: write
# This cache is optional, but you'll save money and time by setting it up!
- name: Set up promptfoo cache
uses: actions/cache@v2
path: ~/.cache/promptfoo
key: ${{ runner.os }}-promptfoo-v1
restore-keys: |
${{ runner.os }}-promptfoo-

# This step will actually run the before/after evaluation
- name: Run promptfoo evaluation
uses: typpo/promptfoo-action@v1
openai-api-key: ${{ secrets.OPENAI_API_KEY }}
github-token: ${{ secrets.GITHUB_TOKEN }}
prompts: 'prompts/**/*.json'
config: 'prompts/promptfooconfig.yaml'
cache-path: ~/.cache/promptfoo


To make this GitHub Action work for your project, you'll need to do a few things:

  1. Set paths: Replace 'prompts/**' with the path to the files you want to monitor for changes. This could either be a list of paths to single files, or a directory where your prompts are stored.

    Don't forget to also update the paths in the "Run promptfoo evaluation" step to point to prompts, as well as your promptfooconfig.yaml configuration file.

  2. Set OpenAI API key: If you're using an OpenAI API, you need to set the OPENAI_API_KEY secret in your GitHub repository.

    To do this, go to your repository's Settings > Secrets and variables > Actions > New repository secret and create one named OPENAI_API_KEY.

  3. Add it to your project: Github automatically runs workflows in the .github/workflows directory, so save it as something like .github/workflows/prompt-eval.yml.

Here are the supported parameters:

github-tokenThe Github token. Used to authenticate requests to the Github API.Yes
promptsThe glob patterns for the prompt files. These patterns are used to find the prompt files that the action should evaluate.Yes
configThe path to the configuration file. This file contains settings for the action.Yes
openai-api-keyThe API key for OpenAI. Used to authenticate requests to the OpenAI API.No
cache-pathThe path to the cache. This is where the action stores temporary data.No

How it works

  1. Caching: We use caching to speed up subsequent runs. The cache stores LLM requests and outputs, which can be reused in future runs to save cost.

  2. Run Promptfoo Evaluation: This is where the magic happens. We run the evaluation, passing in the configuration file and the prompts we want to evaluate. The results of this step are automatically posted to the pull request.

For more information on how to set up the promptfoo config, see the Getting Started docs.