@@ -9,15 +9,7 @@ class Applier
99 def self . apply_to_topic ( topic , concepts )
1010 return if topic . blank? || concepts . blank?
1111
12- concepts . each do |concept |
13- # Use the join table to associate the concept with the topic
14- # Avoid duplicates by using find_or_create_by
15- ActiveRecord ::Base . connection . execute ( <<~SQL )
16- INSERT INTO topics_inferred_concepts (topic_id, inferred_concept_id, created_at, updated_at)
17- VALUES (#{ topic . id } , #{ concept . id } , NOW(), NOW())
18- ON CONFLICT (topic_id, inferred_concept_id) DO NOTHING
19- SQL
20- end
12+ topic . inferred_concepts << concepts
2113 end
2214
2315 # Associates the provided concepts with a post
@@ -26,15 +18,7 @@ def self.apply_to_topic(topic, concepts)
2618 def self . apply_to_post ( post , concepts )
2719 return if post . blank? || concepts . blank?
2820
29- concepts . each do |concept |
30- # Use the join table to associate the concept with the post
31- # Avoid duplicates by using find_or_create_by
32- ActiveRecord ::Base . connection . execute ( <<~SQL )
33- INSERT INTO posts_inferred_concepts (post_id, inferred_concept_id, created_at, updated_at)
34- VALUES (#{ post . id } , #{ concept . id } , NOW(), NOW())
35- ON CONFLICT (post_id, inferred_concept_id) DO NOTHING
36- SQL
37- end
21+ post . inferred_concepts << concepts
3822 end
3923
4024 # Extracts content from a topic for concept analysis
@@ -119,21 +103,32 @@ def self.match_concepts_to_content(content, concept_list)
119103 user_message = content
120104
121105 # Use the ConceptMatcher persona to match concepts
122- llm = DiscourseAi ::Completions ::Llm . default_llm
123- persona = DiscourseAi ::Personas ::ConceptMatcher . new
106+
107+ persona =
108+ AiPersona
109+ . all_personas ( enabled_only : false )
110+ . find { |persona | persona . id == SiteSetting . inferred_concepts_match_persona . to_i }
111+ . new
112+
113+ llm = LlmModel . find ( persona . class . default_llm_id )
114+
115+ input = { type : :user , content : content }
116+
124117 context =
125118 DiscourseAi ::Personas ::BotContext . new (
126- messages : [ { type : :user , content : user_message } ] ,
119+ messages : [ input ] ,
127120 user : Discourse . system_user ,
128- inferred_concepts : DiscourseAi :: InferredConcepts :: Manager . list_concepts ,
121+ inferred_concepts : concept_list ,
129122 )
130123
131- prompt = persona . craft_prompt ( context )
132- response = llm . completion ( prompt , extract_json : true )
124+ bot = DiscourseAi ::Personas ::Bot . as ( Discourse . system_user , persona : persona , model : llm )
125+
126+ response = bot . reply ( context )
127+
128+ debugger
133129
134- return [ ] unless response . success?
130+ matching_concepts = JSON . parse ( response [ 0 ] [ 0 ] ) . dig ( "matching_concepts" )
135131
136- matching_concepts = response . parsed_output [ "matching_concepts" ]
137132 matching_concepts || [ ]
138133 end
139134 end
0 commit comments