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

Commit d10eb8d

Browse files
committed
DEV: Move title suggestion to an array
1 parent 24416c5 commit d10eb8d

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

lib/ai_helper/assistant.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,18 @@ 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+
135+
helper_response = [] if json_summary_schema_key["type"] == "array"
136+
133137
if type == :structured_output
134-
json_summary_schema_key = bot.persona.response_format&.first.to_h
135138
helper_chunk = partial.read_buffered_property(json_summary_schema_key["key"]&.to_sym)
136-
137139
if !helper_chunk.nil? && !helper_chunk.empty?
138-
helper_response << helper_chunk
140+
if json_summary_schema_key["type"] != "array"
141+
helper_response = helper_chunk
142+
else
143+
helper_response << helper_chunk
144+
end
139145
block.call(helper_chunk) if block
140146
end
141147
elsif type.blank?
@@ -169,7 +175,7 @@ def generate_and_send_prompt(
169175

170176
result[:suggestions] = (
171177
if result[:type] == :list
172-
parse_list(helper_response).map { |suggestion| sanitize_result(suggestion) }
178+
helper_response.flatten.map { |suggestion| sanitize_result(suggestion) }
173179
else
174180
sanitized = sanitize_result(helper_response)
175181
result[:diff] = parse_diff(input, sanitized) if result[:type] == :diff
@@ -436,10 +442,6 @@ def parse_diff(text, suggestion)
436442

437443
DiscourseDiff.new(cooked_text, cooked_suggestion).inline_html
438444
end
439-
440-
def parse_list(list)
441-
Nokogiri::HTML5.fragment(list).css("item").map(&:text)
442-
end
443445
end
444446
end
445447
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

0 commit comments

Comments
 (0)