diff options
Diffstat (limited to 'gn3/llms/client.py')
-rw-r--r-- | gn3/llms/client.py | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/gn3/llms/client.py b/gn3/llms/client.py index 05e3500..d57bca2 100644 --- a/gn3/llms/client.py +++ b/gn3/llms/client.py @@ -1,6 +1,5 @@ """Module Contains code for making request to fahamu Api""" # pylint: disable=C0301 -# pylint: disable=R0913 import json import time @@ -36,9 +35,7 @@ class GeneNetworkQAClient(Session): request timeouts, and request retries. Args: - account (str): Base address subdomain. api_key (str): API key. - version (str, optional): API version, defaults to "v3". timeout (int, optional): Timeout value, defaults to 5. total_retries (int, optional): Total retries value, defaults to 5. backoff_factor (int, optional): Retry backoff factor value, @@ -50,7 +47,7 @@ class GeneNetworkQAClient(Session): api_key="XXXXXXXXXXXXXXXXXXX...") """ - def __init__(self, account, api_key, version="v3", timeout=30, + def __init__(self, api_key, timeout=30, total_retries=5, backoff_factor=30): super().__init__() self.headers.update( @@ -95,11 +92,14 @@ class GeneNetworkQAClient(Session): def get_answer(self, taskid, *args, **kwargs): """Fahamu get answer interface""" - query = f"{self.answer_url}?task_id={taskid['task_id']}" - res = self.custom_request('GET', query, *args, **kwargs) - if res.status_code != 200: - return f"Error: Status code -{res.status_code}- Reason::{res.reason}", 0 - return res, 1 + try: + query = f"{self.answer_url}?task_id={taskid['task_id']}" + res = self.custom_request('GET', query, *args, **kwargs) + if res.status_code != 200: + return f"Error: Status code -{res.status_code}- Reason::{res.reason}", 0 + return res, 1 + except TimeoutError: + return "Timeout error occured:try to rephrase your query", 0 def custom_request(self, method, url, *args, **kwargs): """ make custom request to fahamu api ask and get response""" @@ -109,20 +109,18 @@ class GeneNetworkQAClient(Session): try: response = super().request(method, url, *args, **kwargs) response.raise_for_status() + if response.ok: + if method.lower() == "get" and response.json().get("data") is None: + time.sleep(retry_delay) + continue + return response + else: + time.sleep(retry_delay) except requests.exceptions.HTTPError as error: if error.response.status_code == 500: raise LLMError(error.request, error.response, f"Response Error with:status_code:{error.response.status_code},Reason for error: Use of Invalid Fahamu Token") from error - elif error.response.status_code == 404: - raise LLMError(error.request, error.response, f"404 Client Error: Not Found for url: {self.base_url}") from error - raise error + raise LLMError(error.request, error.response, + f"HTTP error occurred with error status:{error.response.status_code}") from error except requests.exceptions.RequestException as error: raise error - if response.ok: - if method.lower() == "get" and response.json().get("data") is None: - time.sleep(retry_delay) - continue - else: - return response - else: - time.sleep(retry_delay) - return response + raise TimeoutError |