cleaning up
This commit is contained in:
parent
1db2ae8dd7
commit
4ad887c835
|
@ -9,15 +9,7 @@ module DiscourseAi
|
||||||
def self.apply_to_topic(topic, concepts)
|
def self.apply_to_topic(topic, concepts)
|
||||||
return if topic.blank? || concepts.blank?
|
return if topic.blank? || concepts.blank?
|
||||||
|
|
||||||
concepts.each do |concept|
|
topic.inferred_concepts << concepts
|
||||||
# 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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Associates the provided concepts with a post
|
# Associates the provided concepts with a post
|
||||||
|
@ -26,15 +18,7 @@ module DiscourseAi
|
||||||
def self.apply_to_post(post, concepts)
|
def self.apply_to_post(post, concepts)
|
||||||
return if post.blank? || concepts.blank?
|
return if post.blank? || concepts.blank?
|
||||||
|
|
||||||
concepts.each do |concept|
|
post.inferred_concepts << concepts
|
||||||
# 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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Extracts content from a topic for concept analysis
|
# Extracts content from a topic for concept analysis
|
||||||
|
@ -119,21 +103,32 @@ module DiscourseAi
|
||||||
user_message = content
|
user_message = content
|
||||||
|
|
||||||
# Use the ConceptMatcher persona to match concepts
|
# 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 =
|
context =
|
||||||
DiscourseAi::Personas::BotContext.new(
|
DiscourseAi::Personas::BotContext.new(
|
||||||
messages: [{ type: :user, content: user_message }],
|
messages: [input],
|
||||||
user: Discourse.system_user,
|
user: Discourse.system_user,
|
||||||
inferred_concepts: DiscourseAi::InferredConcepts::Manager.list_concepts,
|
inferred_concepts: concept_list,
|
||||||
)
|
)
|
||||||
|
|
||||||
prompt = persona.craft_prompt(context)
|
bot = DiscourseAi::Personas::Bot.as(Discourse.system_user, persona: persona, model: llm)
|
||||||
response = llm.completion(prompt, extract_json: true)
|
|
||||||
|
|
||||||
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 || []
|
matching_concepts || []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -73,24 +73,6 @@ module DiscourseAi
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Generate new concepts for a topic and apply them
|
|
||||||
# @param topic [Topic] A Topic instance
|
|
||||||
# @return [Array<InferredConcept>] 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<InferredConcept>] 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
|
# Extract new concepts from arbitrary content
|
||||||
# @param content [String] The content to analyze
|
# @param content [String] The content to analyze
|
||||||
# @return [Array<String>] The identified concept names
|
# @return [Array<String>] The identified concept names
|
||||||
|
@ -205,14 +187,7 @@ module DiscourseAi
|
||||||
# @param opts [Hash] Options to pass to the finder
|
# @param opts [Hash] Options to pass to the finder
|
||||||
# @return [Array<Post>] Array of Post objects that are good candidates
|
# @return [Array<Post>] Array of Post objects that are good candidates
|
||||||
def self.find_candidate_posts(opts = {})
|
def self.find_candidate_posts(opts = {})
|
||||||
Finder.find_candidate_posts(
|
Finder.find_candidate_posts(**opts)
|
||||||
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],
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue