fix: gemini ReAct (#2132)
This commit is contained in:
@@ -123,6 +123,12 @@ function M:parse_messages(opts)
|
||||
end
|
||||
end
|
||||
if tool_use then
|
||||
table.insert(contents, {
|
||||
role = "model",
|
||||
parts = {
|
||||
{ text = Utils.tool_use_to_xml(tool_use.content[1]) },
|
||||
},
|
||||
})
|
||||
role = "user"
|
||||
table.insert(parts, {
|
||||
text = "["
|
||||
@@ -187,9 +193,11 @@ function M.prepare_request_body(provider_instance, prompt_opts, provider_conf, r
|
||||
request_body.temperature = nil
|
||||
request_body.max_tokens = nil
|
||||
|
||||
local use_ReAct_prompt = provider_conf.use_ReAct_prompt == true
|
||||
|
||||
local disable_tools = provider_conf.disable_tools or false
|
||||
|
||||
if not disable_tools and prompt_opts.tools then
|
||||
if not use_ReAct_prompt and not disable_tools and prompt_opts.tools then
|
||||
local function_declarations = {}
|
||||
for _, tool in ipairs(prompt_opts.tools) do
|
||||
table.insert(function_declarations, provider_instance:transform_to_function_declaration(tool))
|
||||
@@ -229,7 +237,7 @@ function M:parse_response(ctx, data_stream, _, opts)
|
||||
if json.candidates and #json.candidates > 0 then
|
||||
local candidate = json.candidates[1]
|
||||
---@type AvanteLLMToolUse[]
|
||||
local tool_use_list = {}
|
||||
ctx.tool_use_list = ctx.tool_use_list or {}
|
||||
|
||||
-- Check if candidate.content and candidate.content.parts exist before iterating
|
||||
if candidate.content and candidate.content.parts then
|
||||
@@ -245,7 +253,7 @@ function M:parse_response(ctx, data_stream, _, opts)
|
||||
name = part.functionCall.name,
|
||||
input_json = vim.json.encode(part.functionCall.args),
|
||||
}
|
||||
table.insert(tool_use_list, tool_use)
|
||||
table.insert(ctx.tool_use_list, tool_use)
|
||||
OpenAI:add_tool_use_message(tool_use, "generated", opts)
|
||||
end
|
||||
end
|
||||
@@ -262,7 +270,7 @@ function M:parse_response(ctx, data_stream, _, opts)
|
||||
-- The tool_use list is added to the table in llm.lua
|
||||
opts.on_stop(vim.tbl_deep_extend("force", { reason = "tool_use" }, stop_details))
|
||||
elseif reason_str == "STOP" then
|
||||
if #tool_use_list > 0 then
|
||||
if ctx.tool_use_list and #ctx.tool_use_list > 0 then
|
||||
-- Natural stop, but tools were found in this final chunk.
|
||||
opts.on_stop(vim.tbl_deep_extend("force", { reason = "tool_use" }, stop_details))
|
||||
else
|
||||
|
||||
@@ -226,6 +226,8 @@ function M:add_text_message(ctx, text, state, opts)
|
||||
if llm_tool_names == nil then llm_tool_names = LlmTools.get_tool_names() end
|
||||
if ctx.content == nil then ctx.content = "" end
|
||||
ctx.content = ctx.content .. text
|
||||
local content = ctx.content:gsub("<tool_code>", ""):gsub("</tool_code>", "")
|
||||
ctx.content = content
|
||||
local msg = HistoryMessage:new({
|
||||
role = "assistant",
|
||||
content = ctx.content,
|
||||
|
||||
Reference in New Issue
Block a user