aboutsummaryrefslogtreecommitdiff
path: root/gn3/api
diff options
context:
space:
mode:
authorAlexander Kabui2024-01-26 12:55:10 +0300
committerGitHub2024-01-26 12:55:10 +0300
commitcf51516b6f9d673c2ad9ec35604d25b831c48b5f (patch)
tree9d4c56cb38abb813089a1215bd83cf62352ce05e /gn3/api
parentb9a8d3d23f3c239c42c1c4a7ce1c59c2c9a1c450 (diff)
downloadgenenetwork3-cf51516b6f9d673c2ad9ec35604d25b831c48b5f.tar.gz
Feature/gn llm caching (#148)
* add logic for querying user gnqa search result * add api endpoints for querying users:qnqa-search-terms,gnqa-results
Diffstat (limited to 'gn3/api')
-rw-r--r--gn3/api/llm.py60
1 files changed, 57 insertions, 3 deletions
diff --git a/gn3/api/llm.py b/gn3/api/llm.py
index 3dd75fa..60a213a 100644
--- a/gn3/api/llm.py
+++ b/gn3/api/llm.py
@@ -3,10 +3,16 @@
# pylint: skip-file
from flask import jsonify, request, Blueprint, current_app
+from gn3.auth.authorisation.oauth2.resource_server import require_oauth
from gn3.llms.process import get_gnqa
-
from gn3.llms.process import rate_document
+from gn3.llms.process import get_user_queries
+from gn3.llms.process import fetch_query_results
+
+from redis import Redis
+import json
+from datetime import timedelta
GnQNA = Blueprint("GnQNA", __name__)
@@ -22,13 +28,20 @@ def gnqa():
task_id, answer, refs = get_gnqa(
query, auth_token)
- return jsonify({
+ response = {
"task_id": task_id,
"query": query,
"answer": answer,
"references": refs
+ }
+ with (Redis.from_url(current_app.config["REDIS_URI"],
+ decode_responses=True) as redis_conn):
+ # The key will be deleted after 60 seconds
+ redis_conn.setex(f"LLM:random_user-{query}", timedelta(days=10), json.dumps(response))
+ return jsonify({
+ **response,
+ "prev_queries": get_user_queries("random_user", redis_conn)
})
-
except Exception as error:
return jsonify({"query": query, "error": f"Request failed-{str(error)}"}), 500
@@ -46,3 +59,44 @@ def rating(task_id, doc_id, rating):
}),
except Exception as error:
return jsonify({"error": str(error), doc_id: doc_id}), 500
+
+
+@GnQNA.route("/history/<query>", methods=["GET"])
+@require_oauth("profile user")
+def fetch_user_hist(query):
+ try:
+
+ with (require_oauth.acquire("profile user") as the_token, Redis.from_url(current_app.config["REDIS_URI"],
+ decode_responses=True) as redis_conn):
+ return jsonify({
+ **fetch_query_results(query, the_token.user.id, redis_conn),
+ "prev_queries": get_user_queries("random_user", redis_conn)
+ })
+
+ except Exception as error:
+ return jsonify({"error": str(error)}), 500
+
+
+@GnQNA.route("/historys/<query>", methods=["GET"])
+def fetch_users_hist_records(query):
+ """method to fetch all users hist:note this is a test functionality to be replaced by fetch_user_hist"""
+ try:
+
+ with Redis.from_url(current_app.config["REDIS_URI"], decode_responses=True) as redis_conn:
+ return jsonify({
+ **fetch_query_results(query, "random_user", redis_conn),
+ "prev_queries": get_user_queries("random_user", redis_conn)
+ })
+
+ except Exception as error:
+ return jsonify({"error": str(error)}), 500
+
+
+@GnQNA.route("/get_hist_names", methods=["GET"])
+def fetch_prev_hist_ids():
+ try:
+ with (Redis.from_url(current_app.config["REDIS_URI"], decode_responses=True)) as redis_conn:
+ return jsonify({"prev_queries": get_user_queries("random_user", redis_conn)})
+
+ except Exception as error:
+ return jsonify({"error": str(error)}), 500