Compare commits

...

2 commits

Author SHA1 Message Date
nsde abb18ad1bf Fixes 2023-08-15 13:56:29 +02:00
nsde 6ea8d2d325 Slight improvements/fixes 2023-08-15 13:55:12 +02:00
4 changed files with 30 additions and 25 deletions

View file

@ -4,7 +4,7 @@ import os
import json import json
import fastapi import fastapi
from users import UserManager from db.users import UserManager
from dhooks import Webhook, Embed from dhooks import Webhook, Embed
from dotenv import load_dotenv from dotenv import load_dotenv

View file

@ -9,7 +9,7 @@ from motor.motor_asyncio import AsyncIOMotorClient
load_dotenv() 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) credits_config = yaml.safe_load(f)
## MONGODB Setup ## MONGODB Setup

View file

@ -9,7 +9,7 @@ from dotenv import load_dotenv
import streaming import streaming
import moderation import moderation
from users import UserManager from db.users import UserManager
from helpers import tokens, errors from helpers import tokens, errors
load_dotenv() load_dotenv()
@ -34,7 +34,10 @@ async def handle(incoming_request):
if method not in allowed_methods: 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.') 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() payload = await incoming_request.json()
except json.decoder.JSONDecodeError:
payload = {}
try: try:
input_tokens = await tokens.count_for_messages(payload.get('messages', [])) 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.""" """Tests if the API server is running."""
try: 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: except httpx.ConnectError as exc:
raise ConnectionError(f'API is not running on port {api_endpoint}.') from 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'] return completion['choices'][0]['message']['content']
def test_library_moderation(): 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(): def test_models():
response = httpx.get( response = httpx.get(
@ -80,25 +80,7 @@ def test_models():
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
def test_all(): def test_api_moderation() -> dict:
"""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:
"""Tests an API api_endpoint.""" """Tests an API api_endpoint."""
response = httpx.get( response = httpx.get(
@ -110,6 +92,26 @@ def test_api_moderation(model: str=MODEL, messages: List[dict]=None) -> dict:
return response.text 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__': if __name__ == '__main__':
api_endpoint = 'https://alpha-api.nova-oss.com/v1' api_endpoint = 'https://alpha-api.nova-oss.com/v1'
closedai.api_base = api_endpoint closedai.api_base = api_endpoint