about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--gn3/api/llm.py19
-rw-r--r--gn3/llms/client.py2
-rw-r--r--gn3/llms/process.py22
3 files changed, 39 insertions, 4 deletions
diff --git a/gn3/api/llm.py b/gn3/api/llm.py
index 68e6acc..5fcc1f5 100644
--- a/gn3/api/llm.py
+++ b/gn3/api/llm.py
@@ -6,6 +6,8 @@ from flask import jsonify, request, Blueprint, current_app
 
 from gn3.llms.process import getGNQA
 
+from gn3.llms.process import rate_document
+
 GnQNA = Blueprint("GnQNA", __name__)
 
 
@@ -17,10 +19,11 @@ def gnqa():
 
     try:
         auth_token = current_app.config.get("FAHAMU_AUTH_TOKEN")
-        answer, refs = getGNQA(
+        task_id, answer, refs = getGNQA(
             query, auth_token)
 
         return jsonify({
+            "task_id": task_id,
             "query": query,
             "answer": answer,
             "references": refs
@@ -28,3 +31,17 @@ def gnqa():
 
     except Exception as error:
         return jsonify({"query": query, "error": "Internal server error"}), 500
+
+
+@GnQNA.route("/rating/<task_id>/<doc_id>/<int:rating>", methods=["POST"])
+def rating(task_id, doc_id, rating):
+    try:
+        results = rate_document(task_id, doc_id, rating,
+                                current_app.config.get("FAHAMU_AUTH_TOKEN"))
+        return jsonify({
+            **results,
+            "doc_id": doc_id,
+            "task_id": task_id,
+        }),
+    except Exception as error:
+        return jsonify({"error": str(error), doc_id: doc_id}), 500
diff --git a/gn3/llms/client.py b/gn3/llms/client.py
index 880df1d..325e609 100644
--- a/gn3/llms/client.py
+++ b/gn3/llms/client.py
@@ -153,7 +153,7 @@ class GeneNetworkQAClient(Session):
                     raise exc
             if response.ok:
                 # Give time to get all the data
-                time.sleep(retry_delay*1.5)
+                time.sleep(retry_delay*2)
                 return response
             else:
                 time.sleep(retry_delay)
diff --git a/gn3/llms/process.py b/gn3/llms/process.py
index 526c1b3..f4a55cf 100644
--- a/gn3/llms/process.py
+++ b/gn3/llms/process.py
@@ -10,6 +10,8 @@ import os
 from urllib.request import urlretrieve
 from urllib.parse import quote
 
+from urllib.parse import urljoin
+
 from gn3.llms.client import GeneNetworkQAClient
 from gn3.llms.response import DocIDs
 
@@ -90,9 +92,9 @@ def getGNQA(query, auth_token):
         answer = respText['data']['answer']
         context = respText['data']['context']
         references = parse_context(context)
-        return answer, references
+        return task_id, answer, references
     else:
-        return res, "Unfortunately I have nothing."
+        return task_id, res, "Unfortunately I have nothing."
 
 
 def parse_context(context):
@@ -112,3 +114,19 @@ def parse_context(context):
         result.append(
             {"doc_id": doc_ids, "bibInfo": bibInfo, "comboTxt": comboTxt})
     return result
+
+
+def rate_document(task_id, doc_id, rating, auth_token):
+    """This method is used to provide feedback for a document by making a rating."""
+    try:
+        resp = requests.post(
+            urljoin(baseUrl, f"/feedback?task_id={task_id}&document_id={doc_id}&feedback={rating}"),
+            headers={"Authorization": f"Bearer {auth_token}"}
+        )
+        resp.raise_for_status()
+        return {"status": "success", **resp.json()}
+    except requests.exceptions.HTTPError as http_error:
+        raise RuntimeError(f"HTTP Error Occurred: {http_error.response.text} -with status code- {http_error.response.status_code}")
+    except Exception as error:
+
+        raise RuntimeError(f"An error occurred: {str(error)}")