|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Github\n", |
| 7 | + "---\n", |
| 8 | + "sidebar_label: Github\n", |
| 9 | + "---" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# GithubToolkit\n", |
8 | 17 | "\n",
|
9 | 18 | "The `Github` toolkit contains tools that enable an LLM agent to interact with a github repository. \n",
|
10 | 19 | "The tool is a wrapper for the [PyGitHub](https://github.com/PyGithub/PyGithub) library. \n",
|
11 | 20 | "\n",
|
12 |
| - "## Quickstart\n", |
| 21 | + "For detailed documentation of all GithubToolkit features and configurations head to the [API reference](https://api.python.langchain.com/en/latest/agent_toolkits/langchain_community.agent_toolkits.github.toolkit.GitHubToolkit.html).\n", |
| 22 | + "\n", |
| 23 | + "## Setup\n", |
| 24 | + "\n", |
| 25 | + "At a high-level, we will:\n", |
13 | 26 | "\n",
|
14 | 27 | "1. Install the pygithub library\n",
|
15 | 28 | "2. Create a Github app\n",
|
16 | 29 | "3. Set your environmental variables\n",
|
17 |
| - "4. Pass the tools to your agent with `toolkit.get_tools()`\n", |
18 |
| - "\n", |
19 |
| - "Each of these steps will be explained in great detail below.\n", |
20 |
| - "\n", |
21 |
| - "1. **Get Issues**- fetches issues from the repository.\n", |
22 |
| - "\n", |
23 |
| - "2. **Get Issue**- fetches details about a specific issue.\n", |
24 |
| - "\n", |
25 |
| - "3. **Comment on Issue**- posts a comment on a specific issue.\n", |
26 |
| - "\n", |
27 |
| - "4. **Create Pull Request**- creates a pull request from the bot's working branch to the base branch.\n", |
28 |
| - "\n", |
29 |
| - "5. **Create File**- creates a new file in the repository.\n", |
30 |
| - "\n", |
31 |
| - "6. **Read File**- reads a file from the repository.\n", |
32 |
| - "\n", |
33 |
| - "7. **Update File**- updates a file in the repository.\n", |
34 |
| - "\n", |
35 |
| - "8. **Delete File**- deletes a file from the repository.\n", |
36 |
| - "\n" |
| 30 | + "4. Pass the tools to your agent with `toolkit.get_tools()`" |
37 | 31 | ]
|
38 | 32 | },
|
39 | 33 | {
|
40 | 34 | "cell_type": "markdown",
|
41 | 35 | "metadata": {},
|
42 | 36 | "source": [
|
43 |
| - "## Setup" |
| 37 | + "If you want to get automated tracing from runs of individual tools, you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")\n", |
| 47 | + "# os.environ[\"LANGSMITH_TRACING\"] = \"true\"" |
44 | 48 | ]
|
45 | 49 | },
|
46 | 50 | {
|
47 | 51 | "cell_type": "markdown",
|
48 | 52 | "metadata": {},
|
49 | 53 | "source": [
|
50 |
| - "### 1. Install the `pygithub` library " |
| 54 | + "### Installation\n", |
| 55 | + "\n", |
| 56 | + "#### 1. Install dependencies\n", |
| 57 | + "\n", |
| 58 | + "This integration is implemented in `langchain-community`. We will also need the `pygithub` dependency:" |
51 | 59 | ]
|
52 | 60 | },
|
53 | 61 | {
|
54 | 62 | "cell_type": "code",
|
55 | 63 | "execution_count": null,
|
56 |
| - "metadata": { |
57 |
| - "vscode": { |
58 |
| - "languageId": "shellscript" |
59 |
| - } |
60 |
| - }, |
| 64 | + "metadata": {}, |
61 | 65 | "outputs": [],
|
62 | 66 | "source": [
|
63 | 67 | "%pip install --upgrade --quiet pygithub langchain-community"
|
|
67 | 71 | "cell_type": "markdown",
|
68 | 72 | "metadata": {},
|
69 | 73 | "source": [
|
70 |
| - "### 2. Create a Github App\n", |
| 74 | + "#### 2. Create a Github App\n", |
71 | 75 | "\n",
|
72 | 76 | "[Follow the instructions here](https://docs.github.com/en/apps/creating-github-apps/registering-a-github-app/registering-a-github-app) to create and register a Github app. Make sure your app has the following [repository permissions:](https://docs.github.com/en/rest/overview/permissions-required-for-github-apps?apiVersion=2022-11-28)\n",
|
73 | 77 | "\n",
|
|
77 | 81 | "* Metadata (read only)\n",
|
78 | 82 | "* Pull requests (read and write)\n",
|
79 | 83 | "\n",
|
80 |
| - "\n", |
81 | 84 | "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](https://github.com/settings/installations).\n",
|
82 | 85 | "\n",
|
83 |
| - "### 3. Set Environmental Variables\n", |
84 | 86 | "\n",
|
85 |
| - "Before initializing your agent, the following environmental variables need to be set:\n", |
| 87 | + "#### 3. Set Environment Variables\n", |
| 88 | + "\n", |
| 89 | + "Before initializing your agent, the following environment variables need to be set:\n", |
86 | 90 | "\n",
|
87 | 91 | "* **GITHUB_APP_ID**- A six digit number found in your app's general settings\n",
|
88 | 92 | "* **GITHUB_APP_PRIVATE_KEY**- The location of your app's private key .pem file, or the full text of that file as a string.\n",
|
89 | 93 | "* **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!*\n",
|
90 | 94 | "* Optional: **GITHUB_BRANCH**- The branch where the bot will make its commits. Defaults to `repo.default_branch`.\n",
|
91 |
| - "* Optional: **GITHUB_BASE_BRANCH**- The base branch of your repo upon which PRs will based from. Defaults to `repo.default_branch`.\n" |
| 95 | + "* Optional: **GITHUB_BASE_BRANCH**- The base branch of your repo upon which PRs will based from. Defaults to `repo.default_branch`." |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "import getpass\n", |
| 105 | + "import os\n", |
| 106 | + "\n", |
| 107 | + "for env_var in [\n", |
| 108 | + " \"GITHUB_APP_ID\",\n", |
| 109 | + " \"GITHUB_APP_PRIVATE_KEY\",\n", |
| 110 | + " \"GITHUB_REPOSITORY\",\n", |
| 111 | + "]:\n", |
| 112 | + " if not os.getenv(env_var):\n", |
| 113 | + " os.environ[env_var] = getpass.getpass()" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "markdown", |
| 118 | + "metadata": {}, |
| 119 | + "source": [ |
| 120 | + "## Instantiation\n", |
| 121 | + "\n", |
| 122 | + "Now we can instantiate our toolkit:" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": 6, |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [], |
| 130 | + "source": [ |
| 131 | + "from langchain_community.agent_toolkits.github.toolkit import GitHubToolkit\n", |
| 132 | + "from langchain_community.utilities.github import GitHubAPIWrapper\n", |
| 133 | + "\n", |
| 134 | + "github = GitHubAPIWrapper()\n", |
| 135 | + "toolkit = GitHubToolkit.from_github_api_wrapper(github)" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "markdown", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "## Tools\n", |
| 143 | + "\n", |
| 144 | + "View available tools:" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": 9, |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [ |
| 152 | + { |
| 153 | + "name": "stdout", |
| 154 | + "output_type": "stream", |
| 155 | + "text": [ |
| 156 | + "Get Issues\n", |
| 157 | + "Get Issue\n", |
| 158 | + "Comment on Issue\n", |
| 159 | + "List open pull requests (PRs)\n", |
| 160 | + "Get Pull Request\n", |
| 161 | + "Overview of files included in PR\n", |
| 162 | + "Create Pull Request\n", |
| 163 | + "List Pull Requests' Files\n", |
| 164 | + "Create File\n", |
| 165 | + "Read File\n", |
| 166 | + "Update File\n", |
| 167 | + "Delete File\n", |
| 168 | + "Overview of existing files in Main branch\n", |
| 169 | + "Overview of files in current working branch\n", |
| 170 | + "List branches in this repository\n", |
| 171 | + "Set active branch\n", |
| 172 | + "Create a new branch\n", |
| 173 | + "Get files from a directory\n", |
| 174 | + "Search issues and pull requests\n", |
| 175 | + "Search code\n", |
| 176 | + "Create review request\n" |
| 177 | + ] |
| 178 | + } |
| 179 | + ], |
| 180 | + "source": [ |
| 181 | + "tools = toolkit.get_tools()\n", |
| 182 | + "\n", |
| 183 | + "for tool in tools:\n", |
| 184 | + " print(tool.name)" |
92 | 185 | ]
|
93 | 186 | },
|
94 | 187 | {
|
95 | 188 | "cell_type": "markdown",
|
96 | 189 | "metadata": {},
|
97 | 190 | "source": [
|
98 |
| - "## Example: Simple Agent" |
| 191 | + "The purpose of these tools is as follows:\n", |
| 192 | + "\n", |
| 193 | + "Each of these steps will be explained in great detail below.\n", |
| 194 | + "\n", |
| 195 | + "1. **Get Issues**- fetches issues from the repository.\n", |
| 196 | + "\n", |
| 197 | + "2. **Get Issue**- fetches details about a specific issue.\n", |
| 198 | + "\n", |
| 199 | + "3. **Comment on Issue**- posts a comment on a specific issue.\n", |
| 200 | + "\n", |
| 201 | + "4. **Create Pull Request**- creates a pull request from the bot's working branch to the base branch.\n", |
| 202 | + "\n", |
| 203 | + "5. **Create File**- creates a new file in the repository.\n", |
| 204 | + "\n", |
| 205 | + "6. **Read File**- reads a file from the repository.\n", |
| 206 | + "\n", |
| 207 | + "7. **Update File**- updates a file in the repository.\n", |
| 208 | + "\n", |
| 209 | + "8. **Delete File**- deletes a file from the repository." |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "markdown", |
| 214 | + "metadata": {}, |
| 215 | + "source": [ |
| 216 | + "## Use within an agent" |
99 | 217 | ]
|
100 | 218 | },
|
101 | 219 | {
|
|
824 | 942 | "\n",
|
825 | 943 | "agent.run(prompt)"
|
826 | 944 | ]
|
| 945 | + }, |
| 946 | + { |
| 947 | + "cell_type": "markdown", |
| 948 | + "metadata": {}, |
| 949 | + "source": [ |
| 950 | + "## API reference\n", |
| 951 | + "\n", |
| 952 | + "For detailed documentation of all `GithubToolkit` features and configurations head to the [API reference](https://api.python.langchain.com/en/latest/agent_toolkits/langchain_community.agent_toolkits.github.toolkit.GitHubToolkit.html)." |
| 953 | + ] |
827 | 954 | }
|
828 | 955 | ],
|
829 | 956 | "metadata": {
|
|
842 | 969 | "name": "python",
|
843 | 970 | "nbconvert_exporter": "python",
|
844 | 971 | "pygments_lexer": "ipython3",
|
845 |
| - "version": "3.10.13" |
| 972 | + "version": "3.10.4" |
846 | 973 | }
|
847 | 974 | },
|
848 | 975 | "nbformat": 4,
|
|
0 commit comments