-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Expand file tree
/
Copy pathazure-openai.ts
More file actions
66 lines (57 loc) · 2.02 KB
/
Copy pathazure-openai.ts
File metadata and controls
66 lines (57 loc) · 2.02 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { DefaultExecutor } from "./default.ts";
import type { ProviderCredentials } from "./base.ts";
import { stripTrailingSlashes } from "../utils/urlSanitize.ts";
import { applyAzureParamRules } from "./azureParamRules.ts";
const DEFAULT_API_VERSION = "2024-12-01-preview";
function normalizeAzureBaseUrl(rawBaseUrl?: string | null): string {
const normalized = stripTrailingSlashes((rawBaseUrl || "").trim());
if (!normalized) return "";
return normalized
.replace(/\/openai$/i, "")
.replace(/\/openai\/deployments\/[^/]+\/chat\/completions[^/]*$/i, "");
}
export class AzureOpenAIExecutor extends DefaultExecutor {
constructor() {
super("azure-openai");
}
buildUrl(
model: string,
stream: boolean,
urlIndex = 0,
credentials: ProviderCredentials | null = null
) {
void urlIndex;
const providerSpecificData = credentials?.providerSpecificData || {};
const baseUrl = normalizeAzureBaseUrl(
typeof providerSpecificData.baseUrl === "string"
? providerSpecificData.baseUrl
: this.config.baseUrl
);
const apiVersion =
typeof providerSpecificData.apiVersion === "string" && providerSpecificData.apiVersion.trim()
? providerSpecificData.apiVersion.trim()
: DEFAULT_API_VERSION;
return `${baseUrl}/openai/deployments/${encodeURIComponent(model)}/chat/completions?api-version=${encodeURIComponent(apiVersion)}`;
}
buildHeaders(credentials: ProviderCredentials | null, stream = true) {
const apiKey = credentials?.apiKey || credentials?.accessToken || "";
const headers: Record<string, string> = {
"Content-Type": "application/json",
"api-key": apiKey,
};
headers.Accept = stream ? "text/event-stream" : "application/json";
return headers;
}
override transformRequest(
model: string,
body: unknown,
stream: boolean,
credentials: ProviderCredentials
): unknown {
return applyAzureParamRules(
model,
body,
super.transformRequest(model, body, stream, credentials)
);
}
}