Files
promptfoo--promptfoo/examples/provider-ruby/provider.rb
T
wehub-resource-sync 0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

85 lines
3.0 KiB
Ruby

require 'net/http'
require 'json'
require 'uri'
##
# Sends the prompt to OpenAI's chat completion endpoint with a system role of a marketer for "Bananamax" and returns the assistant's output along with token usage and metadata.
# @param [String] prompt - The user-facing prompt to submit to the model.
# @param [Hash] options - Optional settings; may include a 'config' key (Hash) which will be returned under metadata.
# @param [Hash] context - Additional contextual information provided by the caller (preserved but not included in the request body).
# @return [Hash] A hash containing:
# - 'output' => String: the assistant message content from the first choice.
# - 'tokenUsage' => Hash or nil: when present, contains 'total', 'prompt', and 'completion' token counts.
# - 'metadata' => Hash: includes 'config' reflecting options['config'] or an empty hash.
def call_api(prompt, options, context)
# Get config values
config = options['config'] || {}
# Prepare API request
uri = URI.parse('https://api.openai.com/v1/chat/completions')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.path)
request['Content-Type'] = 'application/json'
request['Authorization'] = "Bearer #{ENV['OPENAI_API_KEY']}"
request.body = JSON.generate({
'model' => 'gpt-4.1-mini',
'messages' => [
{
'role' => 'system',
'content' => 'You are a marketer working for a startup called Bananamax.'
},
{
'role' => 'user',
'content' => prompt
}
]
})
# Make API call
response = http.request(request)
data = JSON.parse(response.body)
# Extract token usage information from the response
token_usage = nil
if data['usage']
token_usage = {
'total' => data['usage']['total_tokens'],
'prompt' => data['usage']['prompt_tokens'],
'completion' => data['usage']['completion_tokens']
}
end
{
'output' => data['choices'][0]['message']['content'],
'tokenUsage' => token_usage,
'metadata' => {
'config' => config
}
}
end
##
# Appends an instruction to the prompt to produce an all-caps response and forwards the request to the API.
# @param [String] prompt - The user prompt to send.
# @param [Hash] options - Request options; may include a `config` hash used in returned metadata.
# @param [Hash] context - Additional contextual data passed through to the API call.
# @return [Hash] A hash with keys:
# - 'output' => the assistant's message content,
# - 'tokenUsage' => a hash with `total`, `prompt`, and `completion` token counts or `nil`,
# - 'metadata' => a hash containing the provided `config`.
def some_other_function(prompt, options, context)
call_api(prompt + "\nWrite in ALL CAPS", options, context)
end
# Example usage when running the script directly
if __FILE__ == $PROGRAM_NAME
prompt = 'What is the weather in San Francisco?'
options = { 'config' => { 'optionFromYaml' => 123 } }
context = { 'vars' => { 'location' => 'San Francisco' } }
puts JSON.pretty_generate(call_api(prompt, options, context))
end