diff --git a/lib/ai_helper/assistant.rb b/lib/ai_helper/assistant.rb index 9a0aff2a..c986bbaa 100644 --- a/lib/ai_helper/assistant.rb +++ b/lib/ai_helper/assistant.rb @@ -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 diff --git a/lib/personas/titles_generator.rb b/lib/personas/titles_generator.rb index 2f502601..ccc2535b 100644 --- a/lib/personas/titles_generator.rb +++ b/lib/personas/titles_generator.rb @@ -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 XML tags. - Wrap each title between 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 [ [ "In the labyrinth of time, a solitary horse, etched in gold by the setting sun, embarked on an infinite journey.", - "The solitary horseThe horse etched in goldA horse's infinite journeyA horse lost in timeA horse's last ride", + <<~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 diff --git a/spec/lib/modules/ai_helper/assistant_spec.rb b/spec/lib/modules/ai_helper/assistant_spec.rb index 98cd9256..832235d9 100644 --- a/spec/lib/modules/ai_helper/assistant_spec.rb +++ b/spec/lib/modules/ai_helper/assistant_spec.rb @@ -196,7 +196,15 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do let(:mode) { described_class::GENERATE_TITLES } let(:titles) do - "The solitary horseThe horse etched in goldA horse's infinite journeyA horse lost in timeA horse's last ride" + { + 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 diff --git a/spec/requests/ai_helper/assistant_controller_spec.rb b/spec/requests/ai_helper/assistant_controller_spec.rb index 61051a75..7c9ea8b2 100644 --- a/spec/requests/ai_helper/assistant_controller_spec.rb +++ b/spec/requests/ai_helper/assistant_controller_spec.rb @@ -186,7 +186,15 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do context "when suggesting titles with a topic_id" do let(:title_suggestions) do - "What are your favourite fruits?Love for fruitsFruits are amazingFavourite fruit listFruit share topic" + { + 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 - "Apples - the best fruitWhy apples are greatApples are the best fruitMy love for applesI love apples" + { + 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 [ diff --git a/spec/system/ai_helper/ai_composer_helper_spec.rb b/spec/system/ai_helper/ai_composer_helper_spec.rb index c0fbed81..6817cf16 100644 --- a/spec/system/ai_helper/ai_composer_helper_spec.rb +++ b/spec/system/ai_helper/ai_composer_helper_spec.rb @@ -185,7 +185,15 @@ RSpec.describe "AI Composer helper", type: :system, js: true do let(:mode) { DiscourseAi::AiHelper::Assistant::GENERATE_TITLES } let(:titles) do - "Rainy SpainPlane-Bound DelightsMysterious SpainPlane-Rain ChroniclesUnveiling Spain" + { + output: [ + "Rainy Spain", + "Plane-Bound Delights", + "Mysterious Spain", + "Plane-Rain Chronicles", + "Unveiling Spain", + ], + } end it "opens a menu with title suggestions" do diff --git a/spec/system/ai_helper/ai_split_topic_suggestion_spec.rb b/spec/system/ai_helper/ai_split_topic_suggestion_spec.rb index 1bc4221f..6cb3bdd9 100644 --- a/spec/system/ai_helper/ai_split_topic_suggestion_spec.rb +++ b/spec/system/ai_helper/ai_split_topic_suggestion_spec.rb @@ -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 - "Pie: A delicious dessertCake is the best!Croissants are delightfulSome great dessertsWhat is the best dessert?" + { + 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