aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
authorAlexander_Kabui2024-05-17 13:34:37 +0300
committerAlexander_Kabui2024-05-17 14:46:36 +0300
commitd3bc323fe3a965ee5b6917987c4fe7662056e560 (patch)
treeb0793a643ad30ae209d426866b80391f4143c29b /gn3
parent4d39c26b65aad3fa48d35fc11007f5f3afe1c112 (diff)
downloadgenenetwork3-d3bc323fe3a965ee5b6917987c4fe7662056e560.tar.gz
Refactor custom request codebase.
Diffstat (limited to 'gn3')
-rw-r--r--gn3/llms/client.py40
-rw-r--r--gn3/llms/process.py5
2 files changed, 21 insertions, 24 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
diff --git a/gn3/llms/process.py b/gn3/llms/process.py
index 9cb09a1..4725bcb 100644
--- a/gn3/llms/process.py
+++ b/gn3/llms/process.py
@@ -5,7 +5,6 @@ import string
import json
import logging
from urllib.parse import quote
-import requests
from gn3.llms.client import GeneNetworkQAClient
@@ -118,7 +117,7 @@ def get_gnqa(query, auth_token, data_dir=""):
references: contains doc_name,reference,pub_med_info
"""
- api_client = GeneNetworkQAClient(requests.Session(), api_key=auth_token)
+ api_client = GeneNetworkQAClient(api_key=auth_token)
res, task_id = api_client.ask('?ask=' + quote(query), auth_token)
if task_id == 0:
raise RuntimeError(f"Error connecting to Fahamu Api: {str(res)}")
@@ -135,7 +134,7 @@ def get_gnqa(query, auth_token, data_dir=""):
return task_id, answer, references
else:
- return task_id, "Please try to rephrase your question to receive feedback", []
+ return task_id, "We couldn't provide a response,Please try to rephrase your question to receive feedback", []
def fetch_query_results(query, user_id, redis_conn):