codekingpro/portable-devtools
114k
1import json2from typing import Optional3 4from langchain_core.callbacks import CallbackManagerForToolRun5from langchain_core.tools import BaseTool6from pydantic import ConfigDict7 8from langchain_community.utilities.graphql import GraphQLAPIWrapper9 10 11class BaseGraphQLTool(BaseTool):12 """Base tool for querying a GraphQL API."""13 14 graphql_wrapper: GraphQLAPIWrapper15 16 name: str = "query_graphql"17 description: str = """\18 Input to this tool is a detailed and correct GraphQL query, output is a result from the API.19 If the query is not correct, an error message will be returned.20 If an error is returned with 'Bad request' in it, rewrite the query and try again.21 If an error is returned with 'Unauthorized' in it, do not try again, but tell the user to change their authentication.22 23 Example Input: query {{ allUsers {{ id, name, email }} }}\24 """ # noqa: E50125 26 model_config = ConfigDict(27 arbitrary_types_allowed=True,28 )29 30 def _run(31 self,32 tool_input: str,33 run_manager: Optional[CallbackManagerForToolRun] = None,34 ) -> str:35 result = self.graphql_wrapper.run(tool_input)36 return json.dumps(result, indent=2)37 