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 應用程式。請確保您的應用程式具有下列 儲存庫權限:
- 提交狀態(唯讀)
- 內容(讀寫)
- 議題(讀寫)
- 元數據(唯讀)
- 提取請求(讀寫)
應用程式註冊完成後,您必須授予您的應用程式存取權限,以存取您希望它執行的每個儲存庫。請在此處使用 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)
工具
檢視可用工具
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
這些工具的用途如下
以下將詳細說明這些步驟。
-
Get Issues- 從儲存庫中提取議題。
-
Get Issue- 提取關於特定議題的詳細資訊。
-
Comment on Issue- 在特定議題上發佈評論。
-
Create Pull Request- 從機器人的工作分支建立到基礎分支的提取請求。
-
Create File- 在儲存庫中建立新檔案。
-
Read File- 從儲存庫中讀取檔案。
-
Update File- 更新儲存庫中的檔案。
-
Delete File- 從儲存庫中刪除檔案。
包含發佈工具
預設情況下,工具套件不包含與發佈相關的工具。您可以在初始化工具套件時設定 include_release_tools=True
來包含它們
toolkit = GitHubToolkit.from_github_api_wrapper(github, include_release_tools=True)
設定 include_release_tools=True
將包含下列工具
-
Get Latest Release- 從儲存庫中提取最新發佈。
-
Get Releases- 從儲存庫中提取最新的 5 個發佈。
-
Get Release- 從儲存庫中按標籤名稱(例如
v1.0.0
)提取特定發佈。
在代理程式中使用
我們將需要 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.chat_models import init_chat_model
llm = init_chat_model("gpt-4o-mini", model_provider="openai")
使用工具子集初始化代理程式
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)
並向其發出查詢
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 參考文件。