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.



19
20
21
22
23
24
25
26
# File 'lib/smart_prompt/llm_adapter.rb', line 19

def initialize(config)
  super
  @client = OpenAI::Client.new(
    access_token: @config['api_key'],
    uri_base: @config['url'],
    request_timeout: 240
  )
end

Instance Method Details

#send_request(messages, model = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/smart_prompt/llm_adapter.rb', line 28

def send_request(messages, model=nil)
  if model
    model_name = model
  else
    model_name = @config['model']        
  end
  response = @client.chat(
    parameters: {
      model: model_name,
      messages: messages,
      temperature: @config['temperature'] || 0.7
    }
  )
  response.dig("choices", 0, "message", "content")
end