Prompt Chain

Integrate OpenAI with Custom Ruby Providers

Promptfoo example building a custom Ruby provider for OpenAI and validating output with custom Ruby assertions.

Works with openai

92
Spark score
out of 100
Updated last month
Version code-scan-action-0.1
Models

Add to Favorites

Why it matters

Enable promptfoo to interact with the OpenAI API using a custom Ruby provider. This allows for flexible integration of LLM capabilities into your Ruby applications and workflows.

Outcomes

What it gets done

01

Create a custom provider for promptfoo in Ruby.

02

Connect promptfoo to the OpenAI API.

03

Demonstrate LLM integration within a Ruby environment.

04

Facilitate custom prompt engineering workflows with Ruby.

Install

Add it to your toolbox

Run in your project directory:

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

Steps

Steps in the chain

01
Set up Ruby provider example
02
Configure environment variables
03
Implement Ruby provider
04
Create Ruby assertions
05
Configure promptfooconfig.yaml
06
Run evaluation

Overview

Provider Ruby

A promptfoo example building a custom Ruby provider that wraps the OpenAI API, with inline, multiline, and external-file Ruby assertions for output validation. Use when your team works primarily in Ruby and wants a custom provider or assertion logic without switching to JavaScript or Python.

What it does

This example demonstrates how to create a custom Ruby provider for promptfoo that integrates with the OpenAI API, and also demonstrates Ruby assertions for custom validation logic. The Ruby provider lets you use Ruby code as a provider in promptfoo evaluations - useful for calling APIs from Ruby libraries, implementing custom logic before or after calling LLMs, processing responses in specific ways, or tracking token usage and other metrics. Ruby assertions let you write custom validation logic in Ruby, access test context and variables, return detailed grading results with scores and reasons, and reuse assertion logic across multiple tests.

When to use - and when NOT to

Use this example when you want to wrap an LLM call in custom Ruby logic, or write assertions in Ruby instead of JavaScript/Python for teams that already work primarily in Ruby. It is not a general Ruby tutorial - it documents the specific call_api provider interface and the three Ruby assertion styles promptfoo supports.

Inputs and outputs

Requires Ruby 2.7 or higher (using net/http and json from the standard library) and OPENAI_API_KEY. Scaffold with:

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

Running the example shows the prompts submitted to the Ruby provider, OpenAI's responses, per-completion token usage statistics, and evaluation results in table format. Run with npx promptfoo@latest evaluate -c examples/provider-ruby/promptfooconfig.yaml. Three files make up the example: provider.rb (the Ruby provider implementation calling OpenAI's API, defaulting to gpt-4.1-mini), assert.rb (custom Ruby assertion functions), and promptfooconfig.yaml (the eval config).

Integrations

The provider.rb file implements a call_api function with token-usage extraction and multiple sample calling patterns, wired into OpenAI's API. Three Ruby assertion styles are demonstrated: inline assertions (simple one-line checks like output.length > 10), multiline assertions (complex logic returning detailed results and scores), and external file assertions (assert.rb, for reusable assertion functions). Ruby assertions can return a boolean pass/fail, a numeric score, or a detailed GradingResult hash with pass/fail, score, reason, and component results, with access to test context including variables, prompts, and provider responses.

Who it's for

Ruby-centric teams building a custom LLM provider or writing custom eval validation logic who want to stay in Ruby rather than switching to JavaScript or Python for either the provider implementation or the assertions. Having three distinct assertion styles available - a quick inline boolean check, a multiline block for logic that needs a real score and explanation, and an external file for logic reused across many tests - means a team can start with the simplest inline form for a first test and graduate to assert.rb only once the same validation logic is actually needed in more than one place, instead of committing to a separate file up front for even trivial checks.

Source README

provider-ruby (Ruby Provider)

This example demonstrates how to create a custom Ruby provider for promptfoo that integrates with the OpenAI API.

You can run this example with:

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

Overview

The Ruby provider allows you to use Ruby code as a provider in promptfoo evaluations. This example also demonstrates Ruby assertions for custom validation logic.

Ruby Provider is useful when you need to:

  1. Call APIs from Ruby libraries
  2. Implement custom logic before or after calling LLMs
  3. Process responses in specific ways
  4. Track token usage and other metrics

Ruby Assertions allow you to:

  1. Write custom validation logic in Ruby
  2. Access test context and variables
  3. Return detailed grading results with scores and reasons
  4. Reuse assertion logic across multiple tests

Environment Variables

This example requires the following environment variable:

  • OPENAI_API_KEY - Your OpenAI API key

You can set this in a .env file or directly in your environment.

Requirements

  • Ruby 2.7 or higher (with net/http and json from standard library)

Files

  • provider.rb - The Ruby provider implementation that calls OpenAI's API
  • assert.rb - Custom Ruby assertion functions for validation
  • promptfooconfig.yaml - Configuration for promptfoo evaluation with proper YAML schema reference

Implementation Details

Ruby Provider (provider.rb)

The Ruby provider includes:

  1. A call_api function that makes API calls to OpenAI
  2. Token usage extraction from the API response
  3. Multiple sample functions showing different ways to call the API

By default, the example is configured to use gpt-4.1-mini model, but you can modify it to use other models as needed.

Ruby Assertions

The example demonstrates three types of Ruby assertions:

  1. Inline assertions - Simple one-line checks (e.g., output.length > 10)
  2. Multiline assertions - Complex logic with detailed results and scores
  3. External file assertions (assert.rb) - Reusable assertion functions

Ruby assertions can:

  • Return boolean values for pass/fail
  • Return numeric scores
  • Return detailed GradingResult hashes with pass/fail, score, reason, and component results
  • Access test context including variables, prompts, and provider responses

Expected Output

When you run this example, you'll see:

  1. The prompts being submitted to your Ruby provider
  2. Responses from the OpenAI API
  3. Token usage statistics for each completion
  4. Evaluation results in a table format

Run the example with:

npx promptfoo@latest evaluate -c examples/provider-ruby/promptfooconfig.yaml

Learn More

For more information, see the promptfoo documentation:

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.