Compare commits

..

No commits in common. "d1c7180d5d374afe0ed1b427797d59075782fa21" and "eb6768cae5dcc4cbe58344280d6ab99158e13c9d" have entirely different histories.

13 changed files with 71 additions and 69 deletions

View file

@ -1,19 +0,0 @@
import os
import asyncio
from dotenv import load_dotenv
from motor.motor_asyncio import AsyncIOMotorClient
load_dotenv()
class AzureManager:
def __init__(self):
self.conn = AsyncIOMotorClient(os.environ['MONGO_URI'])
async def _get_collection(self, collection_name: str):
azure_db = conn[os.getenv('MONGO_NAME', 'nova-test')][collection_name]
manager = AzureManager()
if __name__ == '__main__':
print(asyncio.run(manager.get_entire_financial_history()))

View file

@ -85,7 +85,7 @@ class KeyManager:
for filename in os.listdir(os.path.join('api', 'secret')): for filename in os.listdir(os.path.join('api', 'secret')):
if filename.endswith('.txt'): if filename.endswith('.txt'):
with open(os.path.join('api', 'secret', filename)) as f: async open(os.path.join('api', 'secret', filename)) as f:
async for line in f: async for line in f:
if not line.strip(): if not line.strip():
continue continue

View file

@ -21,7 +21,17 @@ with open(os.path.join(helpers.root, 'api', 'config', 'config.yml'), encoding='u
class UserManager: class UserManager:
""" """
Manager of all users in the database. ### Manager of all users in the database.
Following methods are available:
- `_get_collection(collection_name)`
- `create(discord_id)`
- `user_by_id(user_id)`
- `user_by_discord_id(discord_id)`
- `user_by_api_key(api_key)`
- `update_by_id(user_id, new_obj)`
- `update_by_filter(filter_object, new_obj)`
- `delete(user_id)`
""" """
def __init__(self): def __init__(self):
@ -35,35 +45,31 @@ class UserManager:
return collection#.find() return collection#.find()
async def create(self, discord_id: str = '') -> dict: async def create(self, discord_id: str = '') -> dict:
db = await self._get_collection('users')
chars = string.ascii_letters + string.digits chars = string.ascii_letters + string.digits
infix = os.getenv('KEYGEN_INFIX', 'S3LFH0ST') infix = os.getenv('KEYGEN_INFIX', 'S3LFH0ST')
suffix = ''.join(random.choices(chars, k=20)) suffix = ''.join(random.choices(chars, k=20))
prefix = ''.join(random.choices(chars, k=20)) prefix = ''.join(random.choices(chars, k=20))
new_api_key = f'nv2-{prefix}{infix}{suffix}' new_api_key = f'nv-{prefix}{infix}{suffix}'
existing_user = await self.user_by_discord_id(discord_id) new_user = {
if existing_user: # just change api key 'api_key': new_api_key,
await db.update_one({'auth.discord': str(int(discord_id))}, {'$set': {'api_key': new_api_key}}) 'credits': credits_config['start-credits'],
else: 'role': '',
new_user = { 'level': '',
'api_key': new_api_key, 'status': {
'credits': credits_config['start-credits'], 'active': True,
'role': '', 'ban_reason': '',
'level': '', },
'status': { 'auth': {
'active': True, 'discord': str(discord_id),
'ban_reason': '', 'github': None
},
'auth': {
'discord': str(discord_id),
'github': None
}
} }
}
await db.insert_one(new_user) db = await self._get_collection('users')
await db.insert_one(new_user)
user = await db.find_one({'api_key': new_api_key}) user = await db.find_one({'api_key': new_api_key})
return user return user

View file

@ -12,11 +12,15 @@ async def _get_module_name(module) -> str:
async def balance_chat_request(payload: dict) -> dict: async def balance_chat_request(payload: dict) -> dict:
""" """
Load balance the chat completion request between chat providers. Load balance the chat completion request between chat providers.
Providers are sorted by streaming and models. Target (provider.chat_completion) is returned
""" """
providers_available = [] providers_available = []
for provider_module in providers.MODULES: for provider_module in providers.MODULES:
if payload['stream'] and not provider_module.STREAMING:
continue
if payload['model'] not in provider_module.MODELS: if payload['model'] not in provider_module.MODELS:
continue continue
@ -49,6 +53,10 @@ async def balance_organic_request(request: dict) -> dict:
if not provider_module.ORGANIC: if not provider_module.ORGANIC:
continue continue
if '/moderations' in request['path']:
if not provider_module.MODERATIONS:
continue
providers_available.append(provider_module) providers_available.append(provider_module)
provider = random.choice(providers_available) provider = random.choice(providers_available)

View file

@ -49,10 +49,7 @@ async def main():
headers=req['headers'], headers=req['headers'],
json=req['payload'], json=req['payload'],
) as response: ) as response:
try: res_json = await response.json()
res_json = await response.json()
except aiohttp.ContentTypeError:
res_json = await response.text()
print(response.status, res_json) print(response.status, res_json)
asyncio.run(main()) asyncio.run(main())

