GitHubFileEditorTool
GitHubFileEditorTool
Agent가 GitHub 저장소의 파일을 편집할 수 있게 해 주는 Tool이에요.
출처: 문서
본문
GitHubFileEditorTool은 GitHubFileEditor 구성 요소를 감싸서, 에이전트 워크플로와 tool 기반 파이프라인에서 쓸 수 있는 tool 인터페이스를 제공해요. 기존 파일 편집, 새 파일 생성, 파일 삭제, 최근 변경 사항 되돌리기를 포함한 여러 파일 작업을 지원하며, 네 가지 주요 명령이 있어요:
- EDIT: 특정 콘텐츠를 교체해 기존 파일 편집
- CREATE: 지정된 콘텐츠로 새 파일 생성
- DELETE: 기존 파일 삭제
- UNDO: 같은 사용자가 만든 마지막 커밋 되돌리기
Parameters
name— 선택. 기본값"file_editor". tool의 이름.description— 선택. tool이 무엇을 하는지 LLM에 알려주는 설명.github_token— 필수. API 인증을 위한 GitHub 개인 액세스 토큰. 기본은GITHUB_TOKEN환경 변수 사용.repo— 선택. owner/repo 형식의 기본 저장소 설정.branch— 선택. 기본값"main". 작업할 기본 브랜치.raise_on_failure— 선택. 기본값True.False면 예외를 던지는 대신 에러를 반환.
Usage
pip install github-haystack
저장소 플레이스홀더: 아래 코드 스니펫을 실행하려면 owner/repo를 자신의 GitHub 저장소 이름으로 바꿔야 해요.
On its own
파일을 편집하는 기본 사용법:
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'}
With an Agent
GitHubFileEditorTool을 Agent 구성 요소와 함께 사용할 수 있어요. Agent는 필요할 때 자동으로 tool을 호출해 GitHub 저장소의 파일을 편집해요.
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"],
)
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'.