FIX: Clean unicode usernames when adding messages through prompt's contrstuctor (#425)

This commit is contained in:
Roman Rizzi 2024-01-15 12:01:40 -03:00 committed by GitHub
parent 37e6ac169e
commit ff4da6ace8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -22,6 +22,10 @@ module DiscourseAi
@messages.concat(messages) @messages.concat(messages)
@messages.each { |message| validate_message(message) } @messages.each { |message| validate_message(message) }
@messages.each do |message|
message[:id] = clean_username(message[:id]) if message[:type] == :user &&
message[:id].present?
end
@messages.each_cons(2) { |last_turn, new_turn| validate_turn(last_turn, new_turn) } @messages.each_cons(2) { |last_turn, new_turn| validate_turn(last_turn, new_turn) }
@tools = tools @tools = tools

View File

@ -21,6 +21,18 @@ RSpec.describe DiscourseAi::Completions::Prompt do
bad_messages3 = [{ content: "some content associated to no one" }] bad_messages3 = [{ content: "some content associated to no one" }]
expect { described_class.new("a bot", messages: bad_messages3) }.to raise_error(ArgumentError) expect { described_class.new("a bot", messages: bad_messages3) }.to raise_error(ArgumentError)
end end
it "cleans unicode usernames" do
unicode_username = "罗马罗马"
prompt =
described_class.new(
"a bot",
messages: [{ type: :user, content: user_msg, id: unicode_username }],
)
expect(prompt.messages.last[:id]).to eq("____")
end
end end
describe "#push" do describe "#push" do
@ -62,5 +74,14 @@ RSpec.describe DiscourseAi::Completions::Prompt do
expect(system_message[:content]).to eq(user_msg) expect(system_message[:content]).to eq(user_msg)
expect(system_message[:id]).to eq(username) expect(system_message[:id]).to eq(username)
end end
it "cleans unicode usernames" do
unicode_username = "罗马罗马"
prompt.push(type: :user, content: user_msg, id: unicode_username)
user_message = prompt.messages.last
expect(user_message[:id]).to eq("____")
end
end end
end end