From 76b510bb1f50ee1ccd673501f0e24330f5735533 Mon Sep 17 00:00:00 2001 From: elliottower Date: Mon, 20 Nov 2023 12:42:45 -0500 Subject: [PATCH 1/2] Update to use openai 1.0.0 API --- chatarena/backends/openai.py | 6 +++--- pyproject.toml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/chatarena/backends/openai.py b/chatarena/backends/openai.py index 2745d683..98cd6381 100644 --- a/chatarena/backends/openai.py +++ b/chatarena/backends/openai.py @@ -13,8 +13,8 @@ is_openai_available = False # logging.warning("openai package is not installed") else: - openai.api_key = os.environ.get("OPENAI_API_KEY") - if openai.api_key is None: + client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) + if client.api_key is None: # logging.warning("OpenAI API key is not set. Please set the environment variable OPENAI_API_KEY") is_openai_available = False else: @@ -72,7 +72,7 @@ def __init__( @retry(stop=stop_after_attempt(6), wait=wait_random_exponential(min=1, max=60)) def _get_response(self, messages): - completion = openai.ChatCompletion.create( + completion = client.chat.completions.create( model=self.model, messages=messages, temperature=self.temperature, diff --git a/pyproject.toml b/pyproject.toml index 62a02845..658d259f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ classifiers = [ "Operating System :: OS Independent", ] dependencies = [ - "openai>=0.27.2", + "openai>=1.0.0", "tenacity==8.2.2", "rich==13.3.3", "prompt_toolkit==3.0.38", From 7b708bd7693daf54c6bc9bcf33ead8af493e97aa Mon Sep 17 00:00:00 2001 From: elliottower Date: Mon, 20 Nov 2023 13:02:42 -0500 Subject: [PATCH 2/2] Add exception to account for OpenAIError on import and client initialization --- chatarena/backends/openai.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chatarena/backends/openai.py b/chatarena/backends/openai.py index 98cd6381..74e577ef 100644 --- a/chatarena/backends/openai.py +++ b/chatarena/backends/openai.py @@ -13,12 +13,12 @@ is_openai_available = False # logging.warning("openai package is not installed") else: - client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) - if client.api_key is None: + try: + client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) + is_openai_available = True + except openai.OpenAIError: # logging.warning("OpenAI API key is not set. Please set the environment variable OPENAI_API_KEY") is_openai_available = False - else: - is_openai_available = True # Default config follows the OpenAI playground DEFAULT_TEMPERATURE = 0.7