Skip to content

Commit 478b8a3

Browse files
committed
OpenAIUser: fail early if OPENAI_API_KEY is not set
1 parent 53f053a commit 478b8a3

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Diff for: locust/contrib/oai.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
import httpx
1111
from openai import OpenAI # dont forget to install openai
1212

13+
# convenience for when running in Locust Cloud, where only LOCUST_* env vars are forwarded
14+
if "LOCUST_OPENAI_API_KEY" in os.environ:
15+
os.environ["OPENAI_API_KEY"] = os.environ["LOCUST_OPENAI_API_KEY"]
16+
17+
if not "OPENAI_API_KEY" in os.environ:
18+
raise Exception("You need to set OPENAI_API_KEY or LOCUST_OPENAI_API_KEY env var")
19+
1320

1421
class OpenAIClient(OpenAI):
1522
def __init__(self, request_event, user, *args, **kwargs):
@@ -41,13 +48,10 @@ def request_end(response):
4148
url=response.url,
4249
)
4350

44-
# dont forget to set OPENAI_API_KEY in your environment
4551
super().__init__(
4652
*args,
4753
**kwargs,
4854
http_client=httpx.Client(event_hooks={"request": [request_start], "response": [request_end]}),
49-
# convenience for when running in Locust Cloud, where only LOCUST_* env vars are forwarded:
50-
api_key=os.getenv("LOCUST_OPENAI_API_KEY", None),
5155
)
5256

5357
@contextmanager

0 commit comments

Comments
 (0)