{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GitHub\n", "\n", "This notebook goes over how to use the GitHub tool.\n", "The GitHub tool allows agents to interact with a given GitHub repository. It implements CRUD operations for modifying files and can read/comment on Issues. The tool wraps the [PyGitHub](https://github.com/PyGithub/PyGithub) library.\n", "\n", "In order to interact with the GitHub API you must create a [GitHub app](https://docs.github.com/en/apps/creating-github-apps/about-creating-github-apps/about-creating-github-apps). Next, you must set the following environment variables:\n", "```\n", "GITHUB_APP_ID\n", "GITHUB_APP_PRIVATE_KEY\n", "GITHUB_REPOSITORY\n", "GITHUB_BRANCH\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "shellscript" } }, "outputs": [], "source": [ "%pip install pygithub" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os\n", "from langchain.agents import AgentType\n", "from langchain.agents import initialize_agent\n", "from langchain.agents.agent_toolkits.github.toolkit import GitHubToolkit\n", "from langchain.llms import OpenAI\n", "from langchain.utilities.github import GitHubAPIWrapper" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "os.environ[\"GITHUB_APP_ID\"] = \"your-github-app-id\"\n", "os.environ[\"GITHUB_APP_PRIVATE_KEY\"] = \"/path/to/your/private/key\"\n", "os.environ[\"GITHUB_REPOSITORY\"] = \"user/repo\"\n", "os.environ[\"GITHUB_BRANCH\"] = \"branch-name\"\n", "os.environ[\"OPENAI_API_KEY\"] = \"your-openai-api-key\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "llm = OpenAI(temperature=0)\n", "github = GitHubAPIWrapper()\n", "toolkit = GitHubToolkit.from_github_api_wrapper(github)\n", "agent = initialize_agent(\n", " toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m I need to figure out what issues need to be completed and how to complete them.\n", "Action: Get Issues\n", "Action Input: N/A\u001b[0m\n", "Observation: \u001b[36;1m\u001b[1;3mFound 1 issues:\n", "[{'title': 'Change the main script to print Hello AI!', 'number': 1}]\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I need to get more information about this issue.\n", "Action: Get Issue\n", "Action Input: 1\u001b[0m\n", "Observation: \u001b[33;1m\u001b[1;3m{'title': 'Change the main script to print Hello AI!', 'body': None, 'comments': '[]'}\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I need to update the main script to print Hello AI!\n", "Action: Update File\n", "Action Input: main.py\n", "OLD <<<<\n", "print(\"Hello World!\")\n", ">>>> OLD\n", "NEW <<<<\n", "print(\"Hello AI!\")\n", ">>>> NEW\u001b[0m\n", "Observation: \u001b[38;5;200m\u001b[1;3mFile content was not updated because the old content was not found. It may be helpful to use the read_file action to get the current file contents.\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I need to read the current file contents.\n", "Action: Read File\n", "Action Input: main.py\u001b[0m\n", "Observation: \u001b[33;1m\u001b[1;3mprint(\"Hello world!\")\n", "\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I need to update the main script to print Hello AI!\n", "Action: Update File\n", "Action Input: main.py\n", "OLD <<<<\n", "print(\"Hello world!\")\n", ">>>> OLD\n", "NEW <<<<\n", "print(\"Hello AI!\")\n", ">>>> NEW\u001b[0m\n", "Observation: \u001b[38;5;200m\u001b[1;3mUpdated file main.py\u001b[0m\n", "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", "Final Answer: The main script has been updated to print \"Hello AI!\"\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'The main script has been updated to print \"Hello AI!\"'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agent.run(\n", " \"You have the software engineering capabilities of a Google Principle engineer. You are tasked with completing issues on a github repository. Please look at the existing issues and complete them.\"\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }