Integrate OpenAI with Custom Ruby Providers
Ruby provider for promptfoo that integrates OpenAI API with custom logic, token tracking, and Ruby-based assertions for LLM evaluation workflows.
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
Create a custom provider for promptfoo in Ruby.
Connect promptfoo to the OpenAI API.
Demonstrate LLM integration within a Ruby environment.
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 Capabilities
What this chain does
Create a custom provider for promptfoo in Ruby.
Connect promptfoo to the OpenAI API.
Demonstrate LLM integration within a Ruby environment.
Facilitate custom prompt engineering workflows with Ruby.
Overview
Provider Ruby
What it does
Provider Ruby is a custom Ruby provider for promptfoo that enables integration with the OpenAI API while allowing custom logic execution before and after LLM calls.
How it connects
Use Provider Ruby when you need to call APIs from Ruby libraries, implement custom preprocessing or postprocessing logic around LLM calls, process responses in specific ways, or track token usage and other metrics within promptfoo evaluations.
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:
- Call APIs from Ruby libraries
- Implement custom logic before or after calling LLMs
- Process responses in specific ways
- Track token usage and other metrics
Ruby Assertions allow you to:
- Write custom validation logic in Ruby
- Access test context and variables
- Return detailed grading results with scores and reasons
- 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/httpandjsonfrom standard library)
Files
provider.rb- The Ruby provider implementation that calls OpenAI's APIassert.rb- Custom Ruby assertion functions for validationpromptfooconfig.yaml- Configuration for promptfoo evaluation with proper YAML schema reference
Implementation Details
Ruby Provider (provider.rb)
The Ruby provider includes:
- A
call_apifunction that makes API calls to OpenAI - Token usage extraction from the API response
- 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:
- Inline assertions - Simple one-line checks (e.g.,
output.length > 10) - Multiline assertions - Complex logic with detailed results and scores
- External file assertions (
assert.rb) - Reusable assertion functions
Ruby assertions can:
- Return boolean values for pass/fail
- Return numeric scores
- Return detailed
GradingResulthashes 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:
- The prompts being submitted to your Ruby provider
- Responses from the OpenAI API
- Token usage statistics for each completion
- 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:
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.