Created
February 7, 2023 14:57
-
-
Save pushmatrix/e8b363df474db057c26a8f48462d2fa5 to your computer and use it in GitHub Desktop.
Sample ruby openai call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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