-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
openAIClient.ts
47 lines (43 loc) · 1.61 KB
/
openAIClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger";
import { TokenCredential, KeyCredential } from "@azure/core-auth";
import { OpenAIClient } from "./clientDefinitions";
/**
* Initialize a new instance of `OpenAIClient`
* @param endpoint - Supported Cognitive Services endpoints (protocol and hostname, for example:
* https://westus.api.cognitive.microsoft.com).
* @param credentials - uniquely identify client credential
* @param options - the parameter for all optional parameters
*/
export default function createClient(
endpoint: string,
credentials: TokenCredential | KeyCredential,
options: ClientOptions = {},
): OpenAIClient {
const baseUrl = options.baseUrl ?? `${endpoint}/openai`;
options.apiVersion = options.apiVersion ?? "2024-02-15-preview";
const userAgentInfo = `azsdk-js-openai-rest/1.0.0-beta.1`;
const userAgentPrefix =
options.userAgentOptions && options.userAgentOptions.userAgentPrefix
? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}`
: `${userAgentInfo}`;
options = {
...options,
userAgentOptions: {
userAgentPrefix,
},
loggingOptions: {
logger: options.loggingOptions?.logger ?? logger.info,
},
credentials: {
scopes: options.credentials?.scopes ?? [
"https://cognitiveservices.azure.com/.default",
],
apiKeyHeaderName: options.credentials?.apiKeyHeaderName ?? "api-key",
},
};
const client = getClient(baseUrl, credentials, options) as OpenAIClient;
return client;
}