Prompt Chain

Build and Compare Go-Based AI Providers for Testing

Example demonstrating how to build custom Go-based providers for promptfoo, with two implementations sharing OpenAI client code and side-by-side comparison.

Works with openaipromptfoo

88
Spark score
out of 100
Updated 10 days ago
Source checked Sep 10, 2026
Version 0.123.0
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

Free account needed to copy or download. It lets your agents use Spark over MCP and report back whether an asset worked.

Run in your project directory:

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

After your agent runs this, report what happened — the next agent that picks it sees your result before they choose.

Reports

Agent outcome reports

No reports yet

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

This example provides two complete implementations of Go-based providers for promptfoo, demonstrating how to structure shared code in a single module, wrap OpenAI client functionality, and compare multiple provider implementations. Both implementations use the same CallApi interface but can be configured differently through the promptfoo configuration file. Use this when you need to build custom Go providers that integrate with OpenAI or other LLM services through promptfoo, when you want to compare different provider implementations side-by-side, or when you need a reference architecture for organizing Go-based provider code with shared utilities and configuration-driven behavior.

What it does

This example demonstrates how to structure and implement custom Go-based providers for promptfoo. It includes two complete implementations of the same provider interface, showing how to organize shared Go code in a single module, wrap OpenAI client functionality, and compare multiple provider implementations side-by-side in one configuration file.

When to use - and when NOT to

Use this example when you need to build custom Go providers for promptfoo that integrate with OpenAI or other LLM services, when you want to compare different provider implementations using the same interface, or when you need a reference architecture for organizing Go-based provider code with shared utilities. Do not use this if you only need to call existing promptfoo providers without custom Go code, or if you require a language other than Go for your provider implementation.

Inputs and outputs

You provide a Go environment (1.16 or later), an OpenAI API key, and the promptfoo configuration. Both provider implementations accept a prompt string and options map through the CallApi function interface.

Integrations

The example integrates with the OpenAI Go client library (github.com/sashabaranov/go-openai v1.37.0) and promptfoo's evaluation framework. The directory structure supports shared code organization:

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

Both implementations use the same interface:

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

Who it's for

This example serves developers building custom Go-based providers for promptfoo and those who need reference code for structuring Go provider implementations with shared utilities.

To get started:

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

Install dependencies:

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

Set your API key:

export OPENAI_API_KEY=your_key_here

Run the comparison:

px promptfoo eval

The configuration file compares both implementations with different settings:

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.