mirror of
https://github.com/NovaOSS/nova-api.git
synced 2024-11-29 10:43:57 +01:00
Compare commits
2 commits
c4137a9eab
...
f01b8e5ecc
Author | SHA1 | Date | |
---|---|---|---|
f01b8e5ecc | |||
4f3a5264f2 |
|
@ -52,6 +52,8 @@ async def get_users(discord_id: int, incoming_request: fastapi.Request):
|
|||
if not user:
|
||||
return await errors.error(404, 'Discord user not found in the API database.', 'Check the `discord_id` parameter.')
|
||||
|
||||
print(type(user))
|
||||
print(user)
|
||||
return user
|
||||
|
||||
async def new_user_webhook(user: dict) -> None:
|
||||
|
@ -128,7 +130,8 @@ async def run_checks(incoming_request: fastapi.Request):
|
|||
|
||||
try:
|
||||
chat = await checks.client.test_chat()
|
||||
except Exception:
|
||||
except Exception as exc:
|
||||
print(exc)
|
||||
chat = None
|
||||
|
||||
try:
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
"""FastAPI setup."""
|
||||
|
||||
import fastapi
|
||||
import pydantic
|
||||
|
||||
from rich import print
|
||||
from dotenv import load_dotenv
|
||||
from bson.objectid import ObjectId
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
import core
|
||||
import transfer
|
||||
|
||||
|
@ -25,6 +28,8 @@ app.include_router(core.router)
|
|||
@app.on_event('startup')
|
||||
async def startup_event():
|
||||
"""Runs when the API starts up."""
|
||||
# https://stackoverflow.com/a/74529009
|
||||
pydantic.json.ENCODERS_BY_TYPE[ObjectId]=str
|
||||
|
||||
@app.get('/')
|
||||
async def root():
|
||||
|
|
|
@ -106,21 +106,21 @@ async def stream(
|
|||
# We haven't done any requests as of right now, everything until now was just preparation
|
||||
# Here, we process the request
|
||||
async with aiohttp.ClientSession(connector=proxies.get_proxy().connector) as session:
|
||||
try:
|
||||
async with session.get(
|
||||
url='https://checkip.amazonaws.com',
|
||||
timeout=aiohttp.ClientTimeout(
|
||||
connect=3,
|
||||
total=float(os.getenv('TRANSFER_TIMEOUT', '5'))
|
||||
)
|
||||
) as response:
|
||||
for actual_ip in os.getenv('ACTUAL_IPS', '').split(' '):
|
||||
if actual_ip in await response.text():
|
||||
raise ValueError(f'Proxy {response.text()} is transparent!')
|
||||
# try:
|
||||
# async with session.get(
|
||||
# url='https://checkip.amazonaws.com',
|
||||
# timeout=aiohttp.ClientTimeout(
|
||||
# connect=0.4,
|
||||
# total=0.7
|
||||
# )
|
||||
# ) as response:
|
||||
# for actual_ip in os.getenv('ACTUAL_IPS', '').split(' '):
|
||||
# if actual_ip in await response.text():
|
||||
# raise ValueError(f'Proxy {response.text()} is transparent!')
|
||||
|
||||
except Exception as exc:
|
||||
print(f'[!] proxy {proxies.get_proxy()} error - ({type(exc)} {exc})')
|
||||
continue
|
||||
# except Exception as exc:
|
||||
# print(f'[!] proxy {proxies.get_proxy()} error - ({type(exc)} {exc})')
|
||||
# continue
|
||||
|
||||
try:
|
||||
async with session.request(
|
||||
|
@ -175,6 +175,10 @@ async def stream(
|
|||
print('[!] aiohttp came up with a dumb excuse to not work again ("cOnNeCtIoN rEsEt")')
|
||||
continue
|
||||
|
||||
except aiohttp.client_exceptions.ClientConnectionError:
|
||||
print('[!] aiohttp came up with a dumb excuse to not work again ("cOnNeCtIoN cLosEd")')
|
||||
continue
|
||||
|
||||
if is_chat and is_stream:
|
||||
yield await chat.create_chat_chunk(chat_id=chat_id, model=model, content=chat.CompletionStop)
|
||||
yield 'data: [DONE]\n\n'
|
||||
|
|
|
@ -10,10 +10,11 @@ import streaming
|
|||
import moderation
|
||||
|
||||
from db.users import UserManager
|
||||
from helpers import tokens, errors
|
||||
from helpers import tokens, errors, network
|
||||
|
||||
load_dotenv()
|
||||
|
||||
users = UserManager()
|
||||
models_list = json.load(open('models.json', encoding='utf8'))
|
||||
|
||||
with open('config/config.yml', encoding='utf8') as f:
|
||||
|
@ -25,9 +26,11 @@ async def handle(incoming_request):
|
|||
Takes the request from the incoming request to the target endpoint.
|
||||
Checks method, token amount, auth and cost along with if request is NSFW.
|
||||
"""
|
||||
users = UserManager()
|
||||
path = incoming_request.url.path.replace('v1/v1', 'v1').replace('//', '/')
|
||||
|
||||
ip_address = await network.get_ip(incoming_request)
|
||||
print(f'[{ip_address}] {path}')
|
||||
|
||||
if '/models' in path:
|
||||
return fastapi.responses.JSONResponse(content=models_list)
|
||||
|
||||
|
@ -69,7 +72,10 @@ async def handle(incoming_request):
|
|||
policy_violation = await moderation.is_policy_violated(inp)
|
||||
|
||||
if policy_violation:
|
||||
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.')
|
||||
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.'
|
||||
)
|
||||
|
||||
role = user.get('role', 'default')
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ MESSAGES = [
|
|||
}
|
||||
]
|
||||
|
||||
api_endpoint = 'http://localhost:2332/v1'
|
||||
api_endpoint = os.getenv('CHECKS_ENDPOINT', 'http://localhost:2332/v1')
|
||||
|
||||
async def test_server():
|
||||
"""Tests if the API server is running."""
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# Script to start the production server
|
||||
|
||||
# Commit to the production branch
|
||||
git commit -am "Auto-trigger - Production server started" && git push origin Production
|
||||
# git commit -am "Auto-trigger - Production server started" && git push origin Production
|
||||
|
||||
# Copy files to production
|
||||
cp -r * /home/nova-prod
|
||||
|
|
Loading…
Reference in a new issue