Class: Ruboty::OpenAIChat::Actions::Chat

Inherits:
Base
  • Object
show all
Defined in:
lib/ruboty/openai_chat/actions/chat.rb

Constant Summary

Constants inherited from Base

Base::NAMESPACE, Base::PROFILE_KEY

Instance Attribute Summary collapse

Attributes inherited from Base

#message

Instance Method Summary collapse

Methods inherited from Base

#client, #initialize, #memory, #openai_model, #pretexts, #robot

Constructor Details

This class inherits a constructor from Ruboty::OpenAIChat::Actions::Base

Instance Attribute Details

#ai_commentString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/ruboty/openai_chat/actions/chat.rb', line 10

def ai_comment
  @ai_comment
end

#human_commentString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/ruboty/openai_chat/actions/chat.rb', line 10

def human_comment
  @human_comment
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruboty/openai_chat/actions/chat.rb', line 12

def call
  human_comment = message[:body]
  response = request_chat(human_comment)
  p response if Ruboty::OpenAIChat.debug_mode?
  raise response.body if response.code >= 400

  ai_comment = response.dig("choices", 0, "message", "content").gsub(/\A\s+/, "") || ""

  remember_messages(
    Message.new(role: :user, content: human_comment, expire_at: expire_at),
    Message.new(role: :assistant, content: ai_comment, expire_at: expire_at)
  )
  message.reply(ai_comment)
rescue StandardError => e
  forget
  message.reply(e.message, code: true)
  raise e if Ruboty::OpenAIChat.debug_mode?

  true
end