Skip to content

Commit

Permalink
feat: support keyless auth mode for azure
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyutaotao committed Dec 31, 2024
1 parent 41350f9 commit 1e00231
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
4 changes: 2 additions & 2 deletions apps/site/docs/en/model-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export MIDSCENE_USE_AZURE_OPENAI=1
export MIDSCENE_AZURE_OPENAI_SCOPE="https://cognitiveservices.azure.com/.default"
export AZURE_OPENAI_ENDPOINT="..."
export AZURE_OPENAI_API_VERSION="2024-05-01-preview"
export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o"
export AZURE_OPENAI_DEPLOYMENT="gpt-4o"
```

Or use keyless authentication
Expand All @@ -52,7 +52,7 @@ export MIDSCENE_USE_AZURE_OPENAI=1
export AZURE_OPENAI_ENDPOINT="..."
export AZURE_OPENAI_KEY="..."
export AZURE_OPENAI_API_VERSION="2024-05-01-preview"
export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o"
export AZURE_OPENAI_DEPLOYMENT="gpt-4o"
```

## Choose a model other than `gpt-4o`
Expand Down
4 changes: 2 additions & 2 deletions apps/site/docs/zh/model-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export MIDSCENE_USE_AZURE_OPENAI=1
export MIDSCENE_AZURE_OPENAI_SCOPE="https://cognitiveservices.azure.com/.default"
export AZURE_OPENAI_ENDPOINT="..."
export AZURE_OPENAI_API_VERSION="2024-05-01-preview"
export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o"
export AZURE_OPENAI_DEPLOYMENT="gpt-4o"
```

使用 keyless 模式
Expand All @@ -49,7 +49,7 @@ export MIDSCENE_USE_AZURE_OPENAI=1
export AZURE_OPENAI_ENDPOINT="..."
export AZURE_OPENAI_KEY="..."
export AZURE_OPENAI_API_VERSION="2024-05-01-preview"
export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o"
export AZURE_OPENAI_DEPLOYMENT="gpt-4o"
```

## 选用 `gpt-4o` 以外的其他模型
Expand Down
41 changes: 25 additions & 16 deletions packages/midscene/src/ai-model/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SocksProxyAgent } from 'socks-proxy-agent';
import {
ANTHROPIC_API_KEY,
AZURE_OPENAI_API_VERSION,
AZURE_OPENAI_DEPLOYMENT_NAME,
AZURE_OPENAI_DEPLOYMENT,
AZURE_OPENAI_ENDPOINT,
AZURE_OPENAI_KEY,
MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON,
Expand Down Expand Up @@ -79,6 +79,11 @@ async function createChatClient(): Promise<{
dangerouslyAllowBrowser: true,
}) as OpenAI;
} else if (getAIConfig(MIDSCENE_USE_AZURE_OPENAI)) {
const extraAzureConfig = getAIConfigInJson(
MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON,
);

// https://learn.microsoft.com/en-us/azure/ai-services/openai/chatgpt-quickstart?tabs=bash%2Cjavascript-key%2Ctypescript-keyless%2Cpython&pivots=programming-language-javascript#rest-api
// keyless authentication
const scope = getAIConfig(MIDSCENE_AZURE_OPENAI_SCOPE);
let tokenProvider: any = undefined;
Expand All @@ -91,22 +96,26 @@ async function createChatClient(): Promise<{

assert(scope, 'MIDSCENE_AZURE_OPENAI_SCOPE is required');
tokenProvider = getBearerTokenProvider(credential, scope);
}

const extraAzureConfig = getAIConfigInJson(
MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON,
);

// endpoint, apiKey, apiVersion, deployment
openai = new AzureOpenAI({
azureADTokenProvider: tokenProvider,
endpoint: getAIConfig(AZURE_OPENAI_ENDPOINT),
apiKey: getAIConfig(AZURE_OPENAI_KEY),
apiVersion: getAIConfig(AZURE_OPENAI_API_VERSION),
deploymentName: getAIConfig(AZURE_OPENAI_DEPLOYMENT_NAME),
...extraConfig,
...extraAzureConfig,
});
openai = new AzureOpenAI({
azureADTokenProvider: tokenProvider,
endpoint: getAIConfig(AZURE_OPENAI_ENDPOINT),
apiVersion: getAIConfig(AZURE_OPENAI_API_VERSION),
deployment: getAIConfig(AZURE_OPENAI_DEPLOYMENT),
...extraConfig,
...extraAzureConfig,
});
} else {
// endpoint, apiKey, apiVersion, deployment
openai = new AzureOpenAI({
apiKey: getAIConfig(AZURE_OPENAI_KEY),
endpoint: getAIConfig(AZURE_OPENAI_ENDPOINT),
apiVersion: getAIConfig(AZURE_OPENAI_API_VERSION),
deployment: getAIConfig(AZURE_OPENAI_DEPLOYMENT),
...extraConfig,
...extraAzureConfig,
});
}
} else if (!getAIConfig(MIDSCENE_USE_ANTHROPIC_SDK)) {
openai = new OpenAI({
baseURL: getAIConfig(OPENAI_BASE_URL),
Expand Down
6 changes: 3 additions & 3 deletions packages/midscene/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON =
export const AZURE_OPENAI_ENDPOINT = 'AZURE_OPENAI_ENDPOINT';
export const AZURE_OPENAI_KEY = 'AZURE_OPENAI_KEY';
export const AZURE_OPENAI_API_VERSION = 'AZURE_OPENAI_API_VERSION';
export const AZURE_OPENAI_DEPLOYMENT_NAME = 'AZURE_OPENAI_DEPLOYMENT_NAME';
export const AZURE_OPENAI_DEPLOYMENT = 'AZURE_OPENAI_DEPLOYMENT';

export const MIDSCENE_USE_ANTHROPIC_SDK = 'MIDSCENE_USE_ANTHROPIC_SDK';
export const ANTHROPIC_API_KEY = 'ANTHROPIC_API_KEY';
Expand Down Expand Up @@ -71,8 +71,8 @@ const allConfigFromEnv = () => {
[AZURE_OPENAI_KEY]: process.env[AZURE_OPENAI_KEY] || undefined,
[AZURE_OPENAI_API_VERSION]:
process.env[AZURE_OPENAI_API_VERSION] || undefined,
[AZURE_OPENAI_DEPLOYMENT_NAME]:
process.env[AZURE_OPENAI_DEPLOYMENT_NAME] || undefined,
[AZURE_OPENAI_DEPLOYMENT]:
process.env[AZURE_OPENAI_DEPLOYMENT] || undefined,
};
};

Expand Down

0 comments on commit 1e00231

Please sign in to comment.