mirror of
https://github.com/NovaOSS/nova-api.git
synced 2024-11-26 00:13:57 +01:00
Compare commits
No commits in common. "947b70aae97e3e9b803e28728bee78139f577fa1" and "c58eaa5b32d4f8af4575f0cb49ac6bce72112e31" have entirely different histories.
947b70aae9
...
c58eaa5b32
|
@ -2,8 +2,6 @@
|
|||
|
||||
import json
|
||||
import yaml
|
||||
import time
|
||||
import orjson
|
||||
import fastapi
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
@ -47,12 +45,6 @@ async def handle(incoming_request: fastapi.Request):
|
|||
if not received_key or not received_key.startswith('Bearer '):
|
||||
return await errors.error(403, 'No NovaAI API key given!', 'Add \'Authorization: Bearer nv-...\' to your request headers.')
|
||||
|
||||
key_tags = ''
|
||||
|
||||
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())
|
||||
|
||||
if not user or not user['status']['active']:
|
||||
|
@ -71,42 +63,6 @@ async def handle(incoming_request: fastapi.Request):
|
|||
if 'chat/completions' in path:
|
||||
cost = costs['chat-models'].get(payload.get('model'), cost)
|
||||
|
||||
role = user.get('role', 'default')
|
||||
|
||||
try:
|
||||
role_cost_multiplier = config['roles'][role]['bonus']
|
||||
except KeyError:
|
||||
role_cost_multiplier = 1
|
||||
|
||||
cost = round(cost * role_cost_multiplier)
|
||||
|
||||
if user['credits'] < cost:
|
||||
return await errors.error(429, 'Not enough credits.', 'Wait or earn more credits. Learn more on our website or Discord server.')
|
||||
|
||||
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)
|
||||
|
||||
policy_violation = False
|
||||
if '/moderations' not in path:
|
||||
inp = ''
|
||||
|
@ -126,6 +82,18 @@ async def handle(incoming_request: fastapi.Request):
|
|||
'We currently don\'t support any NSFW models.'
|
||||
)
|
||||
|
||||
role = user.get('role', 'default')
|
||||
|
||||
try:
|
||||
role_cost_multiplier = config['roles'][role]['bonus']
|
||||
except KeyError:
|
||||
role_cost_multiplier = 1
|
||||
|
||||
cost = round(cost * role_cost_multiplier)
|
||||
|
||||
if user['credits'] < cost:
|
||||
return await errors.error(429, 'Not enough credits.', 'Wait or earn more credits. Learn more on our website or Discord server.')
|
||||
|
||||
if 'chat/completions' in path and not payload.get('stream', False):
|
||||
payload['stream'] = False
|
||||
|
||||
|
|
Loading…
Reference in a new issue