Skip to content
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

feat: support keyless auth mode for azure #227

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions apps/site/docs/en/model-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Optional configs:
# if you want to use a customized endpoint
export OPENAI_BASE_URL="https://..."

# if you want to use Azure OpenAI Service
# if you want to use Azure OpenAI Service. See more details in the next section.
export OPENAI_USE_AZURE="true"

# if you want to specify a model name other than gpt-4o
Expand All @@ -35,10 +35,24 @@ export OPENAI_MAX_TOKENS=2048

## Using Azure OpenAI Service

Use ADT token provider

```bash
export MIDSCENE_USE_AZURE_OPENAI=1
export MIDSCENE_AZURE_OPENAI_SCOPE="https://cognitiveservices.azure.com/.default"
export MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON='{"apiVersion": "2024-11-01-preview", "endpoint": "...", "deployment": "..."}'
export AZURE_OPENAI_ENDPOINT="..."
export AZURE_OPENAI_API_VERSION="2024-05-01-preview"
export AZURE_OPENAI_DEPLOYMENT="gpt-4o"
```

Or use keyless authentication

```bash
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="gpt-4o"
```

## Choose a model other than `gpt-4o`
Expand Down
16 changes: 15 additions & 1 deletion apps/site/docs/zh/model-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,24 @@ export OPENAI_MAX_TOKENS=2048

## 使用 Azure OpenAI 服务时的配置

使用 ADT token provider

```bash
export MIDSCENE_USE_AZURE_OPENAI=1
export MIDSCENE_AZURE_OPENAI_SCOPE="https://cognitiveservices.azure.com/.default"
export MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON='{"apiVersion": "2024-11-01-preview", "endpoint": "...", "deployment": "..."}'
export AZURE_OPENAI_ENDPOINT="..."
export AZURE_OPENAI_API_VERSION="2024-05-01-preview"
export AZURE_OPENAI_DEPLOYMENT="gpt-4o"
```

使用 keyless 模式

```bash
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="gpt-4o"
```

## 选用 `gpt-4o` 以外的其他模型
Expand Down
55 changes: 38 additions & 17 deletions packages/midscene/src/ai-model/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import type { ChatCompletionMessageParam } from 'openai/resources';
import { SocksProxyAgent } from 'socks-proxy-agent';
import {
ANTHROPIC_API_KEY,
AZURE_OPENAI_API_VERSION,
AZURE_OPENAI_DEPLOYMENT,
AZURE_OPENAI_ENDPOINT,
AZURE_OPENAI_KEY,
MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON,
MIDSCENE_AZURE_OPENAI_SCOPE,
MIDSCENE_DANGEROUSLY_PRINT_ALL_CONFIG,
Expand Down Expand Up @@ -75,26 +79,43 @@ async function createChatClient(): Promise<{
dangerouslyAllowBrowser: true,
}) as OpenAI;
} else if (getAIConfig(MIDSCENE_USE_AZURE_OPENAI)) {
// sample code: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/cookbook/simpleCompletionsPage/app.js
const scope = getAIConfig(MIDSCENE_AZURE_OPENAI_SCOPE);

assert(
!ifInBrowser,
'Azure OpenAI is not supported in browser with Midscene.',
);
const credential = new DefaultAzureCredential();

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

const extraAzureConfig = getAIConfigInJson(
MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON,
);
openai = new AzureOpenAI({
azureADTokenProvider: tokenProvider,
...extraConfig,
...extraAzureConfig,
});

// 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;
if (scope) {
assert(
!ifInBrowser,
'Azure OpenAI is not supported in browser with Midscene.',
);
const credential = new DefaultAzureCredential();

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

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
14 changes: 12 additions & 2 deletions packages/midscene/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export const MIDSCENE_AZURE_OPENAI_SCOPE = 'MIDSCENE_AZURE_OPENAI_SCOPE';
export const MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON =
'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 = '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 @@ -55,13 +60,18 @@ const allConfigFromEnv = () => {
[MIDSCENE_USE_AZURE_OPENAI]:
process.env[MIDSCENE_USE_AZURE_OPENAI] || undefined,
[MIDSCENE_AZURE_OPENAI_SCOPE]:
process.env[MIDSCENE_AZURE_OPENAI_SCOPE] ||
'https://cognitiveservices.azure.com/.default',
process.env[MIDSCENE_AZURE_OPENAI_SCOPE] || undefined,
[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON]:
process.env[MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON] || undefined,
[MIDSCENE_USE_ANTHROPIC_SDK]:
process.env[MIDSCENE_USE_ANTHROPIC_SDK] || undefined,
[ANTHROPIC_API_KEY]: process.env[ANTHROPIC_API_KEY] || undefined,
[AZURE_OPENAI_ENDPOINT]: process.env[AZURE_OPENAI_ENDPOINT] || undefined,
[AZURE_OPENAI_KEY]: process.env[AZURE_OPENAI_KEY] || undefined,
[AZURE_OPENAI_API_VERSION]:
process.env[AZURE_OPENAI_API_VERSION] || undefined,
[AZURE_OPENAI_DEPLOYMENT]:
process.env[AZURE_OPENAI_DEPLOYMENT] || undefined,
};
};

Expand Down
Loading