2023-03-31 14:29:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Embeddings
|
|
|
|
class SemanticSearch
|
2023-09-05 10:08:23 -04:00
|
|
|
def self.clear_cache_for(query)
|
|
|
|
digest = OpenSSL::Digest::SHA1.hexdigest(query)
|
|
|
|
|
2023-09-05 17:39:39 -04:00
|
|
|
hyde_key =
|
|
|
|
"semantic-search-#{digest}-#{SiteSetting.ai_embeddings_semantic_search_hyde_model}"
|
|
|
|
|
|
|
|
Discourse.cache.delete(hyde_key)
|
|
|
|
Discourse.cache.delete("#{hyde_key}-#{SiteSetting.ai_embeddings_model}")
|
2023-09-05 10:08:23 -04:00
|
|
|
end
|
|
|
|
|
2023-07-13 11:41:36 -04:00
|
|
|
def initialize(guardian)
|
2023-03-31 14:29:56 -04:00
|
|
|
@guardian = guardian
|
2023-09-05 10:08:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cached_query?(query)
|
|
|
|
digest = OpenSSL::Digest::SHA1.hexdigest(query)
|
2023-09-05 17:39:39 -04:00
|
|
|
embedding_key =
|
|
|
|
build_embedding_key(
|
|
|
|
digest,
|
|
|
|
SiteSetting.ai_embeddings_semantic_search_hyde_model,
|
|
|
|
SiteSetting.ai_embeddings_model,
|
|
|
|
)
|
|
|
|
|
|
|
|
Discourse.cache.read(embedding_key).present?
|
2023-03-31 14:29:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def search_for_topics(query, page = 1)
|
2023-09-12 15:16:33 -04:00
|
|
|
max_results_per_page = 100
|
2023-09-05 10:08:23 -04:00
|
|
|
limit = [Search.per_filter, max_results_per_page].min + 1
|
|
|
|
offset = (page - 1) * limit
|
2023-09-12 15:16:33 -04:00
|
|
|
search = Search.new(query, { guardian: guardian })
|
|
|
|
search_term = search.term
|
2023-09-05 10:08:23 -04:00
|
|
|
|
2023-11-22 14:05:56 -05:00
|
|
|
return [] if search_term.nil? || search_term.length < SiteSetting.min_search_term_length
|
|
|
|
|
2023-09-05 10:08:23 -04:00
|
|
|
strategy = DiscourseAi::Embeddings::Strategies::Truncation.new
|
|
|
|
vector_rep =
|
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::Base.current_representation(strategy)
|
|
|
|
|
2023-09-12 15:16:33 -04:00
|
|
|
digest = OpenSSL::Digest::SHA1.hexdigest(search_term)
|
2023-09-05 17:39:39 -04:00
|
|
|
hyde_key = build_hyde_key(digest, SiteSetting.ai_embeddings_semantic_search_hyde_model)
|
|
|
|
|
|
|
|
embedding_key =
|
|
|
|
build_embedding_key(
|
|
|
|
digest,
|
|
|
|
SiteSetting.ai_embeddings_semantic_search_hyde_model,
|
|
|
|
SiteSetting.ai_embeddings_model,
|
|
|
|
)
|
2023-03-31 14:29:56 -04:00
|
|
|
|
2023-09-05 10:08:23 -04:00
|
|
|
hypothetical_post =
|
|
|
|
Discourse
|
|
|
|
.cache
|
2023-11-23 10:58:54 -05:00
|
|
|
.fetch(hyde_key, expires_in: 1.week) { hypothetical_post_from(search_term) }
|
2023-09-05 10:08:23 -04:00
|
|
|
|
|
|
|
hypothetical_post_embedding =
|
|
|
|
Discourse
|
|
|
|
.cache
|
2023-09-05 17:39:39 -04:00
|
|
|
.fetch(embedding_key, expires_in: 1.week) { vector_rep.vector_from(hypothetical_post) }
|
2023-09-05 10:08:23 -04:00
|
|
|
|
|
|
|
candidate_topic_ids =
|
|
|
|
vector_rep.asymmetric_topics_similarity_search(
|
|
|
|
hypothetical_post_embedding,
|
|
|
|
limit: limit,
|
|
|
|
offset: offset,
|
|
|
|
)
|
2023-03-31 14:29:56 -04:00
|
|
|
|
2023-09-06 09:00:20 -04:00
|
|
|
semantic_results =
|
|
|
|
::Post
|
|
|
|
.where(post_type: ::Topic.visible_post_types(guardian.user))
|
|
|
|
.public_posts
|
|
|
|
.where("topics.visible")
|
|
|
|
.where(topic_id: candidate_topic_ids, post_number: 1)
|
|
|
|
.order("array_position(ARRAY#{candidate_topic_ids}, topic_id)")
|
|
|
|
|
2023-09-12 15:16:33 -04:00
|
|
|
query_filter_results = search.apply_filters(semantic_results)
|
|
|
|
|
|
|
|
guardian.filter_allowed_categories(query_filter_results)
|
2023-07-13 11:41:36 -04:00
|
|
|
end
|
|
|
|
|
2023-03-31 14:29:56 -04:00
|
|
|
private
|
|
|
|
|
2023-09-05 10:08:23 -04:00
|
|
|
attr_reader :guardian
|
2023-09-05 17:39:39 -04:00
|
|
|
|
|
|
|
def build_hyde_key(digest, hyde_model)
|
|
|
|
"semantic-search-#{digest}-#{hyde_model}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_embedding_key(digest, hyde_model, embedding_model)
|
|
|
|
"#{build_hyde_key(digest, hyde_model)}-#{embedding_model}"
|
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
|
|
|
|
def hypothetical_post_from(search_term)
|
2024-01-12 12:36:44 -05:00
|
|
|
prompt = DiscourseAi::Completions::Prompt.new(<<~TEXT.strip)
|
2023-11-23 10:58:54 -05:00
|
|
|
You are a content creator for a forum. The forum description is as follows:
|
|
|
|
#{SiteSetting.title}
|
|
|
|
#{SiteSetting.site_description}
|
2024-01-12 12:36:44 -05:00
|
|
|
|
|
|
|
Put the forum post between <ai></ai> tags.
|
2023-11-23 10:58:54 -05:00
|
|
|
TEXT
|
2024-01-12 12:36:44 -05:00
|
|
|
|
|
|
|
prompt.push(type: :user, content: <<~TEXT.strip)
|
2023-11-23 10:58:54 -05:00
|
|
|
Using this description, write a forum post about the subject inside the <input></input> XML tags:
|
2023-11-28 23:17:46 -05:00
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
<input>#{search_term}</input>
|
|
|
|
TEXT
|
|
|
|
|
|
|
|
llm_response =
|
2023-11-28 23:17:46 -05:00
|
|
|
DiscourseAi::Completions::Llm.proxy(
|
2023-11-23 10:58:54 -05:00
|
|
|
SiteSetting.ai_embeddings_semantic_search_hyde_model,
|
2024-01-04 07:53:47 -05:00
|
|
|
).generate(prompt, user: @guardian.user)
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2023-12-07 08:36:56 -05:00
|
|
|
Nokogiri::HTML5.fragment(llm_response).at("ai")&.text&.presence || llm_response
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
2023-03-31 14:29:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|