codekingpro/portable-devtools
114k
1"""2This tool allows agents to interact with the pygithub library3and operate on a GitHub repository.4 5To use this tool, you must first set as environment variables:6 GITHUB_API_TOKEN7 GITHUB_REPOSITORY -> format: {owner}/{repo}8 9"""10 11from typing import Any, Optional, Type12 13from langchain_core.callbacks import CallbackManagerForToolRun14from langchain_core.tools import BaseTool15from pydantic import BaseModel, Field16 17from langchain_community.utilities.github import GitHubAPIWrapper18 19 20class GitHubAction(BaseTool):21 """Tool for interacting with the GitHub API."""22 23 api_wrapper: GitHubAPIWrapper = Field(default_factory=GitHubAPIWrapper)24 mode: str25 name: str = ""26 description: str = ""27 args_schema: Optional[Type[BaseModel]] = None28 29 def _run(30 self,31 instructions: Optional[str] = "",32 run_manager: Optional[CallbackManagerForToolRun] = None,33 **kwargs: Any,34 ) -> str:35 """Use the GitHub API to run an operation."""36 if not instructions or instructions == "{}":37 # Catch other forms of empty input that GPT-4 likes to send.38 instructions = ""39 if self.args_schema is not None:40 field_names = list(self.args_schema.schema()["properties"].keys())41 if len(field_names) > 1:42 raise AssertionError(43 f"Expected one argument in tool schema, got {field_names}."44 )45 if field_names:46 field = field_names[0]47 else:48 field = ""49 query = str(kwargs.get(field, ""))50 else:51 query = instructions52 return self.api_wrapper.run(self.mode, query)53 