Add cache for embed in queries

This commit is contained in:
Jorge Manrubia
2025-05-13 13:01:47 +02:00
parent 21a6df093f
commit b568711809
2 changed files with 3 additions and 3 deletions
+2 -2
View File
@@ -8,8 +8,8 @@ module Card::Searchable
scope :mentioning, ->(query) do
if query = sanitize_query_syntax(query)
cards = Card.search(query).select(:id).to_sql
comments = Comment.search(query).select(:id).to_sql
cards = Card.search_similar(query).select(:id).to_sql
comments = Comment.search_similar(query).select(:id).to_sql
left_joins(:comments).where("cards.id in (#{cards}) or comments.id in (#{comments})").distinct
else
+1 -1
View File
@@ -21,7 +21,7 @@ module Searchable
scope :search, ->(query) { joins("join #{using} idx on #{table_name}.id = idx.rowid").where("idx.#{as} match ?", query) }
scope :search_similar, ->(query) do
query_embedding = RubyLLM.embed(query)
query_embedding = Rails.cache.fetch("llm:#{query}") { RubyLLM.embed(query) }
joins(:search_embedding)
.where("embedding MATCH ? AND k = ?", query_embedding.vectors.to_json, 20)
.order(:distance)