Class: ActiveIntelligence::LLM::OpenAIAdapter

Inherits:
Adapter show all
Includes:
Concerns::OpenAI
Defined in:
lib/active_intelligence/llm/openai_adapter.rb

Instance Attribute Summary

Attributes inherited from Adapter

#settings

Instance Method Summary collapse

Methods inherited from Adapter

#initialize

Constructor Details

This class inherits a constructor from ActiveIntelligence::Adapter

Instance Method Details

#chat(parameters) ⇒ Object



10
11
12
13
# File 'lib/active_intelligence/llm/openai_adapter.rb', line 10

def chat(parameters)
  response = client.chat(parameters:)
  return response.dig('choices', 0, 'message', 'content')
end

#generate(prompt, _options = {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/active_intelligence/llm/openai_adapter.rb', line 15

def generate(prompt, _options = {})
  parameters = default_parameters
  parameters[:messages] = [{ role: 'user', content: prompt }]

  return chat(parameters)
end

#reply(chat, prompt, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_intelligence/llm/openai_adapter.rb', line 22

def reply(chat, prompt, options = {})
  limit = options[:limit] || settings[:chat_message_limit] || 10

  parameters = default_parameters
  parameters[:messages] = [{ role: 'system', content: prompt }]

  messages = chat.messages.order(created_at: :desc).limit(limit).reverse
  messages.each do |message|
    parameters[:messages] << { role: message.role, content: message.content }
  end

  return chat(parameters)
end