Class: CoachZed::Clients::RubyOpenAI

Inherits:
Object
  • Object
show all
Defined in:
lib/coach_zed/clients/ruby_openai.rb,
sig/coach_zed.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, model: "gpt-4.1") ⇒ RubyOpenAI

Returns a new instance of RubyOpenAI.

Parameters:

  • client: (Object)
  • model: (String) (defaults to: "gpt-4.1")


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

#clientObject (readonly)

Returns the value of attribute client.

Returns:

  • (Object)


31
32
33
# File 'lib/coach_zed/clients/ruby_openai.rb', line 31

def client
  @client
end

#modelString (readonly)

Returns the value of attribute model.

Returns:

  • (String)


31
32
33
# File 'lib/coach_zed/clients/ruby_openai.rb', line 31

def model
  @model
end

Instance Method Details

#extract_content(response) ⇒ String

Parameters:

  • response (Object)

Returns:

  • (String)

Raises:

  • (ArgumentError)


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

Parameters:

  • prompt: (String)

Returns:

  • (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