Skip to content

Commit

Permalink
fix: api key from env if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
goutham794 committed Dec 10, 2024
1 parent 80bb33c commit 0733363
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 1 addition & 2 deletions examples/basic_usage_structured_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ async def analyze_sentiment(sentences: List[str], system_prompt: str) -> Sentime
output, errors = await async_batch_chat_completion(
batch_messages=batch_messages,
gpt_model = 'gpt-4o-mini',
pydantic_model=Sentiment_Prediction_Output,
api_key=os.getenv("OPENAI_API_KEY"))
pydantic_model=Sentiment_Prediction_Output)

print(output)
# Print errors if any
Expand Down
16 changes: 15 additions & 1 deletion src/throttle_openai/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from loguru import logger
import time
import asyncio
import os

import throttle_openai.rate_limiter as rt
import throttle_openai.utils as u
Expand Down Expand Up @@ -113,9 +114,22 @@ class ChatOutput(pydantic_model):
return ChatOutput(id=id, gpt_tokens_used=usage, **result_json)

async def async_batch_chat_completion(
batch_messages: List[Dict[str, Any]], api_key: str, pydantic_model=None, gpt_model=DEFAULT_MODEL
batch_messages: List[Dict[str, Any]], api_key: str = None, pydantic_model=None, gpt_model=DEFAULT_MODEL
):

if api_key is None:
api_key = os.getenv("OPENAI_API_KEY")
if api_key is None:
raise ValueError(
"No API key provided and OPENAI_API_KEY environment variable is not set. "
"Please provide an API key or set the OPENAI_API_KEY environment variable."
)
else:
logger.info(
"No API key provided - using OPENAI_API_KEY environment variable. "
)


t0 = time.monotonic()
rt.set_rate_limiter(MAX_REQUESTS_PER_MIN, MAX_TOKENS_PER_MIN)

Expand Down

0 comments on commit 0733363

Please sign in to comment.