2024-07-02 11:51:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AiSummary < ActiveRecord::Base
|
|
|
|
belongs_to :target, polymorphic: true
|
|
|
|
|
2024-10-15 12:53:26 -04:00
|
|
|
enum :summary_type, { complete: 0, gist: 1 }
|
2024-11-04 15:48:11 -05:00
|
|
|
enum :origin, { human: 0, system: 1 }
|
|
|
|
|
|
|
|
def self.store!(strategy, llm_model, summary, og_content, human:)
|
|
|
|
content_ids = og_content.map { |c| c[:id] }
|
2024-10-15 12:53:26 -04:00
|
|
|
|
|
|
|
AiSummary.create!(
|
2024-11-04 15:48:11 -05:00
|
|
|
target: strategy.target,
|
|
|
|
algorithm: llm_model.name,
|
2024-10-15 12:53:26 -04:00
|
|
|
content_range: (content_ids.first..content_ids.last),
|
|
|
|
summarized_text: summary,
|
|
|
|
original_content_sha: build_sha(content_ids.join),
|
2024-11-04 15:48:11 -05:00
|
|
|
summary_type: strategy.type,
|
|
|
|
origin: !!human ? origins[:human] : origins[:system],
|
2024-10-15 12:53:26 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.build_sha(joined_ids)
|
|
|
|
Digest::SHA256.hexdigest(joined_ids)
|
|
|
|
end
|
|
|
|
|
2024-07-02 11:51:59 -04:00
|
|
|
def mark_as_outdated
|
|
|
|
@outdated = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def outdated
|
|
|
|
@outdated || false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: ai_summaries
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# target_id :integer not null
|
|
|
|
# target_type :string not null
|
|
|
|
# content_range :int4range
|
|
|
|
# summarized_text :string not null
|
|
|
|
# original_content_sha :string not null
|
|
|
|
# algorithm :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2024-10-28 10:07:09 -04:00
|
|
|
# summary_type :integer default("complete"), not null
|
2024-11-04 15:48:11 -05:00
|
|
|
# origin :integer
|
2024-07-02 11:51:59 -04:00
|
|
|
#
|
2024-07-03 22:23:46 -04:00
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_ai_summaries_on_target_type_and_target_id (target_type,target_id)
|
|
|
|
#
|