DEV: Move title suggestion to an array (#1435)

This commit is contained in:
Rafael dos Santos Silva
2025-06-16 18:06:54 -03:00
committed by GitHub
parent 24416c5b87
commit bc8e57d7e8
6 changed files with 79 additions and 16 deletions
+8 -8
View File
@@ -130,12 +130,16 @@ module DiscourseAi
buffer_blk =
Proc.new do |partial, _, type|
json_summary_schema_key = bot.persona.response_format&.first.to_h
helper_response = [] if json_summary_schema_key["type"] == "array"
if type == :structured_output
json_summary_schema_key = bot.persona.response_format&.first.to_h
helper_chunk = partial.read_buffered_property(json_summary_schema_key["key"]&.to_sym)
if !helper_chunk.nil? && !helper_chunk.empty?
helper_response << helper_chunk
if json_summary_schema_key["type"] != "array"
helper_response = helper_chunk
else
helper_response << helper_chunk
end
block.call(helper_chunk) if block
end
elsif type.blank?
@@ -169,7 +173,7 @@ module DiscourseAi
result[:suggestions] = (
if result[:type] == :list
parse_list(helper_response).map { |suggestion| sanitize_result(suggestion) }
helper_response.flatten.map { |suggestion| sanitize_result(suggestion) }
else
sanitized = sanitize_result(helper_response)
result[:diff] = parse_diff(input, sanitized) if result[:type] == :diff
@@ -436,10 +440,6 @@ module DiscourseAi
DiscourseDiff.new(cooked_text, cooked_suggestion).inline_html
end
def parse_list(list)
Nokogiri::HTML5.fragment(list).css("item").map(&:text)
end
end
end
end
+26 -3
View File
@@ -18,19 +18,42 @@ module DiscourseAi
can have a capital letter, and acronyms like LLM can use capital letters. Format some titles
as questions, some as statements. Make sure to use question marks if the title is a question.
You will find the text between <input></input> XML tags.
Wrap each title between <item></item> XML tags.
The title suggestions should be returned in a JSON array, under the `output` key, like this:
{
"output": [
"suggeested title #1",
"suggeested title #2",
"suggeested title #3",
"suggeested title #4",
"suggeested title #5"
]
}
Return only the JSON
PROMPT
end
def response_format
[{ "key" => "output", "type" => "string" }]
[{ "key" => "output", "type" => "array", "array_type" => "string" }]
end
def examples
[
[
"<input>In the labyrinth of time, a solitary horse, etched in gold by the setting sun, embarked on an infinite journey.</input>",
"<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>",
<<~OUTPUT,
{
"output": [
"The solitary horse",
"The horse etched in gold",
"A horse's infinite journey",
"A horse lost in time",
"A horse's last rid"
]
}
OUTPUT
],
]
end
+9 -1
View File
@@ -196,7 +196,15 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
let(:mode) { described_class::GENERATE_TITLES }
let(:titles) do
"<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>"
{
output: [
"The solitary horse",
"The horse etched in gold",
"A horse's infinite journey",
"A horse lost in time",
"A horse's last ride",
],
}
end
it "returns an array with each title" do
@@ -186,7 +186,15 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
context "when suggesting titles with a topic_id" do
let(:title_suggestions) do
"<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>"
{
output: [
"What are your favourite fruits?",
"Love for fruits",
"Fruits are amazing",
"Favourite fruit list",
"Fruit share topic",
],
}
end
let(:title_suggestions_array) do
[
@@ -210,7 +218,15 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
context "when suggesting titles with input text" do
let(:title_suggestions) do
"<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>"
{
output: [
"Apples - the best fruit",
"Why apples are great",
"Apples are the best fruit",
"My love for apples",
"I love apples",
],
}
end
let(:title_suggestions_array) do
[
@@ -185,7 +185,15 @@ RSpec.describe "AI Composer helper", type: :system, js: true do
let(:mode) { DiscourseAi::AiHelper::Assistant::GENERATE_TITLES }
let(:titles) do
"<item>Rainy Spain</item><item>Plane-Bound Delights</item><item>Mysterious Spain</item><item>Plane-Rain Chronicles</item><item>Unveiling Spain</item>"
{
output: [
"Rainy Spain",
"Plane-Bound Delights",
"Mysterious Spain",
"Plane-Rain Chronicles",
"Unveiling Spain",
],
}
end
it "opens a menu with title suggestions" do
@@ -56,7 +56,15 @@ RSpec.describe "AI Post helper", type: :system, js: true do
context "when suggesting titles with AI title suggester" do
let(:mode) { DiscourseAi::AiHelper::Assistant::GENERATE_TITLES }
let(:titles) do
"<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>"
{
output: [
"Pie: A delicious dessert",
"Cake is the best!",
"Croissants are delightful",
"Some great desserts",
"What is the best dessert?",
],
}
end
it "opens a menu with title suggestions" do