FIX: trim first space when getting a reply from anthropic (#164)

Anthropic loves sending a pointless leading space with completions
this throws off the command framework.
This commit is contained in:
Sam 2023-08-29 10:57:36 +10:00 committed by GitHub
parent b14cb864dc
commit 8fdb88604f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -20,7 +20,12 @@ module DiscourseAi
end end
def get_delta(partial, context) def get_delta(partial, context)
partial[:completion] completion = partial[:completion]
if completion&.start_with?(" ") && !context[:processed_first]
completion = completion[1..-1]
context[:processed_first] = true
end
completion
end end
private private

View File

@ -25,9 +25,9 @@ module ::DiscourseAi
SiteSetting.ai_bot_enabled_chat_commands = "read|search" SiteSetting.ai_bot_enabled_chat_commands = "read|search"
functions = DiscourseAi::AiBot::Bot::FunctionCalls.new functions = DiscourseAi::AiBot::Bot::FunctionCalls.new
# note anthropic API has a silly leading space, we need to make sure we can handle that
prompt = <<~REPLY prompt = <<~REPLY
Hi there I am a robot!!! hello world
!search(search_query: "hello world", random_stuff: 77) !search(search_query: "hello world", random_stuff: 77)
!random(search_query: "hello world", random_stuff: 77) !random(search_query: "hello world", random_stuff: 77)
!read(topic_id: 109) !read(topic_id: 109)
@ -52,6 +52,15 @@ module ::DiscourseAi
describe "#update_with_delta" do describe "#update_with_delta" do
describe "get_delta" do describe "get_delta" do
it "can properly remove first leading space" do
context = {}
reply = +""
reply << bot.get_delta({ completion: " Hello" }, context)
reply << bot.get_delta({ completion: " World" }, context)
expect(reply).to eq("Hello World")
end
it "can properly remove Assistant prefix" do it "can properly remove Assistant prefix" do
context = {} context = {}
reply = +"" reply = +""