Class: CoachZed::Clients::RubyOpenAI
- Inherits:
-
Object
- Object
- CoachZed::Clients::RubyOpenAI
- Defined in:
- lib/coach_zed/clients/ruby_openai.rb,
sig/coach_zed.rbs
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#model ⇒ String
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #extract_content(response) ⇒ String
- #generate(prompt:) ⇒ String
-
#initialize(client:, model: "gpt-4.1") ⇒ RubyOpenAI
constructor
A new instance of RubyOpenAI.
Constructor Details
#initialize(client:, model: "gpt-4.1") ⇒ RubyOpenAI
Returns a new instance of RubyOpenAI.
8 9 10 11 |
# File 'lib/coach_zed/clients/ruby_openai.rb', line 8 def initialize(client:, model: "gpt-4.1") @client = client @model = model end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
31 32 33 |
# File 'lib/coach_zed/clients/ruby_openai.rb', line 31 def client @client end |
#model ⇒ String (readonly)
Returns the value of attribute model.
31 32 33 |
# File 'lib/coach_zed/clients/ruby_openai.rb', line 31 def model @model end |
Instance Method Details
#extract_content(response) ⇒ String
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/coach_zed/clients/ruby_openai.rb', line 33 def extract_content(response) return response if response.is_a?(String) if response.respond_to?(:dig) content = response.dig("choices", 0, "message", "content") || response.dig(:choices, 0, :message, :content) return content if content.is_a?(String) end if response.respond_to?(:[]) content = response["choices"]&.first&.dig("message", "content") || response[:choices]&.first&.dig(:message, :content) return content if content.is_a?(String) end raise ArgumentError, "unable to extract content from ruby-openai response" end |
#generate(prompt:) ⇒ String
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/coach_zed/clients/ruby_openai.rb', line 13 def generate(prompt:) response = client.chat(parameters: { model: model, messages: [{role: "user", content: prompt}], response_format: { type: "json_schema", json_schema: { name: "coach_zed_schedule", schema: CoachZed::ScheduleSchema.to_h, strict: true } } }) extract_content(response) end |