Class: Raif::ModelCompletion

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

Overview

Schema Information

Table name: raif_model_completions

id                        :bigint           not null, primary key
available_model_tools     :jsonb            not null
citations                 :jsonb
completion_tokens         :integer
llm_model_key             :string           not null
max_completion_tokens     :integer
messages                  :jsonb            not null
model_api_name            :string           not null
output_token_cost         :decimal(10, 6)
prompt_token_cost         :decimal(10, 6)
prompt_tokens             :integer
raw_response              :text
response_array            :jsonb
response_format           :integer          default("text"), not null
response_format_parameter :string
response_tool_calls       :jsonb
retry_count               :integer          default(0), not null
source_type               :string
stream_response           :boolean          default(FALSE), not null
system_prompt             :text
temperature               :decimal(5, 3)
total_cost                :decimal(10, 6)
total_tokens              :integer
created_at                :datetime         not null
updated_at                :datetime         not null
response_id               :string
source_id                 :bigint

Indexes

index_raif_model_completions_on_created_at  (created_at)
index_raif_model_completions_on_source      (source_type,source_id)

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



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/raif/model_completion.rb', line 67

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



59
60
61
# File 'app/models/raif/model_completion.rb', line 59

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

#set_total_tokensObject



63
64
65
# File 'app/models/raif/model_completion.rb', line 63

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