diff --git a/.gitignore b/.gitignore index d806065..7bc211e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,9 +22,6 @@ last_update.txt .log *.log.* -providers/* -providers/ - secret/* secret/ /secret diff --git a/api/providers/__init__.py b/api/providers/__init__.py new file mode 100644 index 0000000..86f2ea0 --- /dev/null +++ b/api/providers/__init__.py @@ -0,0 +1,10 @@ +from . import \ + closed, \ + closed4 + # closed432 + +MODULES = [ + closed, + closed4, + # closed432, +] diff --git a/api/providers/__main__.py b/api/providers/__main__.py new file mode 100644 index 0000000..dc4b65d --- /dev/null +++ b/api/providers/__main__.py @@ -0,0 +1,52 @@ +import os +import sys + +from rich import print + +def remove_duplicate_keys(file): + with open(file, 'r', encoding='utf8') as f: + lines = f.readlines() + + unique_lines = set(lines) + + with open(file, 'w', encoding='utf8') as f: + f.writelines(unique_lines) + +try: + provider_name = sys.argv[1] + + if provider_name == '--clear': + for file in os.listdir('secret/'): + if file.endswith('.txt'): + remove_duplicate_keys(f'secret/{file}') + + exit() + +except IndexError: + print('List of available providers:') + + for file_name in os.listdir(os.path.dirname(__file__)): + if file_name.endswith('.py') and not file_name.startswith('_'): + print(file_name.split('.')[0]) + + sys.exit(0) + +try: + provider = __import__(provider_name) +except ModuleNotFoundError as exc: + print(f'Provider "{provider_name}" not found.') + print('Available providers:') + for file_name in os.listdir(os.path.dirname(__file__)): + if file_name.endswith('.py') and not file_name.startswith('_'): + print(file_name.split('.')[0]) + sys.exit(1) + +if len(sys.argv) > 2: + model = sys.argv[2] +else: + model = provider.MODELS[-1] + + +print(f'{provider_name} @ {model}') +comp = provider.chat_completion(model=model) +print(comp) diff --git a/api/providers/closed.py b/api/providers/closed.py new file mode 100644 index 0000000..2bb4515 --- /dev/null +++ b/api/providers/closed.py @@ -0,0 +1,35 @@ +from .helpers import utils + +AUTH = True +ORGANIC = True +CONTEXT = True +STREAMING = True +MODERATIONS = True +ENDPOINT = 'https://api.openai.com' +MODELS = utils.GPT_3 + +async def get_key() -> str: + return await utils.random_secret_for('closed') + +async def chat_completion(**kwargs): + payload = kwargs + key = await get_key() + + return { + 'method': 'POST', + 'url': f'{ENDPOINT}/v1/chat/completions', + 'payload': payload, + 'headers': { + 'Authorization': f'Bearer {key}' + }, + 'provider_auth': f'closed>{key}' + } + +async def organify(request: dict) -> dict: + key = await get_key() + + request['url'] = ENDPOINT + request['path'] + request['headers']['Authorization'] = f'Bearer {key}' + request['provider_auth'] = f'closed>{key}' + + return request diff --git a/api/providers/closed4.py b/api/providers/closed4.py new file mode 100644 index 0000000..6da901f --- /dev/null +++ b/api/providers/closed4.py @@ -0,0 +1,35 @@ +from .helpers import utils + +AUTH = True +ORGANIC = False +CONTEXT = True +STREAMING = True +MODERATIONS = True +ENDPOINT = 'https://api.openai.com' +MODELS = utils.GPT_4 + +async def get_key() -> str: + return await utils.random_secret_for('closed4') + +async def chat_completion(**kwargs): + payload = kwargs + key = await get_key() + + return { + 'method': 'POST', + 'url': f'{ENDPOINT}/v1/chat/completions', + 'payload': payload, + 'headers': { + 'Authorization': f'Bearer {key}' + }, + 'provider_auth': f'closed4>{key}' + } + +async def organify(request: dict) -> dict: + key = await get_key() + + request['url'] = ENDPOINT + request['path'] + request['headers']['Authorization'] = f'Bearer {key}' + request['provider_auth'] = f'closed4>{key}' + + return request diff --git a/api/providers/closed432.py b/api/providers/closed432.py new file mode 100644 index 0000000..8215bcf --- /dev/null +++ b/api/providers/closed432.py @@ -0,0 +1,35 @@ +from .helpers import utils + +AUTH = True +ORGANIC = False +CONTEXT = True +STREAMING = True +MODERATIONS = False +ENDPOINT = 'https://api.openai.com' +MODELS = utils.GPT_4_32K + +async def get_key() -> str: + return await utils.random_secret_for('closed432') + +async def chat_completion(**kwargs): + payload = kwargs + key = await get_key() + + return { + 'method': 'POST', + 'url': f'{ENDPOINT}/v1/chat/completions', + 'payload': payload, + 'headers': { + 'Authorization': f'Bearer {key}' + }, + 'provider_auth': f'closed432>{key}' + } + +async def organify(request: dict) -> dict: + key = await get_key() + + request['url'] = ENDPOINT + request['path'] + request['headers']['Authorization'] = f'Bearer {key}' + request['provider_auth'] = f'closed432>{key}' + + return request diff --git a/api/providers/helpers/utils.py b/api/providers/helpers/utils.py new file mode 100644 index 0000000..35c9951 --- /dev/null +++ b/api/providers/helpers/utils.py @@ -0,0 +1,37 @@ +from db import providerkeys + +GPT_3 = [ + 'gpt-3.5-turbo', + 'gpt-3.5-turbo-16k', + 'gpt-3.5-turbo-0613', + 'gpt-3.5-turbo-0301', + 'gpt-3.5-turbo-16k-0613', +] + +GPT_4 = GPT_3 + [ + 'gpt-4', + 'gpt-4-0314', + 'gpt-4-0613', +] + +GPT_4_32K = GPT_4 + [ + 'gpt-4-32k', + 'gpt-4-32k-0314', + 'gpt-4-32k-0613', +] + +async def conversation_to_prompt(conversation: list) -> str: + text = '' + + for message in conversation: + text += f'<|{message["role"]}|>: {message["content"]}\n' + + text += '<|assistant|>:' + + return text + +async def random_secret_for(name: str) -> str: + try: + return await providerkeys.manager.get_key(name) + except ValueError: + raise ValueError(f'Keys missing for "{name}" ')