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

Commit bc8e57d

Browse files
authored
DEV: Move title suggestion to an array (#1435)
1 parent 24416c5 commit bc8e57d

File tree

6 files changed

+79
-16
lines changed

6 files changed

+79
-16
lines changed

lib/ai_helper/assistant.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,16 @@ def generate_prompt(
130130

131131
buffer_blk =
132132
Proc.new do |partial, _, type|
133+
json_summary_schema_key = bot.persona.response_format&.first.to_h
134+
helper_response = [] if json_summary_schema_key["type"] == "array"
133135
if type == :structured_output
134-
json_summary_schema_key = bot.persona.response_format&.first.to_h
135136
helper_chunk = partial.read_buffered_property(json_summary_schema_key["key"]&.to_sym)
136-
137137
if !helper_chunk.nil? && !helper_chunk.empty?
138-
helper_response << helper_chunk
138+
if json_summary_schema_key["type"] != "array"
139+
helper_response = helper_chunk
140+
else
141+
helper_response << helper_chunk
142+
end
139143
block.call(helper_chunk) if block
140144
end
141145
elsif type.blank?
@@ -169,7 +173,7 @@ def generate_and_send_prompt(
169173

170174
result[:suggestions] = (
171175
if result[:type] == :list
172-
parse_list(helper_response).map { |suggestion| sanitize_result(suggestion) }
176+
helper_response.flatten.map { |suggestion| sanitize_result(suggestion) }
173177
else
174178
sanitized = sanitize_result(helper_response)
175179
result[:diff] = parse_diff(input, sanitized) if result[:type] == :diff
@@ -436,10 +440,6 @@ def parse_diff(text, suggestion)
436440

437441
DiscourseDiff.new(cooked_text, cooked_suggestion).inline_html
438442
end
439-
440-
def parse_list(list)
441-
Nokogiri::HTML5.fragment(list).css("item").map(&:text)
442-
end
443443
end
444444
end
445445
end

lib/personas/titles_generator.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,42 @@ def system_prompt
1818
can have a capital letter, and acronyms like LLM can use capital letters. Format some titles
1919
as questions, some as statements. Make sure to use question marks if the title is a question.
2020
You will find the text between <input></input> XML tags.
21-
Wrap each title between <item></item> XML tags.
21+
22+
The title suggestions should be returned in a JSON array, under the `output` key, like this:
23+
24+
{
25+
"output": [
26+
"suggeested title #1",
27+
"suggeested title #2",
28+
"suggeested title #3",
29+
"suggeested title #4",
30+
"suggeested title #5"
31+
]
32+
}
33+
34+
Return only the JSON
2235
PROMPT
2336
end
2437

2538
def response_format
26-
[{ "key" => "output", "type" => "string" }]
39+
[{ "key" => "output", "type" => "array", "array_type" => "string" }]
2740
end
2841

2942
def examples
3043
[
3144
[
3245
"<input>In the labyrinth of time, a solitary horse, etched in gold by the setting sun, embarked on an infinite journey.</input>",
33-
"<item>The solitary horse</item><item>The horse etched in gold</item><item>A horse's infinite journey</item><item>A horse lost in time</item><item>A horse's last ride</item>",
46+
<<~OUTPUT,
47+
{
48+
"output": [
49+
"The solitary horse",
50+
"The horse etched in gold",
51+
"A horse's infinite journey",
52+
"A horse lost in time",
53+
"A horse's last rid"
54+
]
55+
}
56+
OUTPUT
3457
],
3558
]
3659
end

spec/lib/modules/ai_helper/assistant_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,15 @@
196196
let(:mode) { described_class::GENERATE_TITLES }
197197

198198
let(:titles) do
199-
"<item>The solitary horse</item><item>The horse etched in gold</item><item>A horse's infinite journey</item><item>A horse lost in time</item><item>A horse's last ride</item>"
199+
{
200+
output: [
201+
"The solitary horse",
202+
"The horse etched in gold",
203+
"A horse's infinite journey",
204+
"A horse lost in time",
205+
"A horse's last ride",
206+
],
207+
}
200208
end
201209

202210
it "returns an array with each title" do

spec/requests/ai_helper/assistant_controller_spec.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,15 @@
186186

187187
context "when suggesting titles with a topic_id" do
188188
let(:title_suggestions) do
189-
"<item>What are your favourite fruits?</item><item>Love for fruits</item><item>Fruits are amazing</item><item>Favourite fruit list</item><item>Fruit share topic</item>"
189+
{
190+
output: [
191+
"What are your favourite fruits?",
192+
"Love for fruits",
193+
"Fruits are amazing",
194+
"Favourite fruit list",
195+
"Fruit share topic",
196+
],
197+
}
190198
end
191199
let(:title_suggestions_array) do
192200
[
@@ -210,7 +218,15 @@
210218

211219
context "when suggesting titles with input text" do
212220
let(:title_suggestions) do
213-
"<item>Apples - the best fruit</item><item>Why apples are great</item><item>Apples are the best fruit</item><item>My love for apples</item><item>I love apples</item>"
221+
{
222+
output: [
223+
"Apples - the best fruit",
224+
"Why apples are great",
225+
"Apples are the best fruit",
226+
"My love for apples",
227+
"I love apples",
228+
],
229+
}
214230
end
215231
let(:title_suggestions_array) do
216232
[

spec/system/ai_helper/ai_composer_helper_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,15 @@ def trigger_composer_helper(content)
185185
let(:mode) { DiscourseAi::AiHelper::Assistant::GENERATE_TITLES }
186186

187187
let(:titles) do
188-
"<item>Rainy Spain</item><item>Plane-Bound Delights</item><item>Mysterious Spain</item><item>Plane-Rain Chronicles</item><item>Unveiling Spain</item>"
188+
{
189+
output: [
190+
"Rainy Spain",
191+
"Plane-Bound Delights",
192+
"Mysterious Spain",
193+
"Plane-Rain Chronicles",
194+
"Unveiling Spain",
195+
],
196+
}
189197
end
190198

191199
it "opens a menu with title suggestions" do

spec/system/ai_helper/ai_split_topic_suggestion_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ def open_move_topic_modal
5656
context "when suggesting titles with AI title suggester" do
5757
let(:mode) { DiscourseAi::AiHelper::Assistant::GENERATE_TITLES }
5858
let(:titles) do
59-
"<item>Pie: A delicious dessert</item><item>Cake is the best!</item><item>Croissants are delightful</item><item>Some great desserts</item><item>What is the best dessert?</item>"
59+
{
60+
output: [
61+
"Pie: A delicious dessert",
62+
"Cake is the best!",
63+
"Croissants are delightful",
64+
"Some great desserts",
65+
"What is the best dessert?",
66+
],
67+
}
6068
end
6169

6270
it "opens a menu with title suggestions" do

0 commit comments

Comments
 (0)