Class: SmartPrompt::OpenAIAdapter

Inherits:
LLMAdapter show all
Defined in:
lib/smart_prompt/llm_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ OpenAIAdapter

Returns a new instance of OpenAIAdapter.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/smart_prompt/llm_adapter.rb', line 21

def initialize(config)
  super
  api_key = @config['api_key']
  if api_key.is_a?(String) && api_key.start_with?('ENV[') && api_key.end_with?(']')
    api_key = eval(api_key)
  end      
  @client = OpenAI::Client.new(
    access_token: api_key,
    uri_base: @config['url'],
    request_timeout: 240
  )
end

Instance Method Details

#send_request(messages, model = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/smart_prompt/llm_adapter.rb', line 34

def send_request(messages, model=nil)
  SmartPrompt.logger.info "OpenAIAdapter: Sending request to OpenAI"
  if model
    model_name = model
  else
    model_name = @config['model']        
  end
  SmartPrompt.logger.info "OpenAIAdapter: Using model #{model_name}"
  response = @client.chat(
    parameters: {
      model: model_name,
      messages: messages,
      temperature: @config['temperature'] || 0.7
    }
  )
  SmartPrompt.logger.info "OpenAIAdapter: Received response from OpenAI"
  response.dig("choices", 0, "message", "content")
end