Class: OpenAI::Message

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

Overview

An over-engineered solution that ultimately wasn't used for its intent. (ChatGPT isn't brilliant at parsing JSON sructures without starting to reply in JSON, so most of it is useless)

Direct Known Subclasses

BotMessage, SystemMessage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Message



9
10
11
12
13
# File 'lib/open_ai/message.rb', line 9

def initialize(**kwargs)
  kwargs.each_pair { public_send("#{_1}=", _2) }
  @role = :user
  @timestamp = Time.now.to_i
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



6
7
8
# File 'lib/open_ai/message.rb', line 6

def body
  @body
end

#chat_idObject

Returns the value of attribute chat_id.



6
7
8
# File 'lib/open_ai/message.rb', line 6

def chat_id
  @chat_id
end

#chat_threadObject

Returns the value of attribute chat_thread.



6
7
8
# File 'lib/open_ai/message.rb', line 6

def chat_thread
  @chat_thread
end

#costObject

Returns the value of attribute cost.



6
7
8
# File 'lib/open_ai/message.rb', line 6

def cost
  @cost
end

#fromObject

Returns the value of attribute from.



6
7
8
# File 'lib/open_ai/message.rb', line 6

def from
  @from
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/open_ai/message.rb', line 6

def id
  @id
end

#imageObject

Returns the value of attribute image.



6
7
8
# File 'lib/open_ai/message.rb', line 6

def image
  @image
end

#replies_toObject

Returns the value of attribute replies_to.



6
7
8
# File 'lib/open_ai/message.rb', line 6

def replies_to
  @replies_to
end

#roleObject (readonly)

Returns the value of attribute role.



7
8
9
# File 'lib/open_ai/message.rb', line 7

def role
  @role
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



7
8
9
# File 'lib/open_ai/message.rb', line 7

def timestamp
  @timestamp
end

Instance Method Details

#as_jsonObject

Format for OpenAI API



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/open_ai/message.rb', line 20

def as_json
  msg = [from, body].compact.join("\n")

  if image
    {
      role: role,
      content: [
        { type: "text", text: msg },
        { type: "image_url", image_url: { url: "data:image/jpeg;base64,#{image.base64}" } }
      ]
    }
  else
    { role:, content: msg }
  end
end

#for_logsObject

Format for machine-readable logs



37
38
39
# File 'lib/open_ai/message.rb', line 37

def for_logs
  { role:, body:, from:, id:, replies_to:, tokens:, chat_id:, timestamp: }
end

#to_sObject

Format for human-readable logs



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/open_ai/message.rb', line 42

def to_s
  msg_lines = {
    "Chat ID" => chat_id,
    "Message ID" => id,
    "From" => from,
    "To" => replies_to,
    "Body" => body,
    # "Tokens used" => tokens,
    "Image" => (image ? "Some image" : "None")
  }.reject { |_k, v|
    v.blank?
  }.map { |k, v|
    "#{k}: #{v}"
  }

  [Time.now.utc, *msg_lines].join("\n") + "\n\n"
end

#valid?Boolean



15
16
17
# File 'lib/open_ai/message.rb', line 15

def valid?
  [(image || body), from, id, chat_id, chat_thread].all?(&:present?)
end