Class: Raif::Llm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Concerns::Llms::MessageFormatting
Defined in:
app/models/raif/llm.rb

Constant Summary collapse

VALID_RESPONSE_FORMATS =
[:text, :json, :html].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, api_name:, display_name: nil, model_provider_settings: {}, supported_provider_managed_tools: [], supports_native_tool_use: true, temperature: nil, max_completion_tokens: nil, input_token_cost: nil, output_token_cost: nil) ⇒ Llm

Returns a new instance of Llm.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/raif/llm.rb', line 26

def initialize(
  key:,
  api_name:,
  display_name: nil,
  model_provider_settings: {},
  supported_provider_managed_tools: [],
  supports_native_tool_use: true,
  temperature: nil,
  max_completion_tokens: nil,
  input_token_cost: nil,
  output_token_cost: nil
)
  @key = key
  @api_name = api_name
  @display_name = display_name
  @provider_settings = model_provider_settings
  @supports_native_tool_use = supports_native_tool_use
  @default_temperature = temperature || 0.7
  @default_max_completion_tokens = max_completion_tokens
  @input_token_cost = input_token_cost
  @output_token_cost = output_token_cost
  @supported_provider_managed_tools = supported_provider_managed_tools.map(&:to_s)
end

Instance Attribute Details

#api_nameObject

Returns the value of attribute api_name.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def api_name
  @api_name
end

#default_max_completion_tokensObject

Returns the value of attribute default_max_completion_tokens.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def default_max_completion_tokens
  @default_max_completion_tokens
end

#default_temperatureObject

Returns the value of attribute default_temperature.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def default_temperature
  @default_temperature
end

#display_nameObject

Returns the value of attribute display_name.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def display_name
  @display_name
end

#input_token_costObject

Returns the value of attribute input_token_cost.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def input_token_cost
  @input_token_cost
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def key
  @key
end

#output_token_costObject

Returns the value of attribute output_token_cost.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def output_token_cost
  @output_token_cost
end

#provider_settingsObject

Returns the value of attribute provider_settings.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def provider_settings
  @provider_settings
end

#supported_provider_managed_toolsObject

Returns the value of attribute supported_provider_managed_tools.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def supported_provider_managed_tools
  @supported_provider_managed_tools
end

#supports_native_tool_useObject Also known as: supports_native_tool_use?

Returns the value of attribute supports_native_tool_use.



8
9
10
# File 'app/models/raif/llm.rb', line 8

def supports_native_tool_use
  @supports_native_tool_use
end

Class Method Details

.cache_creation_input_token_cost_multiplierObject

Multiplier applied to the base input_token_cost to derive the per-token cost for cache creation writes. Return nil when there is no write surcharge.



157
158
159
# File 'app/models/raif/llm.rb', line 157

def self.cache_creation_input_token_cost_multiplier
  nil
end

.cache_read_input_token_cost_multiplierObject

Multiplier applied to the base input_token_cost to derive the per-token cost for cache reads. Return nil when the provider has no cache pricing.



151
152
153
# File 'app/models/raif/llm.rb', line 151

def self.cache_read_input_token_cost_multiplier
  nil
end

.prompt_tokens_include_cached_tokens?Boolean

Override in subclasses to indicate whether prompt_tokens reported by the provider already include cached tokens as a subset (OpenAI, Google, OpenRouter) or whether cached tokens are reported separately and are additive to prompt_tokens (Anthropic, Bedrock).

Returns:

  • (Boolean)


145
146
147
# File 'app/models/raif/llm.rb', line 145

def self.prompt_tokens_include_cached_tokens?
  true
end

.valid_response_formatsObject



137
138
139
# File 'app/models/raif/llm.rb', line 137

def self.valid_response_formats
  VALID_RESPONSE_FORMATS
end

Instance Method Details

#build_forced_tool_choice(tool_name) ⇒ Hash

Build the tool_choice parameter to force a specific tool to be called. Each provider implements this to return the correct format.

Parameters:

  • tool_name (String)

    The name of the tool to force

Returns:

  • (Hash)

    The tool_choice parameter for the provider’s API

Raises:

  • (NotImplementedError)


169
170
171
# File 'app/models/raif/llm.rb', line 169

def build_forced_tool_choice(tool_name)
  raise NotImplementedError, "#{self.class.name} must implement #build_forced_tool_choice"
end

#build_required_tool_choiceHash, String

Build the tool_choice parameter to require the model to call any tool (but not a specific one). Each provider implements this to return the correct format.

Returns:

  • (Hash, String)

    The tool_choice parameter for the provider’s API

Raises:

  • (NotImplementedError)


176
177
178
# File 'app/models/raif/llm.rb', line 176

def build_required_tool_choice
  raise NotImplementedError, "#{self.class.name} must implement #build_required_tool_choice"
end

