From 168ee869b89349f047b8b07043558a30556b725b Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Tue, 30 Apr 2024 18:07:27 +0300 Subject: Replace SQLITE_DB_PATH with LLM_DB_PATH for llm db path. --- gn3/api/llm.py | 52 +++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) (limited to 'gn3/api') diff --git a/gn3/api/llm.py b/gn3/api/llm.py index fe10046..6fe87fe 100644 --- a/gn3/api/llm.py +++ b/gn3/api/llm.py @@ -5,17 +5,13 @@ from flask import jsonify, request, Blueprint, current_app from functools import wraps -from gn3.auth.authorisation.oauth2.resource_server import require_oauth - 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 gn3.settings import SQLITE_DB_PATH - +from gn3.settings import LLM_DB_PATH from redis import Redis -import os import json import sqlite3 from datetime import timedelta @@ -44,7 +40,6 @@ def gnqa(): auth_token = current_app.config.get("FAHAMU_AUTH_TOKEN") task_id, answer, refs = get_gnqa( query, auth_token, current_app.config.get("DATA_DIR")) - response = { "task_id": task_id, "query": query, @@ -67,33 +62,32 @@ def gnqa(): @require_oauth("profile") def rating(task_id): try: - with require_oauth.acquire("profile") as the_token: - user = the_token.user.user_id + with (require_oauth.acquire("profile") as token, + db.connection(LLM_DB_PATH) as conn): + results = request.json - user_id, query, answer, weight = (the_token.user.user_id, + user_id, query, answer, weight = (token.user.user_id, results.get("query"), results.get("answer"), results.get("weight", 0)) - - with db.connection(os.path.join(SQLITE_DB_PATH, "llm.db")) as conn: - cursor = conn.cursor() - create_table = """CREATE TABLE IF NOT EXISTS Rating( - user_id TEXT NOT NULL, - query TEXT NOT NULL, - answer TEXT NOT NULL, - weight INTEGER NOT NULL DEFAULT 0, - task_id TEXT NOT NULL UNIQUE - )""" - cursor.execute(create_table) - 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": "success", - "status": 0 - }, 200 + cursor = conn.cursor() + create_table = """CREATE TABLE IF NOT EXISTS Rating( + user_id TEXT NOT NULL, + query TEXT NOT NULL, + answer TEXT NOT NULL, + weight INTEGER NOT NULL DEFAULT 0, + task_id TEXT NOT NULL UNIQUE + )""" + cursor.execute(create_table) + 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": "success", + "status": 0 + }, 200 except sqlite3.Error as error: raise error -- cgit v1.2.3 From 92c5afea1eb29182ccbdff6629f1ece1031e52b7 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Tue, 30 Apr 2024 18:26:43 +0300 Subject: Add a general Exception for the /rating api --- gn3/api/llm.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'gn3/api') diff --git a/gn3/api/llm.py b/gn3/api/llm.py index 6fe87fe..7a0eff2 100644 --- a/gn3/api/llm.py +++ b/gn3/api/llm.py @@ -90,6 +90,8 @@ def rating(task_id): }, 200 except sqlite3.Error as error: raise error + except Exception as error: + raise error @GnQNA.route("/history/", methods=["GET"]) -- cgit v1.2.3