Class: LLMs::ConversationMessage
- Inherits:
-
Object
- Object
- LLMs::ConversationMessage
- Defined in:
- lib/llms/conversation_message.rb
Constant Summary collapse
- USER_ROLE =
'user'- ASSISTANT_ROLE =
'assistant'- SYSTEM_ROLE =
'system'
Instance Attribute Summary collapse
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#tool_calls ⇒ Object
readonly
Returns the value of attribute tool_calls.
-
#tool_results ⇒ Object
readonly
Returns the value of attribute tool_results.
Instance Method Summary collapse
- #assistant? ⇒ Boolean
- #empty? ⇒ Boolean
- #images ⇒ Object
-
#initialize(role, content, tool_calls = nil, tool_results = nil) ⇒ ConversationMessage
constructor
A new instance of ConversationMessage.
-
#system? ⇒ Boolean
Only for OpenAI compatible APIs.
- #text ⇒ Object
- #user? ⇒ Boolean
Constructor Details
#initialize(role, content, tool_calls = nil, tool_results = nil) ⇒ ConversationMessage
Returns a new instance of ConversationMessage.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/llms/conversation_message.rb', line 12 def initialize(role, content, tool_calls = nil, tool_results = nil) raise "role (#{role}) is not one of the allowed values" unless role == USER_ROLE || role == ASSISTANT_ROLE || role == SYSTEM_ROLE @role = role @parts = if content.is_a?(String) [{ text: content }] elsif content.is_a?(Array) content else raise "content (#{content}) is not a String or Array" end @tool_calls = tool_calls @tool_results = tool_results end |
Instance Attribute Details
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
10 11 12 |
# File 'lib/llms/conversation_message.rb', line 10 def parts @parts end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
10 11 12 |
# File 'lib/llms/conversation_message.rb', line 10 def role @role end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls.
10 11 12 |
# File 'lib/llms/conversation_message.rb', line 10 def tool_calls @tool_calls end |
#tool_results ⇒ Object (readonly)
Returns the value of attribute tool_results.
10 11 12 |
# File 'lib/llms/conversation_message.rb', line 10 def tool_results @tool_results end |
Instance Method Details
#assistant? ⇒ Boolean
51 52 53 |
# File 'lib/llms/conversation_message.rb', line 51 def assistant? @role == ASSISTANT_ROLE end |
#empty? ⇒ Boolean
41 42 43 44 45 |
# File 'lib/llms/conversation_message.rb', line 41 def empty? (@parts.nil? || @parts.empty? || self.text.nil? || self.text.empty?) && (@tool_calls.nil? || @tool_calls.empty?) && (@tool_results.nil? || @tool_results.empty?) end |
#images ⇒ Object
35 36 37 38 39 |
# File 'lib/llms/conversation_message.rb', line 35 def images if !@parts.nil? @parts.map{ |part| part[:image] }.compact end end |
#system? ⇒ Boolean
Only for OpenAI compatible APIs
56 57 58 |
# File 'lib/llms/conversation_message.rb', line 56 def system? @role == SYSTEM_ROLE end |
#text ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/llms/conversation_message.rb', line 26 def text if !@parts.nil? text_parts = @parts.map{ |part| part[:text] }.compact if !text_parts.empty? text_parts.join end end end |
#user? ⇒ Boolean
47 48 49 |
# File 'lib/llms/conversation_message.rb', line 47 def user? @role == USER_ROLE end |