diff --git a/apps/site/docs/en/model-provider.md b/apps/site/docs/en/model-provider.md index f37a69a9..764568c7 100644 --- a/apps/site/docs/en/model-provider.md +++ b/apps/site/docs/en/model-provider.md @@ -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 @@ -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` diff --git a/apps/site/docs/zh/model-provider.md b/apps/site/docs/zh/model-provider.md index 38fed0e4..a3fe840d 100644 --- a/apps/site/docs/zh/model-provider.md +++ b/apps/site/docs/zh/model-provider.md @@ -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` 以外的其他模型 diff --git a/packages/midscene/src/ai-model/openai/index.ts b/packages/midscene/src/ai-model/openai/index.ts index b17aa301..7a202fbf 100644 --- a/packages/midscene/src/ai-model/openai/index.ts +++ b/packages/midscene/src/ai-model/openai/index.ts @@ -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, @@ -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), diff --git a/packages/midscene/src/env.ts b/packages/midscene/src/env.ts index a35e796c..0b93950f 100644 --- a/packages/midscene/src/env.ts +++ b/packages/midscene/src/env.ts @@ -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'; @@ -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, }; };