Class: Legion::LLM::Types::Message

Inherits:
Data
  • Object
show all
Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/types/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def content
  @content
end

#conversation_idObject (readonly)

Returns the value of attribute conversation_id

Returns:

  • (Object)

    the current value of conversation_id



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def conversation_id
  @conversation_id
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def id
  @id
end

#input_tokensObject (readonly)

Returns the value of attribute input_tokens

Returns:

  • (Object)

    the current value of input_tokens



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def input_tokens
  @input_tokens
end

#modelObject (readonly)

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def name
  @name
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens

Returns:

  • (Object)

    the current value of output_tokens



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def output_tokens
  @output_tokens
end

#parent_idObject (readonly)

Returns the value of attribute parent_id

Returns:

  • (Object)

    the current value of parent_id



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def parent_id
  @parent_id
end

#providerObject (readonly)

Returns the value of attribute provider

Returns:

  • (Object)

    the current value of provider



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def provider
  @provider
end

#roleObject (readonly)

Returns the value of attribute role

Returns:

  • (Object)

    the current value of role



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def role
  @role
end

#seqObject (readonly)

Returns the value of attribute seq

Returns:

  • (Object)

    the current value of seq



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def seq
  @seq
end

#statusObject (readonly)

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def status
  @status
end

#task_idObject (readonly)

Returns the value of attribute task_id

Returns:

  • (Object)

    the current value of task_id



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def task_id
  @task_id
end

#timestampObject (readonly)

Returns the value of attribute timestamp

Returns:

  • (Object)

    the current value of timestamp



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def timestamp
  @timestamp
end

#tool_call_idObject (readonly)

Returns the value of attribute tool_call_id

Returns:

  • (Object)

    the current value of tool_call_id



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def tool_call_id
  @tool_call_id
end

#tool_callsObject (readonly)

Returns the value of attribute tool_calls

Returns:

  • (Object)

    the current value of tool_calls



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def tool_calls
  @tool_calls
end

#versionObject (readonly)

Returns the value of attribute version

Returns:

  • (Object)

    the current value of version



9
10
11
# File 'lib/legion/llm/types/message.rb', line 9

def version
  @version
end

Class Method Details

.build(**kwargs) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/llm/types/message.rb', line 17

def self.build(**kwargs)
  log.debug("[types][message] action=build role=#{kwargs[:role]} id=#{kwargs[:id]}")
  new(
    id:              kwargs[:id] || "msg_#{SecureRandom.hex(12)}",
    parent_id:       kwargs[:parent_id],
    role:            kwargs[:role]&.to_sym || :user,
    content:         kwargs[:content],
    tool_calls:      kwargs[:tool_calls],
    tool_call_id:    kwargs[:tool_call_id],
    name:            kwargs[:name],
    status:          kwargs.fetch(:status, :created),
    version:         kwargs.fetch(:version, 1),
    timestamp:       kwargs[:timestamp] || Time.now,
    seq:             kwargs[:seq],
    provider:        kwargs[:provider],
    model:           kwargs[:model],
    input_tokens:    kwargs[:input_tokens],
    output_tokens:   kwargs[:output_tokens],
    conversation_id: kwargs[:conversation_id],
    task_id:         kwargs[:task_id]
  )
end

.from_hash(hash) ⇒ Object



40
41
42
43
# File 'lib/legion/llm/types/message.rb', line 40

def self.from_hash(hash)
  hash = hash.transform_keys(&:to_sym) if hash.respond_to?(:transform_keys)
  build(**hash)
end

.wrap(input) ⇒ Object



45
46
47
48
49
# File 'lib/legion/llm/types/message.rb', line 45

def self.wrap(input)
  return input if input.is_a?(Message)

  from_hash(input) if input.is_a?(Hash)
end

Instance Method Details

#textObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/llm/types/message.rb', line 51

def text
  case content
  when String then content
  when Array
    content.select { |b| b.is_a?(Hash) ? b[:type] == :text : b.respond_to?(:text) }
           .map { |b| b.is_a?(Hash) ? b[:text] : b.text }
           .join
  else
    content.to_s
  end
end

#to_hObject



63
64
65
# File 'lib/legion/llm/types/message.rb', line 63

def to_h
  super.compact
end

#to_provider_hashObject



67
68
69
# File 'lib/legion/llm/types/message.rb', line 67

def to_provider_hash
  { role: role.to_s, content: text }.compact
end