#chat(message: nil, messages: nil, response_format: :text, available_model_tools: [], source: nil, system_prompt: nil, temperature: nil, max_completion_tokens: nil, tool_choice: nil, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/raif/llm.rb', line 54

def chat(message: nil, messages: nil, response_format: :text, available_model_tools: [], source: nil, system_prompt: nil, temperature: nil,
  max_completion_tokens: nil, tool_choice: nil, &block)
  unless response_format.is_a?(Symbol)
    raise ArgumentError,
      "Raif::Llm#chat - Invalid response format: #{response_format}. Must be a symbol (you passed #{response_format.class}) and be one of: #{VALID_RESPONSE_FORMATS.join(", ")}" # rubocop:disable Layout/LineLength
  end

  unless VALID_RESPONSE_FORMATS.include?(response_format)
    raise ArgumentError, "Raif::Llm#chat - Invalid response format: #{response_format}. Must be one of: #{VALID_RESPONSE_FORMATS.join(", ")}"
  end

  unless message.present? || messages.present?
    raise ArgumentError, "Raif::Llm#chat - You must provide either a message: or messages: argument"
  end

  if message.present? && messages.present?
    raise ArgumentError, "Raif::Llm#chat - You must provide either a message: or messages: argument, not both"
  end

  # Normalize :required / "required" to the symbol form for validation
  tool_choice = :required if tool_choice.to_s == "required"

  if tool_choice == :required
    if available_model_tools.blank?
      raise ArgumentError,
        "Raif::Llm#chat - tool_choice: :required requires at least one available model tool"
    end
  elsif tool_choice.present? && !available_model_tools.map(&:to_s).include?(tool_choice.to_s)
    raise ArgumentError,
      "Raif::Llm#chat - Invalid tool choice: #{tool_choice} is not included in the available model tools: #{available_model_tools.join(", ")}"
  end

  unless Raif.config.llm_api_requests_enabled
    Raif.logger.warn("LLM API requests are disabled. Skipping request to #{api_name}.")
    return
  end

  messages = [{ "role" => "user", "content" => message }] if message.present?

  temperature ||= default_temperature
  max_completion_tokens ||= default_max_completion_tokens

  model_completion = Raif::ModelCompletion.create!(
    messages: format_messages(messages),
    system_prompt: system_prompt,
    response_format: response_format,
    source: source,
    llm_model_key: key.to_s,
    model_api_name: api_name,
    temperature: temperature,
    max_completion_tokens: max_completion_tokens,
    available_model_tools: available_model_tools,
    tool_choice: tool_choice&.to_s,
    stream_response: block_given?
  )

  model_completion.started!

  retry_with_backoff(model_completion) do
    perform_model_completion!(model_completion, &block)
    ensure_model_completion_present!(model_completion)
  end

  model_completion.completed!
  model_completion
rescue Raif::Errors::StreamingError => e
  Rails.logger.error("Raif streaming error -- code: #{e.code} -- type: #{e.type} -- message: #{e.message} -- event: #{e.event}")
  model_completion&.record_failure!(e) unless model_completion&.failed?
  raise e
rescue Faraday::Error => e
  Raif.logger.error("LLM API request failed (status: #{e.response_status}): #{e.message}")
  Raif.logger.error(e.response_body)
  model_completion&.record_failure!(e) unless model_completion&.failed?
  raise e
rescue StandardError => e
  model_completion&.record_failure!(e) unless model_completion&.failed?
  raise e
end

#nameObject



50
51
52
# File 'app/models/raif/llm.rb', line 50

def name
  I18n.t("raif.model_names.#{key}", default: display_name || key.to_s.humanize)
end

#perform_model_completion!(model_completion, &block) ⇒ Object

Raises:

  • (NotImplementedError)


133
134
135
# File 'app/models/raif/llm.rb', line 133

def perform_model_completion!(model_completion, &block)
  raise NotImplementedError, "#{self.class.name} must implement #perform_model_completion!"
end

#supports_faithful_required_tool_choice?(available_model_tools) ⇒ Boolean

Whether the provider can faithfully enforce tool_choice: :required for the given tool set. Override in subclasses when a provider can only enforce required tool use for some tool types.

Returns:

  • (Boolean)


183
184
185
# File 'app/models/raif/llm.rb', line 183

def supports_faithful_required_tool_choice?(available_model_tools)
  available_model_tools.present?
end

#supports_provider_managed_tool?(tool_klass) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'app/models/raif/llm.rb', line 161

def supports_provider_managed_tool?(tool_klass)
  supported_provider_managed_tools&.include?(tool_klass.to_s)
end

#validate_provider_managed_tool_support!(tool) ⇒ Object



187
188
189
190
191
192
# File 'app/models/raif/llm.rb', line 187

def validate_provider_managed_tool_support!(tool)
  unless supports_provider_managed_tool?(tool)
    raise Raif::Errors::UnsupportedFeatureError,
      "Invalid provider-managed tool: #{tool.name} for #{key}"
  end
end