Class: Legion::Extensions::Llm::Routing::ModelOffering

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/llm/routing/model_offering.rb

Overview

Describes one concrete model made available by one provider instance.

Constant Summary collapse

CAPABILITY_ALIASES =
{
  function_calling: :tools,
  functions: :tools,
  tool: :tools,
  tool_use: :tools,
  stream: :streaming,
  stream_chat: :streaming
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ModelOffering

Returns a new instance of ModelOffering.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 22

def initialize(data)
  @metadata = normalize_hash(fetch_value(data, :metadata))
  @provider_family = normalize_symbol(fetch_value(data, :provider_family, fetch_value(data, :provider)))
  @model_family = normalize_symbol(fetch_value(data, :model_family, @metadata[:model_family]))
  @provider_instance = normalize_symbol(fetch_value(data, :provider_instance,
                                                    fetch_value(data, :instance_id, @provider_family)))
  @instance_id = @provider_instance
  @transport = normalize_symbol(fetch_value(data, :transport, :http))
  @tier = normalize_symbol(fetch_value(data, :tier, default_tier))
  @model = fetch_value(data, :model).to_s
  @canonical_model_alias = normalize_model_alias(fetch_value(data, :canonical_model_alias,
                                                             ))
  @routing_metadata = normalize_hash(fetch_value(data, :routing_metadata))
  @usage_type = normalize_usage_type(fetch_value(data, :usage_type,
                                                 fetch_value(data, :type) ||
                                                 fetch_value(data, :kind) ||
                                                 infer_usage_type(data)))
  @capabilities = normalize_capabilities(fetch_value(data, :capabilities))
  @limits = normalize_hash(fetch_value(data, :limits))
  @credentials = fetch_value(data, :credentials)
  @health = normalize_hash(fetch_value(data, :health))
  @cost = normalize_hash(fetch_value(data, :cost))
  @policy_tags = normalize_array(fetch_value(data, :policy_tags)).map(&:to_sym)
  @offering_id = normalize_offering_id(fetch_value(data, :offering_id, default_offering_id))
end

Instance Attribute Details

#canonical_model_aliasObject (readonly)

Returns the value of attribute canonical_model_alias.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def canonical_model_alias
  @canonical_model_alias
end

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def capabilities
  @capabilities
end

#costObject (readonly)

Returns the value of attribute cost.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def cost
  @cost
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def credentials
  @credentials
end

#healthObject (readonly)

Returns the value of attribute health.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def health
  @health
end

#instance_idObject (readonly)

Returns the value of attribute instance_id.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def instance_id
  @instance_id
end

#limitsObject (readonly)

Returns the value of attribute limits.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def limits
  @limits
end

#metadataObject (readonly)

Returns the value of attribute metadata.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def 
  @metadata
end

#modelObject (readonly)

Returns the value of attribute model.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def model
  @model
end

#model_familyObject (readonly)

Returns the value of attribute model_family.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def model_family
  @model_family
end

#offering_idObject (readonly)

Returns the value of attribute offering_id.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def offering_id
  @offering_id
end

#policy_tagsObject (readonly)

Returns the value of attribute policy_tags.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def policy_tags
  @policy_tags
end

#provider_familyObject (readonly)

Returns the value of attribute provider_family.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def provider_family
  @provider_family
end

#provider_instanceObject (readonly)

Returns the value of attribute provider_instance.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def provider_instance
  @provider_instance
end

#routing_metadataObject (readonly)

Returns the value of attribute routing_metadata.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def 
  @routing_metadata
end

#tierObject (readonly)

Returns the value of attribute tier.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def tier
  @tier
end

#transportObject (readonly)

Returns the value of attribute transport.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def transport
  @transport
end

#usage_typeObject (readonly)

Returns the value of attribute usage_type.



18
19
20
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 18

def usage_type
  @usage_type
end

Instance Method Details

#context_windowObject



60
61
62
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 60

def context_window
  integer_limit(:context_window) || integer_limit(:max_input_tokens)
end

#eligibility_fingerprintObject



86
87
88
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 86

def eligibility_fingerprint
  LaneKey.eligibility_fingerprint(self)
end

#eligible_for?(usage_type: nil, required_capabilities: [], min_context_window: nil, policy_tags: []) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 72

def eligible_for?(usage_type: nil, required_capabilities: [], min_context_window: nil, policy_tags: [])
  return false unless enabled?
  return false unless usage_type_matches?(usage_type)
  return false unless capabilities_match?(required_capabilities)
  return false unless context_window_matches?(min_context_window)
  return false unless policy_tags_match?(policy_tags)

  true
end

#embedding?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 52

def embedding?
  usage_type == :embedding
end

#enabled?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 48

def enabled?
  !.key?(:enabled) || [:enabled] != false
end

#inference?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 56

def inference?
  %i[chat inference completion].include?(usage_type)
end

#lane_key(prefix: 'llm.fleet', include_context: true, include_fingerprint: false) ⇒ Object



82
83
84
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 82

def lane_key(prefix: 'llm.fleet', include_context: true, include_fingerprint: false)
  LaneKey.for(self, prefix:, include_context:, include_fingerprint:)
end

#max_output_tokensObject



64
65
66
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 64

def max_output_tokens
  integer_limit(:max_output_tokens)
end

#model_alias?(alias_name) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 90

def model_alias?(alias_name)
  normalized = normalize_model_alias(alias_name)
  [canonical_model_alias, model].compact.any? { |candidate| normalize_model_alias(candidate) == normalized }
end

#supports?(capability) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 68

def supports?(capability)
  normalize_capabilities([capability]).any? { |candidate| capabilities.include?(candidate) }
end

#to_hObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/legion/extensions/llm/routing/model_offering.rb', line 95

def to_h
  {
    offering_id: offering_id,
    provider_family: provider_family,
    model_family: model_family,
    provider_instance: provider_instance,
    instance_id: instance_id,
    transport: transport,
    tier: tier,
    model: model,
    canonical_model_alias: canonical_model_alias,
    routing_metadata: ,
    usage_type: usage_type,
    capabilities: capabilities,
    limits: limits,
    credentials: credentials,
    health: health,
    cost: cost,
    policy_tags: policy_tags,
    metadata: 
  }
end