ensure fragments regenerated on save

This commit is contained in:
Sam Saffron 2024-09-18 14:50:09 +10:00
parent f40355c890
commit 1b6b7d8675
No known key found for this signature in database
GPG Key ID: B9606168D2FFD9F5
2 changed files with 28 additions and 0 deletions

View File

@ -10,6 +10,7 @@ class AiTool < ActiveRecord::Base
has_many :rag_document_fragments, dependent: :destroy, as: :target
has_many :upload_references, as: :target, dependent: :destroy
has_many :uploads, through: :upload_references
before_update :regenerate_rag_fragments
def signature
{ name: name, description: description, parameters: parameters.map(&:symbolize_keys) }
@ -31,6 +32,12 @@ class AiTool < ActiveRecord::Base
AiPersona.persona_cache.flush!
end
def regenerate_rag_fragments
if rag_chunk_tokens_changed? || rag_chunk_overlap_tokens_changed?
RagDocumentFragment.where(target: self).delete_all
end
end
def self.preamble
<<~JS
/**

View File

@ -268,6 +268,27 @@ RSpec.describe AiTool do
]
expect(result).to eq(expected)
# will force a reindex
tool.rag_chunk_tokens = 5
tool.rag_chunk_overlap_tokens = 2
tool.save!
# this part of the API is a bit awkward, maybe we should do it
# automatically
RagDocumentFragment.update_target_uploads(tool, [upload1.id, upload2.id])
result = tool.runner({}, llm: nil, bot_user: nil, context: {}).invoke
expected = [
[{ "fragment" => "48 49 50", "metadata" => nil }],
[
{ "fragment" => "48 49 50", "metadata" => nil },
{ "fragment" => "45 46 47", "metadata" => nil },
{ "fragment" => "42 43 44", "metadata" => nil },
],
]
expect(result).to eq(expected)
end
end
end