From f962939737657a06c3fbfe6094620576ae834364 Mon Sep 17 00:00:00 2001 From: James Collins Date: Tue, 25 Apr 2023 11:38:06 -0700 Subject: [PATCH] Use explicit API keys when querying openai rather than import time manipulation of the package attributes (#3241) --- autogpt/api_manager.py | 3 ++- autogpt/commands/image_gen.py | 2 +- autogpt/config/config.py | 2 -- autogpt/llm_utils.py | 8 +++++--- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/autogpt/api_manager.py b/autogpt/api_manager.py index 16975bae..ace64fbd 100644 --- a/autogpt/api_manager.py +++ b/autogpt/api_manager.py @@ -7,7 +7,6 @@ from autogpt.logs import logger from autogpt.modelsinfo import COSTS cfg = Config() -openai.api_key = cfg.openai_api_key print_total_cost = cfg.debug_mode @@ -50,6 +49,7 @@ class ApiManager: messages=messages, temperature=temperature, max_tokens=max_tokens, + api_key=cfg.openai_api_key, ) else: response = openai.ChatCompletion.create( @@ -57,6 +57,7 @@ class ApiManager: messages=messages, temperature=temperature, max_tokens=max_tokens, + api_key=cfg.openai_api_key, ) if self.debug: logger.debug(f"Response: {response}") diff --git a/autogpt/commands/image_gen.py b/autogpt/commands/image_gen.py index 834432c5..9ed0f44b 100644 --- a/autogpt/commands/image_gen.py +++ b/autogpt/commands/image_gen.py @@ -87,7 +87,6 @@ def generate_image_with_dalle(prompt: str, filename: str, size: int) -> str: Returns: str: The filename of the image """ - openai.api_key = CFG.openai_api_key # Check for supported image sizes if size not in [256, 512, 1024]: @@ -102,6 +101,7 @@ def generate_image_with_dalle(prompt: str, filename: str, size: int) -> str: n=1, size=f"{size}x{size}", response_format="b64_json", + api_key=CFG.openai_api_key, ) print(f"Image Generated for prompt:{prompt}") diff --git a/autogpt/config/config.py b/autogpt/config/config.py index 9de7fe70..66f0253e 100644 --- a/autogpt/config/config.py +++ b/autogpt/config/config.py @@ -128,8 +128,6 @@ class Config(metaclass=Singleton): # Note that indexes must be created on db 0 in redis, this is not configurable. self.memory_backend = os.getenv("MEMORY_BACKEND", "local") - # Initialize the OpenAI API client - openai.api_key = self.openai_api_key self.plugins_dir = os.getenv("PLUGINS_DIR", "plugins") self.plugins: List[AutoGPTPluginTemplate] = [] diff --git a/autogpt/llm_utils.py b/autogpt/llm_utils.py index f2fd9a81..7b565edd 100644 --- a/autogpt/llm_utils.py +++ b/autogpt/llm_utils.py @@ -14,7 +14,6 @@ from autogpt.logs import logger from autogpt.types.openai import Message CFG = Config() -openai.api_key = CFG.openai_api_key def retry_openai_api( @@ -248,5 +247,8 @@ def create_embedding( Returns: openai.Embedding: The embedding object. """ - - return openai.Embedding.create(input=[text], **kwargs) + return openai.Embedding.create( + input=[text], + api_key=CFG.openai_api_key, + **kwargs, + )