From 6f6f15e698ef0b2ae4dcabf0ebcfe373dae36aac Mon Sep 17 00:00:00 2001 From: nsde Date: Sun, 8 Oct 2023 00:28:13 +0200 Subject: [PATCH] pls dont abuse my api thnkx <3 --- api/db/logs.py | 47 +++++++++++++++++----------------- api/db/providerkeys.py | 2 +- api/db/stats.py | 9 ------- api/db/tester.py | 6 ----- api/providers/__init__.py | 10 ++++---- api/providers/azure.py | 2 +- api/providers/helpers/utils.py | 5 +--- api/responder.py | 16 ++++++++---- checks/client.py | 5 +++- playground/notes.txt | 10 ++++++++ 10 files changed, 56 insertions(+), 56 deletions(-) delete mode 100644 api/db/tester.py create mode 100644 playground/notes.txt diff --git a/api/db/logs.py b/api/db/logs.py index 023f3c4..705f1ca 100644 --- a/api/db/logs.py +++ b/api/db/logs.py @@ -1,21 +1,17 @@ import os import time +import asyncio from dotenv import load_dotenv from motor.motor_asyncio import AsyncIOMotorClient -from helpers import network +try: + from helpers import network +except ImportError: + pass load_dotenv() -UA_SIMPLIFY = { - 'Windows NT': 'W', - 'Mozilla/5.0': 'M', - 'Win64; x64': '64', - 'Safari/537.36': 'S', - 'AppleWebKit/537.36 (KHTML, like Gecko)': 'K', -} - ## MONGODB Setup conn = AsyncIOMotorClient(os.environ['MONGO_URI']) @@ -30,18 +26,7 @@ async def replacer(text: str, dict_: dict) -> str: return text async def log_api_request(user: dict, incoming_request, target_url: str): - """Logs the API Request into the database. - No input prompt is logged, however data such as IP & useragent is noted. - This would be useful for security reasons. Other minor data is also collected. - - Args: - user (dict): User dict object - incoming_request (_type_): Request - target_url (str): The URL the api request was targetted to. - - Returns: - _type_: _description_ - """ + """Logs the API Request into the database.""" db = await _get_collection('logs') payload = {} @@ -53,7 +38,6 @@ async def log_api_request(user: dict, incoming_request, target_url: str): model = payload.get('model') ip_address = await network.get_ip(incoming_request) - useragent = await replacer(incoming_request.headers.get('User-Agent', ''), UA_SIMPLIFY) new_log_item = { 'timestamp': time.time(), @@ -62,7 +46,6 @@ async def log_api_request(user: dict, incoming_request, target_url: str): 'user_id': str(user['_id']), 'security': { 'ip': ip_address, - 'useragent': useragent, }, 'details': { 'model': model, @@ -90,5 +73,21 @@ async def delete_by_user_id(user_id: str): db = await _get_collection('logs') return await db.delete_many({'user_id': user_id}) +async def get_logs_time_range(start: int, end: int): + db = await _get_collection('logs') + + entries = [] + async for entry in db.find({'timestamp': {'$gte': start, '$lte': end}}): + entries.append(entry) + + return entries + +async def main(): + # how many requests in last 24 hours? + last_24_hours = time.time() - 86400 + logs = await get_logs_time_range(last_24_hours, time.time()) + + print(f'Number of logs in last 24 hours: {len(logs)}') + if __name__ == '__main__': - pass + asyncio.run(main()) diff --git a/api/db/providerkeys.py b/api/db/providerkeys.py index da3fc19..5365ba7 100644 --- a/api/db/providerkeys.py +++ b/api/db/providerkeys.py @@ -98,7 +98,7 @@ manager = KeyManager() async def main(): keys = await manager.get_possible_keys('closed') - print(len(keys)) + print(keys) if __name__ == '__main__': asyncio.run(main()) diff --git a/api/db/stats.py b/api/db/stats.py index 5f584a3..ebc6895 100644 --- a/api/db/stats.py +++ b/api/db/stats.py @@ -2,8 +2,6 @@ import os import pytz import asyncio import datetime -import json -import time from dotenv import load_dotenv from motor.motor_asyncio import AsyncIOMotorClient @@ -15,13 +13,6 @@ load_dotenv() class StatsManager: """ ### The manager for all statistics tracking - Stats tracked: - - Dates - - IPs - - Target URLs - - Tokens - - Models - - URL Paths """ def __init__(self): diff --git a/api/db/tester.py b/api/db/tester.py deleted file mode 100644 index 3048e37..0000000 --- a/api/db/tester.py +++ /dev/null @@ -1,6 +0,0 @@ -from stats import * -import asyncio - -manager = StatsManager() - -asyncio.run(manager.get_model_usage()) diff --git a/api/providers/__init__.py b/api/providers/__init__.py index b93b017..25ad1eb 100644 --- a/api/providers/__init__.py +++ b/api/providers/__init__.py @@ -1,12 +1,12 @@ from . import \ - azure, \ - closed, \ - closed4 + azure \ + # closed, \ + # closed4 # closed432 MODULES = [ azure, - closed, - closed4, + # closed, + # closed4, # closed432, ] diff --git a/api/providers/azure.py b/api/providers/azure.py index 6dd0219..a25712f 100644 --- a/api/providers/azure.py +++ b/api/providers/azure.py @@ -12,7 +12,7 @@ MODELS = [ 'gpt-4', 'gpt-4-32k' ] -MODELS = [f'{model}-azure' for model in MODELS] +# MODELS = [f'{model}-azure' for model in MODELS] AZURE_API = '2023-07-01-preview' diff --git a/api/providers/helpers/utils.py b/api/providers/helpers/utils.py index 6c21812..2dbb4eb 100644 --- a/api/providers/helpers/utils.py +++ b/api/providers/helpers/utils.py @@ -34,7 +34,4 @@ async def conversation_to_prompt(conversation: list) -> str: 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}" ') + return await providerkeys.manager.get_key(name) diff --git a/api/responder.py b/api/responder.py index d235cc4..7b6f6a8 100644 --- a/api/responder.py +++ b/api/responder.py @@ -2,7 +2,7 @@ import os import json -import logging +import ujson import aiohttp import asyncio import starlette @@ -49,9 +49,7 @@ async def respond( 'Content-Type': 'application/json' } - for i in range(20): - print(i) - # Load balancing: randomly selecting a suitable provider + for i in range(1): try: if is_chat: target_request = await load_balancing.balance_chat_request(payload) @@ -151,13 +149,21 @@ async def respond( async for chunk in response.content.iter_any(): chunk = chunk.decode('utf8').strip() + + if 'azure' in provider_name: + chunk = chunk.strip().replace('data: ', '') + + if not chunk or 'prompt_filter_results' in chunk: + continue + yield chunk + '\n\n' break except Exception as exc: print('[!] exception', exc) - continue + # continue + raise exc else: yield await errors.yield_error(500, 'Sorry, our API seems to have issues connecting to our provider(s).', 'This most likely isn\'t your fault. Please try again later.') diff --git a/checks/client.py b/checks/client.py index 0a22d76..a149377 100644 --- a/checks/client.py +++ b/checks/client.py @@ -215,7 +215,10 @@ async def demo(): else: raise ConnectionError('API Server is not running.') - for func in [test_chat_non_stream_gpt4, test_chat_stream_gpt3]: + for func in [ + # test_chat_non_stream_gpt4, + test_chat_stream_gpt3 + ]: print(f'[*] {func.__name__}') result = await func() print(f'[+] {func.__name__} - {result}') diff --git a/playground/notes.txt b/playground/notes.txt new file mode 100644 index 0000000..7cf266c --- /dev/null +++ b/playground/notes.txt @@ -0,0 +1,10 @@ +--- EXPECTED --- + +data: {"id":"custom-chatcmpl-nUSiapqELukaPT7vEnGcXkbvrS1fR","object":"chat.completion.chunk","created":1696716717,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]} + +data: {"id":"custom-chatcmpl-nUSiapqELukaPT7vEnGcXkbvrS1fR","object":"chat.completion.chunk","created":1696716717,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"content":"123"},"finish_reason":null}]} + +data: {"id":"custom-chatcmpl-nUSiapqELukaPT7vEnGcXkbvrS1fR","object":"chat.completion.chunk","created":1696716717,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]} + +data: [DONE] +