nova-api/api/providers/__main__.py

69 lines
1.8 KiB
Python
Raw Permalink Normal View History

2023-11-07 00:56:43 +01:00
"""CLI Tool"""
2023-10-05 15:06:33 +02:00
import os
import sys
2023-10-06 23:05:38 +02:00
import aiohttp
import asyncio
import importlib
2023-10-05 15:06:33 +02:00
from rich import print
def remove_duplicate_keys(file):
with open(file, 'r', encoding='utf8') as f:
2023-10-06 09:37:16 +02:00
unique_lines = set(f)
2023-10-05 15:06:33 +02:00
with open(file, 'w', encoding='utf8') as f:
f.writelines(unique_lines)
2023-10-06 23:05:38 +02:00
async def main():
try:
provider_name = sys.argv[1]
except IndexError:
print('List of available providers:')
2023-10-12 00:03:15 +02:00
for file_name in os.listdir(os.path.dirname(__file__)):
2023-10-06 23:05:38 +02:00
if file_name.endswith('.py') and not file_name.startswith('_'):
2023-11-07 00:56:43 +01:00
model_name = file_name.split('.')[0]
2023-10-16 23:34:54 +02:00
models = importlib.import_module(f'.{file_name.split(".")[0]}', 'providers').MODELS
2023-11-07 00:56:43 +01:00
text = ''
for model in models:
text += f' - {model}\n'
print(f' {model_name}:\n{text}')
2023-10-06 23:05:38 +02:00
sys.exit(0)
try:
provider = importlib.import_module(f'.{provider_name}', 'providers')
except ModuleNotFoundError as exc:
print(exc)
sys.exit(1)
2023-11-07 00:56:43 +01:00
if len(sys.argv) == 3:
2023-10-06 23:05:38 +02:00
model = sys.argv[2] # choose a specific model
else:
model = provider.MODELS[-1] # choose best model
print(f'{provider_name} @ {model}')
req = await provider.chat_completion(model=model, messages=[{'role': 'user', 'content': '1+1='}])
print(req)
# launch aiohttp
async with aiohttp.ClientSession() as session:
async with session.request(
method=req['method'],
url=req['url'],
headers=req['headers'],
json=req['payload'],
) as response:
2023-10-14 01:15:59 +02:00
try:
res_json = await response.json()
except aiohttp.ContentTypeError:
res_json = await response.text()
2023-10-06 23:05:38 +02:00
print(response.status, res_json)
asyncio.run(main())