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"""
|
2023-06-30 02:49:56 +02:00
|
|
|
|
2023-07-19 23:51:28 +02:00
|
|
|
import json
|
2023-08-04 03:30:56 +02:00
|
|
|
import yaml
|
2023-08-06 21:42:07 +02:00
|
|
|
import fastapi
|
2023-06-30 02:49:56 +02:00
|
|
|
|
2023-06-28 15:21:14 +02:00
|
|
|
from dotenv import load_dotenv
|
2023-08-03 01:46:49 +02:00
|
|
|
|
2023-08-04 03:30:56 +02:00
|
|
|
import streaming
|
2023-08-06 21:42:07 +02:00
|
|
|
import moderation
|
2023-08-03 01:46:49 +02:00
|
|
|
|
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-08-22 20:03:55 +02:00
|
|
|
models_list = json.load(open('models.json', encoding='utf8'))
|
2023-08-09 11:43:05 +02:00
|
|
|
|
2023-08-17 13:11:35 +02:00
|
|
|
with open('config/config.yml', encoding='utf8') as f:
|
|
|
|
config = yaml.safe_load(f)
|
2023-08-04 03:30:56 +02:00
|
|
|
|
2023-08-03 01:46:49 +02:00
|
|
|
async def handle(incoming_request):
|
2023-08-13 17:12:35 +02:00
|
|
|
"""
|
|
|
|
### Transfer a streaming response
|
|
|
|
Takes the request from the incoming request to the target endpoint.
|
|
|
|
Checks method, token amount, auth and cost along with if request is NSFW.
|
|
|
|
"""
|
2023-08-23 23:27:09 +02:00
|
|
|
path = incoming_request.url.path.replace('v1/v1', 'v1').replace('//', '/')
|
2023-06-30 02:49:56 +02:00
|
|
|
|
2023-08-24 14:57:36 +02:00
|
|
|
ip_address = await network.get_ip(incoming_request)
|
2023-08-25 19:13:39 +02:00
|
|
|
print(f'[bold green]>{ip_address}[/bold green]')
|
2023-08-24 14:57:36 +02:00
|
|
|
|
2023-08-19 17:53:48 +02:00
|
|
|
if '/models' in path:
|
|
|
|
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-07-25 02:42:53 +02:00
|
|
|
|
2023-08-01 20:19:00 +02:00
|
|
|
try:
|
2023-08-13 18:19:56 +02:00
|
|
|
input_tokens = await tokens.count_for_messages(payload.get('messages', []))
|
2023-08-03 01:46:49 +02:00
|
|
|
except (KeyError, TypeError):
|
2023-08-01 20:19:00 +02:00
|
|
|
input_tokens = 0
|
|
|
|
|
2023-08-04 03:30:56 +02:00
|
|
|
received_key = incoming_request.headers.get('Authorization')
|
2023-08-01 20:19:00 +02:00
|
|
|
|
2023-08-13 18:19:56 +02:00
|
|
|
if not received_key or not received_key.startswith('Bearer '):
|
2023-08-21 21:09:22 +02:00
|
|
|
return await errors.error(403, 'No NovaAI API key given!', 'Add \'Authorization: Bearer nv-...\' to your request headers.')
|
2023-08-01 20:19:00 +02:00
|
|
|
|
2023-08-14 05:11:15 +02:00
|
|
|
user = await users.user_by_api_key(received_key.split('Bearer ')[1].strip())
|
2023-08-01 20:19:00 +02:00
|
|
|
|
2023-08-13 18:19:56 +02:00
|
|
|
if not user or not user['status']['active']:
|
2023-08-21 21:09:22 +02:00
|
|
|
return await errors.error(403, 'Invalid or inactive NovaAI API key!', 'Create a new NovaOSS API key or reactivate your account.')
|
2023-08-03 01:46:49 +02:00
|
|
|
|
2023-08-25 19:13:39 +02:00
|
|
|
if user.get('auth', {}).get('discord'):
|
|
|
|
print(f'[bold green]>Discord[/bold green] {user["auth"]["discord"]}')
|
|
|
|
|
2023-08-03 01:46:49 +02:00
|
|
|
ban_reason = user['status']['ban_reason']
|
|
|
|
if ban_reason:
|
2023-08-21 20:58:05 +02:00
|
|
|
return await errors.error(403, f'Your NovaAI account has been banned. Reason: \'{ban_reason}\'.', 'Contact the staff for an appeal.')
|
2023-08-03 01:46:49 +02:00
|
|
|
|
2023-08-17 13:11:35 +02:00
|
|
|
costs = config['costs']
|
2023-08-04 03:30:56 +02:00
|
|
|
cost = costs['other']
|
2023-08-03 01:46:49 +02:00
|
|
|
|
2023-08-04 03:30:56 +02:00
|
|
|
if 'chat/completions' in path:
|
2023-08-13 18:19:56 +02:00
|
|
|
cost = costs['chat-models'].get(payload.get('model'), cost)
|
2023-08-03 01:46:49 +02:00
|
|
|
|
2023-08-13 18:19:56 +02:00
|
|
|
policy_violation = False
|
2023-08-23 23:27:09 +02:00
|
|
|
if '/moderations' not in path:
|
|
|
|
if '/chat/completions' in path or ('input' in payload or 'prompt' in payload):
|
|
|
|
inp = payload.get('input', payload.get('prompt', ''))
|
|
|
|
if inp and len(inp) > 2 and not inp.isnumeric():
|
|
|
|
policy_violation = await moderation.is_policy_violated(inp)
|
2023-08-07 23:28:24 +02:00
|
|
|
|
2023-08-08 01:04:35 +02:00
|
|
|
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}".',
|
|
|
|
'We currently don\'t support any NSFW models.'
|
|
|
|
)
|
2023-08-12 17:56:21 +02:00
|
|
|
|
2023-08-17 13:11:35 +02:00
|
|
|
role = user.get('role', 'default')
|
2023-08-18 21:23:00 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
role_cost_multiplier = config['roles'][role]['bonus']
|
|
|
|
except KeyError:
|
|
|
|
role_cost_multiplier = 1
|
|
|
|
|
2023-08-04 03:30:56 +02:00
|
|
|
cost = round(cost * role_cost_multiplier)
|
2023-08-03 01:46:49 +02:00
|
|
|
|
|
|
|
if user['credits'] < cost:
|
2023-08-12 17:56:21 +02:00
|
|
|
return await errors.error(429, 'Not enough credits.', 'Wait or earn more credits. Learn more on our website or Discord server.')
|
|
|
|
|
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
|
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'
|
|
|
|
|
|
|
|
return fastapi.responses.StreamingResponse(
|
2023-08-04 03:30:56 +02:00
|
|
|
content=streaming.stream(
|
|
|
|
user=user,
|
|
|
|
path=path,
|
|
|
|
payload=payload,
|
|
|
|
credits_cost=cost,
|
|
|
|
input_tokens=input_tokens,
|
|
|
|
incoming_request=incoming_request,
|
|
|
|
),
|
2023-08-06 21:42:07 +02:00
|
|
|
media_type=media_type
|
2023-08-04 03:30:56 +02:00
|
|
|
)
|