mirror of
https://github.com/NovaOSS/nova-api.git
synced 2024-11-25 20:23:58 +01:00
Several improvements, about to change DB
This commit is contained in:
parent
cdcb1eedd1
commit
13c9e5ea91
|
@ -2,13 +2,12 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
import tokens
|
||||||
import logging
|
import logging
|
||||||
import starlette
|
import starlette
|
||||||
|
|
||||||
import netclient
|
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from helpers import requesting
|
from helpers import requesting, tokens, errors
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
@ -21,27 +20,45 @@ logging.basicConfig(
|
||||||
|
|
||||||
logging.info('API started')
|
logging.info('API started')
|
||||||
|
|
||||||
async def handle_api_request(incoming_request, target_endpoint: str=''):
|
DEFAULT_ENDPOINT = os.getenv('CLOSEDAI_ENDPOINT')
|
||||||
|
|
||||||
|
async def handle_api_request(incoming_request, target_endpoint: str=DEFAULT_ENDPOINT):
|
||||||
"""Transfer a streaming response from the incoming request to the target endpoint"""
|
"""Transfer a streaming response from the incoming request to the target endpoint"""
|
||||||
if not target_endpoint:
|
|
||||||
target_endpoint = os.getenv('CLOSEDAI_ENDPOINT')
|
|
||||||
|
|
||||||
target_url = f'{target_endpoint}{incoming_request.url.path}'
|
target_url = f'{target_endpoint}{incoming_request.url.path}'
|
||||||
logging.info('TRANSFER %s -> %s', incoming_request.url.path, target_url)
|
|
||||||
|
|
||||||
if target_url.endswith('/v1'):
|
if incoming_request.method not in ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']:
|
||||||
return starlette.responses.Response(status_code=200, content='200. /v1 is the API endpoint root path.')
|
return errors.error(405, f'Method "{incoming_request.method}" is not allowed.', 'Change the request method to the correct one.')
|
||||||
|
|
||||||
if incoming_request.headers.get('Authorization') != f'Bearer {os.getenv("DEMO_AUTH")}':
|
|
||||||
return starlette.responses.Response(status_code=403, content='Invalid API key')
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
payload = await incoming_request.json()
|
payload = await incoming_request.json()
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
payload = {}
|
payload = {}
|
||||||
|
|
||||||
if 'temperature' in payload or 'functions' in payload:
|
try:
|
||||||
target_provider = 'closed'
|
input_tokens = tokens.count_for_messages(payload['messages'])
|
||||||
|
except:
|
||||||
|
input_tokens = 0
|
||||||
|
|
||||||
|
auth_header = incoming_request.headers.get('Authorization')
|
||||||
|
|
||||||
|
if not auth_header:
|
||||||
|
return errors.error(401, 'No NovaOSS API key given!', 'Add "Authorization: Bearer nv-..." to your request headers.')
|
||||||
|
|
||||||
|
received_key = auth_header
|
||||||
|
|
||||||
|
if auth_header.startswith('Bearer '):
|
||||||
|
received_key = auth_header.split('Bearer ')[1]
|
||||||
|
|
||||||
|
user = users.get_user(by_api_key=received_key)
|
||||||
|
|
||||||
|
if not user:
|
||||||
|
return errors.error(401, 'Invalid NovaOSS API key!', 'Create a new NovaOSS API key.')
|
||||||
|
|
||||||
|
if not user['active']:
|
||||||
|
return errors.error(403, 'Your account is not active.', 'Activate your account.')
|
||||||
|
|
||||||
|
logging.info(f'[%s] %s -> %s', incoming_request.method, incoming_request.url.path, target_url)
|
||||||
|
|
||||||
request = requesting.Request(
|
request = requesting.Request(
|
||||||
url=target_url,
|
url=target_url,
|
||||||
|
|
Loading…
Reference in a new issue