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

Commit f420261

Browse files
committed
DEV: Apply feedback from review
1 parent eb93e17 commit f420261

8 files changed

+109
-13
lines changed

db/migrate/20250710180401_copy_ai_summarization_model_to_persona_default.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@ def up
88
# Extract the model ID from the setting value (e.g., "custom:-5" -> "-5")
99
model_id = ai_summarization_model.split(":").last
1010

11-
# Update the summarization personas (IDs -11 and -12) with the extracted model ID
11+
persona_settings = %w[ai_summarization_persona ai_summary_gists_persona]
12+
default_persona_ids = [-11, -12]
13+
14+
persona_ids_query =
15+
persona_settings
16+
.map { |setting| "SELECT value FROM site_settings WHERE name = '#{setting}'" }
17+
.join(" UNION ")
18+
persona_ids = DB.query_single(persona_ids_query).compact
19+
all_persona_ids = (default_persona_ids + persona_ids).map(&:to_i).uniq.join(",")
20+
21+
# Update the summarization personas with the extracted model ID
1222
execute(<<~SQL)
1323
UPDATE ai_personas
1424
SET default_llm_id = #{model_id}
15-
WHERE id IN (-11, -12) AND default_llm_id IS NULL
25+
WHERE id IN (#{all_persona_ids}) AND default_llm_id IS NULL
1626
SQL
1727
end
1828
end

db/migrate/20250710181656_copy_ai_helper_model_to_persona_default.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,32 @@ def up
88
# Extract the model ID from the setting value (e.g., "custom:1" -> "1")
99
model_id = ai_helper_model.split(":").last
1010

11+
# Get persona IDs from site settings
12+
persona_settings = %w[
13+
ai_helper_proofreader_persona
14+
ai_helper_title_suggestions_persona
15+
ai_helper_explain_persona
16+
ai_helper_smart_dates_persona
17+
ai_helper_translator_persona
18+
ai_helper_markdown_tables_persona
19+
ai_helper_custom_prompt_persona
20+
]
21+
22+
default_persona_ids = [-18, -19, -20, -22, -23, -24, -25]
23+
persona_ids_query =
24+
persona_settings
25+
.map { |setting| "SELECT value FROM site_settings WHERE name = '#{setting}'" }
26+
.join(" UNION ")
27+
persona_ids = DB.query_single(persona_ids_query).compact
28+
29+
all_persona_ids = (default_persona_ids + persona_ids).map(&:to_i).uniq.join(",")
30+
1131
# Update the helper personas with the extracted model ID
1232
execute(<<~SQL)
13-
UPDATE ai_personas
14-
SET default_llm_id = #{model_id}
15-
WHERE id IN (-18, -19, -20, -21, -22, -23, -24, -25) AND default_llm_id IS NULL
16-
SQL
33+
UPDATE ai_personas
34+
SET default_llm_id = #{model_id}
35+
WHERE id IN (#{all_persona_ids}) AND default_llm_id IS NULL
36+
SQL
1737
end
1838
end
1939

db/migrate/20250710215720_copy_hyde_model_to_persona.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22
class CopyHydeModelToPersona < ActiveRecord::Migration[7.2]
33
def up
44
hyde_model =
5-
DB.query_single("SELECT value FROM site_settings WHERE name = 'ai_embeddings_semantic_search_hyde_model'").first
5+
DB.query_single(
6+
"SELECT value FROM site_settings WHERE name = 'ai_embeddings_semantic_search_hyde_model'",
7+
).first
68

79
if hyde_model.present? && hyde_model.start_with?("custom:")
810
# Extract the model ID from the setting value (e.g., "custom:1" -> "1")
911
model_id = hyde_model.split(":").last
1012

13+
persona_settings = %w[ai_embeddings_semantic_search_hyde_persona]
14+
default_persona_ids = [-32]
15+
persona_ids_query =
16+
persona_settings
17+
.map { |setting| "SELECT value FROM site_settings WHERE name = '#{setting}'" }
18+
.join(" UNION ")
19+
persona_ids = DB.query_single(persona_ids_query).compact
20+
21+
all_persona_ids = (default_persona_ids + persona_ids).map(&:to_i).uniq.join(",")
22+
1123
# Update the hyde persona with the extracted model ID
1224
execute(<<~SQL)
1325
UPDATE ai_personas
1426
SET default_llm_id = #{model_id}
15-
WHERE id IN (-32) AND default_llm_id IS NULL
27+
WHERE id IN (#{all_persona_ids}) AND default_llm_id IS NULL
1628
SQL
1729
end
1830
end

db/migrate/20250716005451_copy_translation_model_to_persona.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,27 @@ def up
88
# Extract the model ID from the setting value (e.g., "custom:-5" -> "-5")
99
model_id = ai_translation_model.split(":").last
1010

11+
persona_settings = %w[
12+
ai_translation_locale_detector_persona
13+
ai_translation_post_raw_translator_persona
14+
ai_translation_topic_title_translator_persona
15+
ai_translation_short_text_translator_persona
16+
]
17+
default_persona_ids = [-27, -28, -29, -30]
18+
19+
persona_ids_query =
20+
persona_settings
21+
.map { |setting| "SELECT value FROM site_settings WHERE name = '#{setting}'" }
22+
.join(" UNION ")
23+
persona_ids = DB.query_single(persona_ids_query).compact
24+
25+
all_persona_ids = (default_persona_ids + persona_ids).map(&:to_i).uniq.join(",")
26+
1127
# Update the translation personas (IDs -27, -28, -29, -30) with the extracted model ID
1228
execute(<<~SQL)
1329
UPDATE ai_personas
1430
SET default_llm_id = #{model_id}
15-
WHERE id IN (-27, -28, -29, -30) AND default_llm_id IS NULL
31+
WHERE id IN (#{all_persona_ids}) AND default_llm_id IS NULL
1632
SQL
1733
end
1834
end

db/migrate/20250716005855_copy_ai_image_caption_model_to_persona_default.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,22 @@ def up
1111
# Extract the model ID from the setting value (e.g., "custom:1" -> "1")
1212
model_id = ai_helper_image_caption_model.split(":").last
1313

14+
persona_settings = %w[ai_helper_post_illustrator_persona ai_helper_image_caption_persona,]
15+
default_persona_ids = [-21, -26]
16+
17+
persona_ids_query =
18+
persona_settings
19+
.map { |setting| "SELECT value FROM site_settings WHERE name = '#{setting}'" }
20+
.join(" UNION ")
21+
persona_ids = DB.query_single(persona_ids_query).compact
22+
23+
all_persona_ids = (default_persona_ids + persona_ids).map(&:to_i).uniq.join(",")
24+
1425
# Update the helper personas with the extracted model ID
1526
execute(<<~SQL)
1627
UPDATE ai_personas
1728
SET default_llm_id = #{model_id}
18-
WHERE id IN (-26) AND default_llm_id IS NULL
29+
WHERE id IN (#{all_persona_ids}) AND default_llm_id IS NULL
1930
SQL
2031
end
2132
end

lib/ai_helper/chat_thread_titler.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ def suggested_title
1212
return nil if content.blank?
1313

1414
suggested_title = call_llm(content)
15+
return nil if suggested_title.blank?
16+
1517
cleanup(suggested_title)
1618
end
1719

1820
def call_llm(thread_content)
21+
return nil if SiteSetting.ai_default_llm_model.blank?
22+
1923
chat = "<input>\n#{thread_content}\n</input>"
2024

2125
prompt =

lib/ai_helper/painter.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def base64_to_image(artifacts, user_id)
5757
end
5858

5959
def diffusion_prompt(text, user)
60+
llm_model =
61+
AiPersona.find_by(id: SiteSetting.ai_helper_post_illustrator_persona)&.default_llm_id ||
62+
SiteSetting.ai_default_llm_model
63+
64+
return nil if llm_model.blank?
65+
6066
prompt =
6167
DiscourseAi::Completions::Prompt.new(
6268
<<~TEXT.strip,
@@ -66,7 +72,7 @@ def diffusion_prompt(text, user)
6672
messages: [{ type: :user, content: text, id: user.username }],
6773
)
6874

69-
DiscourseAi::Completions::Llm.proxy(SiteSetting.ai_default_llm_model).generate(
75+
DiscourseAi::Completions::Llm.proxy(llm_model).generate(
7076
prompt,
7177
user: user,
7278
feature_name: "illustrate_post",

lib/translation.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@
33
module DiscourseAi
44
module Translation
55
def self.enabled?
6-
SiteSetting.discourse_ai_enabled && SiteSetting.ai_translation_enabled &&
7-
SiteSetting.ai_default_llm_model.present? &&
6+
SiteSetting.discourse_ai_enabled && SiteSetting.ai_translation_enabled && has_llm_model? &&
87
SiteSetting.content_localization_supported_locales.present?
98
end
109

10+
def self.has_llm_model?
11+
persona_ids = [
12+
SiteSetting.ai_translation_locale_detector_persona,
13+
SiteSetting.ai_translation_post_raw_translator_persona,
14+
SiteSetting.ai_translation_topic_title_translator_persona,
15+
SiteSetting.ai_translation_short_text_translator_persona,
16+
]
17+
18+
persona_default_llms = AiPersona.where(id: persona_ids).pluck(:default_llm_id)
19+
default_llm_model = SiteSetting.ai_default_llm_model
20+
21+
if persona_default_llms.any?(&:blank?) && default_llm_model.blank?
22+
false
23+
else
24+
true
25+
end
26+
end
27+
1128
def self.backfill_enabled?
1229
enabled? && SiteSetting.ai_translation_backfill_hourly_rate > 0 &&
1330
SiteSetting.ai_translation_backfill_max_age_days > 0

0 commit comments

Comments
 (0)