Skip to content

Instantly share code, notes, and snippets.

@pushmatrix
Created February 7, 2023 14:57
Show Gist options
  • Save pushmatrix/e8b363df474db057c26a8f48462d2fa5 to your computer and use it in GitHub Desktop.
Save pushmatrix/e8b363df474db057c26a8f48462d2fa5 to your computer and use it in GitHub Desktop.
Sample ruby openai call
require 'http'
class OpenAI
URI = "https://api.openai.com/v1"
def initialize(api_key:)
@api_key = api_key
end
def completion(prompt:, max_tokens: 400, temperature: 1.0, stop: "<|endoftext|>")
response = HTTP.headers(headers).post(
"#{URI}/completions",
json: {
prompt: prompt,
model: "text-davinci-003",
max_tokens: max_tokens,
temperature: temperature,
stop: stop
}
)
response.parse
end
private
attr_reader :api_key
def headers
{
"Content-Type" => "application/json",
"Authorization" => "Bearer #{api_key}",
}
end
end
openai = OpenAI.new(api_key: "YOUR_KEY")
puts openai.completion(prompt:"Make me a fun song about lobsters")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment