Skip to content

Commit

Permalink
feat: add test case for azure
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyutaotao committed Dec 27, 2024
1 parent 1e483fb commit 3600fa7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions connectivity-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@azure/identity": "4.5.0",
"@midscene/core": "latest",
"@midscene/shared": "latest",
"dotenv": "^16.4.5",
Expand Down
38 changes: 36 additions & 2 deletions connectivity-test/tests/connectivity.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { describe, it, expect, vi } from "vitest";
import dotenv from "dotenv";
import OpenAI from "openai";
import OpenAI, { AzureOpenAI } from "openai";
import { join } from "node:path";
import { base64Encoded } from "@midscene/shared/img";
import { callToGetJSONObject } from "@midscene/core/ai-model";
import {
DefaultAzureCredential,
getBearerTokenProvider,
} from "@azure/identity";

// read and parse .env file
const result = dotenv.config({
Expand All @@ -24,8 +28,8 @@ vi.setConfig({
const imagePath = join(__dirname, "some_logo.png");
const imageBase64 = base64Encoded(imagePath);

const model = process.env.MIDSCENE_MODEL_NAME || "gpt-4o";
describe("Use OpenAI SDK directly", () => {
const model = process.env.MIDSCENE_MODEL_NAME || "gpt-4o";
it(`basic call with ${model}`, async () => {
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
Expand Down Expand Up @@ -94,3 +98,33 @@ describe("Use Midscene wrapped OpenAI SDK", () => {
expect(result.content.content.length).toBeGreaterThan(5);
});
});

// remove the ".skip" if you want to test Azure OpenAI Service
describe.skip("Azure OpenAI Service", () => {
it("basic call", async () => {
// sample code: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/samples/cookbook/simpleCompletionsPage/app.js
const scope = process.env.MIDSCENE_AZURE_OPENAI_SCOPE;
if (typeof scope !== "string") {
throw new Error("MIDSCENE_AZURE_OPENAI_SCOPE is required");
}

const credential = new DefaultAzureCredential();
const tokenProvider = getBearerTokenProvider(credential, scope);

const extraAzureConfig = JSON.parse(
process.env.MIDSCENE_AZURE_OPENAI_INIT_CONFIG_JSON || "{}"
);
// console.log(extraAzureConfig);
const openai = new AzureOpenAI({
azureADTokenProvider: tokenProvider,
...extraAzureConfig,
});

const response = await openai.chat.completions.create({
model: model,
messages: [{ role: "user", content: "Hello, how are you?" }],
});

expect(response.choices[0].message.content).toBeTruthy();
});
});

0 comments on commit 3600fa7

Please sign in to comment.