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

Commit 5500b1c

Browse files
committed
tests
1 parent a73e9e6 commit 5500b1c

21 files changed

+1585
-22
lines changed

app/jobs/regular/generate_inferred_concepts.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ def process_item(item, item_type, match_only)
5959
end
6060
else
6161
if item_type == "topics"
62-
DiscourseAi::InferredConcepts::Manager.analyze_topic(item)
62+
DiscourseAi::InferredConcepts::Manager.generate_concepts_from_topic(item)
6363
else # posts
64-
DiscourseAi::InferredConcepts::Manager.analyze_post(item)
64+
DiscourseAi::InferredConcepts::Manager.generate_concepts_from_post(item)
6565
end
6666
end
6767
end

app/models/inferred_concept.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# frozen_string_literal: true
22

33
class InferredConcept < ActiveRecord::Base
4-
has_and_belongs_to_many :topics
5-
has_and_belongs_to_many :posts
4+
has_many :inferred_concept_topics
5+
has_many :topics, through: :inferred_concept_topics
6+
7+
has_many :inferred_concept_posts
8+
has_many :posts, through: :inferred_concept_posts
69

710
validates :name, presence: true, uniqueness: true
811
end
@@ -19,4 +22,4 @@ class InferredConcept < ActiveRecord::Base
1922
# Indexes
2023
#
2124
# index_inferred_concepts_on_name (name) UNIQUE
22-
#
25+
#
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
class InferredConceptPost < ActiveRecord::Base
4+
self.table_name = "inferred_concepts_posts"
5+
6+
belongs_to :inferred_concept
7+
belongs_to :post
8+
9+
validates :inferred_concept_id, presence: true
10+
validates :post_id, presence: true
11+
validates :inferred_concept_id, uniqueness: { scope: :post_id }
12+
end
13+
14+
# == Schema Information
15+
#
16+
# Table name: inferred_concepts_posts
17+
#
18+
# inferred_concept_id :bigint not null
19+
# post_id :bigint not null
20+
# created_at :datetime not null
21+
# updated_at :datetime not null
22+
#
23+
# Indexes
24+
#
25+
# index_inferred_concepts_posts_uniqueness (post_id,inferred_concept_id) UNIQUE
26+
# index_inferred_concepts_posts_on_inferred_concept_id (inferred_concept_id)
27+
#
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
class InferredConceptTopic < ActiveRecord::Base
4+
self.table_name = "inferred_concepts_topics"
5+
6+
belongs_to :inferred_concept
7+
belongs_to :topic
8+
9+
validates :inferred_concept_id, presence: true
10+
validates :topic_id, presence: true
11+
validates :inferred_concept_id, uniqueness: { scope: :topic_id }
12+
end
13+
14+
# == Schema Information
15+
#
16+
# Table name: inferred_concepts_topics
17+
#
18+
# inferred_concept_id :bigint not null
19+
# topic_id :bigint not null
20+
# created_at :datetime not null
21+
# updated_at :datetime not null
22+
#
23+
# Indexes
24+
#
25+
# index_inferred_concepts_topics_uniqueness (topic_id,inferred_concept_id) UNIQUE
26+
# index_inferred_concepts_topics_on_inferred_concept_id (inferred_concept_id)
27+
#

app/serializers/ai_inferred_concept_post_serializer.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ class AiInferredConceptPostSerializer < ApplicationSerializer
1212
:excerpt,
1313
:truncated,
1414
:inferred_concepts
15-
15+
1616
def avatar_template
1717
User.avatar_template(object.username, object.uploaded_avatar_id)
1818
end
19-
19+
2020
def excerpt
2121
Post.excerpt(object.cooked)
2222
end
23-
23+
2424
def truncated
2525
object.cooked.length > SiteSetting.post_excerpt_maxlength
2626
end
27-
27+
2828
def inferred_concepts
2929
ActiveModel::ArraySerializer.new(
3030
object.inferred_concepts,
31-
each_serializer: InferredConceptSerializer
31+
each_serializer: InferredConceptSerializer,
3232
)
3333
end
34-
end
34+
end

app/serializers/inferred_concept_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
class InferredConceptSerializer < ApplicationSerializer
44
attributes :id, :name, :created_at, :updated_at
5-
end
5+
end

db/fixtures/personas/603_ai_personas.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ def from_setting(setting_name)
7272

7373
persona.tools = tools.map { |name, value| [name, value] }
7474

75-
persona.response_format = instance.response_format
75+
# Only set response_format if it's not defined as a method in the persona class
76+
if !instance.class.instance_methods.include?(:response_format)
77+
persona.response_format = instance.response_format
78+
end
7679

77-
persona.examples = instance.examples
80+
# Only set examples if it's not defined as a method in the persona class
81+
persona.examples = instance.examples if !instance.class.instance_methods.include?(:examples)
7882

7983
persona.system_prompt = instance.system_prompt
8084
persona.top_p = instance.top_p

db/migrate/20250508182047_create_inferred_concepts_table.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def change
55
t.string :name, null: false
66
t.timestamps
77
end
8-
8+
99
add_index :inferred_concepts, :name, unique: true
1010
end
1111
end

lib/inferred_concepts/finder.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def self.identify_concepts(content)
1515
.find { |p| p.id == SiteSetting.inferred_concepts_generate_persona.to_i }
1616
.new
1717

18-
llm = LlmModel.find(persona.class.default_llm_id)
18+
llm = LlmModel.find(persona.default_llm_id)
1919
context =
2020
DiscourseAi::Personas::BotContext.new(
2121
messages: [{ type: :user, content: content }],
@@ -79,7 +79,7 @@ def self.find_candidate_topics(
7979
# Exclude topics that already have concepts
8080
topics_with_concepts = <<~SQL
8181
SELECT DISTINCT topic_id
82-
FROM topics_inferred_concepts
82+
FROM inferred_concepts_topics
8383
SQL
8484

8585
query = query.where("topics.id NOT IN (#{topics_with_concepts})")
@@ -129,7 +129,7 @@ def self.find_candidate_posts(
129129
# Exclude posts that already have concepts
130130
posts_with_concepts = <<~SQL
131131
SELECT DISTINCT post_id
132-
FROM posts_inferred_concepts
132+
FROM inferred_concepts_posts
133133
SQL
134134

135135
query = query.where("posts.id NOT IN (#{posts_with_concepts})")
@@ -154,7 +154,7 @@ def self.deduplicate_concepts(concept_names)
154154
.find { |p| p.id == SiteSetting.inferred_concepts_deduplicate_persona.to_i }
155155
.new
156156

157-
llm = LlmModel.find(persona.class.default_llm_id)
157+
llm = LlmModel.find(persona.default_llm_id)
158158

159159
# Create the input for the deduplicator
160160
input = { type: :user, content: concept_names.join(", ") }

lib/post_extensions.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module PostExtensions
1212
class_name: "ClassificationResult",
1313
as: :target
1414

15-
has_and_belongs_to_many :inferred_concepts
15+
has_many :inferred_concept_posts
16+
has_many :inferred_concepts, through: :inferred_concept_posts
1617
end
1718
end
1819
end

0 commit comments

Comments
 (0)