Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 0e817a7

Browse files
committed
cleaning up
1 parent 6ad2f7b commit 0e817a7

File tree

2 files changed

+22
-52
lines changed

2 files changed

+22
-52
lines changed

lib/inferred_concepts/applier.rb

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/inferred_concepts/manager.rb

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,6 @@ def self.deduplicate_concepts_by_letter(per_letter_batch: 50, full_pass_batch: 1
7373
end
7474
end
7575

76-
# Generate new concepts for a topic and apply them
77-
# @param topic [Topic] A Topic instance
78-
# @return [Array<InferredConcept>] The concepts that were applied
79-
def self.analyze_topic(topic)
80-
return [] if topic.blank?
81-
82-
Applier.analyze_and_apply(topic)
83-
end
84-
85-
# Generate new concepts for a post and apply them
86-
# @param post [Post] A Post instance
87-
# @return [Array<InferredConcept>] The concepts that were applied
88-
def self.analyze_post(post)
89-
return [] if post.blank?
90-
91-
Applier.analyze_and_apply_post(post)
92-
end
93-
9476
# Extract new concepts from arbitrary content
9577
# @param content [String] The content to analyze
9678
# @return [Array<String>] The identified concept names
@@ -205,14 +187,7 @@ def self.find_candidate_topics(opts = {})
205187
# @param opts [Hash] Options to pass to the finder
206188
# @return [Array<Post>] Array of Post objects that are good candidates
207189
def self.find_candidate_posts(opts = {})
208-
Finder.find_candidate_posts(
209-
limit: opts[:limit],
210-
min_likes: opts[:min_likes],
211-
exclude_first_posts: opts[:exclude_first_posts],
212-
exclude_post_ids: opts[:exclude_post_ids],
213-
category_ids: opts[:category_ids],
214-
created_after: opts[:created_after],
215-
)
190+
Finder.find_candidate_posts(**opts)
216191
end
217192
end
218193
end

0 commit comments

Comments
 (0)