Skip to content

Commit

Permalink
Use explicit API keys when querying openai rather than import time ma…
Browse files Browse the repository at this point in the history
…nipulation of the package attributes (Significant-Gravitas#3241)
  • Loading branch information
collijk authored Apr 25, 2023
1 parent 2619740 commit f962939
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion autogpt/api_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from autogpt.modelsinfo import COSTS

cfg = Config()
openai.api_key = cfg.openai_api_key
print_total_cost = cfg.debug_mode


Expand Down Expand Up @@ -50,13 +49,15 @@ def create_chat_completion(
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
api_key=cfg.openai_api_key,
)
else:
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
api_key=cfg.openai_api_key,
)
if self.debug:
logger.debug(f"Response: {response}")
Expand Down
2 changes: 1 addition & 1 deletion autogpt/commands/image_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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}")
Expand Down
2 changes: 0 additions & 2 deletions autogpt/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ def __init__(self) -> None:
# 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] = []
Expand Down
8 changes: 5 additions & 3 deletions autogpt/llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from autogpt.types.openai import Message

CFG = Config()
openai.api_key = CFG.openai_api_key


def retry_openai_api(
Expand Down Expand Up @@ -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,
)

0 comments on commit f962939

Please sign in to comment.