Resolved some issues

This commit is contained in:
nsde 2023-08-24 14:57:36 +02:00
parent 4f3a5264f2
commit f01b8e5ecc
6 changed files with 37 additions and 20 deletions

View file

@ -52,6 +52,8 @@ async def get_users(discord_id: int, incoming_request: fastapi.Request):
if not user: if not user:
return await errors.error(404, 'Discord user not found in the API database.', 'Check the `discord_id` parameter.') 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 return user
async def new_user_webhook(user: dict) -> None: async def new_user_webhook(user: dict) -> None:
@ -128,7 +130,7 @@ async def run_checks(incoming_request: fastapi.Request):
try: try:
chat = await checks.client.test_chat() chat = await checks.client.test_chat()
except Exception as exc except Exception as exc:
print(exc) print(exc)
chat = None chat = None

View file

@ -1,10 +1,13 @@
"""FastAPI setup.""" """FastAPI setup."""
import fastapi import fastapi
import pydantic
from rich import print from rich import print
from dotenv import load_dotenv from dotenv import load_dotenv
from bson.objectid import ObjectId
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
import core import core
import transfer import transfer
@ -25,6 +28,8 @@ app.include_router(core.router)
@app.on_event('startup') @app.on_event('startup')
async def startup_event(): async def startup_event():
"""Runs when the API starts up.""" """Runs when the API starts up."""
# https://stackoverflow.com/a/74529009
pydantic.json.ENCODERS_BY_TYPE[ObjectId]=str
@app.get('/') @app.get('/')
async def root(): async def root():

View file

@ -106,21 +106,21 @@ async def stream(
# We haven't done any requests as of right now, everything until now was just preparation # We haven't done any requests as of right now, everything until now was just preparation
# Here, we process the request # Here, we process the request
async with aiohttp.ClientSession(connector=proxies.get_proxy().connector) as session: async with aiohttp.ClientSession(connector=proxies.get_proxy().connector) as session:
try: # try:
async with session.get( # async with session.get(
url='https://checkip.amazonaws.com', # url='https://checkip.amazonaws.com',
timeout=aiohttp.ClientTimeout( # timeout=aiohttp.ClientTimeout(
connect=3, # connect=0.4,
total=float(os.getenv('TRANSFER_TIMEOUT', '5')) # total=0.7
) # )
) as response: # ) as response:
for actual_ip in os.getenv('ACTUAL_IPS', '').split(' '): # for actual_ip in os.getenv('ACTUAL_IPS', '').split(' '):
if actual_ip in await response.text(): # if actual_ip in await response.text():
raise ValueError(f'Proxy {response.text()} is transparent!') # raise ValueError(f'Proxy {response.text()} is transparent!')
except Exception as exc: # except Exception as exc:
print(f'[!] proxy {proxies.get_proxy()} error - ({type(exc)} {exc})') # print(f'[!] proxy {proxies.get_proxy()} error - ({type(exc)} {exc})')
continue # continue
try: try:
async with session.request( 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")') print('[!] aiohttp came up with a dumb excuse to not work again ("cOnNeCtIoN rEsEt")')
continue 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: if is_chat and is_stream:
yield await chat.create_chat_chunk(chat_id=chat_id, model=model, content=chat.CompletionStop) yield await chat.create_chat_chunk(chat_id=chat_id, model=model, content=chat.CompletionStop)
yield 'data: [DONE]\n\n' yield 'data: [DONE]\n\n'

View file

@ -10,10 +10,11 @@ import streaming
import moderation import moderation
from db.users import UserManager from db.users import UserManager
from helpers import tokens, errors from helpers import tokens, errors, network
load_dotenv() load_dotenv()
users = UserManager()
models_list = json.load(open('models.json', encoding='utf8')) models_list = json.load(open('models.json', encoding='utf8'))
with open('config/config.yml', encoding='utf8') as f: 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. Takes the request from the incoming request to the target endpoint.
Checks method, token amount, auth and cost along with if request is NSFW. 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('//', '/') 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: if '/models' in path:
return fastapi.responses.JSONResponse(content=models_list) return fastapi.responses.JSONResponse(content=models_list)
@ -69,7 +72,10 @@ async def handle(incoming_request):
policy_violation = await moderation.is_policy_violated(inp) policy_violation = await moderation.is_policy_violated(inp)
if policy_violation: 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') role = user.get('role', 'default')

View file

@ -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(): async def test_server():
"""Tests if the API server is running.""" """Tests if the API server is running."""

View file

@ -1,7 +1,7 @@
# Script to start the production server # Script to start the production server
# Commit to the production branch # 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 # Copy files to production
cp -r * /home/nova-prod cp -r * /home/nova-prod