2023-06-23 02:18:28 +02:00
|
|
|
"""Tests the API."""
|
|
|
|
|
2023-07-19 23:51:28 +02:00
|
|
|
import os
|
2023-06-28 15:21:14 +02:00
|
|
|
import openai as closedai
|
2023-06-23 02:18:28 +02:00
|
|
|
import httpx
|
|
|
|
|
2023-08-17 16:47:54 +02:00
|
|
|
from rich import print
|
2023-07-19 23:51:28 +02:00
|
|
|
from typing import List
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
load_dotenv()
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
MODEL = 'gpt-3.5-turbo'
|
2023-08-17 13:11:35 +02:00
|
|
|
|
2023-06-23 02:18:28 +02:00
|
|
|
MESSAGES = [
|
|
|
|
{
|
|
|
|
'role': 'user',
|
2023-08-07 23:28:24 +02:00
|
|
|
'content': '1+1=',
|
2023-08-03 01:46:49 +02:00
|
|
|
}
|
2023-06-23 02:18:28 +02:00
|
|
|
]
|
2023-07-19 23:51:28 +02:00
|
|
|
|
2023-08-01 02:38:55 +02:00
|
|
|
api_endpoint = 'http://localhost:2332'
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
def test_server():
|
2023-07-19 23:51:28 +02:00
|
|
|
"""Tests if the API server is running."""
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
try:
|
2023-08-15 13:55:12 +02:00
|
|
|
return httpx.get(f'{api_endpoint.replace("/v1", "")}').json()['status'] == 'ok'
|
2023-06-23 02:18:28 +02:00
|
|
|
except httpx.ConnectError as exc:
|
2023-07-19 23:51:28 +02:00
|
|
|
raise ConnectionError(f'API is not running on port {api_endpoint}.') from exc
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
def test_api(model: str=MODEL, messages: List[dict]=None) -> dict:
|
2023-07-19 23:51:28 +02:00
|
|
|
"""Tests an API api_endpoint."""
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
json_data = {
|
|
|
|
'model': model,
|
|
|
|
'messages': messages or MESSAGES,
|
2023-06-30 02:49:56 +02:00
|
|
|
'stream': True,
|
2023-06-23 02:18:28 +02:00
|
|
|
}
|
|
|
|
|
2023-07-19 23:51:28 +02:00
|
|
|
response = httpx.post(
|
2023-08-06 00:43:36 +02:00
|
|
|
url=f'{api_endpoint}/chat/completions',
|
2023-08-09 11:15:49 +02:00
|
|
|
headers=HEADERS,
|
2023-07-19 23:51:28 +02:00
|
|
|
json=json_data,
|
|
|
|
timeout=20
|
|
|
|
)
|
2023-06-23 02:18:28 +02:00
|
|
|
response.raise_for_status()
|
|
|
|
|
2023-08-01 20:19:18 +02:00
|
|
|
return response.text
|
2023-06-23 02:18:28 +02:00
|
|
|
|
|
|
|
def test_library():
|
2023-07-19 23:51:28 +02:00
|
|
|
"""Tests if the api_endpoint is working with the Python library."""
|
2023-06-23 02:18:28 +02:00
|
|
|
|
2023-06-28 15:21:14 +02:00
|
|
|
completion = closedai.ChatCompletion.create(
|
2023-06-23 02:18:28 +02:00
|
|
|
model=MODEL,
|
2023-08-06 21:42:07 +02:00
|
|
|
messages=MESSAGES
|
2023-06-23 02:18:28 +02:00
|
|
|
)
|
|
|
|
|
2023-08-13 00:59:54 +02:00
|
|
|
print(completion)
|
|
|
|
|
2023-08-06 21:42:07 +02:00
|
|
|
return completion['choices'][0]['message']['content']
|
2023-06-23 02:18:28 +02:00
|
|
|
|
2023-08-06 00:43:36 +02:00
|
|
|
def test_library_moderation():
|
2023-08-16 15:06:16 +02:00
|
|
|
try:
|
|
|
|
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')
|
2023-08-18 21:23:00 +02:00
|
|
|
except closedai.error.InvalidRequestError:
|
2023-08-16 15:06:16 +02:00
|
|
|
return True
|
2023-08-06 00:43:36 +02:00
|
|
|
|
2023-08-09 11:15:49 +02:00
|
|
|
def test_models():
|
|
|
|
response = httpx.get(
|
|
|
|
url=f'{api_endpoint}/models',
|
|
|
|
headers=HEADERS,
|
|
|
|
timeout=5
|
|
|
|
)
|
|
|
|
response.raise_for_status()
|
|
|
|
return response.json()
|
|
|
|
|
2023-08-15 13:55:12 +02:00
|
|
|
def test_api_moderation() -> dict:
|
|
|
|
"""Tests an API api_endpoint."""
|
|
|
|
|
|
|
|
response = httpx.get(
|
|
|
|
url=f'{api_endpoint}/moderations',
|
|
|
|
headers=HEADERS,
|
|
|
|
timeout=20
|
|
|
|
)
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
return response.text
|
|
|
|
|
|
|
|
# ==========================================================================================
|
|
|
|
|
2023-06-23 02:18:28 +02:00
|
|
|
def test_all():
|
|
|
|
"""Runs all tests."""
|
|
|
|
|
2023-08-17 16:47:54 +02:00
|
|
|
print('[lightblue]Running test on API server to check if its running...')
|
2023-08-13 17:16:33 +02:00
|
|
|
print(test_server())
|
|
|
|
|
2023-08-17 16:47:54 +02:00
|
|
|
print('[lightblue]Running a api endpoint to see if requests can go through...')
|
2023-08-13 17:16:33 +02:00
|
|
|
print(test_api())
|
|
|
|
|
2023-08-17 16:47:54 +02:00
|
|
|
print('[lightblue]Checking if the API works with the python library...')
|
2023-08-12 01:58:53 +02:00
|
|
|
print(test_library())
|
2023-08-13 17:16:33 +02:00
|
|
|
|
2023-08-17 16:47:54 +02:00
|
|
|
print('[lightblue]Checking if the moderation endpoint works...')
|
2023-08-13 17:16:33 +02:00
|
|
|
print(test_library_moderation())
|
|
|
|
|
2023-08-17 16:47:54 +02:00
|
|
|
print('[lightblue]Checking the /v1/models endpoint...')
|
2023-08-13 17:16:33 +02:00
|
|
|
print(test_models())
|
2023-08-05 02:30:42 +02:00
|
|
|
|
2023-06-23 02:18:28 +02:00
|
|
|
if __name__ == '__main__':
|
2023-08-06 00:43:36 +02:00
|
|
|
closedai.api_base = api_endpoint
|
|
|
|
closedai.api_key = os.getenv('TEST_NOVA_KEY')
|
2023-08-05 02:30:42 +02:00
|
|
|
|
2023-08-09 11:15:49 +02:00
|
|
|
HEADERS = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': 'Bearer ' + closedai.api_key
|
|
|
|
}
|
|
|
|
|
2023-08-06 00:43:36 +02:00
|
|
|
test_all()
|