Class: RubyLLM::Message
- Inherits:
-
Object
- Object
- RubyLLM::Message
- Defined in:
- lib/ruby_llm/message.rb
Overview
A single message in a chat conversation.
Direct Known Subclasses
Constant Summary collapse
- ROLES =
i[system user assistant tool].freeze
Instance Attribute Summary collapse
-
#cache_creation_tokens ⇒ Object
readonly
Returns the value of attribute cache_creation_tokens.
-
#cached_tokens ⇒ Object
readonly
Returns the value of attribute cached_tokens.
- #content ⇒ Object
-
#input_tokens ⇒ Object
readonly
Returns the value of attribute input_tokens.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#output_tokens ⇒ Object
readonly
Returns the value of attribute output_tokens.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#tool_call_id ⇒ Object
readonly
Returns the value of attribute tool_call_id.
-
#tool_calls ⇒ Object
readonly
Returns the value of attribute tool_calls.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Message
constructor
A new instance of Message.
- #instance_variables ⇒ Object
- #to_h ⇒ Object
- #tool_call? ⇒ Boolean
- #tool_result? ⇒ Boolean
- #tool_results ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Message
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby_llm/message.rb', line 12 def initialize( = {}) @role = .fetch(:role).to_sym @content = normalize_content(.fetch(:content)) @model_id = [:model_id] @tool_calls = [:tool_calls] @tool_call_id = [:tool_call_id] @input_tokens = [:input_tokens] @output_tokens = [:output_tokens] @cached_tokens = [:cached_tokens] @cache_creation_tokens = [:cache_creation_tokens] @raw = [:raw] ensure_valid_role end |
Instance Attribute Details
#cache_creation_tokens ⇒ Object (readonly)
Returns the value of attribute cache_creation_tokens.
8 9 10 |
# File 'lib/ruby_llm/message.rb', line 8 def cache_creation_tokens @cache_creation_tokens end |
#cached_tokens ⇒ Object (readonly)
Returns the value of attribute cached_tokens.
8 9 10 |
# File 'lib/ruby_llm/message.rb', line 8 def cached_tokens @cached_tokens end |
#content ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/ruby_llm/message.rb', line 27 def content if @content.is_a?(Content) && @content.text && @content..empty? @content.text else @content end end |
#input_tokens ⇒ Object (readonly)
Returns the value of attribute input_tokens.
8 9 10 |
# File 'lib/ruby_llm/message.rb', line 8 def input_tokens @input_tokens end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
8 9 10 |
# File 'lib/ruby_llm/message.rb', line 8 def model_id @model_id end |
#output_tokens ⇒ Object (readonly)
Returns the value of attribute output_tokens.
8 9 10 |
# File 'lib/ruby_llm/message.rb', line 8 def output_tokens @output_tokens end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
8 9 10 |
# File 'lib/ruby_llm/message.rb', line 8 def raw @raw end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
8 9 10 |
# File 'lib/ruby_llm/message.rb', line 8 def role @role end |
#tool_call_id ⇒ Object (readonly)
Returns the value of attribute tool_call_id.
8 9 10 |
# File 'lib/ruby_llm/message.rb', line 8 def tool_call_id @tool_call_id end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls.
8 9 10 |
# File 'lib/ruby_llm/message.rb', line 8 def tool_calls @tool_calls end |
Instance Method Details
#instance_variables ⇒ Object
61 62 63 |
# File 'lib/ruby_llm/message.rb', line 61 def instance_variables super - [:@raw] end |
#to_h ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ruby_llm/message.rb', line 47 def to_h { role: role, content: content, model_id: model_id, tool_calls: tool_calls, tool_call_id: tool_call_id, input_tokens: input_tokens, output_tokens: output_tokens, cached_tokens: cached_tokens, cache_creation_tokens: cache_creation_tokens }.compact end |
#tool_call? ⇒ Boolean
35 36 37 |
# File 'lib/ruby_llm/message.rb', line 35 def tool_call? !tool_calls.nil? && !tool_calls.empty? end |
#tool_result? ⇒ Boolean
39 40 41 |
# File 'lib/ruby_llm/message.rb', line 39 def tool_result? !tool_call_id.nil? && !tool_call_id.empty? end |
#tool_results ⇒ Object
43 44 45 |
# File 'lib/ruby_llm/message.rb', line 43 def tool_results content if tool_result? end |