nova-api/api/handler.py

154 lines
5.5 KiB
Python
Raw Normal View History

2023-08-12 17:49:31 +02:00
"""Does quite a few checks and prepares the incoming request for the target endpoint, so it can be streamed"""
import os
2023-07-19 23:51:28 +02:00
import json
2023-08-04 03:30:56 +02:00
import yaml
2023-08-29 01:18:55 +02:00
import time
2023-08-06 21:42:07 +02:00
import fastapi
2023-06-28 15:21:14 +02:00
from dotenv import load_dotenv
2023-09-11 02:47:21 +02:00
import responder
2023-08-06 21:42:07 +02:00
import moderation
2023-08-25 19:13:39 +02:00
from rich import print
2023-08-15 13:55:12 +02:00
from db.users import UserManager
2023-08-24 14:57:36 +02:00
from helpers import tokens, errors, network
2023-06-28 15:21:14 +02:00
load_dotenv()
2023-08-24 14:57:36 +02:00
users = UserManager()
2023-10-06 09:37:16 +02:00
with open(os.path.join('cache', 'models.json'), encoding='utf8') as f:
models_list = json.load(f)
models = [model['id'] for model in models_list['data']]
2023-10-15 22:35:18 +02:00
moderation_debug_key = os.getenv('MODERATION_DEBUG_KEY')
2023-08-27 04:29:16 +02:00
async def handle(incoming_request: fastapi.Request):
"""Transfer a streaming response
2023-08-13 17:12:35 +02:00
Takes the request from the incoming request to the target endpoint.
Checks method, token amount, auth and cost along with if request is NSFW.
"""
path = incoming_request.url.path
path = path.replace('/v1/v1', '/v1')
2023-10-16 23:34:54 +02:00
ip_address = network.get_ip(incoming_request)
2023-08-24 14:57:36 +02:00
2023-10-09 19:09:01 +02:00
if '/dashboard' in path:
2023-10-16 23:34:54 +02:00
return await errors.error(404, 'You can\'t access /dashboard.', 'This is a private endpoint.')
2023-10-09 19:09:01 +02:00
if path.startswith('/v1/models'):
2023-08-19 17:53:48 +02:00
return fastapi.responses.JSONResponse(content=models_list)
2023-08-15 13:55:12 +02:00
try:
payload = await incoming_request.json()
except json.decoder.JSONDecodeError:
payload = {}
2023-08-30 20:55:31 +02:00
except UnicodeDecodeError:
payload = {}
2023-07-25 02:42:53 +02:00
2023-08-04 03:30:56 +02:00
received_key = incoming_request.headers.get('Authorization')
2023-08-13 18:19:56 +02:00
if not received_key or not received_key.startswith('Bearer '):
return await errors.error(401, 'No NovaAI API key given!', 'Add \'Authorization: Bearer nv-...\' to your request headers.')
2023-08-29 01:23:00 +02:00
key_tags = ''
2023-08-29 01:18:55 +02:00
if '#' in received_key:
key_tags = received_key.split('#')[1]
received_key = received_key.split('#')[0]
user = await users.user_by_api_key(received_key.split('Bearer ')[1].strip())
2023-08-13 18:19:56 +02:00
if not user or not user['status']['active']:
return await errors.error(418, 'Invalid or inactive NovaAI API key!', 'Create a new NovaOSS API key or reactivate your account.')
ban_reason = user['status']['ban_reason']
if ban_reason:
return await errors.error(403, f'Your NovaAI account has been banned. Reason: \'{ban_reason}\'.', 'Contact the staff for an appeal.')
2023-10-15 22:50:13 +02:00
is_enterprise_key = 'enterprise' in user.get('role', 'default')
if path.startswith('/enterprise/v1') and not is_enterprise_key:
return await errors.error(403, 'Enterprise API is not available for your API key.', 'Contact the staff for an upgrade.')
2023-09-22 02:38:21 +02:00
if 'account/credits' in path:
return fastapi.responses.JSONResponse({'credits': user['credits']})
2023-10-16 23:34:54 +02:00
if user['credits'] < 1:
2023-08-29 01:18:55 +02:00
return await errors.error(429, 'Not enough credits.', 'Wait or earn more credits. Learn more on our website or Discord server.')
if 'DISABLE_VARS' not in key_tags:
2023-08-30 20:55:31 +02:00
payload_with_vars = json.dumps(payload)
replace_dict = {
'timestamp': str(int(time.time())),
'date': time.strftime('%Y-%m-%d'),
'time': time.strftime('%H:%M:%S'),
'datetime': time.strftime('%Y-%m-%d %H:%M:%S'),
'model': payload.get('model', 'unknown'),
}
if 'ALLOW_INSECURE_VARS' in key_tags:
replace_dict.update({
'my.ip': ip_address,
'my.id': str(user['_id']),
'my.role': user.get('role', 'default'),
'my.credits': str(user['credits']),
'my.discord': user.get('auth', {}).get('discord', ''),
})
for key, value in replace_dict.items():
payload_with_vars = payload_with_vars.replace(f'[[{key}]]', value)
payload = json.loads(payload_with_vars)
2023-08-29 01:18:55 +02:00
2023-08-13 18:19:56 +02:00
policy_violation = False
2023-10-15 22:35:18 +02:00
if not (moderation_debug_key and moderation_debug_key in key_tags and 'gpt-3' in payload.get('model', '')):
if '/moderations' not in path:
inp = ''
if 'input' in payload or 'prompt' in payload:
inp = payload.get('input', payload.get('prompt', ''))
if isinstance(payload.get('messages'), list):
2023-09-14 18:18:19 +02:00
inp = ''
for message in payload.get('messages', []):
if message.get('role') == 'user':
inp += message.get('content', '') + '\n'
if 'functions' in payload:
inp += '\n'.join([function.get('description', '') for function in payload.get('functions', [])])
if inp and len(inp) > 2 and not inp.isnumeric():
policy_violation = await moderation.is_policy_violated(inp)
if policy_violation:
2023-08-24 14:57:36 +02:00
return await errors.error(
400, f'The request contains content which violates this model\'s policies for <{policy_violation}>.',
2023-08-24 14:57:36 +02:00
'We currently don\'t support any NSFW models.'
)
2023-08-12 17:56:21 +02:00
2023-08-13 18:19:56 +02:00
if 'chat/completions' in path and not payload.get('stream', False):
2023-08-04 03:30:56 +02:00
payload['stream'] = False
if 'chat/completions' in path and not payload.get('model'):
payload['model'] = 'gpt-3.5-turbo'
2023-08-03 03:50:04 +02:00
2023-08-06 21:42:07 +02:00
media_type = 'text/event-stream' if payload.get('stream', False) else 'application/json'
2023-09-22 02:38:21 +02:00
if (model := payload.get('model')) not in models and model is not None:
return await errors.error(404, 'Model not found.', 'Check the model name and try again.')
2023-08-06 21:42:07 +02:00
return fastapi.responses.StreamingResponse(
2023-09-11 02:47:21 +02:00
content=responder.respond(
2023-08-04 03:30:56 +02:00
user=user,
path=path,
payload=payload,
incoming_request=incoming_request,
),
2023-08-06 21:42:07 +02:00
media_type=media_type
2023-08-04 03:30:56 +02:00
)