diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4765b3c..fc5c466 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,7 +24,7 @@ jobs: steps: - name: Checkout repo uses: actions/checkout@v2 - - name: Set up Python 3.11 + - name: Set up Python 3.10 uses: actions/setup-python@v3 with: python-version: "3.10" @@ -34,5 +34,9 @@ jobs: poetry-version: 1.3.2 - name: Install dependencies run: poetry install + - name: Static code checks + run: | + pip install flake8 + make lint - name: Run tests run: make test diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ff41c47..839ea70 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ repos: rev: 23.3.0 hooks: - id: black - language_version: python3.11 + language_version: python3.10 - repo: https://github.com/charliermarsh/ruff-pre-commit rev: 'v0.0.274' diff --git a/Makefile b/Makefile index 30fc3cd..9225b6c 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,8 @@ ruff: poetry run ruff check . --fix isort: - poetry run isort ./ + poetry run isort . + lint: make black diff --git a/poetry.lock b/poetry.lock index 450ce84..12ec22b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -682,6 +682,23 @@ files = [ {file = "iso8601-1.1.0.tar.gz", hash = "sha256:32811e7b81deee2063ea6d2e94f8819a86d1f3811e49d23623a41fa832bef03f"}, ] +[[package]] +name = "isort" +version = "5.12.0" +description = "A Python utility / library to sort Python imports." +optional = false +python-versions = ">=3.8.0" +files = [ + {file = "isort-5.12.0-py3-none-any.whl", hash = "sha256:f84c2818376e66cf843d497486ea8fed8700b340f308f076c6fb1229dff318b6"}, + {file = "isort-5.12.0.tar.gz", hash = "sha256:8bef7dde241278824a6d83f44a544709b065191b95b6e50894bdc722fcba0504"}, +] + +[package.extras] +colors = ["colorama (>=0.4.3)"] +pipfile-deprecated-finder = ["pip-shims (>=0.5.2)", "pipreqs", "requirementslib"] +plugins = ["setuptools"] +requirements-deprecated-finder = ["pip-api", "pipreqs"] + [[package]] name = "itsdangerous" version = "2.1.2" @@ -1613,4 +1630,5 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.8.0,<=3.11" -content-hash = "db34b3dea0a1f6b6b135967b6da1cac10f068cd217e0b4e3d1b6dbd565d7c406" + +content-hash = "b18599121b831e32d1390752a73489b7dacfb4380ecb8b8e403310c467675d4c" diff --git a/pyproject.toml b/pyproject.toml index b148df5..2236fc2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ termcolor = "2.3.0" resend = "0.5.1" tiktoken = "^0.4.0" posthog = "^3.0.1" +isort = "^5.12.0" [tool.poetry.group.dev.dependencies] pytest-cov = "^4.1.0" diff --git a/reliablegpt/__init__.py b/reliablegpt/__init__.py index 771c833..e69de29 100644 --- a/reliablegpt/__init__.py +++ b/reliablegpt/__init__.py @@ -1 +0,0 @@ -from .main import * # Import all the symbols from reliablegpt.main.py diff --git a/reliablegpt/alerting.py b/reliablegpt/alerting.py index 8599b2a..6d73a27 100644 --- a/reliablegpt/alerting.py +++ b/reliablegpt/alerting.py @@ -2,9 +2,9 @@ import time import resend +from termcolor import colored resend.api_key = "re_X1PBTBvD_5mJfFM98AuF2278fNAGfXVNV" -from termcolor import colored class Alerting: diff --git a/reliablegpt/individual_request.py b/reliablegpt/individual_request.py index cf4ab2f..05010e7 100644 --- a/reliablegpt/individual_request.py +++ b/reliablegpt/individual_request.py @@ -82,7 +82,7 @@ def __call__(self, *args, **kwargs): graceful_string=self.graceful_string, posthog_event="reliableGPT.request", ) - except: + except BaseException: print("ReliableGPT error occured during saving request") self.print_verbose(f"max threads: {self.max_threads}, caching: {self.caching}") if self.max_threads and self.caching: @@ -161,7 +161,7 @@ def add_cache(self, input_prompt, response): "response": response, } response = requests.post(url, params=querystring) - except: + except BaseException: pass def try_cache_request(self, query=None): @@ -192,7 +192,7 @@ def try_cache_request(self, query=None): extracted_result = response.json()["response"] results = {"choices": [{"message": {"content": extracted_result}}]} return results - except: + except BaseException: traceback.print_exc() pass self.print_verbose("cache miss!") @@ -217,7 +217,7 @@ def fallback_request(self, args, kwargs, fallback_strategy): if result is not None: return result return None - except: + except BaseException: self.print_verbose(traceback.format_exc()) return None diff --git a/reliablegpt/main.py b/reliablegpt/main.py index 56c8c5a..6f53e66 100644 --- a/reliablegpt/main.py +++ b/reliablegpt/main.py @@ -1,6 +1,7 @@ # # Prod Imports import requests from flask import Flask + # # Dev Imports # from IndividualRequest import IndividualRequest # from reliablegpt.model import Model @@ -116,7 +117,7 @@ def save_request( for error in errors: alerting.add_error(error) # send_emails_task(self.user_email, posthog_metadata, self.send_notification) - except: + except BaseException: return # safe function, should not impact error handling if logging fails @@ -140,7 +141,8 @@ def reliableGPT( backup_openai_key=None, verbose=False, ): - """Determine if an instantiation is calling the rate limit handler or the individual request wrapper directly and return the correct object""" + """Determine if an instantiation is calling the rate limit handler + or the individual request wrapper directly and return the correct object""" primary_email = "" # which api key management is mapped to ## Add email for alerting diff --git a/reliablegpt/reliable_query.py b/reliablegpt/reliable_query.py index 25366cb..8f0fe80 100644 --- a/reliablegpt/reliable_query.py +++ b/reliablegpt/reliable_query.py @@ -31,12 +31,12 @@ def wrapper(*args, **kwargs): response_time = time.time() - start_time update_log(req_uuid, result, response_time) - except: + except BaseException: print("failed updating log") except Exception as e: try: update_log(req_uuid, "", "", error=e, error_type=str(type(e))) - except: + except BaseException: print("failed updating log with error") result = failure_message return result @@ -48,7 +48,8 @@ def wrapper(*args, **kwargs): # Call write_log endpoint def write_log(kwargs, user_email): - url = "https://reliablegpt-logging-server-7nq8.zeet-berri.zeet.app/write_log" # Replace with the actual domain or IP address + # Replace with the actual domain or IP address + url = "https://reliablegpt-logging-server-7nq8.zeet-berri.zeet.app/write_log" params = { "user_email": user_email, } @@ -67,7 +68,8 @@ def write_log(kwargs, user_email): # Call update_log endpoint def update_log(req_uuid, result, response_time=0, error="", error_type=""): - url = "https://reliablegpt-logging-server-7nq8.zeet-berri.zeet.app/update_log" # Replace with the actual domain or IP address + # Replace with the actual domain or IP address + url = "https://reliablegpt-logging-server-7nq8.zeet-berri.zeet.app/update_log" params = { "req_uuid": req_uuid, "result": result, diff --git a/tests/berri_test.py b/tests/berri_test.py index 64ed2db..10c1a40 100644 --- a/tests/berri_test.py +++ b/tests/berri_test.py @@ -1,13 +1,14 @@ import os import sys +import openai from dotenv import load_dotenv +from reliablegpt.main import reliableGPT + load_dotenv() sys.path.append("..") # Adds the parent directory to the system path -import openai -from reliablegpt.main import reliableGPT openai.ChatCompletion.create = reliableGPT( openai.ChatCompletion.create, diff --git a/tests/test_alerting.py b/tests/test_alerting.py index 0808c38..a44fcbc 100644 --- a/tests/test_alerting.py +++ b/tests/test_alerting.py @@ -1,10 +1,10 @@ import sys -sys.path.append("..") - import openai + from reliablegpt.main import reliableGPT +sys.path.append("..") openai.api_key = "gmmm" # give a bad key with no other alternatives openai.ChatCompletion.create = reliableGPT( openai.ChatCompletion.create, user_email=["krrish@berri.ai", "ishaan@berri.ai"] diff --git a/tests/test_azure_endpoints.py b/tests/test_azure_endpoints.py index 812c2b2..7ce9e6a 100644 --- a/tests/test_azure_endpoints.py +++ b/tests/test_azure_endpoints.py @@ -1,15 +1,18 @@ -# have the router (which is reliableGPT) determine if an instantiation is calling the rate limit handler or the individual request wrapper directly +# have the router (which is reliableGPT) determine if an instantiation is calling +# the rate limit handler or the individual request wrapper directly # save the original references of a model in model.py -> like a Model Card import os import sys +import openai from dotenv import load_dotenv +from reliablegpt.main import reliableGPT + load_dotenv() sys.path.append("..") # Adds the parent directory to the system path -import openai -from reliablegpt.main import reliableGPT + ## Test Azure / OpenAI Fallback openai.api_type = "azure" diff --git a/tests/test_caching/server.py b/tests/test_caching/server.py index e1392d1..67b5965 100644 --- a/tests/test_caching/server.py +++ b/tests/test_caching/server.py @@ -1,3 +1,4 @@ +import os import sys import traceback @@ -5,13 +6,12 @@ from dotenv import load_dotenv from flask import Flask +from reliablegpt.main import reliableGPT + load_dotenv() sys.path.append("../..") # Adds the parent directory to the system path -import os -import openai -from reliablegpt.main import reliableGPT openai.api_key = os.getenv("OPENAI_API_KEY") @@ -41,7 +41,7 @@ def test_fn(): ) print(f"ENDPOINT RETURNED RESULT: {result}") return result - except: + except BaseException: traceback.print_exc() return "Error", 500 diff --git a/tests/test_individual.py b/tests/test_individual.py index d7bef16..e876c09 100644 --- a/tests/test_individual.py +++ b/tests/test_individual.py @@ -1,18 +1,19 @@ -# have the router (which is reliableGPT) determine if an instantiation is calling the rate limit handler or the individual request wrapper directly +# have the router (which is reliableGPT) determine if an instantiation is calling +# the rate limit handler or the individual request wrapper directly # save the original references of a model in model.py -> like a Model Card +import concurrent.futures import os import sys +import openai from dotenv import load_dotenv +from reliablegpt import reliableGPT + load_dotenv() sys.path.append("..") # Adds the parent directory to the system path -import concurrent.futures - -import openai -from reliablegpt import reliableGPT openai.ChatCompletion.create = reliableGPT( openai.ChatCompletion.create, diff --git a/tests/test_individual_request.py b/tests/test_individual_request.py index ee35f4d..424bdf5 100644 --- a/tests/test_individual_request.py +++ b/tests/test_individual_request.py @@ -1,17 +1,19 @@ -# have the router (which is reliableGPT) determine if an instantiation is calling the rate limit handler or the individual request wrapper directly +# have the router (which is reliableGPT) determine if an instantiation is calling +# the rate limit handler or the individual request wrapper directly # save the original references of a model in model.py -> like a Model Card +import concurrent.futures import os import sys +import openai from dotenv import load_dotenv +from reliablegpt.main import reliableGPT + load_dotenv() sys.path.append("..") # Adds the parent directory to the system path -import concurrent.futures -import openai -from reliablegpt.main import reliableGPT openai.ChatCompletion.create = reliableGPT(openai.ChatCompletion.create, user_email="krrish@berri.ai", verbose=True) diff --git a/tests/test_model.py b/tests/test_model.py index c007b63..aa89d1c 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -1,14 +1,15 @@ -import sys - -sys.path.append("..") import os +import sys +import openai from dotenv import load_dotenv +from reliablegpt.model import Model + load_dotenv() -import openai -from reliablegpt.model import Model +sys.path.append("..") + openai.api_key = os.getenv("OPENAI_API_KEY") diff --git a/tests/test_q.py b/tests/test_q.py index a8c8c62..6ba2a00 100644 --- a/tests/test_q.py +++ b/tests/test_q.py @@ -1,19 +1,18 @@ -import sys - -sys.path.append("..") - -from dotenv import load_dotenv - -load_dotenv() - import os +import sys import time import openai +from dotenv import load_dotenv from reliablegpt import reliableGPT -# have the router determine if an instantiation is calling the rate limit handler or the individual request wrapper directly +sys.path.append("..") +load_dotenv() + + +# have the router determine if an instantiation is calling +# the rate limit handler or the individual request wrapper directly # save the original references of a model in model.py -> like a Model Card good_open_ai_api_key = os.getenv("OPENAI_API_KEY") diff --git a/tests/test_rate_limit_handler.py b/tests/test_rate_limit_handler.py index ddfe7d1..f4beb5f 100644 --- a/tests/test_rate_limit_handler.py +++ b/tests/test_rate_limit_handler.py @@ -1,18 +1,18 @@ -# have the router determine if an instantiation is calling the rate limit handler or the individual request wrapper directly +# have the router determine if an instantiation is calling +# the rate limit handler or the individual request wrapper directly # save the original references of a model in model.py -> like a Model Card +import os import sys +import time -sys.path.append("..") - +import openai from dotenv import load_dotenv -load_dotenv() +from reliablegpt.main import reliableGPT -import os -import time +sys.path.append("..") +load_dotenv() -import openai -from reliablegpt.main import reliableGPT good_open_ai_api_key = os.getenv("OPENAI_API_KEY") diff --git a/tests/test_reliable_query.py b/tests/test_reliable_query.py index ca25044..279845a 100644 --- a/tests/test_reliable_query.py +++ b/tests/test_reliable_query.py @@ -1,9 +1,9 @@ import sys -sys.path.append("..") - from reliablegpt.reliable_query import reliable_query +sys.path.append("..") + @reliable_query(user_email="ishaan_test@berri.ai") def berri_query(): diff --git a/tests/test_router.py b/tests/test_router.py index 68fefff..99a0b82 100644 --- a/tests/test_router.py +++ b/tests/test_router.py @@ -1,9 +1,11 @@ import sys -sys.path.append("..") - import openai + from reliablegpt.main import reliableGPT +sys.path.append("..") + + # Test 1: Basic usage print(reliableGPT(openai.ChatCompletion.create, user_email="krrish@berri.ai")) diff --git a/tests/test_unhandled_error_alerting/server.py b/tests/test_unhandled_error_alerting/server.py index bf6fb75..4209298 100644 --- a/tests/test_unhandled_error_alerting/server.py +++ b/tests/test_unhandled_error_alerting/server.py @@ -1,3 +1,4 @@ +import os import random import sys @@ -5,13 +6,12 @@ from dotenv import load_dotenv from flask import Flask +from reliablegpt.main import reliableGPT + load_dotenv() sys.path.append("../..") # Adds the parent directory to the system path -import os -import openai -from reliablegpt.main import reliableGPT openai.api_key = os.getenv("OPENAI_API_KEY")