-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expire azure token as needed #615
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
|
@@ -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 | ||
|
@@ -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() | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comparison
|
||
this._azureToken = await createAzureToken(signal) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function
|
||
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>{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameter
signal
in the functioncreateAzureToken
is missing a type annotation. Please add a type annotation to improve code readability and maintainability. 📚