generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 36
FEATURE: add inferred concepts system #1330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
xfalcox
wants to merge
11
commits into
main
Choose a base branch
from
inferred-concepts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fb0d364
FEATURE: add inferred concepts system
xfalcox 5f0d682
FEATURE: Extend inferred concepts to include posts
xfalcox f525947
lint
xfalcox a8644fa
small fixes
xfalcox 4280255
tests
xfalcox 5b353bd
pass context
xfalcox 1c4806b
dots are not commas
xfalcox 0664ec5
Dedup concepts
xfalcox 1db2ae8
Working deduplication
xfalcox 4ad887c
cleaning up
xfalcox 74d47a9
post rebase fixes
xfalcox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"permissions": { | ||
"allow": [ | ||
"Bash(bundle exec rails g migration:*)" | ||
], | ||
"deny": [] | ||
} | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module Jobs | ||
class GenerateInferredConcepts < ::Jobs::Base | ||
sidekiq_options queue: "low" | ||
|
||
# Process items to generate new concepts | ||
# | ||
# @param args [Hash] Contains job arguments | ||
# @option args [String] :item_type Required - Type of items to process ('topics' or 'posts') | ||
# @option args [Array<Integer>] :item_ids Required - List of item IDs to process | ||
# @option args [Integer] :batch_size (100) Number of items to process in each batch | ||
# @option args [Boolean] :match_only (false) Only match against existing concepts without generating new ones | ||
def execute(args = {}) | ||
return if args[:item_ids].blank? || args[:item_type].blank? | ||
|
||
if %w[topics posts].exclude?(args[:item_type]) | ||
Rails.logger.error("Invalid item_type for GenerateInferredConcepts: #{args[:item_type]}") | ||
return | ||
end | ||
|
||
# Process items in smaller batches to avoid memory issues | ||
batch_size = args[:batch_size] || 100 | ||
|
||
# Get the list of item IDs | ||
item_ids = args[:item_ids] | ||
match_only = args[:match_only] || false | ||
|
||
# Process items in batches | ||
item_ids.each_slice(batch_size) do |batch_item_ids| | ||
process_batch(batch_item_ids, args[:item_type], match_only) | ||
end | ||
end | ||
|
||
private | ||
|
||
def process_batch(item_ids, item_type, match_only) | ||
klass = item_type.singularize.classify.constantize | ||
items = klass.where(id: item_ids) | ||
|
||
items.each do |item| | ||
begin | ||
process_item(item, item_type, match_only) | ||
rescue => e | ||
Rails.logger.error( | ||
"Error generating concepts from #{item_type.singularize} #{item.id}: #{e.message}\n#{e.backtrace.join("\n")}", | ||
) | ||
end | ||
end | ||
end | ||
|
||
def process_item(item, item_type, match_only) | ||
# Use the Manager method that handles both identifying and creating concepts | ||
if match_only | ||
if item_type == "topics" | ||
DiscourseAi::InferredConcepts::Manager.match_topic_to_concepts(item) | ||
else # posts | ||
DiscourseAi::InferredConcepts::Manager.match_post_to_concepts(item) | ||
end | ||
else | ||
if item_type == "topics" | ||
DiscourseAi::InferredConcepts::Manager.analyze_topic(item) | ||
else # posts | ||
DiscourseAi::InferredConcepts::Manager.analyze_post(item) | ||
end | ||
end | ||
end | ||
end | ||
end |
85 changes: 85 additions & 0 deletions
85
app/jobs/scheduled/generate_concepts_from_popular_items.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# frozen_string_literal: true | ||
|
||
module Jobs | ||
class GenerateConceptsFromPopularItems < ::Jobs::Scheduled | ||
every 1.day | ||
|
||
# This job runs daily and generates new concepts from popular topics and posts | ||
# It selects items based on engagement metrics and generates concepts from their content | ||
def execute(_args) | ||
return unless SiteSetting.inferred_concepts_enabled | ||
|
||
process_popular_topics | ||
process_popular_posts | ||
end | ||
|
||
private | ||
|
||
def process_popular_topics | ||
# Find candidate topics that are popular and don't have concepts yet | ||
candidates = | ||
DiscourseAi::InferredConcepts::Manager.find_candidate_topics( | ||
limit: SiteSetting.inferred_concepts_daily_topics_limit || 20, | ||
min_posts: SiteSetting.inferred_concepts_min_posts || 5, | ||
min_likes: SiteSetting.inferred_concepts_min_likes || 10, | ||
min_views: SiteSetting.inferred_concepts_min_views || 100, | ||
created_after: SiteSetting.inferred_concepts_lookback_days.days.ago, | ||
) | ||
|
||
return if candidates.blank? | ||
|
||
# Process candidate topics - first generate concepts, then match | ||
Jobs.enqueue( | ||
:generate_inferred_concepts, | ||
item_type: "topics", | ||
item_ids: candidates.map(&:id), | ||
batch_size: 10, | ||
) | ||
|
||
if SiteSetting.inferred_concepts_background_match | ||
# Schedule a follow-up job to match existing concepts | ||
Jobs.enqueue_in( | ||
1.hour, | ||
:generate_inferred_concepts, | ||
item_type: "topics", | ||
item_ids: candidates.map(&:id), | ||
batch_size: 10, | ||
match_only: true, | ||
) | ||
end | ||
end | ||
|
||
def process_popular_posts | ||
# Find candidate posts that are popular and don't have concepts yet | ||
candidates = | ||
DiscourseAi::InferredConcepts::Manager.find_candidate_posts( | ||
limit: SiteSetting.inferred_concepts_daily_posts_limit || 30, | ||
min_likes: SiteSetting.inferred_concepts_post_min_likes || 5, | ||
exclude_first_posts: true, | ||
created_after: SiteSetting.inferred_concepts_lookback_days.days.ago, | ||
) | ||
|
||
return if candidates.blank? | ||
|
||
# Process candidate posts - first generate concepts, then match | ||
Jobs.enqueue( | ||
:generate_inferred_concepts, | ||
item_type: "posts", | ||
item_ids: candidates.map(&:id), | ||
batch_size: 10, | ||
) | ||
|
||
if SiteSetting.inferred_concepts_background_match | ||
# Schedule a follow-up job to match against existing concepts | ||
Jobs.enqueue_in( | ||
1.hour, | ||
:generate_inferred_concepts, | ||
item_type: "posts", | ||
item_ids: candidates.map(&:id), | ||
batch_size: 10, | ||
match_only: true, | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
class InferredConcept < ActiveRecord::Base | ||
has_and_belongs_to_many :topics | ||
has_and_belongs_to_many :posts | ||
|
||
validates :name, presence: true, uniqueness: true | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: inferred_concepts | ||
# | ||
# id :bigint not null, primary key | ||
# name :string not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_inferred_concepts_on_name (name) UNIQUE | ||
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
class AiInferredConceptPostSerializer < ApplicationSerializer | ||
attributes :id, | ||
:post_number, | ||
:topic_id, | ||
:topic_title, | ||
:username, | ||
:avatar_template, | ||
:created_at, | ||
:updated_at, | ||
:excerpt, | ||
:truncated, | ||
:inferred_concepts | ||
|
||
def avatar_template | ||
User.avatar_template(object.username, object.uploaded_avatar_id) | ||
end | ||
|
||
def excerpt | ||
Post.excerpt(object.cooked) | ||
end | ||
|
||
def truncated | ||
object.cooked.length > SiteSetting.post_excerpt_maxlength | ||
end | ||
|
||
def inferred_concepts | ||
ActiveModel::ArraySerializer.new( | ||
object.inferred_concepts, | ||
each_serializer: InferredConceptSerializer | ||
) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class InferredConceptSerializer < ApplicationSerializer | ||
attributes :id, :name, :created_at, :updated_at | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
db/migrate/20250508182047_create_inferred_concepts_table.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
class CreateInferredConceptsTable < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :inferred_concepts do |t| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not using polymorphic association here instead of two more tables? |
||
t.string :name, null: false | ||
t.timestamps | ||
end | ||
|
||
add_index :inferred_concepts, :name, unique: true | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20250508183456_create_inferred_concepts_topics.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateInferredConceptsTopics < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :inferred_concepts_topics, id: false do |t| | ||
t.belongs_to :inferred_concept | ||
t.belongs_to :topic | ||
t.timestamps | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrate/20250509000001_create_inferred_concepts_posts.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateInferredConceptsPosts < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :inferred_concepts_posts, id: false do |t| | ||
t.belongs_to :inferred_concept | ||
t.belongs_to :post | ||
t.timestamps | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this?