From 852f5c65cdf78c92012afcf9790d272b4e3f4419 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Thu, 16 May 2024 13:05:46 +0300 Subject: Pep8 Cleanup for llms/errors file --- gn3/llms/errors.py | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) (limited to 'gn3/llms/errors.py') diff --git a/gn3/llms/errors.py b/gn3/llms/errors.py index e9f7c02..af3d7b0 100644 --- a/gn3/llms/errors.py +++ b/gn3/llms/errors.py @@ -1,32 +1,11 @@ - -# pylint: skip-file +""" Error handlers for Fahamu Api""" import json - from requests import HTTPError class UnprocessableEntity(HTTPError): - """An HTTP 422 Unprocessable Entity error occurred. - + """Error for HTTP 422 Unprocessable Entity https://help.helpjuice.com/en_US/api-v3/api-v3#errors - - The request could not be processed, usually due to a missing or invalid parameter. - - The response will also include an error object with an explanation of fields that - are missing or invalid. Here is an example: - - .. code-block:: - - HTTP/1.1 422 Unprocessable Entity - - - { - "errors": [ - { - "email": "is not valid." - } - ] - } """ def __init__(self, request, response): @@ -57,6 +36,7 @@ class UnprocessableEntity(HTTPError): class LLMError(HTTPError): + """Custom error from making Fahamu APi request """ def __init__(self, request, response, msg): super(HTTPError, self).__init__( msg, request=request, response=response) -- cgit v1.2.3 From 12100489a73094016602926183e0ee51002fb9c6 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Wed, 22 May 2024 13:37:32 +0300 Subject: Register LLM error in app. * do refactoring for gn3:llm:errors --- gn3/errors.py | 17 ++++++++++++++++- gn3/llms/errors.py | 14 +++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) (limited to 'gn3/llms/errors.py') diff --git a/gn3/errors.py b/gn3/errors.py index 1833bf6..ac9e070 100644 --- a/gn3/errors.py +++ b/gn3/errors.py @@ -16,7 +16,7 @@ from authlib.oauth2.rfc6749.errors import OAuth2Error from flask import Flask, jsonify, Response, current_app from gn3.auth.authorisation.errors import AuthorisationError - +from gn3.llms.errors import LLMError def add_trace(exc: Exception, jsonmsg: dict) -> dict: """Add the traceback to the error handling object.""" @@ -106,6 +106,20 @@ def handle_generic(exc: Exception) -> Response: return resp +def handle_llm_error(exc: Exception) -> Response: + """ Handle llm erros if not handled anywhere else. """ + resp = jsonify({ + "query": exc.query if exc.query else "", + "error_type": type(exc).__name__, + "error": ( + exc.args if bool(exc.args) else "Fahamu gnqa error occurred" + ), + "trace": traceback.format_exc() + }) + resp.status_code = 500 + return resp + + def register_error_handlers(app: Flask): """Register application-level error handlers.""" app.register_error_handler(NotFound, page_not_found) @@ -115,6 +129,7 @@ def register_error_handlers(app: Flask): app.register_error_handler(AuthorisationError, handle_authorisation_error) app.register_error_handler(RemoteDisconnected, internal_server_error) app.register_error_handler(URLError, url_server_error) + app.register_error_handler(LLMError, handle_llm_error) for exc in ( EndPointInternalError, EndPointNotFound, diff --git a/gn3/llms/errors.py b/gn3/llms/errors.py index af3d7b0..3512f4d 100644 --- a/gn3/llms/errors.py +++ b/gn3/llms/errors.py @@ -35,8 +35,12 @@ class UnprocessableEntity(HTTPError): msg, request=request, response=response) -class LLMError(HTTPError): - """Custom error from making Fahamu APi request """ - def __init__(self, request, response, msg): - super(HTTPError, self).__init__( - msg, request=request, response=response) +class LLMErrorMIxins(Exception): + """base class for llm errors""" + + +class LLMError(LLMErrorMIxins): + """custom exception for LLMErrorMIxins""" + def __init__(self, *args, **kwargs): + super().__init__(*args) + self.query = kwargs.get("query") -- cgit v1.2.3 From d9233e0a4811203e59161b635c33a7c1b753b3d8 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Fri, 24 May 2024 14:20:02 +0300 Subject: Remove redundant llm base class exception. --- gn3/llms/errors.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'gn3/llms/errors.py') diff --git a/gn3/llms/errors.py b/gn3/llms/errors.py index 3512f4d..c5439d6 100644 --- a/gn3/llms/errors.py +++ b/gn3/llms/errors.py @@ -35,11 +35,7 @@ class UnprocessableEntity(HTTPError): msg, request=request, response=response) -class LLMErrorMIxins(Exception): - """base class for llm errors""" - - -class LLMError(LLMErrorMIxins): +class LLMError(Exception): """custom exception for LLMErrorMIxins""" def __init__(self, *args, **kwargs): super().__init__(*args) -- cgit v1.2.3 From 188f84ef3895de613e998c63da7ec2338e25a55c Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Fri, 24 May 2024 16:30:59 +0300 Subject: Remove kwargs from LLMErrorr Exceptions and update relevant code. --- gn3/api/llm.py | 7 +------ gn3/llms/client.py | 6 +++--- gn3/llms/errors.py | 1 - 3 files changed, 4 insertions(+), 10 deletions(-) (limited to 'gn3/llms/errors.py') diff --git a/gn3/api/llm.py b/gn3/api/llm.py index 03ce815..c1f6304 100644 --- a/gn3/api/llm.py +++ b/gn3/api/llm.py @@ -1,7 +1,5 @@ """Api endpoints for gnqa""" import json -import sqlite3 -from authlib.integrations.flask_oauth2.errors import _HTTPException from flask import Blueprint from flask import current_app from flask import jsonify @@ -10,7 +8,6 @@ from flask import request from gn3.llms.process import get_gnqa from gn3.llms.errors import LLMError from gn3.auth.authorisation.oauth2.resource_server import require_oauth -from gn3.auth.authorisation.errors import AuthorisationError from gn3.auth import db @@ -26,8 +23,7 @@ def search(): fahamu_token = current_app.config.get("FAHAMU_AUTH_TOKEN") if not fahamu_token: raise LLMError( - "Request failed:an LLM authorisation token is required ", - query=query) + "Request failed:an LLM authorisation token is required ", query) task_id, answer, refs = get_gnqa( query, fahamu_token, current_app.config.get("DATA_DIR")) response = { @@ -61,7 +57,6 @@ def rate_queries(task_id): """Endpoint for rating qnqa query and answer""" with (require_oauth.acquire("profile") as token, db.connection(current_app.config["LLM_DB_PATH"]) as conn): - results = request.json user_id, query, answer, weight = (token.user.user_id, results.get("query"), diff --git a/gn3/llms/client.py b/gn3/llms/client.py index 5c4fa0e..d29d2a1 100644 --- a/gn3/llms/client.py +++ b/gn3/llms/client.py @@ -117,11 +117,11 @@ class GeneNetworkQAClient(Session): continue return response else: - raise LLMError( f"Request error with code:\ + raise LLMError(f"Request error with code:\ {response.status_code} occurred with reason:\ {response_msg.get(response.status_code,response.reason)}", - query=self.query) + self.query) #time.sleep(retry_delay) raise LLMError("Time error: We couldn't provide a response,Please try\ to rephrase your question to receive feedback", - query=self.query) + self.query) diff --git a/gn3/llms/errors.py b/gn3/llms/errors.py index c5439d6..77e0f9a 100644 --- a/gn3/llms/errors.py +++ b/gn3/llms/errors.py @@ -39,4 +39,3 @@ class LLMError(Exception): """custom exception for LLMErrorMIxins""" def __init__(self, *args, **kwargs): super().__init__(*args) - self.query = kwargs.get("query") -- cgit v1.2.3 From 3ea881f3bc28e8087be08f1d507991ac9b2a4230 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Fri, 24 May 2024 16:36:57 +0300 Subject: Pylint fixes. --- gn3/llms/client.py | 4 ++-- gn3/llms/errors.py | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'gn3/llms/errors.py') diff --git a/gn3/llms/client.py b/gn3/llms/client.py index d29d2a1..ad6c400 100644 --- a/gn3/llms/client.py +++ b/gn3/llms/client.py @@ -109,7 +109,7 @@ class GeneNetworkQAClient(Session): for _i in range(max_retries): response = super().request(method, url, *args, **kwargs) if response.ok: - if method.lower() == "get" and not (response.json().get("data")): + if method.lower() == "get" and not response.json().get("data"): # note this is a dirty trick to check if fahamu has returned the results # the issue is that the api only returns 500 or 200 satus code # TODO: fix this on their end @@ -122,6 +122,6 @@ class GeneNetworkQAClient(Session): {response_msg.get(response.status_code,response.reason)}", self.query) #time.sleep(retry_delay) - raise LLMError("Time error: We couldn't provide a response,Please try\ + raise LLMError("Timeout error: We couldn't provide a response,Please try\ to rephrase your question to receive feedback", self.query) diff --git a/gn3/llms/errors.py b/gn3/llms/errors.py index 77e0f9a..a3a47a3 100644 --- a/gn3/llms/errors.py +++ b/gn3/llms/errors.py @@ -37,5 +37,3 @@ class UnprocessableEntity(HTTPError): class LLMError(Exception): """custom exception for LLMErrorMIxins""" - def __init__(self, *args, **kwargs): - super().__init__(*args) -- cgit v1.2.3