Class: Rails::Llm::Structured::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/llm/structured/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ Client

Returns a new instance of Client.

Raises:



9
10
11
12
13
14
# File 'lib/rails/llm/structured/client.rb', line 9

def initialize(api_key: nil)
  @api_key = api_key || ENV['OPENAI_API_KEY']
  raise Error, "OpenAI API key not found. Set OPENAI_API_KEY environment variable." unless @api_key
  
  @client = OpenAI::Client.new(access_token: @api_key)
end

Instance Method Details

#chat(model:, messages:, response_format:, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails/llm/structured/client.rb', line 16

def chat(model:, messages:, response_format:, **options)
  parameters = {
    model: model,
    messages: messages,
    response_format: response_format
  }
  
  parameters[:temperature] = options[:temperature] if options[:temperature]
  parameters[:max_tokens] = options[:max_tokens] if options[:max_tokens]
  
  @client.chat(parameters: parameters)
end