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:, 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.



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

def initialize(
  key:,
  api_name:,
  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
  @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

#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

.valid_response_formatsObject



111
112
113
# File 'app/models/raif/llm.rb', line 111

def self.valid_response_formats
  VALID_RESPONSE_FORMATS
end

Instance Method Details

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



51
52
53
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
# File 'app/models/raif/llm.rb', line 51

def chat(message: nil, messages: nil, response_format: :text, available_model_tools: [], source: nil, system_prompt: nil, temperature: nil,
  max_completion_tokens: 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

  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.new(
    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,
    stream_response: block_given?
  )

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

  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}")
  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)
  raise e
end

#nameObject



47
48
49
# File 'app/models/raif/llm.rb', line 47

def name
  I18n.t("raif.model_names.#{key}")
end

#perform_model_completion!(model_completion, &block) ⇒ Object

Raises:

  • (NotImplementedError)


107
108
109
# File 'app/models/raif/llm.rb', line 107

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

#supports_provider_managed_tool?(tool_klass) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/models/raif/llm.rb', line 115

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

#validate_provider_managed_tool_support!(tool) ⇒ Object



119
120
121
122
123
124
# File 'app/models/raif/llm.rb', line 119

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