Skip to main content

Github Toolkit

Github工具包包含了一些工具,使LLM代理能够与github存储库进行交互。这些工具是PyGitHub库的一个封装。

快速入门

  1. 安装pygithub库
  2. 创建一个Github应用
  3. 设置环境变量
  4. 使用toolkit.get_tools()将工具传递给代理

下面将详细解释每个步骤。

  1. 获取问题- 从存储库中获取问题。

  2. 获取问题详情- 获取特定问题的详细信息。

  3. 在问题上发表评论- 在特定问题上发表评论。

  4. 创建拉取请求- 从机器人的工作分支创建拉取请求到基础分支。

  5. 创建文件- 在存储库中创建一个新文件。

  6. 读取文件- 从存储库中读取一个文件。

  7. 更新文件- 更新存储库中的一个文件。

  8. 删除文件- 从存储库中删除一个文件。

1. 安装pygithub库

%pip install pygithub

2. 创建一个Github应用

按照这里的说明创建和注册一个Github应用。确保你的应用具有以下存储库权限:

  • 提交状态(只读)
  • 内容(读写)
  • 问题(读写)
  • 元数据(只读)
  • 拉取请求(读写)

注册应用后,将其添加到您希望机器人操作的存储库中。

3. 设置环境变量

在初始化代理之前,需要设置以下环境变量:

  • GITHUB_APP_ID- 在应用的常规设置中找到的一个六位数
  • GITHUB_APP_PRIVATE_KEY- 应用私钥.pem文件的位置
  • GITHUB_REPOSITORY- 您希望机器人操作的Github存储库的名称。必须遵循{用户名}/{存储库名称}的格式。确保已将应用添加到此存储库中!
  • GITHUB_BRANCH- 机器人将进行提交的分支。默认为'master'。
  • GITHUB_BASE_BRANCH- 您的存储库的基本分支,通常为'main'或'master'。这是拉取请求的基础分支。默认为'master'。

示例用法- 简单代理

import os
from langchain.agents import AgentType
from langchain.agents import initialize_agent
from langchain.agents.agent_toolkits.github.toolkit import GitHubToolkit
from langchain.llms import OpenAI
from langchain.utilities.github import GitHubAPIWrapper
# 使用os.environ设置您的环境变量
os.environ["GITHUB_APP_ID"] = "123456"
os.environ["GITHUB_APP_PRIVATE_KEY"] = "path/to/your/private-key.pem"
os.environ["GITHUB_REPOSITORY"] = "username/repo-name"
os.environ["GITHUB_BRANCH"] = "bot-branch-name"
os.environ["GITHUB_BASE_BRANCH"] = "main"

