From 6ea8d2d325a9a4da948d2738a0838630575e502b Mon Sep 17 00:00:00 2001 From: nsde Date: Tue, 15 Aug 2023 13:55:12 +0200 Subject: [PATCH] Slight improvements/fixes --- api/core.py | 2 +- api/db/users.py | 2 +- api/transfer.py | 7 +++++-- tests/__main__.py | 44 +++++++++++++++++++++++--------------------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/api/core.py b/api/core.py index 8946516..25f5e1d 100644 --- a/api/core.py +++ b/api/core.py @@ -4,7 +4,7 @@ import os import json import fastapi -from users import UserManager +from db.users import UserManager from dhooks import Webhook, Embed from dotenv import load_dotenv diff --git a/api/db/users.py b/api/db/users.py index 6f2b862..962b752 100644 --- a/api/db/users.py +++ b/api/db/users.py @@ -9,7 +9,7 @@ from motor.motor_asyncio import AsyncIOMotorClient load_dotenv() -with open('config/credits.yml', encoding='utf8') as f: +with open('config/config.yml', encoding='utf8') as f: credits_config = yaml.safe_load(f) ## MONGODB Setup diff --git a/api/transfer.py b/api/transfer.py index 5e643a6..835da23 100644 --- a/api/transfer.py +++ b/api/transfer.py @@ -9,7 +9,7 @@ from dotenv import load_dotenv import streaming import moderation -from users import UserManager +from db.users import UserManager from helpers import tokens, errors load_dotenv() @@ -34,7 +34,10 @@ async def handle(incoming_request): if method not in allowed_methods: return await errors.error(405, f'Method "{method}" is not allowed.', 'Change the request method to the correct one.') - payload = await incoming_request.json() + try: + payload = await incoming_request.json() + except json.decoder.JSONDecodeError: + payload = {} try: input_tokens = await tokens.count_for_messages(payload.get('messages', [])) diff --git a/tests/__main__.py b/tests/__main__.py index 335d4d5..544a681 100644 --- a/tests/__main__.py +++ b/tests/__main__.py @@ -33,7 +33,7 @@ def test_server(): """Tests if the API server is running.""" try: - return httpx.get(f'{api_endpoint}').json()['status'] == 'ok' + return httpx.get(f'{api_endpoint.replace("/v1", "")}').json()['status'] == 'ok' except httpx.ConnectError as exc: raise ConnectionError(f'API is not running on port {api_endpoint}.') from exc @@ -69,7 +69,7 @@ def test_library(): return completion['choices'][0]['message']['content'] def test_library_moderation(): - return closedai.Moderation.create("I wanna kill myself, I wanna kill myself; It's all I hear right now, it's all I hear right now") + return closedai.Moderation.create('I wanna kill myself, I wanna kill myself; It\'s all I hear right now, it\'s all I hear right now') def test_models(): response = httpx.get( @@ -80,25 +80,7 @@ def test_models(): response.raise_for_status() return response.json() -def test_all(): - """Runs all tests.""" - - print("Running test on API server to check if its running.." - print(test_server()) - - print("Running a api endpoint to see if requests can go through..." - print(test_api()) - - print("Checking if the API works with the python library..." - print(test_library()) - - print("Checking if the moderation endpoint works...") - print(test_library_moderation()) - - print("Checking if all models can be GET" - print(test_models()) - -def test_api_moderation(model: str=MODEL, messages: List[dict]=None) -> dict: +def test_api_moderation() -> dict: """Tests an API api_endpoint.""" response = httpx.get( @@ -110,6 +92,26 @@ def test_api_moderation(model: str=MODEL, messages: List[dict]=None) -> dict: return response.text +# ========================================================================================== + +def test_all(): + """Runs all tests.""" + + print('Running test on API server to check if its running..') + print(test_server()) + + print('Running a api endpoint to see if requests can go through...') + print(test_api()) + + print('Checking if the API works with the python library...') + print(test_library()) + + print('Checking if the moderation endpoint works...') + print(test_library_moderation()) + + print('Checking if all models can be GET') + print(test_models()) + if __name__ == '__main__': api_endpoint = 'https://alpha-api.nova-oss.com/v1' closedai.api_base = api_endpoint