Class: Raif::ModelCompletion
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Raif::ModelCompletion
- Includes:
- Concerns::BooleanTimestamp, Concerns::HasAvailableModelTools, Concerns::HasRuntimeDuration, Concerns::LlmResponseParsing, Concerns::ProviderManagedToolCalls
- 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
cache_creation_input_tokens :integer
cache_read_input_tokens :integer
citations :jsonb
completed_at :datetime
completion_tokens :integer
failed_at :datetime
failure_error :string
failure_reason :text
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
started_at :datetime
stream_response :boolean default(FALSE), not null
system_prompt :text
temperature :decimal(5, 3)
tool_choice :string
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_completed_at (completed_at)
index_raif_model_completions_on_created_at (created_at)
index_raif_model_completions_on_failed_at (failed_at)
index_raif_model_completions_on_source (source_type,source_id)
index_raif_model_completions_on_started_at (started_at)
Constant Summary
Constants included from Concerns::LlmResponseParsing
Concerns::LlmResponseParsing::ASCII_CONTROL_CHARS
Instance Method Summary collapse
- #calculate_costs ⇒ Object
- #json_response_schema ⇒ Object
- #record_failure!(exception) ⇒ Object
- #set_total_tokens ⇒ Object
Methods included from Concerns::ProviderManagedToolCalls
#provider_managed_tool_calls, #sanitized_citations
Methods included from Concerns::HasRuntimeDuration
#runtime_duration, #runtime_duration_seconds, #runtime_ended_at
Methods included from Concerns::HasAvailableModelTools
Methods included from Concerns::LlmResponseParsing
#parse_html_response, #parse_json_response, #parsed_response
Methods inherited from ApplicationRecord
table_name_prefix, where_json_not_blank
Instance Method Details
#calculate_costs ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/raif/model_completion.rb', line 88 def calculate_costs # Each retry resends the same prompt, so the provider charges input tokens # for every attempt. Factor in retry_count to reflect actual billing. total_attempts = (retry_count || 0) + 1 if prompt_tokens.present? && llm_config[:input_token_cost].present? self.prompt_token_cost = calculate_prompt_token_cost(total_attempts) 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_schema ⇒ Object
80 81 82 |
# File 'app/models/raif/model_completion.rb', line 80 def json_response_schema source.json_response_schema if source&.respond_to?(:json_response_schema) end |
#record_failure!(exception) ⇒ Object
106 107 108 109 110 111 |
# File 'app/models/raif/model_completion.rb', line 106 def record_failure!(exception) self.failed_at = Time.current self.failure_error = exception.class.name self.failure_reason = exception..truncate(255) save! end |
#set_total_tokens ⇒ Object
84 85 86 |
# File 'app/models/raif/model_completion.rb', line 84 def set_total_tokens self.total_tokens ||= completion_tokens.present? && prompt_tokens.present? ? completion_tokens + prompt_tokens : nil end |