Class: Raif::ModelCompletion

Inherits:
ApplicationRecord show all
Includes:
Concerns::HasAvailableModelTools, Concerns::LlmResponseParsing
Defined in:
app/models/raif/model_completion.rb

Constant Summary

Constants included from Concerns::LlmResponseParsing

Concerns::LlmResponseParsing::ASCII_CONTROL_CHARS

Instance Method Summary collapse

Methods included from Concerns::HasAvailableModelTools

#available_model_tools_map

Methods included from Concerns::LlmResponseParsing

#parse_html_response, #parse_json_response, #parsed_response

Methods inherited from ApplicationRecord

table_name_prefix

Instance Method Details

#calculate_costsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/raif/model_completion.rb', line 30

def calculate_costs
  if prompt_tokens.present? && llm_config[:input_token_cost].present?
    self.prompt_token_cost = llm_config[:input_token_cost] * prompt_tokens
  end

  if completion_tokens.present? && llm_config[:output_token_cost].present?
    self.output_token_cost = llm_config[:output_token_cost] * completion_tokens
  end

  if prompt_token_cost.present? || output_token_cost.present?
    self.total_cost = (prompt_token_cost || 0) + (output_token_cost || 0)
  end
end

#json_response_schemaObject



22
23
24
# File 'app/models/raif/model_completion.rb', line 22

def json_response_schema
  source.json_response_schema if source&.respond_to?(:json_response_schema)
end

#set_total_tokensObject



26
27
28
# File 'app/models/raif/model_completion.rb', line 26

def set_total_tokens
  self.total_tokens ||= completion_tokens.present? && prompt_tokens.present? ? completion_tokens + prompt_tokens : nil
end