Prompt Chain

Build and Compare Go-Based AI Providers for Testing

Go provider template for promptfoo that demonstrates how to build, structure, and compare multiple AI provider implementations in a single module.

Works with openaipromptfoo

88
Spark score
out of 100
Updated 18 days ago
Version 0.121.18
Models
gpt 4o

Add to Favorites

Why it matters

Enable developers to create, structure, and evaluate custom Go-based providers for promptfoo testing frameworks, allowing side-by-side comparison of multiple implementations using the same interface.

Outcomes

What it gets done

01

Implement Go provider functions that call OpenAI APIs with configurable parameters

02

Structure multi-package Go modules with shared utilities and alternative implementations

03

Compare different provider implementations through promptfoo configuration files

04

Evaluate provider responses using standardized testing and viewing workflows

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/pfoo-provider-golang | bash

Steps

Steps in the chain

01
Install Go Provider Example
02
Install Go (1.16 or later)
03
Install OpenAI Go Client Library
04
Set OpenAI API Key
05
Run Evaluation
06
View Results

Overview

Provider Golang

A reference implementation showing how to structure Go-based providers for promptfoo, with two example implementations sharing common code. Use when building custom Go providers for promptfoo or comparing multiple provider implementations with shared utilities.

What it does

This example demonstrates how to structure Go-based providers for promptfoo. It shows two implementations of the same provider interface that share common code, enabling comparison of multiple Go provider implementations side-by-side in a single evaluation run.

When to use - and when NOT to

Use this example when you need to build custom providers in Go for promptfoo, want to compare different provider implementations with shared utilities, or need to integrate Go-based logic into your evaluation workflow.

Do NOT use this if you only need simple prompt testing without custom provider logic, or if you're looking for a production-ready provider rather than a reference implementation to build upon.

Inputs and outputs

You provide a Go implementation of the CallApi function with signature func CallApi(prompt string, options map[string]interface{}) (string, error), along with a promptfooconfig.yml that references your Go files.

You receive evaluation results comparing both provider implementations, viewable through the promptfoo web interface.

Integrations

The example integrates with OpenAI through the github.com/sashabaranov/go-openai@v1.37.0 client library, wrapped in a custom client implementation. It works within the promptfoo evaluation framework, allowing Go providers to be compared alongside other provider types.

The directory structure supports modular Go code:

provider-golang/
├── go.mod            # Root module definition
├── main.go           # Root provider implementation
├── core/             # Supporting code
│   └── openai.go     # OpenAI client wrapper
├── pkg1/             # Shared utilities
│   └── utils.go      # Configuration
├── evaluation/       # Alternative implementation
│   └── main.go      # Provider with same interface
└── promptfooconfig.yml  # Config comparing both implementations

Who it's for

This example is for developers building custom providers in Go for promptfoo evaluations and those comparing different provider implementation strategies.

To get started:

npx promptfoo@latest init --example provider-golang
cd provider-golang

Set your OpenAI API key:

export OPENAI_API_KEY=your_key_here

Install the required Go dependency:

go get github.com/sashabaranov/go-openai@v1.37.0

Run the evaluation:

npx promptfoo eval

Then view results:

npx promptfoo view

The config demonstrates comparing implementations:

providers:
  - id: 'file://evaluation/main.go:CallApi'
    label: 'Provider in evaluation/'

  - id: 'file://main.go:CallApi'
    label: 'Provider in root'
    config:
      reasoning_effort: 'high'
Source README

provider-golang (Golang Provider Example)

You can run this example with:

npx promptfoo@latest init --example provider-golang
cd provider-golang

This example demonstrates how to structure a Go-based provider for promptfoo. For detailed documentation, see Go Provider documentation.

To get started with this example:

promptfoo init --example provider-golang

Directory Structure

This example shows two implementations of the same provider interface:

provider-golang/
├── go.mod            # Root module definition
├── main.go           # Root provider implementation
├── core/             # Supporting code
│   └── openai.go     # OpenAI client wrapper
├── pkg1/             # Shared utilities
│   └── utils.go      # Configuration
├── evaluation/       # Alternative implementation
│   └── main.go      # Provider with same interface
└── promptfooconfig.yml  # Config comparing both implementations

The structure demonstrates how to:

  1. Keep shared Go code in a single module
  2. Implement the same provider interface in different ways
  3. Compare multiple implementations in one config

Prerequisites

  1. Go installed (1.16 or later)

  2. OpenAI Go client library:

    go get github.com/sashabaranov/go-openai@v1.37.0
    
  3. Set your API key:

    export OPENAI_API_KEY=your_key_here
    

Usage

Run the comparison:

npx promptfoo eval

Then view the results with:

npx promptfoo view

Configuration

The config compares both implementations:

providers:
  - id: 'file://evaluation/main.go:CallApi'
    label: 'Provider in evaluation/'

  - id: 'file://main.go:CallApi'
    label: 'Provider in root'
    config:
      reasoning_effort: 'high'

Provider Implementations

Both main.go and evaluation/main.go implement the same interface:

func CallApi(prompt string, options map[string]interface{}) (string, error)

They share the same OpenAI client code but can be configured differently through the config file.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.