diff --git a/.gitignore b/.gitignore index 80e3f6c..293af7e 100644 --- a/.gitignore +++ b/.gitignore @@ -143,3 +143,5 @@ temp/ api/dataset.pkl api/dataset_big.pkl api/dataset_300.pkl + +api/.env.backup diff --git a/api/.env.example b/api/.env.example index 3ba88c3..4e40cd4 100644 --- a/api/.env.example +++ b/api/.env.example @@ -1,3 +1,3 @@ OPENAI_API_KEY="sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -PINECONE_API_KEY="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -LOGGING_URL="" # leave blank if you're not testing logging specifically \ No newline at end of file +PINECONE_API_KEY="" # leave blank to use our online API instead +LOGGING_URL="" # leave blank if you're not testing logging specifically diff --git a/api/Pipfile b/api/Pipfile index b1c0256..079d058 100644 --- a/api/Pipfile +++ b/api/Pipfile @@ -20,6 +20,7 @@ tiktoken = "*" pinecone-client = "*" python-dotenv = "*" discord-webhook = "*" +requests = "*" [dev-packages] diff --git a/api/Pipfile.lock b/api/Pipfile.lock index a2762e9..29e52ac 100644 --- a/api/Pipfile.lock +++ b/api/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "749858b5ccd522452ddca640dacc919efe05a817bbe907afbd6ee799dfa16cc4" + "sha256": "08f83a57e2634a1c749ac33ed618b5f6345f56b11630024574b2c002bc3878f1" }, "pipfile-spec": 6, "requires": { @@ -728,19 +728,19 @@ }, "requests": { "hashes": [ - "sha256:10e94cc4f3121ee6da529d358cdaeaff2f1c409cd377dbc72b825852f2f7e294", - "sha256:239d7d4458afcb28a692cdd298d87542235f4ca8d36d03a15bfc128a6559a2f4" + "sha256:58cd2187c01e70e6e26505bca751777aa9f2ee0b7f4300988b709f44e013003f", + "sha256:942c5a758f98d790eaed1a29cb6eefc7ffb0d1cf7af05c3d2791656dbd6ad1e1" ], - "markers": "python_version >= '3.7'", - "version": "==2.30.0" + "index": "pypi", + "version": "==2.31.0" }, "setuptools": { "hashes": [ - "sha256:23aaf86b85ca52ceb801d32703f12d77517b2556af839621c641fca11287952b", - "sha256:f104fa03692a2602fa0fec6c6a9e63b6c8a968de13e17c026957dd1f53d80990" + "sha256:5df61bf30bb10c6f756eb19e7c9f3b473051f48db77fddbe06ff2ca307df9a6f", + "sha256:62642358adc77ffa87233bc4d2354c4b2682d214048f500964dbe760ccedf102" ], "markers": "python_version >= '3.7'", - "version": "==67.7.2" + "version": "==67.8.0" }, "six": { "hashes": [ @@ -803,11 +803,11 @@ }, "typing-extensions": { "hashes": [ - "sha256:5cb5f4a79139d699607b3ef622a1dedafa84e115ab0024e0d9c044a9479ca7cb", - "sha256:fb33085c39dd998ac16d1431ebc293a8b3eedd00fd4a32de0ff79002c19511b4" + "sha256:6ad00b63f849b7dcc313b70b6b304ed67b2b2963b3098a33efe18056b1a9a223", + "sha256:ff6b238610c747e44c268aa4bb23c8c735d665a63726df3f9431ce707f2aa768" ], "markers": "python_version >= '3.7'", - "version": "==4.5.0" + "version": "==4.6.0" }, "urllib3": { "hashes": [ diff --git a/api/get_blocks.py b/api/get_blocks.py index 61574f3..3fc1c70 100644 --- a/api/get_blocks.py +++ b/api/get_blocks.py @@ -5,6 +5,7 @@ import itertools import numpy as np import openai import regex as re +import requests import time # ---------------------------------- constants --------------------------------- @@ -46,7 +47,27 @@ def get_embedding(text: str) -> np.ndarray: # Get the k blocks most semantically similar to the query using Pinecone. -def get_top_k_blocks(index, user_query: str, k: int = 20) -> List[Block]: +def get_top_k_blocks(index, user_query: str, k: int) -> List[Block]: + + # Default to querying embeddings from live website if pinecone url not + # present in .env + # + # This helps people getting started developing or messing around with the + # site, since setting up a vector DB with the embeddings is by far the + # hardest part for those not already on the team. + + if index is None: + + print('Pinecone index not found, performing semantic search on alignmentsearch-api.up.railway.app endpoint.') + response = requests.post( + "https://alignmentsearch-api.up.railway.app/semantic", + json = { + "query": user_query, + "k": k + } + ) + + return [Block(**block) for block in response.json()] # print time t = time.time() diff --git a/api/main.py b/api/main.py index f225f6e..3cc0ea3 100644 --- a/api/main.py +++ b/api/main.py @@ -11,26 +11,32 @@ from discord_webhook import DiscordWebhook # ---------------------------------- env setup --------------------------------- - if os.path.exists('.env'): from dotenv import load_dotenv load_dotenv() +else: + print("'api/.env' not found. Rename the 'api/.env.example' file and fill in values.") -OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') -openai.api_key = OPENAI_API_KEY +OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY') PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY') -PINECONE_ENV = "us-east1-gcp" -pinecone.init( - api_key=PINECONE_API_KEY, - environment=PINECONE_ENV -) -INDEX_NAME = "alignment-search" -index = pinecone.Index(index_name=INDEX_NAME) +LOGGING_URL = os.environ.get('LOGGING_URL') +PINECONE_INDEX = None -LOGGING_URL = os.environ.get('LOGGING_URL') +openai.api_key = OPENAI_API_KEY # non-optional -def log(*args, end="\n"): +# Only init pinecone if we have an env value for it. +if PINECONE_API_KEY is not None and PINECONE_API_KEY != "": + + pinecone.init( + api_key = PINECONE_API_KEY, + environment = "us-east1-gcp", + ) + + PINECONE_INDEX = pinecone.Index(index_name="alignment-search") + +# log something only if the logging url is set +def log(*args, end="\n"): message = " ".join([str(arg) for arg in args]) + end # print(message) if LOGGING_URL is not None and LOGGING_URL != "": @@ -59,7 +65,9 @@ def stream(src): @cross_origin() def semantic(): query = request.json['query'] - return jsonify([dataclasses.asdict(block) for block in get_top_k_blocks(index, query)]) + k = request.json['k'] if 'k' in request.json else 20 + return jsonify([dataclasses.asdict(block) for block in get_top_k_blocks(PINECONE_INDEX, query, k)]) + # ------------------------------------ chat ------------------------------------ @@ -72,7 +80,7 @@ def chat(): query = request.json['query'] history = request.json['history'] - return Response(stream(talk_to_robot(index, query, history, log = log)), mimetype='text/event-stream') + return Response(stream(talk_to_robot(PINECONE_INDEX, query, history, log = log)), mimetype='text/event-stream') # ------------------------------------------------------------------------------