FIX: cut completion short after function call is found (#182)

Previous to this change we would keep completing and throw away
result
This commit is contained in:
Sam 2023-09-05 10:37:58 +10:00 committed by GitHub
parent 86591cf1d2
commit 38af2ca63e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -8,6 +8,7 @@ module DiscourseAi
@functions = [] @functions = []
@current_function = nil @current_function = nil
@found = false @found = false
@cancel_completion = false
end end
def found? def found?
@ -18,6 +19,14 @@ module DiscourseAi
@found = true @found = true
end end
def cancel_completion?
@cancel_completion
end
def cancel_completion!
@cancel_completion = true
end
def add_function(name) def add_function(name)
@current_function = { name: name, arguments: +"" } @current_function = { name: name, arguments: +"" }
@functions << @current_function @functions << @current_function
@ -126,6 +135,8 @@ module DiscourseAi
functions: functions, functions: functions,
done: false, done: false,
) )
cancel&.call if functions.cancel_completion?
end end
reply << current_delta if !functions.found? reply << current_delta if !functions.found?
@ -315,6 +326,9 @@ module DiscourseAi
def populate_functions(partial:, reply:, functions:, done:) def populate_functions(partial:, reply:, functions:, done:)
if !done if !done
if functions.found?
functions.cancel_completion! if reply.split("\n")[-1].match?(/^\s*[^!]+/)
end
functions.found! if reply.match?(/^!/i) functions.found! if reply.match?(/^!/i)
else else
reply reply

View File

@ -21,6 +21,36 @@ module ::DiscourseAi
end end
describe "parsing a reply prompt" do describe "parsing a reply prompt" do
it "can correctly predict that a completion needs to be cancelled" do
SiteSetting.ai_bot_enabled_chat_commands = "read|search"
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.strip
hello world
!search(search_query: "hello world", random_stuff: 77)
!search(search_query: "hello world 2", random_stuff: 77
REPLY
bot.populate_functions(partial: nil, reply: prompt, functions: functions, done: false)
expect(functions.found?).to eq(true)
expect(functions.cancel_completion?).to eq(false)
prompt << ")\n"
bot.populate_functions(partial: nil, reply: prompt, functions: functions, done: false)
expect(functions.found?).to eq(true)
expect(functions.cancel_completion?).to eq(false)
prompt << "a test test"
bot.populate_functions(partial: nil, reply: prompt, functions: functions, done: false)
expect(functions.cancel_completion?).to eq(true)
end
it "can correctly detect commands from a prompt" do it "can correctly detect commands from a prompt" do
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