From 5c39cb440a1b51153a66a52b8e393ca189b484eb Mon Sep 17 00:00:00 2001 From: Parker Erickson Date: Thu, 14 Dec 2023 12:49:24 -0600 Subject: [PATCH 1/6] feat(ai): add nlqs support --- pyTigerGraph/ai/ai.py | 77 ++++++++++++++++++++++++++++++++++++ pyTigerGraph/pyTigerGraph.py | 13 ++++++ 2 files changed, 90 insertions(+) create mode 100644 pyTigerGraph/ai/ai.py diff --git a/pyTigerGraph/ai/ai.py b/pyTigerGraph/ai/ai.py new file mode 100644 index 00000000..7938096d --- /dev/null +++ b/pyTigerGraph/ai/ai.py @@ -0,0 +1,77 @@ +import json + +class AI: + def __init__(self, conn: "TigerGraphConnection") -> None: + """NO DOC: Initiate an AI object. + Args: + conn (TigerGraphConnection): + Accept a TigerGraphConnection to run queries with + + Returns: + None + """ + self.conn = conn + self.nlqs_host = None + + def configureInquiryAIHost(self, hostname: str): + """ Configure the hostname of the InquiryAI service. + Args: + hostname (str): + The hostname (and port number) of the InquiryAI serivce. + """ + self.nlqs_host = hostname + + def registerCustomQuery(self, function_header: str, description: str, docstring: str, param_types: dict = {}): + """ Register a custom query with the InquiryAI service. + Args: + function_header (str): + The name of the query being registered. + description (str): + The high-level description of the query being registered. + docstring (str): + The docstring of the query being registered. Includes information about each parameter. + param_types (Dict[str, str]): + The types of the parameters. In the format {"param_name": "param_type"} + Returns: + Hash of query that was registered. + """ + data = { + "function_header": function_header, + "description": description, + "docstring": docstring, + "param_types": param_types + } + url = self.nlqs_host+"/"+self.conn.graphname+"/registercustomquery" + return self.conn._req("POST", url, authMode="pwd", data = data, jsonData=True, resKey=None) + + def retrieveDocs(self, query:str, top_k:int = 3): + """ Retrieve docs from the vector store. + Args: + query (str): + The natural language query to retrieve docs with. + top_k (int): + The number of docs to retrieve. + Returns: + List of docs retrieved. + """ + data = { + "query": query + } + + url = self.nlqs_host+"/"+self.conn.graphname+"/retrievedocs?top_k="+str(top_k) + return self.conn._req("POST", url, authMode="pwd", data = data, jsonData=True, resKey=None, skipCheck=True) + + def query(self, query): + """ Query the database with natural language. + Args: + query (str): + Natural language query to ask about the database. + Returns: + JSON including the natural language response, a answered_question flag, and answer sources. + """ + data = { + "query": query + } + + url = self.nlqs_host+"/"+self.conn.graphname+"/query" + return self.conn._req("POST", url, authMode="pwd", data = data, jsonData=True, resKey=None) \ No newline at end of file diff --git a/pyTigerGraph/pyTigerGraph.py b/pyTigerGraph/pyTigerGraph.py index 8620a903..24a15738 100644 --- a/pyTigerGraph/pyTigerGraph.py +++ b/pyTigerGraph/pyTigerGraph.py @@ -36,6 +36,7 @@ def __init__(self, host: str = "http://127.0.0.1", graphname: str = "MyGraph", gsPort, gsqlVersion, version, apiToken, useCert, certPath, debug, sslPort, gcp) self.gds = None + self.ai = None def __getattribute__(self, name): if name == "gds": @@ -50,6 +51,18 @@ def __getattribute__(self, name): "Check the https://docs.tigergraph.com/pytigergraph/current/getting-started/install#_install_pytigergraphgds for more details.") else: return super().__getattribute__(name) + elif name == "ai": + if super().__getattribute__(name) is None: + try: + from .ai import ai + self.ai = ai.AI(self) + return super().__getattribute__(name) + except: + raise Exception( + "Error importing AI submodule." + ) + else: + return super().__getattribute__(name) else: return super().__getattribute__(name) From 06ffb91a8ed7530178e30095a3c7aeefbfba97df Mon Sep 17 00:00:00 2001 From: Parker Erickson Date: Thu, 14 Dec 2023 12:50:35 -0600 Subject: [PATCH 2/6] doc(ai): add beta test note. --- pyTigerGraph/ai/ai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyTigerGraph/ai/ai.py b/pyTigerGraph/ai/ai.py index 7938096d..e2717f68 100644 --- a/pyTigerGraph/ai/ai.py +++ b/pyTigerGraph/ai/ai.py @@ -2,7 +2,7 @@ class AI: def __init__(self, conn: "TigerGraphConnection") -> None: - """NO DOC: Initiate an AI object. + """NO DOC: Initiate an AI object. Currently in beta testing. Args: conn (TigerGraphConnection): Accept a TigerGraphConnection to run queries with From ee05aa382a00153ff1625008e73b546be21dd6f5 Mon Sep 17 00:00:00 2001 From: Parker Erickson Date: Wed, 17 Jan 2024 16:33:50 -0600 Subject: [PATCH 3/6] fix(ai): add init module --- pyTigerGraph/ai/__init__.py | 0 pyTigerGraph/pyTigerGraph.py | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 pyTigerGraph/ai/__init__.py diff --git a/pyTigerGraph/ai/__init__.py b/pyTigerGraph/ai/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pyTigerGraph/pyTigerGraph.py b/pyTigerGraph/pyTigerGraph.py index 24a15738..3e3cbb4c 100644 --- a/pyTigerGraph/pyTigerGraph.py +++ b/pyTigerGraph/pyTigerGraph.py @@ -57,9 +57,9 @@ def __getattribute__(self, name): from .ai import ai self.ai = ai.AI(self) return super().__getattribute__(name) - except: + except Exception as e: raise Exception( - "Error importing AI submodule." + "Error importing AI submodule. "+str(e) ) else: return super().__getattribute__(name) From e8780f9695c18b9f0f7748f4b3991fd005680ceb Mon Sep 17 00:00:00 2001 From: Parker Erickson Date: Tue, 13 Feb 2024 15:33:14 -0600 Subject: [PATCH 4/6] fix(getToken): fix getToken() --- pyTigerGraph/pyTigerGraphAuth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyTigerGraph/pyTigerGraphAuth.py b/pyTigerGraph/pyTigerGraphAuth.py index 4476db60..8577cd80 100644 --- a/pyTigerGraph/pyTigerGraphAuth.py +++ b/pyTigerGraph/pyTigerGraphAuth.py @@ -231,7 +231,7 @@ def getToken(self, secret: str = None, setToken: bool = True, lifetime: int = No elif not(success) and not(secret): res = self._post(self.restppUrl+"/requesttoken", authMode="pwd", data=str({"graph": self.graphname}), resKey="results") success = True - else: + elif not(success) and (int(s) < 3 or (int(s) == 3 and int(m) < 5)): raise TigerGraphException("Cannot request a token with username/password for versions < 3.5.") From 855713c71cb6668b00ef0c1329df5a74f9699de6 Mon Sep 17 00:00:00 2001 From: Parker Erickson Date: Tue, 13 Feb 2024 21:17:33 -0600 Subject: [PATCH 5/6] fix(doc): update return types --- pyTigerGraph/pyTigerGraphAuth.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pyTigerGraph/pyTigerGraphAuth.py b/pyTigerGraph/pyTigerGraphAuth.py index 8577cd80..fa5e775a 100644 --- a/pyTigerGraph/pyTigerGraphAuth.py +++ b/pyTigerGraph/pyTigerGraphAuth.py @@ -177,7 +177,7 @@ def dropSecret(self, alias: Union[str, list], ignoreErrors: bool = True) -> str: return res - def getToken(self, secret: str = None, setToken: bool = True, lifetime: int = None) -> tuple: + def getToken(self, secret: str = None, setToken: bool = True, lifetime: int = None) -> Union[tuple, str]: """Requests an authorization token. This function returns a token only if REST++ authentication is enabled. If not, an exception @@ -194,9 +194,12 @@ def getToken(self, secret: str = None, setToken: bool = True, lifetime: int = No Duration of token validity (in seconds, default 30 days = 2,592,000 seconds). Returns: - A tuple of `(, , )`. + If your TigerGraph instance is running version 3.5 or before, the return value is + a tuple of `(, , )`. The return value can be ignored, as the token is automatically set for the connection after this call. + If your TigerGraph instance is running version 3.6 or later, the return value is just the token. + [NOTE] The expiration timestamp's time zone might be different from your computer's local time zone. From 061e1051b58d59f988ba55692fcf2d99c5fc686d Mon Sep 17 00:00:00 2001 From: Parker Erickson Date: Thu, 15 Feb 2024 13:19:15 -0600 Subject: [PATCH 6/6] bump(version): v1.5.2 --- pyTigerGraph/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyTigerGraph/__init__.py b/pyTigerGraph/__init__.py index 5f51690a..3253a4b4 100644 --- a/pyTigerGraph/__init__.py +++ b/pyTigerGraph/__init__.py @@ -1,5 +1,5 @@ from pyTigerGraph.pyTigerGraph import TigerGraphConnection -__version__ = "1.5.1" +__version__ = "1.5.2" __license__ = "Apache 2"