nova-api/api/load_balancing.py

57 lines
1.4 KiB
Python
Raw Normal View History

2023-08-04 03:30:56 +02:00
import random
import asyncio
import providers
2023-08-04 03:30:56 +02:00
provider_modules = [
# providers.twa,
# providers.quantum,
providers.churchless,
providers.closed,
providers.closed4
2023-08-04 03:30:56 +02:00
]
2023-08-04 17:29:49 +02:00
def _get_module_name(module) -> str:
name = module.__name__
if '.' in name:
return name.split('.')[-1]
return name
2023-08-04 03:30:56 +02:00
async def balance_chat_request(payload: dict) -> dict:
providers_available = []
for provider_module in provider_modules:
if payload['stream'] and not provider_module.STREAMING:
continue
if payload['model'] not in provider_module.MODELS:
continue
providers_available.append(provider_module)
2023-08-05 02:30:42 +02:00
if not providers_available:
raise NotImplementedError('This model does not exist.')
2023-08-04 03:30:56 +02:00
provider = random.choice(providers_available)
2023-08-04 17:29:49 +02:00
target = provider.chat_completion(**payload)
target['module'] = _get_module_name(provider)
return target
2023-08-04 03:30:56 +02:00
async def balance_organic_request(request: dict) -> dict:
providers_available = []
for provider_module in provider_modules:
if provider_module.ORGANIC:
providers_available.append(provider_module)
provider = random.choice(providers_available)
2023-08-04 17:29:49 +02:00
target = provider.organify(request)
target['module'] = _get_module_name(provider)
2023-08-04 03:30:56 +02:00
2023-08-04 17:29:49 +02:00
return target
2023-08-04 03:30:56 +02:00
if __name__ == '__main__':
req = asyncio.run(balance_chat_request(payload={'model': 'gpt-3.5-turbo', 'stream': True}))
print(req['url'])