11# frozen_string_literal: true
22
33class RagDocumentFragment < ActiveRecord ::Base
4+ # TODO Jan 2025 - remove
5+ self . ignored_columns = %i[ ai_persona_id ]
6+
47 belongs_to :upload
5- belongs_to :ai_persona
8+ belongs_to :target , polymorphic : true
69
710 class << self
8- def link_persona_and_uploads ( persona , upload_ids )
9- return if persona . blank?
11+ def link_target_and_uploads ( target , upload_ids )
12+ return if target . blank?
1013 return if upload_ids . blank?
1114 return if !SiteSetting . ai_embeddings_enabled?
1215
13- UploadReference . ensure_exist! ( upload_ids : upload_ids , target : persona )
16+ UploadReference . ensure_exist! ( upload_ids : upload_ids , target : target )
1417
1518 upload_ids . each do |upload_id |
16- Jobs . enqueue ( :digest_rag_upload , ai_persona_id : persona . id , upload_id : upload_id )
19+ Jobs . enqueue (
20+ :digest_rag_upload ,
21+ target_id : target . id ,
22+ target_type : target . class . to_s ,
23+ upload_id : upload_id ,
24+ )
1725 end
1826 end
1927
20- def update_persona_uploads ( persona , upload_ids )
21- return if persona . blank?
28+ def update_target_uploads ( target , upload_ids )
29+ return if target . blank?
2230 return if !SiteSetting . ai_embeddings_enabled?
2331
2432 if upload_ids . blank?
25- RagDocumentFragment . where ( ai_persona : persona ) . destroy_all
26- UploadReference . where ( target : persona ) . destroy_all
33+ RagDocumentFragment . where ( target : target ) . destroy_all
34+ UploadReference . where ( target : target ) . destroy_all
2735 else
28- RagDocumentFragment . where ( ai_persona : persona ) . where . not ( upload_id : upload_ids ) . destroy_all
29- link_persona_and_uploads ( persona , upload_ids )
36+ RagDocumentFragment . where ( target : target ) . where . not ( upload_id : upload_ids ) . destroy_all
37+ link_target_and_uploads ( target , upload_ids )
3038 end
3139 end
3240
@@ -37,18 +45,25 @@ def indexing_status(persona, uploads)
3745
3846 embeddings_table = vector_rep . rag_fragments_table_name
3947
40- results = DB . query ( <<~SQL , persona_id : persona . id , upload_ids : uploads . map ( &:id ) )
48+ results =
49+ DB . query (
50+ <<~SQL ,
4151 SELECT
4252 uploads.id,
4353 SUM(CASE WHEN (rdf.upload_id IS NOT NULL) THEN 1 ELSE 0 END) AS total,
4454 SUM(CASE WHEN (eft.rag_document_fragment_id IS NOT NULL) THEN 1 ELSE 0 END) as indexed,
4555 SUM(CASE WHEN (rdf.upload_id IS NOT NULL AND eft.rag_document_fragment_id IS NULL) THEN 1 ELSE 0 END) as left
4656 FROM uploads
47- LEFT OUTER JOIN rag_document_fragments rdf ON uploads.id = rdf.upload_id AND rdf.ai_persona_id = :persona_id
57+ LEFT OUTER JOIN rag_document_fragments rdf ON uploads.id = rdf.upload_id AND rdf.target_id = :target_id
58+ AND rdf.target_type = :target_type
4859 LEFT OUTER JOIN #{ embeddings_table } eft ON rdf.id = eft.rag_document_fragment_id
4960 WHERE uploads.id IN (:upload_ids)
5061 GROUP BY uploads.id
5162 SQL
63+ target_id : persona . id ,
64+ target_type : persona . class . to_s ,
65+ upload_ids : uploads . map ( &:id ) ,
66+ )
5267
5368 results . reduce ( { } ) do |acc , r |
5469 acc [ r . id ] = { total : r . total , indexed : r . indexed , left : r . left }
@@ -78,4 +93,10 @@ def publish_status(upload, status)
7893# created_at :datetime not null
7994# updated_at :datetime not null
8095# metadata :text
96+ # target_id :integer
97+ # target_type :string(800)
98+ #
99+ # Indexes
100+ #
101+ # index_rag_document_fragments_on_target_type_and_target_id (target_type,target_id)
81102#
0 commit comments