aboutsummaryrefslogtreecommitdiff
path: root/gn3/llms
diff options
context:
space:
mode:
authorAlexander_Kabui2024-01-03 19:29:04 +0300
committerAlexander_Kabui2024-01-03 19:29:04 +0300
commita227ee89544a4a12f63291a7ed6c203a715215ad (patch)
tree424262cca4f8b7c52974a3200a6a5c3a24e3ce65 /gn3/llms
parentbc943ce0413e55d9a4903aea95baad86d808bf47 (diff)
downloadgenenetwork3-a227ee89544a4a12f63291a7ed6c203a715215ad.tar.gz
fix fetch token error from curent app
Diffstat (limited to 'gn3/llms')
-rw-r--r--gn3/llms/client.py17
-rw-r--r--gn3/llms/process.py70
2 files changed, 43 insertions, 44 deletions
diff --git a/gn3/llms/client.py b/gn3/llms/client.py
index 6bcbf31..1c3acce 100644
--- a/gn3/llms/client.py
+++ b/gn3/llms/client.py
@@ -5,7 +5,7 @@ import os
import datetime
import time
import requests
-from flask import current_app
+
from requests import Session
from urllib.parse import urljoin
from requests.packages.urllib3.util.retry import Retry
@@ -59,7 +59,8 @@ class GeneNetworkQAClient(Session):
def __init__(self, account, api_key, version="v3", timeout=5, total_retries=5, backoff_factor=30):
super().__init__()
- self.headers.update({"Authorization": "Bearer " + current_app.config.get("FAHAMU_AUTH_TOKEN","")})
+ self.headers.update(
+ {"Authorization": "Bearer " + api_key})
self.answer_url = f"{self.BASE_URL}/answers"
self.feedback_url = f"{self.BASE_URL}/feedback"
@@ -75,8 +76,6 @@ class GeneNetworkQAClient(Session):
self.mount("https://", adapter)
self.mount("http://", adapter)
-
-
@staticmethod
def format_bibliography_info(bib_info):
@@ -86,9 +85,9 @@ class GeneNetworkQAClient(Session):
elif isinstance(bib_info, dict):
# Format string bibliography information
bib_info = "{0}.{1}.{2}.{3} ".format(bib_info.get('author', ''),
- bib_info.get('title', ''),
- bib_info.get('year', ''),
- bib_info.get('doi', ''))
+ bib_info.get('title', ''),
+ bib_info.get('year', ''),
+ bib_info.get('doi', ''))
return bib_info
@staticmethod
@@ -133,7 +132,7 @@ class GeneNetworkQAClient(Session):
return res, 1
def custom_request(self, method, url, *args, **kwargs):
- max_retries = 3
+ max_retries = 5
retry_delay = 10
response = super().request(method, url, *args, **kwargs)
@@ -151,7 +150,7 @@ class GeneNetworkQAClient(Session):
raise exc
if response.ok:
# Give time to get all the data
- time.sleep(retry_delay*3)
+ time.sleep(retry_delay*1.5)
return response
else:
time.sleep(retry_delay)
diff --git a/gn3/llms/process.py b/gn3/llms/process.py
index b8e7ded..58d565a 100644
--- a/gn3/llms/process.py
+++ b/gn3/llms/process.py
@@ -11,17 +11,9 @@ from gn3.llms.client import GeneNetworkQAClient
from gn3.llms.response import DocIDs
-baseUrl = 'https://genenetwork.fahamuai.com/api/tasks'
-answerUrl = baseUrl + '/answers'
-basedir = os.path.abspath(os.path.dirname(__file__))
-apiClient = GeneNetworkQAClient(requests.Session(), api_key='')
-
-
-
-
-
-
-
+baseUrl = 'https://genenetwork.fahamuai.com/api/tasks'
+answerUrl = baseUrl + '/answers'
+basedir = os.path.abspath(os.path.dirname(__file__))
def formatBibliographyInfo(bibInfo):
@@ -30,27 +22,29 @@ def formatBibliographyInfo(bibInfo):
bibInfo = bibInfo.removesuffix('.txt')
elif isinstance(bibInfo, dict):
# format string bibliography information
- bibInfo = "{0}.{1}.{2}.{3} ".format(bibInfo['author'], bibInfo['title'], bibInfo['year'], bibInfo['doi'])
+ bibInfo = "{0}.{1}.{2}.{3} ".format(
+ bibInfo['author'], bibInfo['title'], bibInfo['year'], bibInfo['doi'])
return bibInfo
-def askTheDocuments( extendUrl, my_auth ):
+def askTheDocuments(extendUrl, my_auth):
try:
- res = requests.post(baseUrl+extendUrl,
+ res = requests.post(baseUrl+extendUrl,
data={},
headers=my_auth)
res.raise_for_status()
except:
- raise # what
+ raise # what
if (res.status_code != 200):
return negativeStatusMsg(res), 0
- task_id = getTaskIDFromResult(res)
- res = getAnswerUsingTaskID(task_id, my_auth)
+ task_id = getTaskIDFromResult(res)
+ res = getAnswerUsingTaskID(task_id, my_auth)
if (res.status_code != 200):
return negativeStatusMsg(res), 0
return res, 1
-def getAnswerUsingTaskID( extendUrl, my_auth ):
+
+def getAnswerUsingTaskID(extendUrl, my_auth):
try:
res = requests.get(answerUrl+extendUrl, data={}, headers=my_auth)
res.raise_for_status()
@@ -58,8 +52,9 @@ def getAnswerUsingTaskID( extendUrl, my_auth ):
raise
return res
+
def openAPIConfig():
- f = open(os.path.join(basedir, "api.config.json") , "rb" )
+ f = open(os.path.join(basedir, "api.config.json"), "rb")
result = json.load(f)
f.close()
return result
@@ -67,45 +62,50 @@ def openAPIConfig():
def getTaskIDFromResult(res):
task_id = json.loads(res.text)
- result = '?task_id=' + str(task_id['task_id'])
+ result = '?task_id=' + str(task_id['task_id'])
return result
+
def negativeStatusMsg(res):
- return 'Problems\n\tStatus code => {0}\n\tReason=> {1}'.format(res.status_code, res.reason) # mypy: ignore
+ # mypy: ignore
+ return 'Problems\n\tStatus code => {0}\n\tReason=> {1}'.format(res.status_code, res.reason)
+
def filterResponseText(val):
return json.loads(''.join([str(char) for char in val if char in string.printable]))
-def getGNQA(query):
- res, task_id = apiClient.ask('?ask=' + query)
+
+def getGNQA(query, auth_token):
+ apiClient = GeneNetworkQAClient(requests.Session(), api_key=auth_token)
+ res, task_id = apiClient.ask('?ask=' + query, auth_token)
res, success = apiClient.get_answer(task_id)
- if ( success == 1 ):
- respText = filterResponseText(res.text)
+ if (success == 1):
+ respText = filterResponseText(res.text)
if respText.get("data") is None:
- return "Unfortunately I have nothing on the query",[]
- answer = respText['data']['answer']
- context = respText['data']['context']
+ return "Unfortunately I have nothing on the query", []
+ answer = respText['data']['answer']
+ context = respText['data']['context']
references = parse_context(context)
- return answer,references
+ return answer, references
else:
return res, "Unfortunately I have nothing."
-
def parse_context(context):
"""parse content map id to reference"""
result = []
- for doc_ids,summary in context.items():
+ for doc_ids, summary in context.items():
comboTxt = ""
- for entry in summary:
+ for entry in summary:
comboTxt += '\t' + entry['text']
docInfo = DocIDs().getInfo(doc_ids)
- if doc_ids !=docInfo:
+ if doc_ids != docInfo:
bibInfo = formatBibliographyInfo(docInfo)
else:
bibInfo = doc_ids
- result.append({"doc_id":doc_ids,"bibInfo":bibInfo,"comboTxt":comboTxt})
- return result \ No newline at end of file
+ result.append(
+ {"doc_id": doc_ids, "bibInfo": bibInfo, "comboTxt": comboTxt})
+ return result