feat: ReAct tool calling (#2104)
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
local Utils = require("avante.utils")
|
||||
local P = require("avante.providers")
|
||||
local Providers = require("avante.providers")
|
||||
local Config = require("avante.config")
|
||||
local Clipboard = require("avante.clipboard")
|
||||
local HistoryMessage = require("avante.history_message")
|
||||
local Prompts = require("avante.utils.prompts")
|
||||
|
||||
---@class AvanteProviderFunctor
|
||||
local M = {}
|
||||
|
||||
setmetatable(M, { __index = Providers.openai })
|
||||
|
||||
M.api_key_name = "" -- Ollama typically doesn't require API keys for local use
|
||||
|
||||
M.role_map = {
|
||||
@@ -12,35 +17,182 @@ M.role_map = {
|
||||
assistant = "assistant",
|
||||
}
|
||||
|
||||
M.parse_messages = P.openai.parse_messages
|
||||
M.is_reasoning_model = P.openai.is_reasoning_model
|
||||
function M:parse_messages(opts)
|
||||
local messages = {}
|
||||
local provider_conf, _ = Providers.parse_config(self)
|
||||
|
||||
local system_prompt = Prompts.get_ReAct_system_prompt(provider_conf, opts)
|
||||
|
||||
if self.is_reasoning_model(provider_conf.model) then
|
||||
table.insert(messages, { role = "developer", content = system_prompt })
|
||||
else
|
||||
table.insert(messages, { role = "system", content = system_prompt })
|
||||
end
|
||||
|
||||
vim.iter(opts.messages):each(function(msg)
|
||||
if type(msg.content) == "string" then
|
||||
table.insert(messages, { role = self.role_map[msg.role], content = msg.content })
|
||||
elseif type(msg.content) == "table" then
|
||||
local content = {}
|
||||
for _, item in ipairs(msg.content) do
|
||||
if type(item) == "string" then
|
||||
table.insert(content, { type = "text", text = item })
|
||||
elseif item.type == "text" then
|
||||
table.insert(content, { type = "text", text = item.text })
|
||||
elseif item.type == "image" then
|
||||
table.insert(content, {
|
||||
type = "image_url",
|
||||
image_url = {
|
||||
url = "data:" .. item.source.media_type .. ";" .. item.source.type .. "," .. item.source.data,
|
||||
},
|
||||
})
|
||||
end
|
||||
end
|
||||
if not provider_conf.disable_tools then
|
||||
if msg.content[1].type == "tool_result" then
|
||||
local tool_use = nil
|
||||
for _, msg_ in ipairs(opts.messages) do
|
||||
if type(msg_.content) == "table" and #msg_.content > 0 then
|
||||
if msg_.content[1].type == "tool_use" and msg_.content[1].id == msg.content[1].tool_use_id then
|
||||
tool_use = msg_
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if tool_use then
|
||||
msg.role = "user"
|
||||
table.insert(content, {
|
||||
type = "text",
|
||||
text = "["
|
||||
.. tool_use.content[1].name
|
||||
.. " for '"
|
||||
.. (tool_use.content[1].input.path or tool_use.content[1].input.rel_path or "")
|
||||
.. "'] Result:",
|
||||
})
|
||||
table.insert(content, {
|
||||
type = "text",
|
||||
text = msg.content[1].content,
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
if #content > 0 then
|
||||
local text_content = {}
|
||||
for _, item in ipairs(content) do
|
||||
if type(item) == "table" and item.type == "text" then table.insert(text_content, item.text) end
|
||||
end
|
||||
table.insert(messages, { role = self.role_map[msg.role], content = table.concat(text_content, "\n\n") })
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
if Config.behaviour.support_paste_from_clipboard and opts.image_paths and #opts.image_paths > 0 then
|
||||
local message_content = messages[#messages].content
|
||||
if type(message_content) ~= "table" or message_content[1] == nil then
|
||||
message_content = { { type = "text", text = message_content } }
|
||||
end
|
||||
for _, image_path in ipairs(opts.image_paths) do
|
||||
table.insert(message_content, {
|
||||
type = "image_url",
|
||||
image_url = {
|
||||
url = "data:image/png;base64," .. Clipboard.get_base64_content(image_path),
|
||||
},
|
||||
})
|
||||
end
|
||||
messages[#messages].content = message_content
|
||||
end
|
||||
|
||||
local final_messages = {}
|
||||
local prev_role = nil
|
||||
|
||||
vim.iter(messages):each(function(message)
|
||||
local role = message.role
|
||||
if role == prev_role and role ~= "tool" then
|
||||
if role == self.role_map["assistant"] then
|
||||
table.insert(final_messages, { role = self.role_map["user"], content = "Ok" })
|
||||
else
|
||||
table.insert(final_messages, { role = self.role_map["assistant"], content = "Ok, I understand." })
|
||||
end
|
||||
end
|
||||
prev_role = role
|
||||
table.insert(final_messages, message)
|
||||
end)
|
||||
|
||||
return final_messages
|
||||
end
|
||||
|
||||
function M:is_disable_stream() return false end
|
||||
|
||||
---@class avante.OllamaFunction
|
||||
---@field name string
|
||||
---@field arguments table
|
||||
|
||||
---@class avante.OllamaToolCall
|
||||
---@field function avante.OllamaFunction
|
||||
|
||||
---@param tool_calls avante.OllamaToolCall[]
|
||||
---@param opts AvanteLLMStreamOptions
|
||||
function M:add_tool_use_messages(tool_calls, opts)
|
||||
local msgs = {}
|
||||
for _, tool_call in ipairs(tool_calls) do
|
||||
local id = Utils.uuid()
|
||||
local func = tool_call["function"]
|
||||
local msg = HistoryMessage:new({
|
||||
role = "assistant",
|
||||
content = {
|
||||
{
|
||||
type = "tool_use",
|
||||
name = func.name,
|
||||
id = id,
|
||||
input = func.arguments,
|
||||
},
|
||||
},
|
||||
}, {
|
||||
state = "generated",
|
||||
uuid = id,
|
||||
})
|
||||
table.insert(msgs, msg)
|
||||
end
|
||||
if opts.on_messages_add then opts.on_messages_add(msgs) end
|
||||
end
|
||||
|
||||
function M:parse_stream_data(ctx, data, opts)
|
||||
local ok, json_data = pcall(vim.json.decode, data)
|
||||
if not ok or not json_data then
|
||||
local ok, jsn = pcall(vim.json.decode, data)
|
||||
if not ok or not jsn then
|
||||
-- Add debug logging
|
||||
Utils.debug("Failed to parse JSON", data)
|
||||
return
|
||||
end
|
||||
|
||||
if json_data.message and json_data.message.content then
|
||||
local content = json_data.message.content
|
||||
P.openai:add_text_message(ctx, content, "generating", opts)
|
||||
if content and content ~= "" and opts.on_chunk then opts.on_chunk(content) end
|
||||
if jsn.message then
|
||||
if jsn.message.content then
|
||||
local content = jsn.message.content
|
||||
if content and content ~= "" then
|
||||
Providers.openai:add_text_message(ctx, content, "generating", opts)
|
||||
if opts.on_chunk then opts.on_chunk(content) end
|
||||
end
|
||||
end
|
||||
if jsn.message.tool_calls then
|
||||
ctx.has_tool_use = true
|
||||
local tool_calls = jsn.message.tool_calls
|
||||
self:add_tool_use_messages(tool_calls, opts)
|
||||
end
|
||||
end
|
||||
|
||||
if json_data.done then
|
||||
P.openai:finish_pending_messages(ctx, opts)
|
||||
opts.on_stop({ reason = "complete" })
|
||||
if jsn.done then
|
||||
Providers.openai:finish_pending_messages(ctx, opts)
|
||||
if ctx.has_tool_use or (ctx.tool_use_list and #ctx.tool_use_list > 0) then
|
||||
opts.on_stop({ reason = "tool_use" })
|
||||
else
|
||||
opts.on_stop({ reason = "complete" })
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
---@param prompt_opts AvantePromptOptions
|
||||
function M:parse_curl_args(prompt_opts)
|
||||
local provider_conf, request_body = P.parse_config(self)
|
||||
local provider_conf, request_body = Providers.parse_config(self)
|
||||
local keep_alive = provider_conf.keep_alive or "5m"
|
||||
|
||||
if not provider_conf.model or provider_conf.model == "" then error("Ollama model must be specified in config") end
|
||||
@@ -50,7 +202,7 @@ function M:parse_curl_args(prompt_opts)
|
||||
["Accept"] = "application/json",
|
||||
}
|
||||
|
||||
if P.env.require_api_key(provider_conf) then
|
||||
if Providers.env.require_api_key(provider_conf) then
|
||||
local api_key = self.parse_api_key()
|
||||
if api_key and api_key ~= "" then
|
||||
headers["Authorization"] = "Bearer " .. api_key
|
||||
@@ -66,7 +218,6 @@ function M:parse_curl_args(prompt_opts)
|
||||
model = provider_conf.model,
|
||||
messages = self:parse_messages(prompt_opts),
|
||||
stream = true,
|
||||
system = prompt_opts.system_prompt,
|
||||
keep_alive = keep_alive,
|
||||
}, request_body),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user