Slight improvements/fixes

This commit is contained in:
nsde 2023-08-15 13:55:12 +02:00
parent 69bc2e33de
commit 6ea8d2d325
4 changed files with 30 additions and 25 deletions

View file

@ -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

View file

@ -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

View file

@ -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.')
try:
payload = await incoming_request.json()
except json.decoder.JSONDecodeError:
payload = {}
try:
input_tokens = await tokens.count_for_messages(payload.get('messages', []))

View file

@ -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