From 715241466e6426c2f3650e6cd9e0990078ad80f5 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Wed, 15 May 2024 19:00:12 +0300 Subject: Pep8 formatting for llm Api file. --- gn3/api/llm.py | 64 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/gn3/api/llm.py b/gn3/api/llm.py index 7d860d8..91779a5 100644 --- a/gn3/api/llm.py +++ b/gn3/api/llm.py @@ -1,24 +1,26 @@ -"""API for data used to generate menus""" - -# pylint: skip-file +"""Api endpoints for gnqa""" +from datetime import timedelta +from functools import wraps +import json +import sqlite3 +from redis import Redis -from flask import jsonify, request, Blueprint, current_app +from flask import Blueprint +from flask import current_app +from flask import jsonify +from flask import request -from functools import wraps from gn3.llms.process import get_gnqa from gn3.llms.process import get_user_queries from gn3.llms.process import fetch_query_results from gn3.auth.authorisation.oauth2.resource_server import require_oauth from gn3.auth import db -from redis import Redis -import json -import sqlite3 -from datetime import timedelta GnQNA = Blueprint("GnQNA", __name__) def handle_errors(func): + """general error handling decorator function""" @wraps(func) def decorated_function(*args, **kwargs): try: @@ -30,7 +32,7 @@ def handle_errors(func): @GnQNA.route("/gnqna", methods=["POST"]) def gnqa(): - # todo add auth + """Main gnqa endpoint""" query = request.json.get("querygnqa", "") if not query: return jsonify({"error": "querygnqa is missing in the request"}), 400 @@ -38,7 +40,8 @@ def gnqa(): try: fahamu_token = current_app.config.get("FAHAMU_AUTH_TOKEN") if fahamu_token is None: - return jsonify({"query": query, "error": "Use of invalid fahamu auth token"}), 500 + return jsonify({"query": query, + "error": "Use of invalid fahamu auth token"}), 500 task_id, answer, refs = get_gnqa( query, fahamu_token, current_app.config.get("DATA_DIR")) response = { @@ -49,19 +52,22 @@ def gnqa(): } 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)) + 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 + return jsonify({"query": query, + "error": f"Request failed-{str(error)}"}), 500 @GnQNA.route("/rating/", methods=["POST"]) @require_oauth("profile") def rating(task_id): + """Endpoint for rating qnqa query and answer""" try: llm_db_path = current_app.config["LLM_DB_PATH"] with (require_oauth.acquire("profile") as token, @@ -81,14 +87,16 @@ def rating(task_id): task_id TEXT NOT NULL UNIQUE )""" cursor.execute(create_table) - cursor.execute("""INSERT INTO Rating(user_id,query,answer,weight,task_id) + cursor.execute("""INSERT INTO Rating(user_id,query, + answer,weight,task_id) VALUES(?,?,?,?,?) ON CONFLICT(task_id) DO UPDATE SET weight=excluded.weight """, (str(user_id), query, answer, weight, task_id)) return { - "message": "You have successfully rated this query:Thank you!!" - }, 200 + "message": + "You have successfully rated this query:Thank you!!" + }, 200 except sqlite3.Error as error: return jsonify({"error": str(error)}), 500 except Exception as error: @@ -99,9 +107,10 @@ def rating(task_id): @require_oauth("profile user") @handle_errors def fetch_user_hist(query): - - with (require_oauth.acquire("profile user") as the_token, Redis.from_url(current_app.config["REDIS_URI"], - decode_responses=True) as redis_conn): + """"Endpoint to fetch previos searches for User""" + 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) @@ -111,9 +120,12 @@ def fetch_user_hist(query): @GnQNA.route("/historys/", methods=["GET"]) @handle_errors 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""" + """method to fetch all users hist:note this is a test functionality + to be replaced by fetch_user_hist + """ - with Redis.from_url(current_app.config["REDIS_URI"], decode_responses=True) as redis_conn: + 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) @@ -123,6 +135,8 @@ def fetch_users_hist_records(query): @GnQNA.route("/get_hist_names", methods=["GET"]) @handle_errors def fetch_prev_hist_ids(): - - 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)}) + """Test method for fetching history for Anony Users""" + 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)}) -- cgit v1.2.3 From d2b5a1c11f6e09ee13b72669068e326b8131f65c Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Wed, 15 May 2024 19:01:56 +0300 Subject: Remove broad error handling. --- gn3/api/llm.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/gn3/api/llm.py b/gn3/api/llm.py index 91779a5..08783db 100644 --- a/gn3/api/llm.py +++ b/gn3/api/llm.py @@ -1,6 +1,5 @@ """Api endpoints for gnqa""" from datetime import timedelta -from functools import wraps import json import sqlite3 from redis import Redis @@ -19,17 +18,6 @@ from gn3.auth import db GnQNA = Blueprint("GnQNA", __name__) -def handle_errors(func): - """general error handling decorator function""" - @wraps(func) - def decorated_function(*args, **kwargs): - try: - return func(*args, **kwargs) - except Exception as error: - return jsonify({"error": str(error)}), 500 - return decorated_function - - @GnQNA.route("/gnqna", methods=["POST"]) def gnqa(): """Main gnqa endpoint""" @@ -105,7 +93,6 @@ def rating(task_id): @GnQNA.route("/history/", methods=["GET"]) @require_oauth("profile user") -@handle_errors def fetch_user_hist(query): """"Endpoint to fetch previos searches for User""" with (require_oauth.acquire("profile user") as the_token, @@ -118,7 +105,6 @@ def fetch_user_hist(query): @GnQNA.route("/historys/", methods=["GET"]) -@handle_errors 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 @@ -133,7 +119,6 @@ def fetch_users_hist_records(query): @GnQNA.route("/get_hist_names", methods=["GET"]) -@handle_errors def fetch_prev_hist_ids(): """Test method for fetching history for Anony Users""" with (Redis.from_url(current_app.config["REDIS_URI"], -- cgit v1.2.3 From 82c89c302a87082c47d7d773264dae4872ba6d1c Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Wed, 15 May 2024 19:08:53 +0300 Subject: Rename GnQNA blueprint to gnqa. * register gnqa api endpoint --- gn3/api/llm.py | 20 +++++++++----------- gn3/app.py | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/gn3/api/llm.py b/gn3/api/llm.py index 08783db..442252f 100644 --- a/gn3/api/llm.py +++ b/gn3/api/llm.py @@ -12,14 +12,15 @@ from flask import request from gn3.llms.process import get_gnqa from gn3.llms.process import get_user_queries from gn3.llms.process import fetch_query_results +from gn3.llms.errors import LLMError from gn3.auth.authorisation.oauth2.resource_server import require_oauth from gn3.auth import db -GnQNA = Blueprint("GnQNA", __name__) +gnqa = Blueprint("gnqa", __name__) -@GnQNA.route("/gnqna", methods=["POST"]) -def gnqa(): +@gnqa.route("/gnqna", methods=["POST"]) +def gnqna(): """Main gnqa endpoint""" query = request.json.get("querygnqa", "") if not query: @@ -47,12 +48,12 @@ def gnqa(): **response, "prev_queries": get_user_queries("random_user", redis_conn) }) - except Exception as error: + except LLMError as error: return jsonify({"query": query, "error": f"Request failed-{str(error)}"}), 500 -@GnQNA.route("/rating/", methods=["POST"]) +@gnqa.route("/rating/", methods=["POST"]) @require_oauth("profile") def rating(task_id): """Endpoint for rating qnqa query and answer""" @@ -87,11 +88,9 @@ def rating(task_id): }, 200 except sqlite3.Error as error: return jsonify({"error": str(error)}), 500 - except Exception as error: - raise error -@GnQNA.route("/history/", methods=["GET"]) +@gnqa.route("/history/", methods=["GET"]) @require_oauth("profile user") def fetch_user_hist(query): """"Endpoint to fetch previos searches for User""" @@ -104,12 +103,11 @@ def fetch_user_hist(query): }) -@GnQNA.route("/historys/", methods=["GET"]) +@gnqa.route("/historys/", 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 """ - with Redis.from_url(current_app.config["REDIS_URI"], decode_responses=True) as redis_conn: return jsonify({ @@ -118,7 +116,7 @@ def fetch_users_hist_records(query): }) -@GnQNA.route("/get_hist_names", methods=["GET"]) +@gnqa.route("/get_hist_names", methods=["GET"]) def fetch_prev_hist_ids(): """Test method for fetching history for Anony Users""" with (Redis.from_url(current_app.config["REDIS_URI"], diff --git a/gn3/app.py b/gn3/app.py index 3f1e6ee..c8f0c5a 100644 --- a/gn3/app.py +++ b/gn3/app.py @@ -25,7 +25,7 @@ from gn3.api.menu import menu from gn3.api.search import search from gn3.api.metadata import metadata from gn3.api.sampledata import sampledata -from gn3.api.llm import GnQNA +from gn3.api.llm import gnqa from gn3.auth import oauth2 from gn3.case_attributes import caseattr @@ -78,7 +78,7 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask: app.register_blueprint(sampledata, url_prefix="/api/sampledata") app.register_blueprint(oauth2, url_prefix="/api/oauth2") app.register_blueprint(caseattr, url_prefix="/api/case-attribute") - app.register_blueprint(GnQNA, url_prefix="/api/llm") + app.register_blueprint(gnqa, url_prefix="/api/llm") register_error_handlers(app) return app -- cgit v1.2.3