From 4ad887c8353b2c426bf60536343b5a67684852a0 Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Fri, 16 May 2025 15:18:33 -0300 Subject: [PATCH] cleaning up --- lib/inferred_concepts/applier.rb | 47 ++++++++++++++------------------ lib/inferred_concepts/manager.rb | 27 +----------------- 2 files changed, 22 insertions(+), 52 deletions(-) diff --git a/lib/inferred_concepts/applier.rb b/lib/inferred_concepts/applier.rb index ace1bd74..1a2f4c4a 100644 --- a/lib/inferred_concepts/applier.rb +++ b/lib/inferred_concepts/applier.rb @@ -9,15 +9,7 @@ module DiscourseAi def self.apply_to_topic(topic, concepts) return if topic.blank? || concepts.blank? - concepts.each do |concept| - # Use the join table to associate the concept with the topic - # Avoid duplicates by using find_or_create_by - ActiveRecord::Base.connection.execute(<<~SQL) - INSERT INTO topics_inferred_concepts (topic_id, inferred_concept_id, created_at, updated_at) - VALUES (#{topic.id}, #{concept.id}, NOW(), NOW()) - ON CONFLICT (topic_id, inferred_concept_id) DO NOTHING - SQL - end + topic.inferred_concepts << concepts end # Associates the provided concepts with a post @@ -26,15 +18,7 @@ module DiscourseAi def self.apply_to_post(post, concepts) return if post.blank? || concepts.blank? - concepts.each do |concept| - # Use the join table to associate the concept with the post - # Avoid duplicates by using find_or_create_by - ActiveRecord::Base.connection.execute(<<~SQL) - INSERT INTO posts_inferred_concepts (post_id, inferred_concept_id, created_at, updated_at) - VALUES (#{post.id}, #{concept.id}, NOW(), NOW()) - ON CONFLICT (post_id, inferred_concept_id) DO NOTHING - SQL - end + post.inferred_concepts << concepts end # Extracts content from a topic for concept analysis @@ -119,21 +103,32 @@ module DiscourseAi user_message = content # Use the ConceptMatcher persona to match concepts - llm = DiscourseAi::Completions::Llm.default_llm - persona = DiscourseAi::Personas::ConceptMatcher.new + + persona = + AiPersona + .all_personas(enabled_only: false) + .find { |persona| persona.id == SiteSetting.inferred_concepts_match_persona.to_i } + .new + + llm = LlmModel.find(persona.class.default_llm_id) + + input = { type: :user, content: content } + context = DiscourseAi::Personas::BotContext.new( - messages: [{ type: :user, content: user_message }], + messages: [input], user: Discourse.system_user, - inferred_concepts: DiscourseAi::InferredConcepts::Manager.list_concepts, + inferred_concepts: concept_list, ) - prompt = persona.craft_prompt(context) - response = llm.completion(prompt, extract_json: true) + bot = DiscourseAi::Personas::Bot.as(Discourse.system_user, persona: persona, model: llm) - return [] unless response.success? + response = bot.reply(context) + + debugger + + matching_concepts = JSON.parse(response[0][0]).dig("matching_concepts") - matching_concepts = response.parsed_output["matching_concepts"] matching_concepts || [] end end diff --git a/lib/inferred_concepts/manager.rb b/lib/inferred_concepts/manager.rb index 9af0bf19..f1e1e061 100644 --- a/lib/inferred_concepts/manager.rb +++ b/lib/inferred_concepts/manager.rb @@ -73,24 +73,6 @@ module DiscourseAi end end - # Generate new concepts for a topic and apply them - # @param topic [Topic] A Topic instance - # @return [Array] The concepts that were applied - def self.analyze_topic(topic) - return [] if topic.blank? - - Applier.analyze_and_apply(topic) - end - - # Generate new concepts for a post and apply them - # @param post [Post] A Post instance - # @return [Array] The concepts that were applied - def self.analyze_post(post) - return [] if post.blank? - - Applier.analyze_and_apply_post(post) - end - # Extract new concepts from arbitrary content # @param content [String] The content to analyze # @return [Array] The identified concept names @@ -205,14 +187,7 @@ module DiscourseAi # @param opts [Hash] Options to pass to the finder # @return [Array] Array of Post objects that are good candidates def self.find_candidate_posts(opts = {}) - Finder.find_candidate_posts( - limit: opts[:limit], - min_likes: opts[:min_likes], - exclude_first_posts: opts[:exclude_first_posts], - exclude_post_ids: opts[:exclude_post_ids], - category_ids: opts[:category_ids], - created_after: opts[:created_after], - ) + Finder.find_candidate_posts(**opts) end end end