2023-03-15 16:21:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Embeddings
|
|
|
|
class EntryPoint
|
|
|
|
def inject_into(plugin)
|
2023-07-31 17:33:37 -04:00
|
|
|
# Include random topics in the suggested list *only* if there are no related topics.
|
|
|
|
plugin.register_modifier(
|
|
|
|
:topic_view_suggested_topics_options,
|
|
|
|
) do |suggested_options, topic_view|
|
|
|
|
related_topics = topic_view.related_topics
|
2024-04-14 19:31:07 -04:00
|
|
|
include_random = !related_topics || related_topics.topics.length == 0
|
2023-07-31 17:33:37 -04:00
|
|
|
suggested_options.merge(include_random: include_random)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Query and serialize related topics.
|
2023-03-30 18:07:22 -04:00
|
|
|
plugin.add_to_class(:topic_view, :related_topics) do
|
2023-03-30 20:04:34 -04:00
|
|
|
if topic.private_message? || !SiteSetting.ai_embeddings_semantic_related_topics_enabled
|
2023-03-30 18:07:22 -04:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2023-08-08 14:44:10 -04:00
|
|
|
@related_topics ||=
|
2024-04-14 19:31:07 -04:00
|
|
|
::DiscourseAi::Embeddings::SemanticTopicQuery.new(@user).list_semantic_related_topics(
|
|
|
|
topic,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# define_method must be used (instead of add_to_class) to make sure
|
|
|
|
# that method still works when plugin is disabled too
|
|
|
|
TopicView.alias_method(:categories_old, :categories)
|
|
|
|
TopicView.define_method(:categories) do
|
|
|
|
@categories ||= [*categories_old, *related_topics&.categories].flatten.uniq.compact
|
2023-03-30 18:07:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
%i[topic_view TopicViewPosts].each do |serializer|
|
2023-04-24 11:07:17 -04:00
|
|
|
plugin.add_to_serializer(
|
|
|
|
serializer,
|
|
|
|
:related_topics,
|
|
|
|
include_condition: -> { SiteSetting.ai_embeddings_semantic_related_topics_enabled },
|
|
|
|
) do
|
2023-03-30 20:04:34 -04:00
|
|
|
if object.next_page.nil? && !object.topic.private_message?
|
2024-04-14 19:31:07 -04:00
|
|
|
object.related_topics.topics.map do |t|
|
2023-03-30 18:07:22 -04:00
|
|
|
SuggestedTopicSerializer.new(t, scope: scope, root: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-28 13:32:03 -05:00
|
|
|
plugin.register_html_builder("server:topic-show-after-posts-crawler") do |controller|
|
2024-01-10 22:52:50 -05:00
|
|
|
::DiscourseAi::Embeddings::SemanticRelated.related_topics_for_crawler(controller)
|
2023-12-28 13:32:03 -05:00
|
|
|
end
|
|
|
|
|
2023-07-31 17:33:37 -04:00
|
|
|
# embeddings generation.
|
2023-03-15 16:21:45 -04:00
|
|
|
callback =
|
2023-12-29 10:28:45 -05:00
|
|
|
Proc.new do |target|
|
2024-01-23 20:09:27 -05:00
|
|
|
if SiteSetting.ai_embeddings_enabled &&
|
|
|
|
(target.is_a?(Topic) || SiteSetting.ai_embeddings_per_post_enabled)
|
2023-12-29 10:28:45 -05:00
|
|
|
Jobs.enqueue(
|
|
|
|
:generate_embeddings,
|
|
|
|
target_id: target.id,
|
|
|
|
target_type: target.class.name,
|
|
|
|
)
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
plugin.on(:topic_created, &callback)
|
|
|
|
plugin.on(:topic_edited, &callback)
|
2023-12-29 10:28:45 -05:00
|
|
|
plugin.on(:post_created, &callback)
|
|
|
|
plugin.on(:post_edited, &callback)
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|