-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
llms.ts
65 lines (57 loc) · 1.97 KB
/
llms.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { type ClientOptions } from "openai";
import { type BaseLLMParams } from "@langchain/core/language_models/llms";
import { OpenAI } from "../llms.js";
import type {
OpenAIInput,
AzureOpenAIInput,
LegacyOpenAIInput,
} from "../types.js";
export class AzureOpenAI extends OpenAI {
get lc_aliases(): Record<string, string> {
return {
openAIApiKey: "openai_api_key",
openAIApiVersion: "openai_api_version",
openAIBasePath: "openai_api_base",
};
}
constructor(
fields?: Partial<OpenAIInput> & {
openAIApiKey?: string;
openAIApiVersion?: string;
openAIBasePath?: string;
deploymentName?: string;
} & Partial<AzureOpenAIInput> &
BaseLLMParams & {
configuration?: ClientOptions & LegacyOpenAIInput;
}
) {
// assume the base URL does not contain "openai" nor "deployments" prefix
let basePath = fields?.openAIBasePath ?? "";
if (!basePath.endsWith("/")) basePath += "/";
if (!basePath.endsWith("openai/deployments"))
basePath += "openai/deployments";
const newFields = fields ? { ...fields } : fields;
if (newFields) {
newFields.azureOpenAIBasePath = basePath;
newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
newFields.azureOpenAIApiKey = newFields.openAIApiKey;
newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
}
super(newFields);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
toJSON(): any {
const json = super.toJSON() as unknown;
function isRecord(obj: unknown): obj is Record<string, unknown> {
return typeof obj === "object" && obj != null;
}
if (isRecord(json) && isRecord(json.kwargs)) {
delete json.kwargs.azure_openai_base_path;
delete json.kwargs.azure_openai_api_deployment_name;
delete json.kwargs.azure_openai_api_key;
delete json.kwargs.azure_openai_api_version;
delete json.kwargs.azure_open_ai_base_path;
}
return json;
}
}