We're FULLY open source on GitHub now!

This commit is contained in:
nsde 2023-10-05 15:06:33 +02:00
parent 6ef8441681
commit 21331874db
7 changed files with 204 additions and 3 deletions

3
.gitignore vendored
View file

@ -22,9 +22,6 @@ last_update.txt
.log .log
*.log.* *.log.*
providers/*
providers/
secret/* secret/*
secret/ secret/
/secret /secret

10
api/providers/__init__.py Normal file
View file

@ -0,0 +1,10 @@
from . import \
closed, \
closed4
# closed432
MODULES = [
closed,
closed4,
# closed432,
]

52
api/providers/__main__.py Normal file
View file

@ -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)

35
api/providers/closed.py Normal file
View file

@ -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

35
api/providers/closed4.py Normal file
View file

@ -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

View file

@ -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

View file

@ -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}" <no_keys>')