View file

@ -1,6 +1,8 @@
from .helpers import utils from .helpers import utils
ORGANIC = False # If all OpenAI endpoints should be used for the provider. If false, only a chat completions are used for this provider. AUTH = True # If the provider requires an API key
ORGANIC = False # If all OpenAI endpoints are available on the provider. If false, only a chat completions are available.
STREAMING = True # If the provider supports streaming completions
ENDPOINT = 'https://nova-00001.openai.azure.com' # (Important: read below) The endpoint for the provider. ENDPOINT = 'https://nova-00001.openai.azure.com' # (Important: read below) The endpoint for the provider.
#! IMPORTANT: If this is an ORGANIC provider, this should be the endpoint for the API with anything BEFORE the "/v1". #! IMPORTANT: If this is an ORGANIC provider, this should be the endpoint for the API with anything BEFORE the "/v1".
MODELS = [ MODELS = [
@ -9,6 +11,21 @@ MODELS = [
'gpt-4', 'gpt-4',
'gpt-4-32k' 'gpt-4-32k'
] ]
MODELS += [f'{model}-azure' for model in MODELS]
AZURE_API = '2023-08-01-preview'
async def chat_completion(**payload): async def chat_completion(**payload):
return await utils.azure_chat_completion(ENDPOINT, 'azure-nva1', payload) key = await utils.random_secret_for('azure-nva1')
deployment = payload['model'].replace('.', '').replace('-azure', '')
return {
'method': 'POST',
'url': f'{ENDPOINT}/openai/deployments/{deployment}/chat/completions?api-version={AZURE_API}',
'payload': payload,
'headers': {
'api-key': key
},
'provider_auth': f'azure-nva1>{key}'
}

View file

@ -1,6 +1,8 @@
from .helpers import utils from .helpers import utils
AUTH = True
ORGANIC = True ORGANIC = True
STREAMING = True
ENDPOINT = 'https://api.openai.com' ENDPOINT = 'https://api.openai.com'
MODELS = utils.GPT_3 MODELS = utils.GPT_3

View file

@ -1,6 +1,8 @@
from .helpers import utils from .helpers import utils
AUTH = True
ORGANIC = False ORGANIC = False
STREAMING = True
ENDPOINT = 'https://api.openai.com' ENDPOINT = 'https://api.openai.com'
MODELS = utils.GPT_4 MODELS = utils.GPT_4

View file

@ -1,6 +1,8 @@
from .helpers import utils from .helpers import utils
AUTH = True
ORGANIC = False ORGANIC = False
STREAMING = True
ENDPOINT = 'https://api.openai.com' ENDPOINT = 'https://api.openai.com'
MODELS = utils.GPT_4_32K MODELS = utils.GPT_4_32K

View file

@ -23,22 +23,5 @@ GPT_4_32K = GPT_4 + [
'gpt-4-32k-0613', 'gpt-4-32k-0613',
] ]
AZURE_API = '2023-08-01-preview'
async def random_secret_for(name: str) -> str: async def random_secret_for(name: str) -> str:
return await providerkeys.manager.get_key(name) return await providerkeys.manager.get_key(name)
async def azure_chat_completion(endpoint: str, provider: str, payload: dict) -> dict:
key = await random_secret_for(provider)
model = payload['model']
deployment = model.replace('.', '').replace('-azure', '')
return {
'method': 'POST',
'url': f'{endpoint}/openai/deployments/{deployment}/chat/completions?api-version={AZURE_API}',
'payload': payload,
'headers': {
'api-key': key
},
'provider_auth': f'{provider}>{key}'
}

View file

@ -1,6 +1,8 @@
from .helpers import utils from .helpers import utils
AUTH = True
ORGANIC = False ORGANIC = False
STREAMING = True
MODELS = ['llama-2-7b-chat'] MODELS = ['llama-2-7b-chat']
async def chat_completion(**kwargs): async def chat_completion(**kwargs):

View file

@ -1,23 +1,25 @@
from .helpers import utils from .helpers import utils
AUTH = True
ORGANIC = False ORGANIC = False
STREAMING = True
MODELS = [ MODELS = [
'gpt-3.5-turbo', 'gpt-3.5-turbo-0613',
'gpt-3.5-turbo-0301', 'gpt-3.5-turbo-0301',
'gpt-3.5-turbo-16k-0613' 'gpt-3.5-turbo-16k-0613'
] ]
async def chat_completion(**kwargs): async def chat_completion(**kwargs):
payload = kwargs payload = kwargs
key = await utils.random_secret_for('ai.ls') key = await utils.random_secret_for('webraft')
return { return {
'method': 'POST', 'method': 'POST',
'url': 'https://api.caipacity.com/v1/chat/completions', 'url': 'https://thirdparty.webraft.in/v1/chat/completions',
'payload': payload, 'payload': payload,
'headers': { 'headers': {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': f'Bearer {key}' 'Authorization': f'Bearer {key}'
}, },
'provider_auth': f'ai.ls>{key}' 'provider_auth': f'webraft>{key}'
} }