Skip to content

Commit

Permalink
expire azure token as needed (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan authored Aug 12, 2024
1 parent 4e8484c commit c61970a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 12 additions & 2 deletions packages/cli/src/azuretoken.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { AZURE_OPENAI_TOKEN_SCOPES } from "../../core/src/constants"

export async function createAzureToken(signal: AbortSignal): Promise<string> {
export interface AuthenticationToken {
token: string
expiresOnTimestamp: number
}

export async function createAzureToken(
signal: AbortSignal
): Promise<AuthenticationToken> {
const { DefaultAzureCredential } = await import("@azure/identity")
const azureToken = await new DefaultAzureCredential().getToken(
AZURE_OPENAI_TOKEN_SCOPES.slice(),
{ abortSignal: signal }
)
return azureToken.token
return {
token: azureToken.token,
expiresOnTimestamp: azureToken.expiresOnTimestamp,
}
}
11 changes: 7 additions & 4 deletions packages/cli/src/nodehost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
import { AbortSignalOptions, TraceOptions } from "../../core/src/trace"
import { logVerbose, unique } from "../../core/src/util"
import { parseModelIdentifier } from "../../core/src/models"
import { createAzureToken } from "./azuretoken"
import { AuthenticationToken, createAzureToken } from "./azuretoken"
import { LanguageModel } from "../../core/src/chat"
import { errorMessage } from "../../core/src/error"

Expand Down Expand Up @@ -144,7 +144,7 @@ export class NodeHost implements RuntimeHost {
}
clientLanguageModel: LanguageModel

private _azureToken: string
private _azureToken: AuthenticationToken
async getLanguageModelConfiguration(
modelId: string,
options?: { token?: boolean } & AbortSignalOptions & TraceOptions
Expand All @@ -158,10 +158,13 @@ export class NodeHost implements RuntimeHost {
!tok.token &&
tok.provider === MODEL_PROVIDER_AZURE
) {
if (!this._azureToken)
if (
!this._azureToken ||
this._azureToken.expiresOnTimestamp >= Date.now()
)
this._azureToken = await createAzureToken(signal)
if (!this._azureToken) throw new Error("Azure token not available")
tok.token = "Bearer " + this._azureToken
tok.token = "Bearer " + this._azureToken.token
}
if (!tok && this.clientLanguageModel) {
return <LanguageModelConfiguration>{
Expand Down

0 comments on commit c61970a

Please sign in to comment.