feat: python llm tool (#1334)

This commit is contained in:
yetone
2025-02-21 13:04:02 +08:00
committed by GitHub
parent 40ab7ef537
commit 48ae1fed78
2 changed files with 55 additions and 1 deletions

View File

@@ -533,6 +533,27 @@ function M.git_commit(opts, on_log)
return true, nil
end
---@param opts { code: string, rel_path: string }
---@param on_log? fun(log: string): nil
---@return string|nil result
---@return string|nil error
function M.python(opts, on_log)
local abs_path = get_abs_path(opts.rel_path)
if not has_permission_to_access(abs_path) then return nil, "No permission to access path: " .. abs_path end
if not Path:new(abs_path):exists() then return nil, "Path not found: " .. abs_path end
if on_log then on_log("cwd: " .. abs_path) end
if on_log then on_log("code: " .. opts.code) end
---change cwd to abs_path
local old_cwd = vim.fn.getcwd()
vim.fn.chdir(abs_path)
local output = vim.fn.system({ "python", "-c", opts.code })
local exit_code = vim.v.shell_error
vim.fn.chdir(old_cwd)
if exit_code ~= 0 then return nil, "Error: " .. output end
Utils.debug("output", output)
return output, nil
end
---@class AvanteLLMTool
---@field name string
---@field description string
@@ -558,6 +579,38 @@ end
---@type AvanteLLMTool[]
M.tools = {
{
name = "python",
description = "Run python code",
param = {
type = "table",
fields = {
{
name = "code",
description = "Python code to run",
type = "string",
},
{
name = "rel_path",
description = "Relative path to the directory, as cwd",
type = "string",
},
},
},
returns = {
{
name = "result",
description = "Python output",
type = "string",
},
{
name = "error",
description = "Error message if the python code failed",
type = "string",
optional = true,
},
},
},
{
name = "git_diff",
description = "Get git diff for generating commit message",

View File

@@ -20,7 +20,8 @@ Tools Usage Guide:
- When attempting to modify a file that is not in the context, please first use the `list_files` tool and `search_files` tool to check if the file you want to modify exists, then use the `read_file` tool to read the file content. Don't modify blindly!
- When generating files, first use `list_files` tool to read the directory structure, don't generate blindly!
- When creating files, first check if the directory exists. If it doesn't exist, create the directory before creating the file.
- After `web_search`, if you don't get detailed enough information, do not continue use `web_search`, just continue using the `fetch` tool to get more information you need from the links in the search results.
- After `web_search` tool returns, if you don't get detailed enough information, do not continue use `web_search` tool, just continue using the `fetch` tool to get more information you need from the links in the search results.
- For any mathematical calculation problems, please prioritize using the `python` tool to solve them. Please try to avoid mathematical symbols in the return value of the `python` tool for mathematical problems and directly output human-readable results, because large models don't understand mathematical symbols, they only understand human natural language.
{% if system_info -%}
Use the appropriate shell based on the user's system info: