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)