From 93a344f4c51ab5100de6afd10f73c7c07a5278de Mon Sep 17 00:00:00 2001 From: Cyberes Date: Thu, 14 Sep 2023 14:26:25 -0600 Subject: [PATCH] check if the backend crapped out, print some more stuff --- llm_server/routes/cache.py | 2 +- llm_server/routes/openai/chat_completions.py | 14 +++++--------- llm_server/routes/request_handler.py | 9 +++++++++ llm_server/routes/v1/generate.py | 8 +++++--- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/llm_server/routes/cache.py b/llm_server/routes/cache.py index dd962c0..3af1005 100644 --- a/llm_server/routes/cache.py +++ b/llm_server/routes/cache.py @@ -23,7 +23,7 @@ class RedisWrapper: self.set('____', 1) except redis_pkg.exceptions.ConnectionError as e: print('Failed to connect to the Redis server:', e) - print('Did you install and start Redis?') + print('Did you install and start the Redis server?') sys.exit(1) def _key(self, key): diff --git a/llm_server/routes/openai/chat_completions.py b/llm_server/routes/openai/chat_completions.py index 3a56242..ee7b615 100644 --- a/llm_server/routes/openai/chat_completions.py +++ b/llm_server/routes/openai/chat_completions.py @@ -5,14 +5,6 @@ from ..helpers.http import validate_json from ..openai_request_handler import OpenAIRequestHandler -class FakeFlaskRequest(): - def __init__(self, *args, **kwargs): - self.data = kwargs.get('data') - self.headers = kwargs.get('headers') - self.json = kwargs.get('json') - self.remote_addr = kwargs.get('remote_addr') - - @openai_bp.route('/chat/completions', methods=['POST']) def openai_chat_completions(): # TODO: make this work with oobabooga @@ -20,4 +12,8 @@ def openai_chat_completions(): if not request_valid_json or not request_json_body.get('messages'): return jsonify({'code': 400, 'msg': 'invalid JSON'}), 400 else: - return OpenAIRequestHandler(request).handle_request() + try: + return OpenAIRequestHandler(request).handle_request() + except Exception as e: + print(f'EXCEPTION on {request.url}!!!', f'{e.__class__.__name__}: {e}') + print(request.data) diff --git a/llm_server/routes/request_handler.py b/llm_server/routes/request_handler.py index 0550df8..0b57149 100644 --- a/llm_server/routes/request_handler.py +++ b/llm_server/routes/request_handler.py @@ -122,9 +122,18 @@ class RequestHandler: # =============================================== response_valid_json, response_json_body = validate_json(response) + return_json_err = False # The backend didn't send valid JSON if not response_valid_json: + return_json_err = True + + # Make sure the backend didn't crap out. + results = response_json_body.get('results', []) + if len(results) and not results[0].get('text'): + return_json_err = True + + if return_json_err: error_msg = 'The backend did not return valid JSON.' backend_response = format_sillytavern_err(error_msg, 'error') log_prompt(self.client_ip, self.token, prompt, backend_response, elapsed_time, self.parameters, dict(self.request.headers), response_status_code, self.request.url, is_error=True) diff --git a/llm_server/routes/v1/generate.py b/llm_server/routes/v1/generate.py index 69cf2d0..5294259 100644 --- a/llm_server/routes/v1/generate.py +++ b/llm_server/routes/v1/generate.py @@ -3,7 +3,6 @@ from flask import jsonify, request from . import bp from ..helpers.http import validate_json from ..ooba_request_handler import OobaRequestHandler -from ... import opts @bp.route('/generate', methods=['POST']) @@ -12,5 +11,8 @@ def generate(): if not request_valid_json or not request_json_body.get('prompt'): return jsonify({'code': 400, 'msg': 'Invalid JSON'}), 400 else: - handler = OobaRequestHandler(request) - return handler.handle_request() + try: + return OobaRequestHandler(request).handle_request() + except Exception as e: + print(f'EXCEPTION on {request.url}!!!', f'{e.__class__.__name__}: {e}') + print(request.data)