Class: OpenAI::Message
- Inherits:
-
Object
- Object
- OpenAI::Message
- 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
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#chat_id ⇒ Object
Returns the value of attribute chat_id.
-
#chat_thread ⇒ Object
Returns the value of attribute chat_thread.
-
#cost ⇒ Object
Returns the value of attribute cost.
-
#from ⇒ Object
Returns the value of attribute from.
-
#id ⇒ Object
Returns the value of attribute id.
-
#image ⇒ Object
Returns the value of attribute image.
-
#replies_to ⇒ Object
Returns the value of attribute replies_to.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Instance Method Summary collapse
-
#as_json ⇒ Object
Format for OpenAI API.
-
#for_logs ⇒ Object
Format for machine-readable logs.
-
#initialize(**kwargs) ⇒ Message
constructor
A new instance of Message.
-
#to_s ⇒ Object
Format for human-readable logs.
- #valid? ⇒ Boolean
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
#body ⇒ Object
Returns the value of attribute body.
6 7 8 |
# File 'lib/open_ai/message.rb', line 6 def body @body end |
#chat_id ⇒ Object
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_thread ⇒ Object
Returns the value of attribute chat_thread.
6 7 8 |
# File 'lib/open_ai/message.rb', line 6 def chat_thread @chat_thread end |
#cost ⇒ Object
Returns the value of attribute cost.
6 7 8 |
# File 'lib/open_ai/message.rb', line 6 def cost @cost end |
#from ⇒ Object
Returns the value of attribute from.
6 7 8 |
# File 'lib/open_ai/message.rb', line 6 def from @from end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/open_ai/message.rb', line 6 def id @id end |
#image ⇒ Object
Returns the value of attribute image.
6 7 8 |
# File 'lib/open_ai/message.rb', line 6 def image @image end |
#replies_to ⇒ Object
Returns the value of attribute replies_to.
6 7 8 |
# File 'lib/open_ai/message.rb', line 6 def replies_to @replies_to end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
7 8 9 |
# File 'lib/open_ai/message.rb', line 7 def role @role end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
7 8 9 |
# File 'lib/open_ai/message.rb', line 7 def @timestamp end |
Instance Method Details
#as_json ⇒ Object
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_logs ⇒ Object
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_s ⇒ Object
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 |