Class: Legion::Extensions::Llm::Message
- Inherits:
-
Object
- Object
- Legion::Extensions::Llm::Message
- Defined in:
- lib/legion/extensions/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
- #content ⇒ Object
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#stop_reason ⇒ Object
readonly
Returns the value of attribute stop_reason.
-
#thinking ⇒ Object
readonly
Returns the value of attribute thinking.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
-
#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
- #cache_creation_tokens ⇒ Object
- #cached_tokens ⇒ Object
-
#initialize(options = {}) ⇒ Message
constructor
A new instance of Message.
- #input_tokens ⇒ Object
- #instance_variables ⇒ Object
- #output_tokens ⇒ Object
- #reasoning_tokens ⇒ Object
- #thinking_tokens ⇒ Object
- #to_h ⇒ Object
- #to_internal_h ⇒ Object
- #tool_call? ⇒ Boolean
- #tool_result? ⇒ Boolean
- #tool_results ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Message
Returns a new instance of Message.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/legion/extensions/llm/message.rb', line 13 def initialize( = {}) @role = .fetch(:role).to_sym @tool_calls = [:tool_calls] @content = normalize_content(.fetch(:content), role: @role, tool_calls: @tool_calls) @model_id = [:model_id] @tool_call_id = [:tool_call_id] @tokens = [:tokens] || Tokens.build( input: [:input_tokens], output: [:output_tokens], cached: [:cached_tokens], cache_creation: [:cache_creation_tokens], thinking: [:thinking_tokens], reasoning: [:reasoning_tokens] ) @raw = [:raw] @thinking = [:thinking] @stop_reason = [:stop_reason] ensure_valid_role end |
Instance Attribute Details
#content ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/legion/extensions/llm/message.rb', line 34 def content if @content.is_a?(Content) && @content.text && @content..empty? @content.text else @content end end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
10 11 12 |
# File 'lib/legion/extensions/llm/message.rb', line 10 def model_id @model_id end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
10 11 12 |
# File 'lib/legion/extensions/llm/message.rb', line 10 def raw @raw end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
10 11 12 |
# File 'lib/legion/extensions/llm/message.rb', line 10 def role @role end |
#stop_reason ⇒ Object (readonly)
Returns the value of attribute stop_reason.
10 11 12 |
# File 'lib/legion/extensions/llm/message.rb', line 10 def stop_reason @stop_reason end |
#thinking ⇒ Object (readonly)
Returns the value of attribute thinking.
10 11 12 |
# File 'lib/legion/extensions/llm/message.rb', line 10 def thinking @thinking end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
10 11 12 |
# File 'lib/legion/extensions/llm/message.rb', line 10 def tokens @tokens end |
#tool_call_id ⇒ Object (readonly)
Returns the value of attribute tool_call_id.
10 11 12 |
# File 'lib/legion/extensions/llm/message.rb', line 10 def tool_call_id @tool_call_id end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls.
10 11 12 |
# File 'lib/legion/extensions/llm/message.rb', line 10 def tool_calls @tool_calls end |
Instance Method Details
#cache_creation_tokens ⇒ Object
66 67 68 |
# File 'lib/legion/extensions/llm/message.rb', line 66 def cache_creation_tokens tokens&.cache_creation end |
#cached_tokens ⇒ Object
62 63 64 |
# File 'lib/legion/extensions/llm/message.rb', line 62 def cached_tokens tokens&.cached end |
#input_tokens ⇒ Object
54 55 56 |
# File 'lib/legion/extensions/llm/message.rb', line 54 def input_tokens tokens&.input end |
#instance_variables ⇒ Object
96 97 98 |
# File 'lib/legion/extensions/llm/message.rb', line 96 def instance_variables super - [:@raw] end |
#output_tokens ⇒ Object
58 59 60 |
# File 'lib/legion/extensions/llm/message.rb', line 58 def output_tokens tokens&.output end |
#reasoning_tokens ⇒ Object
74 75 76 |
# File 'lib/legion/extensions/llm/message.rb', line 74 def reasoning_tokens tokens&.thinking end |
#thinking_tokens ⇒ Object
70 71 72 |
# File 'lib/legion/extensions/llm/message.rb', line 70 def thinking_tokens tokens&.thinking end |
#to_h ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/legion/extensions/llm/message.rb', line 78 def to_h { role: role, content: content, model_id: model_id, tool_calls: tool_calls, tool_call_id: tool_call_id }.merge(tokens ? tokens.to_h : {}).compact end |
#to_internal_h ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/legion/extensions/llm/message.rb', line 88 def to_internal_h to_h.merge( thinking: thinking&.text, thinking_signature: thinking&.signature, raw: raw ).compact end |
#tool_call? ⇒ Boolean
42 43 44 |
# File 'lib/legion/extensions/llm/message.rb', line 42 def tool_call? !tool_calls.nil? && !tool_calls.empty? end |
#tool_result? ⇒ Boolean
46 47 48 |
# File 'lib/legion/extensions/llm/message.rb', line 46 def tool_result? !tool_call_id.nil? && !tool_call_id.empty? end |
#tool_results ⇒ Object
50 51 52 |
# File 'lib/legion/extensions/llm/message.rb', line 50 def tool_results content if tool_result? end |