Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Use explicit API keys when querying openai rather than import time manipulation of the package attributes (#3241)

James Collinsf96293973765
diff --git a/autogpt/api_manager.py b/autogpt/api_manager.py
index 16975bae45..ace64fbdaf 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 834432c591..9ed0f44b32 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 9de7fe70fc..66f0253e22 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 f2fd9a81bc..7b565edd8f 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,
+ )