classQueryEngineTool(AsyncBaseTool):"""Query engine tool. A tool making use of a query engine. Args: query_engine (BaseQueryEngine): A query engine. metadata (ToolMetadata): The associated metadata of the query engine. """def__init__(self,query_engine:BaseQueryEngine,metadata:ToolMetadata,resolve_input_errors:bool=True,)->None:self._query_engine=query_engineself._metadata=metadataself._resolve_input_errors=resolve_input_errors@classmethoddeffrom_defaults(cls,query_engine:BaseQueryEngine,name:Optional[str]=None,description:Optional[str]=None,return_direct:bool=False,resolve_input_errors:bool=True,)->"QueryEngineTool":name=nameorDEFAULT_NAMEdescription=descriptionorDEFAULT_DESCRIPTIONmetadata=ToolMetadata(name=name,description=description,return_direct=return_direct)returncls(query_engine=query_engine,metadata=metadata,resolve_input_errors=resolve_input_errors,)@propertydefquery_engine(self)->BaseQueryEngine:returnself._query_engine@propertydefmetadata(self)->ToolMetadata:returnself._metadatadefcall(self,*args:Any,**kwargs:Any)->ToolOutput:query_str=self._get_query_str(*args,**kwargs)response=self._query_engine.query(query_str)returnToolOutput(content=str(response),tool_name=self.metadata.name,raw_input={"input":query_str},raw_output=response,)asyncdefacall(self,*args:Any,**kwargs:Any)->ToolOutput:query_str=self._get_query_str(*args,**kwargs)response=awaitself._query_engine.aquery(query_str)returnToolOutput(content=str(response),tool_name=self.metadata.name,raw_input={"input":query_str},raw_output=response,)defas_langchain_tool(self)->"LlamaIndexTool":fromllama_index.core.langchain_helpers.agents.toolsimport(IndexToolConfig,LlamaIndexTool,)tool_config=IndexToolConfig(query_engine=self.query_engine,name=self.metadata.name,description=self.metadata.description,)returnLlamaIndexTool.from_tool_config(tool_config=tool_config)def_get_query_str(self,*args:Any,**kwargs:Any)->str:ifargsisnotNoneandlen(args)>0:query_str=str(args[0])elifkwargsisnotNoneand"input"inkwargs:# NOTE: this assumes our default function schema of `input`query_str=kwargs["input"]elifkwargsisnotNoneandself._resolve_input_errors:query_str=str(kwargs)else:raiseValueError("Cannot call query engine without specifying `input` parameter.")returnquery_str