Skip to content

Commit

Permalink
fix: fixed token renewal logic (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik authored Jun 18, 2024
1 parent 94c33d4 commit 2371cb8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Copy `.env.example` to `.env` and customize it for your environment:
|DALLE3_DEPLOYMENTS|``|Comma-separated list of deployments that support DALL-E 3 API. Example: `dall-e-3,dalle3,dall-e`|
|GPT4_VISION_DEPLOYMENTS|``|Comma-separated list of deployments that support GPT-4V API. Example: `gpt-4-vision-preview,gpt-4-vision`|
|GPT4_VISION_MAX_TOKENS|1024|Default value of `max_tokens` parameter for GPT-4V when it wasn't provided in the request|
|ACCESS_TOKEN_EXPIRATION_WINDOW|10|Expiration window of access token in seconds|
|ACCESS_TOKEN_EXPIRATION_WINDOW|10|The Azure access token is renewed this many seconds before its actual expiration time. The buffer ensures that the token does not expire in the middle of an operation due to processing time and potential network delays.|
|AZURE_OPEN_AI_SCOPE|https://cognitiveservices.azure.com/.default|Provided scope of access token to Azure OpenAI services|
|API_VERSIONS_MAPPING|`{}`|The mapping of versions API for requests to Azure OpenAI API. Example: `{"2023-03-15-preview": "2023-05-15", "": "2024-02-15-preview"}`. An empty key sets the default api version for the case when the user didn't pass it in the request|
|DALLE3_AZURE_API_VERSION|2024-02-01|The version API for requests to Azure DALL-E-3 API|
Expand Down
7 changes: 5 additions & 2 deletions aidial_adapter_openai/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@


async def get_api_key() -> str:
now = int(time.time()) - EXPIRATION_WINDOW_IN_SEC
now = int(time.time())
global access_token

if access_token is None or now > access_token.expires_on:
if (
access_token is None
or now + EXPIRATION_WINDOW_IN_SEC > access_token.expires_on
):
try:
access_token = await default_credential.get_token(
AZURE_OPEN_AI_SCOPE
Expand Down

0 comments on commit 2371cb8

Please sign in to comment.