Github Toolkit
The Github
toolkit contains tools that enable an LLM agent to interact with a github repository.
The tool is a wrapper for the PyGitHub library.
For detailed documentation of all GithubToolkit features and configurations head to the API reference.
Setup
At a high-level, we will:
- Install the pygithub library
- Create a Github app
- Set your environmental variables
- Pass the tools to your agent with
toolkit.get_tools()
If you want to get automated tracing from runs of individual tools, you can also set your LangSmith API key by uncommenting below:
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"
Installation
1. Install dependencies
This integration is implemented in langchain-community
. We will also need the pygithub
dependency:
%pip install --upgrade --quiet pygithub langchain-community
2. Create a Github App
Follow the instructions here to create and register a Github app. Make sure your app has the following repository permissions:
- Commit statuses (read only)
- Contents (read and write)
- Issues (read and write)
- Metadata (read only)
- Pull requests (read and write)
Once the app has been registered, you must give your app permission to access each of the repositories you whish it to act upon. Use the App settings on github.com here.
3. Set Environment Variables
Before initializing your agent, the following environment variables need to be set:
- GITHUB_APP_ID- A six digit number found in your app's general settings
- GITHUB_APP_PRIVATE_KEY- The location of your app's private key .pem file, or the full text of that file as a string.
- GITHUB_REPOSITORY- The name of the Github repository you want your bot to act upon. Must follow the format {username}/{repo-name}. Make sure the app has been added to this repository first!
- Optional: GITHUB_BRANCH- The branch where the bot will make its commits. Defaults to
repo.default_branch
. - Optional: GITHUB_BASE_BRANCH- The base branch of your repo upon which PRs will based from. Defaults to
repo.default_branch
.
import getpass
import os
for env_var in [
"GITHUB_APP_ID",
"GITHUB_APP_PRIVATE_KEY",
"GITHUB_REPOSITORY",
]:
if not os.getenv(env_var):
os.environ[env_var] = getpass.getpass()
Instantiation
Now we can instantiate our toolkit:
from langchain_community.agent_toolkits.github.toolkit import GitHubToolkit
from langchain_community.utilities.github import GitHubAPIWrapper
github = GitHubAPIWrapper()
toolkit = GitHubToolkit.from_github_api_wrapper(github)
Tools
View available tools:
tools = toolkit.get_tools()
for tool in tools:
print(tool.name)
Get Issues
Get Issue
Comment on Issue
List open pull requests (PRs)
Get Pull Request
Overview of files included in PR
Create Pull Request
List Pull Requests' Files
Create File
Read File
Update File
Delete File
Overview of existing files in Main branch
Overview of files in current working branch
List branches in this repository
Set active branch
Create a new branch
Get files from a directory
Search issues and pull requests
Search code
Create review request
The purpose of these tools is as follows:
Each of these steps will be explained in great detail below.
-
Get Issues- fetches issues from the repository.
-
Get Issue- fetches details about a specific issue.
-
Comment on Issue- posts a comment on a specific issue.
-
Create Pull Request- creates a pull request from the bot's working branch to the base branch.
-
Create File- creates a new file in the repository.
-
Read File- reads a file from the repository.
-
Update File- updates a file in the repository.
-
Delete File- deletes a file from the repository.