community[patch]: support named arguments in github toolkit (#24986)

Parameters may be passed in by name if generated from tool calls.
This commit is contained in:
ccurme
2024-08-02 14:27:32 -04:00
committed by GitHub
parent 4797b806c2
commit 22c1a4041b
2 changed files with 72 additions and 695 deletions

View File

@@ -8,7 +8,7 @@ To use this tool, you must first set as environment variables:
"""
from typing import Optional, Type
from typing import Any, Optional, Type
from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.pydantic_v1 import BaseModel, Field
@@ -30,9 +30,23 @@ class GitHubAction(BaseTool):
self,
instructions: Optional[str] = "",
run_manager: Optional[CallbackManagerForToolRun] = None,
**kwargs: Any,
) -> str:
"""Use the GitHub API to run an operation."""
if not instructions or instructions == "{}":
# Catch other forms of empty input that GPT-4 likes to send.
instructions = ""
return self.api_wrapper.run(self.mode, instructions)
if self.args_schema is not None:
field_names = list(self.args_schema.schema()["properties"].keys())
if len(field_names) > 1:
raise AssertionError(
f"Expected one argument in tool schema, got {field_names}."
)
if field_names:
field = field_names[0]
else:
field = ""
query = str(kwargs.get(field, ""))
else:
query = instructions
return self.api_wrapper.run(self.mode, query)