Class: Seo::Providers::OpenAI
Constant Summary collapse
- DEFAULT_MODEL =
"gpt-4o"- API_URL =
"https://api.openai.com/v1/chat/completions"
Instance Method Summary collapse
- #complete(prompt, max_tokens:) ⇒ Object
-
#initialize(api_key:, model: DEFAULT_MODEL) ⇒ OpenAI
constructor
A new instance of OpenAI.
Constructor Details
#initialize(api_key:, model: DEFAULT_MODEL) ⇒ OpenAI
Returns a new instance of OpenAI.
14 15 16 17 |
# File 'lib/seo/providers/openai.rb', line 14 def initialize(api_key:, model: DEFAULT_MODEL) @api_key = api_key @model = model end |
Instance Method Details
#complete(prompt, max_tokens:) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/seo/providers/openai.rb', line 19 def complete(prompt, max_tokens:) uri = URI(API_URL) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true req = Net::HTTP::Post.new(uri) req["Authorization"] = "Bearer #{@api_key}" req["Content-Type"] = "application/json" req.body = JSON.generate({ model: @model, max_tokens: max_tokens, messages: [{ role: "user", content: prompt }] }) data = JSON.parse(http.request(req).body) data.dig("choices", 0, "message", "content").to_s end |