GitHubFileEditorTool
一个允许 Agent 和 ToolInvoker 编辑 GitHub 存储库中文件的工具。
| 必需的初始化变量 | "github_token": GitHub 个人访问令牌。可以设置为GITHUB_TOKEN 环境变量。 |
| API 参考 | Tools (工具) |
| GitHub 链接 | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/github |
概述
GitHubFileEditorTool 封装了 GitHubFileEditor 组件,为在 agent 工作流和基于工具的管道中使用提供了工具接口。
该工具支持多种文件操作,包括编辑现有文件、创建新文件、删除文件以及撤销最近的更改。它支持四种主要命令:
- EDIT:通过替换特定内容来编辑现有文件
- CREATE:使用指定内容创建新文件
- DELETE:删除现有文件
- UNDO:撤销同一用户进行的最后一次提交
参数
name是可选的,默认为“file_editor”。指定工具的名称。description是可选的,并为 LLM 提供有关工具功能的上下文。github_token是必需的,并且必须是用于 API 身份验证的 GitHub 个人访问令牌。默认设置使用环境变量GITHUB_TOKEN.repo是可选的,并以 owner/repo 格式设置默认存储库。branch是可选的,默认为“main”。设置要使用的默认分支。raise_on_failure是可选的,默认为True。如果为 False,则会返回错误而不是引发异常。
用法
安装 GitHub 集成即可使用GitHubFileEditorTool:
pip install github-haystack
仓库占位符
要运行以下代码片段,您需要将
owner/repo替换为您自己的 GitHub 仓库名称。
单独使用
编辑文件的基本用法
from haystack_integrations.tools.github import GitHubFileEditorTool
tool = GitHubFileEditorTool()
result = tool.invoke(
command="edit",
payload={
"path": "src/example.py",
"original": "def old_function():",
"replacement": "def new_function():",
"message": "Renamed function for clarity"
},
repo="owner/repo",
branch="main"
)
print(result)
{'result': 'Edit successful'}
与 Agent 一起使用
您可以使用GitHubFileEditorTool 与 Agent 组件一起使用。当需要编辑 GitHub 存储库中的文件时,Agent 将自动调用该工具。
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.components.agents import Agent
from haystack_integrations.tools.github import GitHubFileEditorTool
editor_tool = GitHubFileEditorTool(repo="owner/repo")
agent = Agent(
chat_generator=OpenAIChatGenerator(),
tools=[editor_tool],
exit_conditions=["text"]
)
agent.warm_up()
response = agent.run(messages=[
ChatMessage.from_user("Edit the file README.md in the repository \"owner/repo\" and replace the original string 'tpyo' with the replacement 'typo'. This is all context you need.")
])
print(response["last_message"].text)
The file `README.md` has been successfully edited to correct the spelling of 'tpyo' to 'typo'.
更新于 4 个月前
