Skip to content
Open
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
15 changes: 14 additions & 1 deletion ts/packages/aiclient/src/bing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,30 @@ import { getEnvSetting } from "./common.js";

export type ApiSettings = {
apiKey: string;
/**
* Base URL that /search and /images/search are appended to.
* Defaults to DEFAULT_BING_ENDPOINT.
*/
endpoint?: string;
maxRetryAttempts?: number;
retryPauseMs?: number;
};

export const DEFAULT_BING_ENDPOINT = "https://api.bing.microsoft.com/v7.0";

export enum EnvVars {
BING_API_KEY = "BING_API_KEY",
BING_ENDPOINT = "BING_ENDPOINT",
}

export function apiSettingsFromEnv(
env?: Record<string, string | undefined>,
): ApiSettings {
env ??= process.env;
const endpoint = env[EnvVars.BING_ENDPOINT]?.trim();
return {
apiKey: getEnvSetting(env, EnvVars.BING_API_KEY),
...(endpoint ? { endpoint } : {}),
};
}

Expand Down Expand Up @@ -138,7 +148,10 @@ export async function createBingSearch(
return error(`Could not create Bing Client:\n${e}`);
}

const baseUrl = "https://api.bing.microsoft.com/v7.0";
const baseUrl = (settings.endpoint || DEFAULT_BING_ENDPOINT).replace(
/\/+$/,
"",
);
const webEndpoint = baseUrl + "/search";
const imageEndpoint = baseUrl + "/images/search";
const headers = createApiHeaders(settings);
Expand Down
90 changes: 90 additions & 0 deletions ts/packages/aiclient/test/bing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { jest } from "@jest/globals";
import {
apiSettingsFromEnv,
createBingSearch,
DEFAULT_BING_ENDPOINT,
} from "../src/bing.js";

describe("bing endpoint", () => {
const originalFetch = globalThis.fetch;
let requested: string[];

beforeEach(() => {
requested = [];
globalThis.fetch = (async (input: unknown) => {
requested.push(String(input));
return new Response(
JSON.stringify({
webPages: {
value: [
{
name: "n",
url: "https://example.com",
snippet: "s",
},
],
},
value: [],
}),
{
status: 200,
headers: { "content-type": "application/json" },
},
);
}) as typeof globalThis.fetch;
jest.spyOn(console, "warn").mockImplementation(() => {});
jest.spyOn(console, "error").mockImplementation(() => {});
});

afterEach(() => {
globalThis.fetch = originalFetch;
jest.restoreAllMocks();
});

test("apiSettingsFromEnv leaves the endpoint unset by default", () => {
const settings = apiSettingsFromEnv({ BING_API_KEY: "key" });
expect(settings.endpoint).toBeUndefined();
expect(DEFAULT_BING_ENDPOINT).toBe(
"https://api.bing.microsoft.com/v7.0",
);
});

test("apiSettingsFromEnv reads BING_ENDPOINT", () => {
const settings = apiSettingsFromEnv({
BING_API_KEY: "key",
BING_ENDPOINT: " http://127.0.0.1:8080/v7.0 ",
});
expect(settings.endpoint).toBe("http://127.0.0.1:8080/v7.0");
});

test("requests go to Bing without an endpoint", async () => {
const client = await createBingSearch({ apiKey: "key" });
expect(client.success).toBe(true);
if (!client.success) return;
const pages = await client.data.webSearch("typeagent");
await client.data.imageSearch("typeagent");
expect(pages.success).toBe(true);
expect(requested).toEqual([
"https://api.bing.microsoft.com/v7.0/search?q=typeagent",
"https://api.bing.microsoft.com/v7.0/images/search?q=typeagent",
]);
});

test("requests go to the configured endpoint", async () => {
const client = await createBingSearch({
apiKey: "key",
endpoint: "http://127.0.0.1:8080/v7.0/",
});
expect(client.success).toBe(true);
if (!client.success) return;
await client.data.webSearch("typeagent", { count: 3 });
await client.data.imageSearch("typeagent");
expect(requested).toEqual([
"http://127.0.0.1:8080/v7.0/search?q=typeagent&count=3",
"http://127.0.0.1:8080/v7.0/images/search?q=typeagent",
]);
});
});
1 change: 1 addition & 0 deletions ts/packages/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ YAML maps are flattened into the `EnvVars` flat-key shape used by
| `azure.openai.deployments[].endpoint` (suffix=foo) | `AZURE_OPENAI_ENDPOINT_FOO` |
| `openai.api_key` | `OPENAI_API_KEY` |
| `bing.api_key` | `BING_API_KEY` |
| `bing.endpoint` | `BING_ENDPOINT` |
| `extra.<KEY>` | `<KEY>` (passthrough for unknown keys) |

See [src/flatten.ts](./src/flatten.ts) for the full mapping.
Expand Down
Loading