Class: AiAgent::OpenAI

Inherits:
Base
  • Object
show all
Defined in:
lib/ai_agent/ai_agent/open_ai.rb

Defined Under Namespace

Modules: Model

Instance Attribute Summary

Attributes inherited from Base

#agent, #api_key, #endpoint, #timeout

Instance Method Summary collapse

Methods inherited from Base

#analyze_sentiment, #recognize_entities, #summarize_text

Constructor Details

#initialize(api_key: nil, endpoint: nil, timeout: 60) ⇒ OpenAI

Returns a new instance of OpenAI.



19
20
21
22
23
24
# File 'lib/ai_agent/ai_agent/open_ai.rb', line 19

def initialize(api_key: nil, endpoint: nil, timeout: 60)
  self.agent = OPENAI
  self.api_key = api_key || ENV['OPENAI_API_KEY']
  self.endpoint = endpoint # nil for default as defined in openai/client
  self.timeout = timeout
end

Instance Method Details

#chat(messages, options: {}) ⇒ Object



30
31
32
33
# File 'lib/ai_agent/ai_agent/open_ai.rb', line 30

def chat(messages, options: {})
  params = options.reverse_merge(model: Model::GPT_4O, temperature: 0.1)
  client.chat(parameters: params.merge(messages: messages))
end

#clientObject



26
27
28
# File 'lib/ai_agent/ai_agent/open_ai.rb', line 26

def client
  openai if agent == OPENAI
end

#format_response(response) ⇒ Object



35
36
37
# File 'lib/ai_agent/ai_agent/open_ai.rb', line 35

def format_response(response)
  response['choices'][0]['message']['content'] rescue "ERROR: Couldn't extract text from OpenAI response"
end