# 这个示例还需要一个OpenAI API密钥
os.environ["OPENAI_API_KEY"] = ""
llm = OpenAI(temperature=0)
github = GitHubAPIWrapper()
toolkit = GitHubToolkit.from_github_api_wrapper(github)
agent = initialize_agent(
toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
agent.run(
"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."
)
    

> 进入新的AgentExecutor链...
我需要弄清楚哪些问题需要完成。
动作:获取问题
动作输入:无
观察结果:找到1个问题:
[{'title': 'Update README file', 'number': 9}]
思考:我需要获取关于这个问题的更多信息。
动作:获取问题
动作输入:9
观察结果:{"title": "Update README file", "body": "Find what the most popular frontend framework is right now and add a short blurb to the readme.md file about how this website will take advantage of it.", "comments": "[]"}
思考:我需要更新README文件。
动作:创建文件
动作输入:README.md
观察结果:README.md文件已存在。请使用update_file代替
思考:我需要更新现有的README文件。
动作:更新文件
动作输入:README.md
旧的 <<<<
This is a sample website
>>>> 旧的
新的 <<<<
This is a sample website that uses the most popular frontend framework.
>>>> 新的
观察结果:文件内容未更新,因为找不到旧内容。使用read_file操作获取当前文件内容可能会有帮助。
思考:我需要获取当前文件内容。
动作:读取文件
动作输入:README.md
观察结果:This is my awesome website!

思考:我需要使用新内容更新README文件。
动作:更新文件
动作输入:README.md
旧的 <<<<
This is my awesome website!
>>>> 旧的
新的 <<<<
This is my awesome website that uses the most popular frontend framework.
>>>> 新的
观察结果:已更新文件README.md
思考:我现在知道最终答案了。
最终答案:README.md文件已使用新内容更新。

> 完成链。






'README.md文件已使用新内容更新。'

示例用法- 高级代理

如果您的代理不需要使用所有8个工具,您可以单独构建工具来使用。在这个示例中,我们将创建一个不使用create_file、delete_file或create_pull_request工具的代理,但可以使用duckduckgo-search。

%pip install duckduckgo-search
from langchain.tools.github.tool import GitHubAction
from langchain.tools import DuckDuckGoSearchRun
from langchain.agents import Tool
from langchain.chat_models import ChatOpenAI

tools = []
unwanted_tools = ['Get Issue','Delete File', 'Create File', 'Create Pull Request']

for tool in toolkit.get_tools():
if tool.name not in unwanted_tools:
tools.append(tool)
tools+= [
Tool(
name = "Search",
func = DuckDuckGoSearchRun().run,
description = "useful for when you need to search the web"
)]

agent = initialize_agent(
tools = tools,
llm = ChatOpenAI(temperature=0.1),
agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose = True
)

最后让我们构建一个提示并进行测试!

# GitHubAPIWrapper也可以在代理之外使用
# 这里获取问题编号为9的信息,因为我们想要强制代理处理这个特定问题。

issue = github.get_issue(9)

prompt = f"""
You are a seinor frontend developer who is experienced in HTML, CSS, and JS- especially React.
You have been assigned the below issue. Complete it to the best of your ability.
Remember to first make a plan and pay attention to details like file names and commonsense.
Then execute the plan and use tools appropriately.
Finally, make a pull request to merge your changes.
Issue: {issue["title"]}
Issue Description: {issue['body']}
Comments: {issue['comments']}"""

agent.run(prompt)
    

> 进入新的AgentExecutor链...
要完成此问题,我需要找到最受欢迎的前端框架,并在readme.md文件中添加一个关于这个网站如何利用它的简短说明。我应该先研究最受欢迎的前端框架,然后相应地更新readme文件。我将使用"Search"工具来研究最受欢迎的前端框架。

动作:搜索
动作输入:"most popular frontend framework"
观察结果:Alex Ivanovs February 25, 2023 Table of Contents What are the current Front-end trends? Top Front-end Frameworks for 2023 #1 - React #2 - Angular #3 - Vue #4 - Svelte #5 - Preact #6 - Ember #7 - Solid #8 - Lit #9 - Alpine #10 - Stencil #11 - Qwik Front-end Frameworks: A Summary Top 6 Frontend Frameworks To Use in 2022 by Nwose Lotanna Victor August 26, 2022 Web 0 Comments This post reveals the top six frontend libraries to use in 2022. The list is fresh and very different from the previous years. State of JS Though React is the most popular framework for frontend development, it also has some shortcomings. Due to its limitations, the idea was to design a small-size framework that will offer the same features as React. This is how a tiny version of React — Preact — appeared. Top 10 Popular Frontend Frameworks to Use in 2023 Sep 26, 2022 10 min Сontents 1. What is a framework? 2. Front-end frameworks vs backend frameworks 3. The best front-end frameworks in 2023 React Vue.js Angular Svelte JQuery Ember Backbone Semantic UI 4. Final words Technostacks Jan 11 2023 Top Frontend Frameworks of 2023 for Web Development Developing what the users see on their screens is the role of a front-end web developer. Unarguably, front-end developers worldwide are trying to use the best front-end frameworks to provide the best user experience.
思考:根据我的研究,目前最受欢迎的前端框架是React。现在我将更新readme.md文件,包括一个关于这个网站如何利用React的简短说明。

动作:更新文件
动作输入:
README.md
旧的 <<<<
This is the readme file for the website.
>>>> 旧的
新的 <<<<
This is the readme file for the website.

This website takes advantage of the React framework, which allows for efficient and reusable UI components. With React, we can easily manage the state of our application and create interactive user interfaces. It provides a smooth and seamless user experience, making this website highly responsive and dynamic.
>>>> 新的

观察结果:文件内容未更新,因为找不到旧内容。使用read_file操作获取当前文件内容可能会有帮助。
思考:我需要首先读取README.md文件的内容以获取当前内容。然后我可以使用新内容更新文件。

动作:读取文件
动作输入:README.md
观察结果:This is my awesome website that uses the most popular frontend framework.

思考:README.md文件的当前内容是"This is my awesome website that uses the most popular frontend framework.",我现在可以使用新内容更新文件。

动作:更新文件
动作输入:
README.md
旧的 <<<<
This is my awesome website that uses the most popular frontend framework.
>>>> 旧的
新的 <<<<
This is my awesome website that uses the most popular frontend framework.

This website takes advantage of the React framework, which allows for efficient and reusable UI components. With React, we can easily manage the state of our application and create interactive user interfaces. It provides a smooth and seamless user experience, making this website highly responsive and dynamic.
>>>> 新的

观察结果:已更新文件README.md
思考:我已成功更新README.md文件,添加了关于这个网站如何利用React框架的简短说明。

最终答案:目前最受欢迎的前端框架是React。这个网站利用React来创建高效和可重用的UI组件,管理应用程序状态,并提供流畅和无缝的用户体验。

> 完成链。






'目前最受欢迎的前端框架是React。这个网站利用React来创建高效和可重用的UI组件,管理应用程序状态,并提供流畅和无缝的用户体验。'