Github 工具組 (Github Toolkit)
Github
工具組包含讓 LLM 代理程式與 GitHub 儲存庫互動的工具。 此工具是 PyGitHub 函式庫的包裝器。
如需所有 GithubToolkit 功能和配置的詳細文件,請前往 API 參考資料。
設定
大致上,我們會:
- 安裝 pygithub 函式庫
- 建立 Github 應用程式
- 設定您的環境變數
- 使用
toolkit.get_tools()
將工具傳遞給您的代理程式
如果您想從個別工具的執行中取得自動追蹤,您也可以取消註解以下內容來設定您的 LangSmith API 金鑰
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"
安裝
1. 安裝相依性
此整合實作於 langchain-community
中。 我們還需要 pygithub
相依性
%pip install --upgrade --quiet pygithub langchain-community
2. 建立 Github 應用程式
請依照此處的指示建立並註冊 Github 應用程式。 請確保您的應用程式具有下列 儲存庫權限:
- Commit 狀態 (唯讀)
- 內容 (讀寫)
- Issue (讀寫)
- 中繼資料 (唯讀)
- Pull request (讀寫)
註冊應用程式後,您必須授與您的應用程式存取您希望其作用的每個儲存庫的權限。 請使用 github.com 上的應用程式設定。
3. 設定環境變數
在初始化您的代理程式之前,需要設定下列環境變數
- GITHUB_APP_ID - 在您應用程式的一般設定中找到的六位數數字
- GITHUB_APP_PRIVATE_KEY - 您的應用程式私密金鑰 .pem 檔案的位置,或該檔案的完整文字字串。
- GITHUB_REPOSITORY - 您希望您的機器人作用的 Github 儲存庫的名稱。 必須遵循 {username}/{repo-name} 格式。 請確保應用程式已先新增至此儲存庫!
- 選用:GITHUB_BRANCH - 機器人將進行提交的分支。 預設為
repo.default_branch
。 - 選用:GITHUB_BASE_BRANCH - 您的儲存庫的基本分支,PR 將以此為基礎。 預設為
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()
實例化
現在我們可以實例化我們的工具組
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)
API 參考資料:GitHubToolkit | GitHubAPIWrapper
工具
檢視可用的工具
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
這些工具的目的是如下:
以下將詳細說明每個步驟。
-
取得 Issue - 從儲存庫擷取 issue。
-
取得 Issue - 擷取有關特定 issue 的詳細資訊。
-
評論 Issue - 在特定 issue 上張貼評論。
-
建立 Pull Request - 從機器人的工作分支建立到基本分支的 pull request。
-
建立檔案 - 在儲存庫中建立新檔案。
-
讀取檔案 - 從儲存庫讀取檔案。
-
更新檔案 - 更新儲存庫中的檔案。
-
刪除檔案 - 刪除儲存庫中的檔案。
在代理程式中使用
我們需要 LLM 或聊天模型
選取 聊天模型
pip install -qU langchain-openai
import getpass
import os
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
使用工具的子集初始化代理程式
from langgraph.prebuilt import create_react_agent
tools = [tool for tool in toolkit.get_tools() if tool.name == "Get Issue"]
assert len(tools) == 1
tools[0].name = "get_issue"
agent_executor = create_react_agent(llm, tools)
API 參考資料:create_react_agent
並向其發出查詢
example_query = "What is the title of issue 24888?"
events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
for event in events:
event["messages"][-1].pretty_print()
================================[1m Human Message [0m=================================
What is the title of issue 24888?
==================================[1m Ai Message [0m==================================
Tool Calls:
get_issue (call_iSYJVaM7uchfNHOMJoVPQsOi)
Call ID: call_iSYJVaM7uchfNHOMJoVPQsOi
Args:
issue_number: 24888
=================================[1m Tool Message [0m=================================
Name: get_issue
{"number": 24888, "title": "Standardize KV-Store Docs", "body": "To make our KV-store integrations as easy to use as possible we need to make sure the docs for them are thorough and standardized. There are two parts to this: updating the KV-store docstrings and updating the actual integration docs.\r\n\r\nThis needs to be done for each KV-store integration, ideally with one PR per KV-store.\r\n\r\nRelated to broader issues #21983 and #22005.\r\n\r\n## Docstrings\r\nEach KV-store class docstring should have the sections shown in the [Appendix](#appendix) below. The sections should have input and output code blocks when relevant.\r\n\r\nTo build a preview of the API docs for the package you're working on run (from root of repo):\r\n\r\n\`\`\`shell\r\nmake api_docs_clean; make api_docs_quick_preview API_PKG=openai\r\n\`\`\`\r\n\r\nwhere `API_PKG=` should be the parent directory that houses the edited package (e.g. community, openai, anthropic, huggingface, together, mistralai, groq, fireworks, etc.). This should be quite fast for all the partner packages.\r\n\r\n## Doc pages\r\nEach KV-store [docs page](https://langchain-python.dev.org.tw/docs/integrations/stores/) should follow [this template](https://github.com/langchain-ai/langchain/blob/master/libs/cli/langchain_cli/integration_template/docs/kv_store.ipynb).\r\n\r\nHere is an example: https://langchain-python.dev.org.tw/docs/integrations/stores/in_memory/\r\n\r\nYou can use the `langchain-cli` to quickly get started with a new chat model integration docs page (run from root of repo):\r\n\r\n\`\`\`shell\r\npoetry run pip install -e libs/cli\r\npoetry run langchain-cli integration create-doc --name \"foo-bar\" --name-class FooBar --component-type kv_store --destination-dir ./docs/docs/integrations/stores/\r\n\`\`\`\r\n\r\nwhere `--name` is the integration package name without the \"langchain-\" prefix and `--name-class` is the class name without the \"ByteStore\" suffix. This will create a template doc with some autopopulated fields at docs/docs/integrations/stores/foo_bar.ipynb.\r\n\r\nTo build a preview of the docs you can run (from root):\r\n\r\n\`\`\`shell\r\nmake docs_clean\r\nmake docs_build\r\ncd docs/build/output-new\r\nyarn\r\nyarn start\r\n\`\`\`\r\n\r\n## Appendix\r\nExpected sections for the KV-store class docstring.\r\n\r\n\`\`\`python\r\n \"\"\"__ModuleName__ completion KV-store integration.\r\n\r\n # TODO: Replace with relevant packages, env vars.\r\n Setup:\r\n Install ``__package_name__`` and set environment variable ``__MODULE_NAME___API_KEY``.\r\n\r\n .. code-block:: bash\r\n\r\n pip install -U __package_name__\r\n export __MODULE_NAME___API_KEY=\"your-api-key\"\r\n\r\n # TODO: Populate with relevant params.\r\n Key init args \u2014 client params:\r\n api_key: Optional[str]\r\n __ModuleName__ API key. If not passed in will be read from env var __MODULE_NAME___API_KEY.\r\n\r\n See full list of supported init args and their descriptions in the params section.\r\n\r\n # TODO: Replace with relevant init params.\r\n Instantiate:\r\n .. code-block:: python\r\n\r\n from __module_name__ import __ModuleName__ByteStore\r\n\r\n kv_store = __ModuleName__ByteStore(\r\n # api_key=\"...\",\r\n # other params...\r\n )\r\n\r\n Set keys:\r\n .. code-block:: python\r\n\r\n kv_pairs = [\r\n [\"key1\", \"value1\"],\r\n [\"key2\", \"value2\"],\r\n ]\r\n\r\n kv_store.mset(kv_pairs)\r\n\r\n .. code-block:: python\r\n\r\n Get keys:\r\n .. code-block:: python\r\n\r\n kv_store.mget([\"key1\", \"key2\"])\r\n\r\n .. code-block:: python\r\n\r\n # TODO: Example output.\r\n\r\n Delete keys:\r\n ..code-block:: python\r\n\r\n kv_store.mdelete([\"key1\", \"key2\"])\r\n\r\n ..code-block:: python\r\n \"\"\" # noqa: E501\r\n\`\`\`", "comments": "[]", "opened_by": "jacoblee93"}
==================================[1m Ai Message [0m==================================
The title of issue 24888 is "Standardize KV-Store Docs".
API 參考資料
如需所有 GithubToolkit
功能和配置的詳細文件,請前往 API 參考資料。