Class: Contai::Providers::OpenAI
- Includes:
- HTTParty
- Defined in:
- lib/contai/providers/openai.rb
Instance Method Summary collapse
Methods inherited from Base
Constructor Details
This class inherits a constructor from Contai::Providers::Base
Instance Method Details
#generate(prompt) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/contai/providers/openai.rb', line 10 def generate(prompt) response = self.class.post("/chat/completions", headers: { "Authorization" => "Bearer #{@options[:api_key]}", "Content-Type" => "application/json" }, body: { model: @options[:model] || "gpt-3.5-turbo", messages: [ { role: "user", content: prompt } ], max_tokens: @options[:max_tokens] || 1000 }.to_json, timeout: @options[:timeout] || Contai.configuration.timeout ) if response.success? content = response.parsed_response.dig("choices", 0, "message", "content") success_result(content) else error_result("OpenAI API error: #{response.code} #{response.}") end rescue => e error_result(e.) end |