[docs]classTigerGraph(GraphStore):"""TigerGraph wrapper for graph operations. *Security note*: Make sure that the database connection uses credentials that are narrowly-scoped to only include necessary permissions. Failure to do so may result in data corruption or loss, since the calling code may attempt commands that would result in deletion, mutation of data if appropriately prompted or reading sensitive data if such data is present in the database. The best way to guard against such negative outcomes is to (as appropriate) limit the permissions granted to the credentials used with this tool. See https://python.langchain.com/docs/security for more information. """
[docs]def__init__(self,conn:Any)->None:"""Create a new TigerGraph graph wrapper instance."""self.set_connection(conn)self.set_schema()
[docs]defset_connection(self,conn:Any)->None:try:frompyTigerGraphimportTigerGraphConnectionexceptImportError:raiseImportError("Could not import pyTigerGraph python package. ""Please install it with `pip install pyTigerGraph`.")ifnotisinstance(conn,TigerGraphConnection):msg="**conn** parameter must inherit from TigerGraphConnection"raiseTypeError(msg)ifconn.ai.nlqs_hostisNone:msg="""**conn** parameter does not have nlqs_host parameter defined. Define hostname of NLQS service."""raiseConnectionError(msg)self._conn:TigerGraphConnection=connself.set_schema()
[docs]defset_schema(self,schema:Optional[Dict[str,Any]]=None)->None:""" Set the schema of the TigerGraph Database. Auto-generates Schema if **schema** is None. """self._schema=self.generate_schema()ifschemaisNoneelseschema
[docs]defgenerate_schema(self,)->Dict[str,List[Dict[str,Any]]]:""" Generates the schema of the TigerGraph Database and returns it User can specify a **sample_ratio** (0 to 1) to determine the ratio of documents/edges used (in relation to the Collection size) to render each Collection Schema. """returnself._conn.getSchema(force=True)
[docs]defquery(self,query:str)->Dict[str,Any]:# type: ignore[override]"""Query the TigerGraph database."""answer=self._conn.ai.query(query)returnanswer
[docs]defregister_query(self,function_header:str,description:str,docstring:str,param_types:dict={},)->List[str]:""" Wrapper function to register a custom GSQL query to the TigerGraph NLQS. """returnself._conn.ai.registerCustomQuery(function_header,description,docstring,param_types)