From 437d0a93aae18a1e45ebbb7355d68ea7b91598d8 Mon Sep 17 00:00:00 2001 From: Mehrin Kiani Date: Wed, 7 Feb 2024 17:57:28 -0500 Subject: [PATCH] Added Chroma as optional dependency --- python-sdk/Makefile | 2 +- python-sdk/poetry.lock | 133 +++++++++++----------- python-sdk/pyproject.toml | 5 +- python-sdk/rebuff/detect_pi_vectorbase.py | 104 +++++++++-------- python-sdk/rebuff/sdk.py | 22 ++-- python-sdk/tests/test_sdk.py | 6 +- 6 files changed, 149 insertions(+), 123 deletions(-) diff --git a/python-sdk/Makefile b/python-sdk/Makefile index a93ed11..a95084e 100644 --- a/python-sdk/Makefile +++ b/python-sdk/Makefile @@ -1,7 +1,7 @@ VERSION ?= $(shell dunamai from git --style pep440 --format "{base}.dev{distance}+{commit}") install-dev: - poetry install --with dev + poetry install --with dev --extras "chromadb" install: poetry install diff --git a/python-sdk/poetry.lock b/python-sdk/poetry.lock index 4ae1367..03acbbd 100644 --- a/python-sdk/poetry.lock +++ b/python-sdk/poetry.lock @@ -150,7 +150,7 @@ trio = ["trio (>=0.23)"] name = "asgiref" version = "3.7.2" description = "ASGI specs, helper code, and adapters" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "asgiref-3.7.2-py3-none-any.whl", hash = "sha256:89b2ef2247e3b562a16eef663bc0e2e703ec6468e2fa8a5cd61cd449786d4f6e"}, @@ -197,7 +197,7 @@ tests-no-zope = ["attrs[tests-mypy]", "cloudpickle", "hypothesis", "pympler", "p name = "backoff" version = "2.2.1" description = "Function decoration for backoff and retry" -optional = false +optional = true python-versions = ">=3.7,<4.0" files = [ {file = "backoff-2.2.1-py3-none-any.whl", hash = "sha256:63579f9a0628e06278f7e47b7d7d5b6ce20dc65c5e96a6f3ca99a6adca0396e8"}, @@ -232,7 +232,7 @@ yaml = ["PyYAML"] name = "bcrypt" version = "4.1.2" description = "Modern password hashing for your software and your servers" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "bcrypt-4.1.2-cp37-abi3-macosx_10_12_universal2.whl", hash = "sha256:ac621c093edb28200728a9cca214d7e838529e557027ef0581685909acd28b5e"}, @@ -318,7 +318,7 @@ uvloop = ["uvloop (>=0.15.2)"] name = "build" version = "1.0.3" description = "A simple, correct Python build frontend" -optional = false +optional = true python-versions = ">= 3.7" files = [ {file = "build-1.0.3-py3-none-any.whl", hash = "sha256:589bf99a67df7c9cf07ec0ac0e5e2ea5d4b37ac63301c4986d1acb126aa83f8f"}, @@ -342,7 +342,7 @@ virtualenv = ["virtualenv (>=20.0.35)"] name = "cachetools" version = "5.3.2" description = "Extensible memoizing collections and decorators" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "cachetools-5.3.2-py3-none-any.whl", hash = "sha256:861f35a13a451f94e301ce2bec7cac63e881232ccce7ed67fab9b5df4d3beaa1"}, @@ -463,7 +463,7 @@ files = [ name = "chroma-hnswlib" version = "0.7.3" description = "Chromas fork of hnswlib" -optional = false +optional = true python-versions = "*" files = [ {file = "chroma-hnswlib-0.7.3.tar.gz", hash = "sha256:b6137bedde49fffda6af93b0297fe00429fc61e5a072b1ed9377f909ed95a932"}, @@ -500,7 +500,7 @@ numpy = "*" name = "chromadb" version = "0.4.22" description = "Chroma." -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "chromadb-0.4.22-py3-none-any.whl", hash = "sha256:ad210b27b4cda2f09d15adc9c83c81bfa66b69f39648a27b637306e40de0680d"}, @@ -566,7 +566,7 @@ files = [ name = "coloredlogs" version = "15.0.1" description = "Colored terminal output for Python's logging module" -optional = false +optional = true python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" files = [ {file = "coloredlogs-15.0.1-py2.py3-none-any.whl", hash = "sha256:612ee75c546f53e92e70049c9dbfcc18c935a2b9a53b66085ce9ef6a6e5c0934"}, @@ -598,7 +598,7 @@ typing-inspect = ">=0.4.0,<1" name = "deprecated" version = "1.2.14" description = "Python @deprecated decorator to deprecate old python classes, functions or methods." -optional = false +optional = true python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" files = [ {file = "Deprecated-1.2.14-py2.py3-none-any.whl", hash = "sha256:6fac8b097794a90302bdbb17b9b815e732d3c4720583ff1b198499d78470466c"}, @@ -654,7 +654,7 @@ test = ["pytest (>=6)"] name = "fastapi" version = "0.109.0" description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "fastapi-0.109.0-py3-none-any.whl", hash = "sha256:8c77515984cd8e8cfeb58364f8cc7a28f0692088475e2614f7bf03275eba9093"}, @@ -673,7 +673,7 @@ all = ["email-validator (>=2.0.0)", "httpx (>=0.23.0)", "itsdangerous (>=1.1.0)" name = "filelock" version = "3.13.1" description = "A platform independent file lock." -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "filelock-3.13.1-py3-none-any.whl", hash = "sha256:57dbda9b35157b05fb3e58ee91448612eb674172fab98ee235ccb0b5bee19a1c"}, @@ -689,7 +689,7 @@ typing = ["typing-extensions (>=4.8)"] name = "flatbuffers" version = "23.5.26" description = "The FlatBuffers serialization format for Python" -optional = false +optional = true python-versions = "*" files = [ {file = "flatbuffers-23.5.26-py2.py3-none-any.whl", hash = "sha256:c0ff356da363087b915fde4b8b45bdda73432fc17cddb3c8157472eab1422ad1"}, @@ -786,7 +786,7 @@ files = [ name = "fsspec" version = "2023.12.2" description = "File-system specification" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "fsspec-2023.12.2-py3-none-any.whl", hash = "sha256:d800d87f72189a745fa3d6b033b9dc4a34ad069f60ca60b943a63599f5501960"}, @@ -852,7 +852,7 @@ test = ["black", "coverage[toml]", "ddt (>=1.1.1,!=1.4.3)", "mock", "mypy", "pre name = "google-auth" version = "2.27.0" description = "Google Authentication Library" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "google-auth-2.27.0.tar.gz", hash = "sha256:e863a56ccc2d8efa83df7a80272601e43487fa9a728a376205c86c26aaefa821"}, @@ -875,7 +875,7 @@ requests = ["requests (>=2.20.0,<3.0.0.dev0)"] name = "googleapis-common-protos" version = "1.62.0" description = "Common protobufs used in Google APIs" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "googleapis-common-protos-1.62.0.tar.gz", hash = "sha256:83f0ece9f94e5672cced82f592d2a5edf527a96ed1794f0bab36d5735c996277"}, @@ -892,7 +892,7 @@ grpc = ["grpcio (>=1.44.0,<2.0.0.dev0)"] name = "graphlib-backport" version = "1.0.3" description = "Backport of the Python 3.9 graphlib module for Python 3.6+" -optional = false +optional = true python-versions = ">=3.6,<4.0" files = [ {file = "graphlib_backport-1.0.3-py3-none-any.whl", hash = "sha256:24246967b9e7e6a91550bc770e6169585d35aa32790258579a8a3899a8c18fde"}, @@ -974,7 +974,7 @@ test = ["objgraph", "psutil"] name = "grpcio" version = "1.60.1" description = "HTTP/2-based RPC framework" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "grpcio-1.60.1-cp310-cp310-linux_armv7l.whl", hash = "sha256:14e8f2c84c0832773fb3958240c69def72357bc11392571f87b2d7b91e0bb092"}, @@ -1072,7 +1072,7 @@ trio = ["trio (>=0.22.0,<0.23.0)"] name = "httptools" version = "0.6.1" description = "A collection of framework independent HTTP protocol utils." -optional = false +optional = true python-versions = ">=3.8.0" files = [ {file = "httptools-0.6.1-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:d2f6c3c4cb1948d912538217838f6e9960bc4a521d7f9b323b3da579cd14532f"}, @@ -1144,7 +1144,7 @@ socks = ["socksio (==1.*)"] name = "huggingface-hub" version = "0.20.3" description = "Client library to download and publish models, datasets and other repos on the huggingface.co hub" -optional = false +optional = true python-versions = ">=3.8.0" files = [ {file = "huggingface_hub-0.20.3-py3-none-any.whl", hash = "sha256:d988ae4f00d3e307b0c80c6a05ca6dbb7edba8bba3079f74cda7d9c2e562a7b6"}, @@ -1176,7 +1176,7 @@ typing = ["types-PyYAML", "types-requests", "types-simplejson", "types-toml", "t name = "humanfriendly" version = "10.0" description = "Human friendly output for text interfaces using Python" -optional = false +optional = true python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" files = [ {file = "humanfriendly-10.0-py2.py3-none-any.whl", hash = "sha256:1697e1a8a8f550fd43c2865cd84542fc175a61dcb779b6fee18cf6b6ccba1477"}, @@ -1201,7 +1201,7 @@ files = [ name = "importlib-metadata" version = "6.11.0" description = "Read metadata from Python packages" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "importlib_metadata-6.11.0-py3-none-any.whl", hash = "sha256:f0afba6205ad8f8947c7d338b5342d5db2afbfd82f9cbef7879a9539cc12eb9b"}, @@ -1220,7 +1220,7 @@ testing = ["flufl.flake8", "importlib-resources (>=1.3)", "packaging", "pyfakefs name = "importlib-resources" version = "6.1.1" description = "Read resources from Python packages" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "importlib_resources-6.1.1-py3-none-any.whl", hash = "sha256:e8bf90d8213b486f428c9c39714b920041cb02c184686a3dee24905aaa8105d6"}, @@ -1274,7 +1274,7 @@ files = [ name = "kubernetes" version = "29.0.0" description = "Kubernetes python client" -optional = false +optional = true python-versions = ">=3.6" files = [ {file = "kubernetes-29.0.0-py2.py3-none-any.whl", hash = "sha256:ab8cb0e0576ccdfb71886366efb102c6a20f268d817be065ce7f9909c631e43e"}, @@ -1477,7 +1477,7 @@ files = [ name = "mmh3" version = "4.1.0" description = "Python extension for MurmurHash (MurmurHash3), a set of fast and robust hash functions." -optional = false +optional = true python-versions = "*" files = [ {file = "mmh3-4.1.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:be5ac76a8b0cd8095784e51e4c1c9c318c19edcd1709a06eb14979c8d850c31a"}, @@ -1569,7 +1569,7 @@ test = ["mypy (>=1.0)", "pytest (>=7.0.0)"] name = "monotonic" version = "1.6" description = "An implementation of time.monotonic() for Python 2 & < 3.3" -optional = false +optional = true python-versions = "*" files = [ {file = "monotonic-1.6-py2.py3-none-any.whl", hash = "sha256:68687e19a14f11f26d140dd5c86f3dba4bf5df58003000ed467e0e2a69bca96c"}, @@ -1580,7 +1580,7 @@ files = [ name = "mpmath" version = "1.3.0" description = "Python library for arbitrary-precision floating-point arithmetic" -optional = false +optional = true python-versions = "*" files = [ {file = "mpmath-1.3.0-py3-none-any.whl", hash = "sha256:a0b2b9fe80bbcd81a6647ff13108738cfb482d481d826cc0e02f5b35e5c88d2c"}, @@ -1775,7 +1775,7 @@ files = [ name = "oauthlib" version = "3.2.2" description = "A generic, spec-compliant, thorough implementation of the OAuth request-signing logic" -optional = false +optional = true python-versions = ">=3.6" files = [ {file = "oauthlib-3.2.2-py3-none-any.whl", hash = "sha256:8139f29aac13e25d502680e9e19963e83f16838d48a0d71c287fe40e7067fbca"}, @@ -1791,7 +1791,7 @@ signedtoken = ["cryptography (>=3.0.0)", "pyjwt (>=2.0.0,<3)"] name = "onnxruntime" version = "1.17.0" description = "ONNX Runtime is a runtime accelerator for Machine Learning models" -optional = false +optional = true python-versions = "*" files = [ {file = "onnxruntime-1.17.0-cp310-cp310-macosx_11_0_universal2.whl", hash = "sha256:d2b22a25a94109cc983443116da8d9805ced0256eb215c5e6bc6dcbabefeab96"}, @@ -1856,7 +1856,7 @@ datalib = ["numpy (>=1)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)"] name = "opentelemetry-api" version = "1.22.0" description = "OpenTelemetry Python API" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_api-1.22.0-py3-none-any.whl", hash = "sha256:43621514301a7e9f5d06dd8013a1b450f30c2e9372b8e30aaeb4562abf2ce034"}, @@ -1871,7 +1871,7 @@ importlib-metadata = ">=6.0,<7.0" name = "opentelemetry-exporter-otlp-proto-common" version = "1.22.0" description = "OpenTelemetry Protobuf encoding" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_exporter_otlp_proto_common-1.22.0-py3-none-any.whl", hash = "sha256:3f2538bec5312587f8676c332b3747f54c89fe6364803a807e217af4603201fa"}, @@ -1886,7 +1886,7 @@ opentelemetry-proto = "1.22.0" name = "opentelemetry-exporter-otlp-proto-grpc" version = "1.22.0" description = "OpenTelemetry Collector Protobuf over gRPC Exporter" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_exporter_otlp_proto_grpc-1.22.0-py3-none-any.whl", hash = "sha256:b5bcadc129272004316a455e9081216d3380c1fc2231a928ea6a70aa90e173fb"}, @@ -1910,7 +1910,7 @@ test = ["pytest-grpc"] name = "opentelemetry-instrumentation" version = "0.43b0" description = "Instrumentation Tools & Auto Instrumentation for OpenTelemetry Python" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_instrumentation-0.43b0-py3-none-any.whl", hash = "sha256:0ff1334d7e359e27640e9d420024efeb73eacae464309c2e14ede7ba6c93967e"}, @@ -1926,7 +1926,7 @@ wrapt = ">=1.0.0,<2.0.0" name = "opentelemetry-instrumentation-asgi" version = "0.43b0" description = "ASGI instrumentation for OpenTelemetry" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_instrumentation_asgi-0.43b0-py3-none-any.whl", hash = "sha256:1f593829fa039e9367820736fb063e92acd15c25b53d7bcb5d319971b8e93fd7"}, @@ -1948,7 +1948,7 @@ test = ["opentelemetry-instrumentation-asgi[instruments]", "opentelemetry-test-u name = "opentelemetry-instrumentation-fastapi" version = "0.43b0" description = "OpenTelemetry FastAPI Instrumentation" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_instrumentation_fastapi-0.43b0-py3-none-any.whl", hash = "sha256:b79c044df68a52e07b35fa12a424e7cc0dd27ff0a171c5fdcc41dea9de8fc938"}, @@ -1970,7 +1970,7 @@ test = ["httpx (>=0.22,<1.0)", "opentelemetry-instrumentation-fastapi[instrument name = "opentelemetry-proto" version = "1.22.0" description = "OpenTelemetry Python Proto" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_proto-1.22.0-py3-none-any.whl", hash = "sha256:ce7188d22c75b6d0fe53e7fb58501613d0feade5139538e79dedd9420610fa0c"}, @@ -1984,7 +1984,7 @@ protobuf = ">=3.19,<5.0" name = "opentelemetry-sdk" version = "1.22.0" description = "OpenTelemetry Python SDK" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_sdk-1.22.0-py3-none-any.whl", hash = "sha256:a730555713d7c8931657612a88a141e3a4fe6eb5523d9e2d5a8b1e673d76efa6"}, @@ -2000,7 +2000,7 @@ typing-extensions = ">=3.7.4" name = "opentelemetry-semantic-conventions" version = "0.43b0" description = "OpenTelemetry Semantic Conventions" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_semantic_conventions-0.43b0-py3-none-any.whl", hash = "sha256:291284d7c1bf15fdaddf309b3bd6d3b7ce12a253cec6d27144439819a15d8445"}, @@ -2011,7 +2011,7 @@ files = [ name = "opentelemetry-util-http" version = "0.43b0" description = "Web util for OpenTelemetry" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "opentelemetry_util_http-0.43b0-py3-none-any.whl", hash = "sha256:f25a820784b030f6cb86b3d76e5676c769b75ed3f55a210bcdae0a5e175ebadb"}, @@ -2022,7 +2022,7 @@ files = [ name = "overrides" version = "7.7.0" description = "A decorator to automatically detect mismatch when overriding a method." -optional = false +optional = true python-versions = ">=3.6" files = [ {file = "overrides-7.7.0-py3-none-any.whl", hash = "sha256:c7ed9d062f78b8e4c1a7b70bd8796b35ead4d9f510227ef9c5dc7626c60d7e49"}, @@ -2116,7 +2116,7 @@ testing = ["pytest", "pytest-benchmark"] name = "posthog" version = "3.3.4" description = "Integrate PostHog into any python application." -optional = false +optional = true python-versions = "*" files = [ {file = "posthog-3.3.4-py2.py3-none-any.whl", hash = "sha256:2fec5112c6df1d6a214a899e409659ed354511236537e861f1556a0c88e3fd26"}, @@ -2139,7 +2139,7 @@ test = ["coverage", "flake8", "freezegun (==0.3.15)", "mock (>=2.0.0)", "pylint" name = "protobuf" version = "4.25.2" description = "" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "protobuf-4.25.2-cp310-abi3-win32.whl", hash = "sha256:b50c949608682b12efb0b2717f53256f03636af5f60ac0c1d900df6213910fd6"}, @@ -2159,7 +2159,7 @@ files = [ name = "pulsar-client" version = "3.4.0" description = "Apache Pulsar Python client library" -optional = false +optional = true python-versions = "*" files = [ {file = "pulsar_client-3.4.0-cp310-cp310-macosx_10_15_universal2.whl", hash = "sha256:ebf99db5244ff69479283b25621b070492acc4bb643d162d86b90387cb6fdb2a"}, @@ -2206,7 +2206,7 @@ functions = ["apache-bookkeeper-client (>=4.16.1)", "grpcio (>=1.60.0)", "promet name = "pyasn1" version = "0.5.1" description = "Pure-Python implementation of ASN.1 types and DER/BER/CER codecs (X.208)" -optional = false +optional = true python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,>=2.7" files = [ {file = "pyasn1-0.5.1-py2.py3-none-any.whl", hash = "sha256:4439847c58d40b1d0a573d07e3856e95333f1976294494c325775aeca506eb58"}, @@ -2217,7 +2217,7 @@ files = [ name = "pyasn1-modules" version = "0.3.0" description = "A collection of ASN.1-based protocols modules" -optional = false +optional = true python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,>=2.7" files = [ {file = "pyasn1_modules-0.3.0-py2.py3-none-any.whl", hash = "sha256:d3ccd6ed470d9ffbc716be08bd90efbd44d0734bc9303818f7336070984a162d"}, @@ -2382,7 +2382,7 @@ windows-terminal = ["colorama (>=0.4.6)"] name = "pypika" version = "0.48.9" description = "A SQL query builder API for Python" -optional = false +optional = true python-versions = "*" files = [ {file = "PyPika-0.48.9.tar.gz", hash = "sha256:838836a61747e7c8380cd1b7ff638694b7a7335345d0f559b04b2cd832ad5378"}, @@ -2392,7 +2392,7 @@ files = [ name = "pyproject-hooks" version = "1.0.0" description = "Wrappers to call pyproject.toml-based build backend hooks." -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "pyproject_hooks-1.0.0-py3-none-any.whl", hash = "sha256:283c11acd6b928d2f6a7c73fa0d01cb2bdc5f07c57a2eeb6e83d5e56b97976f8"}, @@ -2406,7 +2406,7 @@ tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} name = "pyreadline3" version = "3.4.1" description = "A python implementation of GNU readline." -optional = false +optional = true python-versions = "*" files = [ {file = "pyreadline3-3.4.1-py3-none-any.whl", hash = "sha256:b0efb6516fd4fb07b45949053826a62fa4cb353db5be2bbb4a7aa1fdd1e345fb"}, @@ -2454,7 +2454,7 @@ pytest = ">=7" name = "python-dateutil" version = "2.8.2" description = "Extensions to the standard Python datetime module" -optional = false +optional = true python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" files = [ {file = "python-dateutil-2.8.2.tar.gz", hash = "sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86"}, @@ -2468,7 +2468,7 @@ six = ">=1.5" name = "python-dotenv" version = "1.0.1" description = "Read key-value pairs from a .env file and set them as environment variables" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "python-dotenv-1.0.1.tar.gz", hash = "sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca"}, @@ -2654,7 +2654,7 @@ use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] name = "requests-oauthlib" version = "1.3.1" description = "OAuthlib authentication support for Requests." -optional = false +optional = true python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" files = [ {file = "requests-oauthlib-1.3.1.tar.gz", hash = "sha256:75beac4a47881eeb94d5ea5d6ad31ef88856affe2332b9aafb52c6452ccf0d7a"}, @@ -2691,7 +2691,7 @@ jupyter = ["ipywidgets (>=7.5.1,<9)"] name = "rsa" version = "4.9" description = "Pure-Python RSA implementation" -optional = false +optional = true python-versions = ">=3.6,<4" files = [ {file = "rsa-4.9-py3-none-any.whl", hash = "sha256:90260d9058e514786967344d0ef75fa8727eed8a7d2e43ce9f4bcf1b536174f7"}, @@ -2705,7 +2705,7 @@ pyasn1 = ">=0.1.3" name = "setuptools" version = "69.0.3" description = "Easily download, build, install, upgrade, and uninstall Python packages" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "setuptools-69.0.3-py3-none-any.whl", hash = "sha256:385eb4edd9c9d5c17540511303e39a147ce2fc04bc55289c322b9e5904fe2c05"}, @@ -2721,7 +2721,7 @@ testing-integration = ["build[virtualenv] (>=1.0.3)", "filelock (>=3.4.0)", "jar name = "six" version = "1.16.0" description = "Python 2 and 3 compatibility utilities" -optional = false +optional = true python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" files = [ {file = "six-1.16.0-py2.py3-none-any.whl", hash = "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"}, @@ -2841,7 +2841,7 @@ sqlcipher = ["sqlcipher3_binary"] name = "starlette" version = "0.35.1" description = "The little ASGI library that shines." -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "starlette-0.35.1-py3-none-any.whl", hash = "sha256:50bbbda9baa098e361f398fda0928062abbaf1f54f4fadcbe17c092a01eb9a25"}, @@ -2873,7 +2873,7 @@ pbr = ">=2.0.0,<2.1.0 || >2.1.0" name = "sympy" version = "1.12" description = "Computer algebra system (CAS) in Python" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "sympy-1.12-py3-none-any.whl", hash = "sha256:c3588cd4295d0c0f603d0f2ae780587e64e2efeedb3521e46b9bb1d08d184fa5"}, @@ -2953,7 +2953,7 @@ blobfile = ["blobfile (>=2)"] name = "tokenizers" version = "0.15.1" description = "" -optional = false +optional = true python-versions = ">=3.7" files = [ {file = "tokenizers-0.15.1-cp310-cp310-macosx_10_12_x86_64.whl", hash = "sha256:32c9491dd1bcb33172c26b454dbd607276af959b9e78fa766e2694cafab3103c"}, @@ -3111,7 +3111,7 @@ telegram = ["requests"] name = "typer" version = "0.9.0" description = "Typer, build great CLIs. Easy to code. Based on Python type hints." -optional = false +optional = true python-versions = ">=3.6" files = [ {file = "typer-0.9.0-py3-none-any.whl", hash = "sha256:5d96d986a21493606a358cae4461bd8cdf83cbf33a5aa950ae629ca3b51467ee"}, @@ -3188,7 +3188,7 @@ zstd = ["zstandard (>=0.18.0)"] name = "uvicorn" version = "0.27.0.post1" description = "The lightning-fast ASGI server." -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "uvicorn-0.27.0.post1-py3-none-any.whl", hash = "sha256:4b85ba02b8a20429b9b205d015cbeb788a12da527f731811b643fd739ef90d5f"}, @@ -3214,7 +3214,7 @@ standard = ["colorama (>=0.4)", "httptools (>=0.5.0)", "python-dotenv (>=0.13)", name = "uvloop" version = "0.19.0" description = "Fast implementation of asyncio event loop on top of libuv" -optional = false +optional = true python-versions = ">=3.8.0" files = [ {file = "uvloop-0.19.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:de4313d7f575474c8f5a12e163f6d89c0a878bc49219641d49e6f1444369a90e"}, @@ -3258,7 +3258,7 @@ test = ["Cython (>=0.29.36,<0.30.0)", "aiohttp (==3.9.0b0)", "aiohttp (>=3.8.1)" name = "watchfiles" version = "0.21.0" description = "Simple, modern and high performance file watching and code reload in python." -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "watchfiles-0.21.0-cp310-cp310-macosx_10_7_x86_64.whl", hash = "sha256:27b4035013f1ea49c6c0b42d983133b136637a527e48c132d368eb19bf1ac6aa"}, @@ -3345,7 +3345,7 @@ anyio = ">=3.0.0" name = "websocket-client" version = "1.7.0" description = "WebSocket client for Python with low level API options" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "websocket-client-1.7.0.tar.gz", hash = "sha256:10e511ea3a8c744631d3bd77e61eb17ed09304c413ad42cf6ddfa4c7787e8fe6"}, @@ -3361,7 +3361,7 @@ test = ["websockets"] name = "websockets" version = "12.0" description = "An implementation of the WebSocket Protocol (RFC 6455 & 7692)" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "websockets-12.0-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:d554236b2a2006e0ce16315c16eaa0d628dab009c33b63ea03f41c6107958374"}, @@ -3442,7 +3442,7 @@ files = [ name = "wrapt" version = "1.16.0" description = "Module for decorators, wrappers and monkey patching." -optional = false +optional = true python-versions = ">=3.6" files = [ {file = "wrapt-1.16.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:ffa565331890b90056c01db69c0fe634a776f8019c143a5ae265f9c6bc4bd6d4"}, @@ -3624,7 +3624,7 @@ multidict = ">=4.0" name = "zipp" version = "3.17.0" description = "Backport of pathlib-compatible object wrapper for zip files" -optional = false +optional = true python-versions = ">=3.8" files = [ {file = "zipp-3.17.0-py3-none-any.whl", hash = "sha256:0e923e726174922dce09c53c59ad483ff7bbb8e572e00c7f7c46b88556409f31"}, @@ -3635,7 +3635,10 @@ files = [ docs = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (<7.2.5)", "sphinx (>=3.5)", "sphinx-lint"] testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-ignore-flaky", "pytest-mypy (>=0.9.1)", "pytest-ruff"] +[extras] +chromadb = ["chromadb"] + [metadata] lock-version = "2.0" python-versions = ">=3.8.1,<3.13" -content-hash = "f1d55dbc3e60a4aaf686677e31cbdbfe5bfa36dffd6a0ebb0c10335085381cd3" +content-hash = "21127a7ac1c193e2012cdd1091a588bf27369a9c88972751704eefe1a62ab768" diff --git a/python-sdk/pyproject.toml b/python-sdk/pyproject.toml index a6c06e5..01a682e 100644 --- a/python-sdk/pyproject.toml +++ b/python-sdk/pyproject.toml @@ -14,10 +14,13 @@ pydantic = "^2.5.3" requests = "^2.31.0" openai = "^1.8.0" pinecone-client = "^3.0.0" -chromadb = "^0.4.22" langchain = "^0.1.1" langchain-openai = "^0.0.3" tiktoken = "^0.5.2" +chromadb = { version = "^0.4.22", optional = true } + +[tool.poetry.extras] +chromadb = ["chromadb"] [tool.poetry.group.dev.dependencies] black = "^23.12.1" diff --git a/python-sdk/rebuff/detect_pi_vectorbase.py b/python-sdk/rebuff/detect_pi_vectorbase.py index d0c117e..4338c66 100644 --- a/python-sdk/rebuff/detect_pi_vectorbase.py +++ b/python-sdk/rebuff/detect_pi_vectorbase.py @@ -1,15 +1,26 @@ -from typing import Dict, List, Tuple +from typing import Dict, List, Tuple, Union, Optional import pinecone from langchain.vectorstores.pinecone import Pinecone -import chromadb from langchain_community.vectorstores import Chroma from langchain_core.documents.base import Document from langchain_openai import OpenAIEmbeddings +try: + import chromadb + + chromadb_installed = True +except ImportError: + print( + "To use Chromadb, please install rebuff with rebuff extras. 'pip install \"rebuff\[chromadb]\"'" + ) + chromadb_installed = False + def detect_pi_using_vector_database( - input: str, similarity_threshold: float, vector_store: Pinecone + input: str, + similarity_threshold: float, + vector_store: Union[Pinecone, Optional[Chroma]], ) -> Dict: """ Detects Prompt Injection using similarity search with vector database. @@ -17,7 +28,7 @@ def detect_pi_using_vector_database( Args: input (str): user input to be checked for prompt injection similarity_threshold (float): The threshold for similarity between entries in vector database and the user input. - vector_store (Pinecone): Vector database of prompt injections + vector_store (Pinecone or Chroma): Vector database of prompt injections Returns: Dict (str, Union[float, int]): top_score (float) that contains the highest score wrt similarity between vector database and the user input. @@ -74,54 +85,57 @@ def init_pinecone(api_key: str, index: str, openai_api_key: str) -> Pinecone: return vector_store -class ChromaCosineSimilarity(Chroma): - """ - Our code expects a similarity score where similar vectors are close to 1, but Chroma returns a distance score - where similar vectors are close to 0. - """ +if chromadb_installed: - def similarity_search_with_score( - self, query: str, k: int, filter=None - ) -> List[Tuple[Document, float]]: + class ChromaCosineSimilarity(Chroma): """ - Detects Prompt Injection using similarity search with Chroma database. - - Args: - query (str): user input to be checked for prompt injection - k (int): The threshold for similarity between entries in vector database and the user input. - - Returns: - List[Tuple[Document, float]]: Documents with most similarity with the query and the correspoding similarity scores. + Our code expects a similarity score where similar vectors are close to 1, but Chroma returns a distance score + where similar vectors are close to 0. """ - results = super().similarity_search_with_score(query, k, filter) - return [(document, 1 - score) for document, score in results] + def similarity_search_with_score( + self, query: str, k: int, filter=None + ) -> List[Tuple[Document, float]]: + """ + Detects Prompt Injection using similarity search with Chroma database. + Args: + query (str): user input to be checked for prompt injection + k (int): The threshold for similarity between entries in vector database and the user input. -def init_chroma(url: str, collection_name: str, openai_api_key: str) -> Chroma: - """ - Initializes Chroma vector database. + Returns: + List[Tuple[Document, float]]: Documents with most similarity with the query and the correspoding similarity scores. + """ - Args: - url: str, Chroma URL - collection_name: str, Chroma collection name - openai_api_key (str): Open AI API key - Returns: - vector_store (Chroma) + results = super().similarity_search_with_score(query, k, filter) + return [(document, 1 - score) for document, score in results] - """ - if not url: - raise ValueError("Chroma url definition missing") - if not collection_name: - raise ValueError("Chroma collection name definition missing") + def init_chroma( + url: str, collection_name: str, openai_api_key: str + ) -> ChromaCosineSimilarity: + """ + Initializes Chroma vector database. - openai_embeddings = OpenAIEmbeddings( - openai_api_key=openai_api_key, model="text-embedding-ada-002" - ) + Args: + url: str, Chroma URL + collection_name: str, Chroma collection name + openai_api_key (str): Open AI API key + Returns: + vector_store (ChromaCosineSimilarity) - chroma_collection = ChromaCosineSimilarity( - client=chromadb.Client(), - collection_name=collection_name, - embedding_function=openai_embeddings, - ) - return chroma_collection + """ + if not url: + raise ValueError("Chroma url definition missing") + if not collection_name: + raise ValueError("Chroma collection name definition missing") + + openai_embeddings = OpenAIEmbeddings( + openai_api_key=openai_api_key, model="text-embedding-ada-002" + ) + + chroma_collection = ChromaCosineSimilarity( + client=chromadb.Client(), + collection_name=collection_name, + embedding_function=openai_embeddings, + ) + return chroma_collection diff --git a/python-sdk/rebuff/sdk.py b/python-sdk/rebuff/sdk.py index 11926d3..16deac8 100644 --- a/python-sdk/rebuff/sdk.py +++ b/python-sdk/rebuff/sdk.py @@ -12,7 +12,7 @@ from rebuff.detect_pi_vectorbase import ( detect_pi_using_vector_database, init_pinecone, - init_chroma, + chromadb_installed, ) @@ -33,8 +33,8 @@ class RebuffSdk: def __init__( self, openai_apikey: str, - pinecone_apikey: Optional[str] = None, - pinecone_index: Optional[str] = None, + pinecone_apikey: str, + pinecone_index: str, chroma_url: Optional[str] = "http://localhost:8000", chroma_collection_name: Optional[str] = "rebuff", openai_model: Optional[str] = "gpt-3.5-turbo", @@ -48,20 +48,22 @@ def __init__( self.vector_store = None def initialize_vector_store(self) -> None: - if self.pinecone_apikey and self.pinecone_index: - self.vector_store = init_pinecone( - self.pinecone_apikey, - self.pinecone_index, - self.openai_apikey, - ) + if chromadb_installed: + from rebuff.detect_pi_vectorbase import init_chroma - else: self.vector_store = init_chroma( self.chroma_url, self.chroma_collection_name, self.openai_apikey, ) + else: + self.vector_store = init_pinecone( + self.pinecone_apikey, + self.pinecone_index, + self.openai_apikey, + ) + def detect_injection( self, user_input: str, diff --git a/python-sdk/tests/test_sdk.py b/python-sdk/tests/test_sdk.py index 1341b6a..629e394 100644 --- a/python-sdk/tests/test_sdk.py +++ b/python-sdk/tests/test_sdk.py @@ -6,7 +6,11 @@ @pytest.fixture() def rebuff() -> RebuffSdk: - rb = RebuffSdk(get_environment_variable("OPENAI_API_KEY")) + rb = RebuffSdk( + get_environment_variable("OPENAI_API_KEY"), + get_environment_variable("PINECONE_API_KEY"), + get_environment_variable("PINECONE_INDEX_NAME"), + ) return rb