-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
llms.ts
165 lines (133 loc) Β· 4.22 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { LLM, type BaseLLMParams } from "@langchain/core/language_models/llms";
const apiUrl =
"https://llm.api.cloud.yandex.net/foundationModels/v1/completion";
export interface YandexGPTInputs extends BaseLLMParams {
/**
* What sampling temperature to use.
* Should be a double number between 0 (inclusive) and 1 (inclusive).
*/
temperature?: number;
/**
* Maximum limit on the total number of tokens
* used for both the input prompt and the generated response.
*/
maxTokens?: number;
/** Model name to use. */
model?: string;
/** Model version to use. */
modelVersion?: string;
/** Model URI to use. */
modelURI?: string;
/**
* Yandex Cloud Folder ID
*/
folderID?: string;
/**
* Yandex Cloud Api Key for service account
* with the `ai.languageModels.user` role.
*/
apiKey?: string;
/**
* Yandex Cloud IAM token for service or user account
* with the `ai.languageModels.user` role.
*/
iamToken?: string;
}
export class YandexGPT extends LLM implements YandexGPTInputs {
lc_serializable = true;
static lc_name() {
return "YandexGPT";
}
get lc_secrets(): { [key: string]: string } | undefined {
return {
apiKey: "YC_API_KEY",
iamToken: "YC_IAM_TOKEN",
folderID: "YC_FOLDER_ID",
};
}
temperature = 0.6;
maxTokens = 1700;
model = "yandexgpt-lite";
modelVersion = "latest";
modelURI?: string;
apiKey?: string;
iamToken?: string;
folderID?: string;
constructor(fields?: YandexGPTInputs) {
super(fields ?? {});
const apiKey = fields?.apiKey ?? getEnvironmentVariable("YC_API_KEY");
const iamToken = fields?.iamToken ?? getEnvironmentVariable("YC_IAM_TOKEN");
const folderID = fields?.folderID ?? getEnvironmentVariable("YC_FOLDER_ID");
if (apiKey === undefined && iamToken === undefined) {
throw new Error(
"Please set the YC_API_KEY or YC_IAM_TOKEN environment variable or pass it to the constructor as the apiKey or iamToken field."
);
}
this.modelURI = fields?.modelURI;
this.apiKey = apiKey;
this.iamToken = iamToken;
this.folderID = folderID;
this.maxTokens = fields?.maxTokens ?? this.maxTokens;
this.temperature = fields?.temperature ?? this.temperature;
this.model = fields?.model ?? this.model;
this.modelVersion = fields?.modelVersion ?? this.modelVersion;
if (this.modelURI === undefined && folderID === undefined) {
throw new Error(
"Please set the YC_FOLDER_ID environment variable or pass Yandex GPT model URI to the constructor as the modelURI field."
);
}
if (!this.modelURI) {
this.modelURI = `gpt://${this.folderID}/${this.model}/${this.modelVersion}`;
}
}
_llmType() {
return "yandexgpt";
}
/** @ignore */
async _call(
prompt: string,
options: this["ParsedCallOptions"]
): Promise<string> {
// Hit the `generate` endpoint on the `large` model
return this.caller.callWithOptions({ signal: options.signal }, async () => {
const headers = {
"Content-Type": "application/json",
Authorization: "",
"x-folder-id": "",
};
if (this.apiKey !== undefined) {
headers.Authorization = `Api-Key ${this.apiKey}`;
} else {
headers.Authorization = `Bearer ${this.iamToken}`;
if (this.folderID !== undefined) {
headers["x-folder-id"] = this.folderID;
}
}
const bodyData = {
modelUri: this.modelURI,
completionOptions: {
temperature: this.temperature,
maxTokens: this.maxTokens,
},
messages: [{ role: "user", text: prompt }],
};
try {
const response = await fetch(apiUrl, {
method: "POST",
headers,
body: JSON.stringify(bodyData),
});
if (!response.ok) {
throw new Error(
`Failed to fetch ${apiUrl} from YandexGPT: ${response.status}`
);
}
const responseData = await response.json();
return responseData.result.alternatives[0].message.text;
} catch (error) {
throw new Error(`Failed to fetch ${apiUrl} from YandexGPT ${error}`);
}
});
}
}