From 4d0732395dcc870e83a49f9ff7d81175c4e59a8a Mon Sep 17 00:00:00 2001 From: Yuhong Sun Date: Thu, 27 Jul 2023 16:33:04 -0700 Subject: [PATCH] Standalone Script to Test OpenAI API Key (#243) --- backend/scripts/test-openapi-key.py | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 backend/scripts/test-openapi-key.py diff --git a/backend/scripts/test-openapi-key.py b/backend/scripts/test-openapi-key.py new file mode 100644 index 00000000000..8730241191e --- /dev/null +++ b/backend/scripts/test-openapi-key.py @@ -0,0 +1,43 @@ +from typing import cast + +import openai + + +VALID_MODEL_LIST = ["text-davinci-003", "gpt-3.5-turbo", "gpt-4"] + + +if __name__ == "__main__": + model_version = None + while model_version not in VALID_MODEL_LIST: + model_version = input("Please provide an OpenAI model version to test: ") + if model_version not in VALID_MODEL_LIST: + print(f"Model must be from valid list: {', '.join(VALID_MODEL_LIST)}") + + api_key = input("Please provide an OpenAI API Key to test: ") + openai.api_key = api_key + + prompt = "The boy went to the " + print(f"Asking OpenAI to finish the sentence using {model_version}") + print(prompt) + try: + if model_version == "text-davinci-003": + response = openai.Completion.create( + model=model_version, prompt=prompt, max_tokens=5, temperature=2 + ) + print(cast(str, response["choices"][0]["text"]).strip()) + + else: + messages = [ + {"role": "system", "content": "Finish the sentence"}, + {"role": "user", "content": prompt}, + ] + response = openai.ChatCompletion.create( + model=model_version, messages=messages, max_tokens=5, temperature=2 + ) + print(cast(str, response["choices"][0]["message"]["content"]).strip()) + print("Success! Feel free to use this API key for Danswer.") + except Exception: + print( + "Failed, provided API key is invalid for Danswer, please address the error from OpenAI." + ) + raise