langchain 0.0.197-rc.1 → 0.0.198
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chains/openai_moderation.cjs +2 -2
- package/dist/chains/openai_moderation.d.ts +1 -1
- package/dist/chains/openai_moderation.js +1 -1
- package/dist/chat_models/anthropic.cjs +351 -15
- package/dist/chat_models/anthropic.d.ts +157 -1
- package/dist/chat_models/anthropic.js +348 -1
- package/dist/chat_models/cloudflare_workersai.cjs +5 -0
- package/dist/chat_models/cloudflare_workersai.d.ts +3 -0
- package/dist/chat_models/cloudflare_workersai.js +5 -0
- package/dist/chat_models/fireworks.d.ts +1 -1
- package/dist/chat_models/iflytek_xinghuo/common.d.ts +1 -1
- package/dist/chat_models/llama_cpp.cjs +24 -0
- package/dist/chat_models/llama_cpp.d.ts +3 -1
- package/dist/chat_models/llama_cpp.js +24 -0
- package/dist/chat_models/minimax.d.ts +1 -1
- package/dist/chat_models/openai.cjs +698 -4
- package/dist/chat_models/openai.d.ts +137 -4
- package/dist/chat_models/openai.js +695 -2
- package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
- package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
- package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
- package/dist/document_loaders/fs/pptx.cjs +39 -0
- package/dist/document_loaders/fs/pptx.d.ts +23 -0
- package/dist/document_loaders/fs/pptx.js +35 -0
- package/dist/embeddings/openai.cjs +240 -2
- package/dist/embeddings/openai.d.ts +82 -1
- package/dist/embeddings/openai.js +239 -1
- package/dist/experimental/openai_assistant/index.cjs +35 -3
- package/dist/experimental/openai_assistant/index.d.ts +27 -1
- package/dist/experimental/openai_assistant/index.js +33 -1
- package/dist/experimental/openai_assistant/schema.d.ts +1 -1
- package/dist/experimental/openai_files/index.cjs +2 -2
- package/dist/experimental/openai_files/index.d.ts +1 -1
- package/dist/experimental/openai_files/index.js +1 -1
- package/dist/experimental/tools/pyinterpreter.cjs +248 -0
- package/dist/experimental/tools/pyinterpreter.d.ts +18 -0
- package/dist/experimental/tools/pyinterpreter.js +244 -0
- package/dist/graphs/neo4j_graph.cjs +49 -14
- package/dist/graphs/neo4j_graph.d.ts +30 -0
- package/dist/graphs/neo4j_graph.js +49 -14
- package/dist/llms/fireworks.d.ts +1 -1
- package/dist/llms/hf.cjs +13 -2
- package/dist/llms/hf.d.ts +5 -0
- package/dist/llms/hf.js +13 -2
- package/dist/llms/llama_cpp.cjs +17 -3
- package/dist/llms/llama_cpp.d.ts +4 -1
- package/dist/llms/llama_cpp.js +17 -3
- package/dist/llms/openai-chat.cjs +445 -3
- package/dist/llms/openai-chat.d.ts +123 -4
- package/dist/llms/openai-chat.js +443 -2
- package/dist/llms/openai.cjs +530 -6
- package/dist/llms/openai.d.ts +123 -4
- package/dist/llms/openai.js +525 -2
- package/dist/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/output_parsers/json.cjs +4 -0
- package/dist/output_parsers/json.js +4 -0
- package/dist/schema/index.d.ts +1 -1
- package/dist/tools/convert_to_openai.cjs +38 -4
- package/dist/tools/convert_to_openai.d.ts +11 -1
- package/dist/tools/convert_to_openai.js +35 -1
- package/dist/types/openai-types.d.ts +133 -1
- package/dist/util/env.cjs +9 -70
- package/dist/util/env.d.ts +1 -21
- package/dist/util/env.js +1 -62
- package/dist/util/openai-format-fndef.cjs +81 -0
- package/dist/util/openai-format-fndef.d.ts +44 -0
- package/dist/util/openai-format-fndef.js +77 -0
- package/dist/util/openai.cjs +18 -2
- package/dist/util/openai.d.ts +1 -1
- package/dist/util/openai.js +17 -1
- package/dist/util/openapi.d.ts +2 -2
- package/dist/util/prompt-layer.d.ts +1 -1
- package/dist/vectorstores/clickhouse.cjs +286 -0
- package/dist/vectorstores/clickhouse.d.ts +126 -0
- package/dist/vectorstores/clickhouse.js +259 -0
- package/dist/vectorstores/pgvector.cjs +142 -18
- package/dist/vectorstores/pgvector.d.ts +21 -0
- package/dist/vectorstores/pgvector.js +142 -18
- package/dist/vectorstores/weaviate.cjs +45 -2
- package/dist/vectorstores/weaviate.d.ts +27 -1
- package/dist/vectorstores/weaviate.js +45 -2
- package/document_loaders/fs/pptx.cjs +1 -0
- package/document_loaders/fs/pptx.d.ts +1 -0
- package/document_loaders/fs/pptx.js +1 -0
- package/experimental/tools/pyinterpreter.cjs +1 -0
- package/experimental/tools/pyinterpreter.d.ts +1 -0
- package/experimental/tools/pyinterpreter.js +1 -0
- package/package.json +41 -9
- package/vectorstores/clickhouse.cjs +1 -0
- package/vectorstores/clickhouse.d.ts +1 -0
- package/vectorstores/clickhouse.js +1 -0
|
@@ -1,6 +1,699 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OpenAI as OpenAIClient } from "openai";
|
|
2
|
+
import { AIMessage, AIMessageChunk, ChatGenerationChunk, ChatMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk, } from "../schema/index.js";
|
|
3
|
+
import { formatToOpenAITool } from "../tools/convert_to_openai.js";
|
|
4
|
+
import { getEndpoint } from "../util/azure.js";
|
|
5
|
+
import { getEnvironmentVariable } from "../util/env.js";
|
|
2
6
|
import { promptLayerTrackRequest } from "../util/prompt-layer.js";
|
|
3
|
-
|
|
7
|
+
import { BaseChatModel } from "./base.js";
|
|
8
|
+
import { wrapOpenAIClientError } from "../util/openai.js";
|
|
9
|
+
import { formatFunctionDefinitions, } from "../util/openai-format-fndef.js";
|
|
10
|
+
function extractGenericMessageCustomRole(message) {
|
|
11
|
+
if (message.role !== "system" &&
|
|
12
|
+
message.role !== "assistant" &&
|
|
13
|
+
message.role !== "user" &&
|
|
14
|
+
message.role !== "function" &&
|
|
15
|
+
message.role !== "tool") {
|
|
16
|
+
console.warn(`Unknown message role: ${message.role}`);
|
|
17
|
+
}
|
|
18
|
+
return message.role;
|
|
19
|
+
}
|
|
20
|
+
function messageToOpenAIRole(message) {
|
|
21
|
+
const type = message._getType();
|
|
22
|
+
switch (type) {
|
|
23
|
+
case "system":
|
|
24
|
+
return "system";
|
|
25
|
+
case "ai":
|
|
26
|
+
return "assistant";
|
|
27
|
+
case "human":
|
|
28
|
+
return "user";
|
|
29
|
+
case "function":
|
|
30
|
+
return "function";
|
|
31
|
+
case "tool":
|
|
32
|
+
return "tool";
|
|
33
|
+
case "generic": {
|
|
34
|
+
if (!ChatMessage.isInstance(message))
|
|
35
|
+
throw new Error("Invalid generic chat message");
|
|
36
|
+
return extractGenericMessageCustomRole(message);
|
|
37
|
+
}
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`Unknown message type: ${type}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function openAIResponseToChatMessage(message) {
|
|
43
|
+
switch (message.role) {
|
|
44
|
+
case "assistant":
|
|
45
|
+
return new AIMessage(message.content || "", {
|
|
46
|
+
function_call: message.function_call,
|
|
47
|
+
tool_calls: message.tool_calls,
|
|
48
|
+
});
|
|
49
|
+
default:
|
|
50
|
+
return new ChatMessage(message.content || "", message.role ?? "unknown");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function _convertDeltaToMessageChunk(
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
+
delta, defaultRole) {
|
|
56
|
+
const role = delta.role ?? defaultRole;
|
|
57
|
+
const content = delta.content ?? "";
|
|
58
|
+
let additional_kwargs;
|
|
59
|
+
if (delta.function_call) {
|
|
60
|
+
additional_kwargs = {
|
|
61
|
+
function_call: delta.function_call,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
else if (delta.tool_calls) {
|
|
65
|
+
additional_kwargs = {
|
|
66
|
+
tool_calls: delta.tool_calls,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
additional_kwargs = {};
|
|
71
|
+
}
|
|
72
|
+
if (role === "user") {
|
|
73
|
+
return new HumanMessageChunk({ content });
|
|
74
|
+
}
|
|
75
|
+
else if (role === "assistant") {
|
|
76
|
+
return new AIMessageChunk({ content, additional_kwargs });
|
|
77
|
+
}
|
|
78
|
+
else if (role === "system") {
|
|
79
|
+
return new SystemMessageChunk({ content });
|
|
80
|
+
}
|
|
81
|
+
else if (role === "function") {
|
|
82
|
+
return new FunctionMessageChunk({
|
|
83
|
+
content,
|
|
84
|
+
additional_kwargs,
|
|
85
|
+
name: delta.name,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else if (role === "tool") {
|
|
89
|
+
return new ToolMessageChunk({
|
|
90
|
+
content,
|
|
91
|
+
additional_kwargs,
|
|
92
|
+
tool_call_id: delta.tool_call_id,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
return new ChatMessageChunk({ content, role });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function convertMessagesToOpenAIParams(messages) {
|
|
100
|
+
// TODO: Function messages do not support array content, fix cast
|
|
101
|
+
return messages.map((message) => ({
|
|
102
|
+
role: messageToOpenAIRole(message),
|
|
103
|
+
content: message.content,
|
|
104
|
+
name: message.name,
|
|
105
|
+
function_call: message.additional_kwargs.function_call,
|
|
106
|
+
tool_calls: message.additional_kwargs.tool_calls,
|
|
107
|
+
tool_call_id: message.tool_call_id,
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Wrapper around OpenAI large language models that use the Chat endpoint.
|
|
112
|
+
*
|
|
113
|
+
* To use you should have the `openai` package installed, with the
|
|
114
|
+
* `OPENAI_API_KEY` environment variable set.
|
|
115
|
+
*
|
|
116
|
+
* To use with Azure you should have the `openai` package installed, with the
|
|
117
|
+
* `AZURE_OPENAI_API_KEY`,
|
|
118
|
+
* `AZURE_OPENAI_API_INSTANCE_NAME`,
|
|
119
|
+
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
120
|
+
* and `AZURE_OPENAI_API_VERSION` environment variable set.
|
|
121
|
+
* `AZURE_OPENAI_BASE_PATH` is optional and will override `AZURE_OPENAI_API_INSTANCE_NAME` if you need to use a custom endpoint.
|
|
122
|
+
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* Any parameters that are valid to be passed to {@link
|
|
125
|
+
* https://platform.openai.com/docs/api-reference/chat/create |
|
|
126
|
+
* `openai.createChatCompletion`} can be passed through {@link modelKwargs}, even
|
|
127
|
+
* if not explicitly available on this class.
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* // Create a new instance of ChatOpenAI with specific temperature and model name settings
|
|
131
|
+
* const model = new ChatOpenAI({
|
|
132
|
+
* temperature: 0.9,
|
|
133
|
+
* modelName: "ft:gpt-3.5-turbo-0613:{ORG_NAME}::{MODEL_ID}",
|
|
134
|
+
* });
|
|
135
|
+
*
|
|
136
|
+
* // Invoke the model with a message and await the response
|
|
137
|
+
* const message = await model.invoke("Hi there!");
|
|
138
|
+
*
|
|
139
|
+
* // Log the response to the console
|
|
140
|
+
* console.log(message);
|
|
141
|
+
*
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export class ChatOpenAI extends BaseChatModel {
|
|
145
|
+
static lc_name() {
|
|
146
|
+
return "ChatOpenAI";
|
|
147
|
+
}
|
|
148
|
+
get callKeys() {
|
|
149
|
+
return [
|
|
150
|
+
...super.callKeys,
|
|
151
|
+
"options",
|
|
152
|
+
"function_call",
|
|
153
|
+
"functions",
|
|
154
|
+
"tools",
|
|
155
|
+
"tool_choice",
|
|
156
|
+
"promptIndex",
|
|
157
|
+
"response_format",
|
|
158
|
+
"seed",
|
|
159
|
+
];
|
|
160
|
+
}
|
|
161
|
+
get lc_secrets() {
|
|
162
|
+
return {
|
|
163
|
+
openAIApiKey: "OPENAI_API_KEY",
|
|
164
|
+
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
|
|
165
|
+
organization: "OPENAI_ORGANIZATION",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
get lc_aliases() {
|
|
169
|
+
return {
|
|
170
|
+
modelName: "model",
|
|
171
|
+
openAIApiKey: "openai_api_key",
|
|
172
|
+
azureOpenAIApiVersion: "azure_openai_api_version",
|
|
173
|
+
azureOpenAIApiKey: "azure_openai_api_key",
|
|
174
|
+
azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
|
|
175
|
+
azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
constructor(fields,
|
|
179
|
+
/** @deprecated */
|
|
180
|
+
configuration) {
|
|
181
|
+
super(fields ?? {});
|
|
182
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
183
|
+
enumerable: true,
|
|
184
|
+
configurable: true,
|
|
185
|
+
writable: true,
|
|
186
|
+
value: true
|
|
187
|
+
});
|
|
188
|
+
Object.defineProperty(this, "temperature", {
|
|
189
|
+
enumerable: true,
|
|
190
|
+
configurable: true,
|
|
191
|
+
writable: true,
|
|
192
|
+
value: 1
|
|
193
|
+
});
|
|
194
|
+
Object.defineProperty(this, "topP", {
|
|
195
|
+
enumerable: true,
|
|
196
|
+
configurable: true,
|
|
197
|
+
writable: true,
|
|
198
|
+
value: 1
|
|
199
|
+
});
|
|
200
|
+
Object.defineProperty(this, "frequencyPenalty", {
|
|
201
|
+
enumerable: true,
|
|
202
|
+
configurable: true,
|
|
203
|
+
writable: true,
|
|
204
|
+
value: 0
|
|
205
|
+
});
|
|
206
|
+
Object.defineProperty(this, "presencePenalty", {
|
|
207
|
+
enumerable: true,
|
|
208
|
+
configurable: true,
|
|
209
|
+
writable: true,
|
|
210
|
+
value: 0
|
|
211
|
+
});
|
|
212
|
+
Object.defineProperty(this, "n", {
|
|
213
|
+
enumerable: true,
|
|
214
|
+
configurable: true,
|
|
215
|
+
writable: true,
|
|
216
|
+
value: 1
|
|
217
|
+
});
|
|
218
|
+
Object.defineProperty(this, "logitBias", {
|
|
219
|
+
enumerable: true,
|
|
220
|
+
configurable: true,
|
|
221
|
+
writable: true,
|
|
222
|
+
value: void 0
|
|
223
|
+
});
|
|
224
|
+
Object.defineProperty(this, "modelName", {
|
|
225
|
+
enumerable: true,
|
|
226
|
+
configurable: true,
|
|
227
|
+
writable: true,
|
|
228
|
+
value: "gpt-3.5-turbo"
|
|
229
|
+
});
|
|
230
|
+
Object.defineProperty(this, "modelKwargs", {
|
|
231
|
+
enumerable: true,
|
|
232
|
+
configurable: true,
|
|
233
|
+
writable: true,
|
|
234
|
+
value: void 0
|
|
235
|
+
});
|
|
236
|
+
Object.defineProperty(this, "stop", {
|
|
237
|
+
enumerable: true,
|
|
238
|
+
configurable: true,
|
|
239
|
+
writable: true,
|
|
240
|
+
value: void 0
|
|
241
|
+
});
|
|
242
|
+
Object.defineProperty(this, "user", {
|
|
243
|
+
enumerable: true,
|
|
244
|
+
configurable: true,
|
|
245
|
+
writable: true,
|
|
246
|
+
value: void 0
|
|
247
|
+
});
|
|
248
|
+
Object.defineProperty(this, "timeout", {
|
|
249
|
+
enumerable: true,
|
|
250
|
+
configurable: true,
|
|
251
|
+
writable: true,
|
|
252
|
+
value: void 0
|
|
253
|
+
});
|
|
254
|
+
Object.defineProperty(this, "streaming", {
|
|
255
|
+
enumerable: true,
|
|
256
|
+
configurable: true,
|
|
257
|
+
writable: true,
|
|
258
|
+
value: false
|
|
259
|
+
});
|
|
260
|
+
Object.defineProperty(this, "maxTokens", {
|
|
261
|
+
enumerable: true,
|
|
262
|
+
configurable: true,
|
|
263
|
+
writable: true,
|
|
264
|
+
value: void 0
|
|
265
|
+
});
|
|
266
|
+
Object.defineProperty(this, "openAIApiKey", {
|
|
267
|
+
enumerable: true,
|
|
268
|
+
configurable: true,
|
|
269
|
+
writable: true,
|
|
270
|
+
value: void 0
|
|
271
|
+
});
|
|
272
|
+
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
273
|
+
enumerable: true,
|
|
274
|
+
configurable: true,
|
|
275
|
+
writable: true,
|
|
276
|
+
value: void 0
|
|
277
|
+
});
|
|
278
|
+
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
279
|
+
enumerable: true,
|
|
280
|
+
configurable: true,
|
|
281
|
+
writable: true,
|
|
282
|
+
value: void 0
|
|
283
|
+
});
|
|
284
|
+
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
285
|
+
enumerable: true,
|
|
286
|
+
configurable: true,
|
|
287
|
+
writable: true,
|
|
288
|
+
value: void 0
|
|
289
|
+
});
|
|
290
|
+
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
291
|
+
enumerable: true,
|
|
292
|
+
configurable: true,
|
|
293
|
+
writable: true,
|
|
294
|
+
value: void 0
|
|
295
|
+
});
|
|
296
|
+
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
297
|
+
enumerable: true,
|
|
298
|
+
configurable: true,
|
|
299
|
+
writable: true,
|
|
300
|
+
value: void 0
|
|
301
|
+
});
|
|
302
|
+
Object.defineProperty(this, "organization", {
|
|
303
|
+
enumerable: true,
|
|
304
|
+
configurable: true,
|
|
305
|
+
writable: true,
|
|
306
|
+
value: void 0
|
|
307
|
+
});
|
|
308
|
+
Object.defineProperty(this, "client", {
|
|
309
|
+
enumerable: true,
|
|
310
|
+
configurable: true,
|
|
311
|
+
writable: true,
|
|
312
|
+
value: void 0
|
|
313
|
+
});
|
|
314
|
+
Object.defineProperty(this, "clientConfig", {
|
|
315
|
+
enumerable: true,
|
|
316
|
+
configurable: true,
|
|
317
|
+
writable: true,
|
|
318
|
+
value: void 0
|
|
319
|
+
});
|
|
320
|
+
this.openAIApiKey =
|
|
321
|
+
fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY");
|
|
322
|
+
this.azureOpenAIApiKey =
|
|
323
|
+
fields?.azureOpenAIApiKey ??
|
|
324
|
+
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
|
|
325
|
+
if (!this.azureOpenAIApiKey && !this.openAIApiKey) {
|
|
326
|
+
throw new Error("OpenAI or Azure OpenAI API key not found");
|
|
327
|
+
}
|
|
328
|
+
this.azureOpenAIApiInstanceName =
|
|
329
|
+
fields?.azureOpenAIApiInstanceName ??
|
|
330
|
+
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
331
|
+
this.azureOpenAIApiDeploymentName =
|
|
332
|
+
fields?.azureOpenAIApiDeploymentName ??
|
|
333
|
+
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME");
|
|
334
|
+
this.azureOpenAIApiVersion =
|
|
335
|
+
fields?.azureOpenAIApiVersion ??
|
|
336
|
+
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
|
|
337
|
+
this.azureOpenAIBasePath =
|
|
338
|
+
fields?.azureOpenAIBasePath ??
|
|
339
|
+
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
|
|
340
|
+
this.organization =
|
|
341
|
+
fields?.configuration?.organization ??
|
|
342
|
+
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
343
|
+
this.modelName = fields?.modelName ?? this.modelName;
|
|
344
|
+
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
345
|
+
this.timeout = fields?.timeout;
|
|
346
|
+
this.temperature = fields?.temperature ?? this.temperature;
|
|
347
|
+
this.topP = fields?.topP ?? this.topP;
|
|
348
|
+
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
|
|
349
|
+
this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
|
|
350
|
+
this.maxTokens = fields?.maxTokens;
|
|
351
|
+
this.n = fields?.n ?? this.n;
|
|
352
|
+
this.logitBias = fields?.logitBias;
|
|
353
|
+
this.stop = fields?.stop;
|
|
354
|
+
this.user = fields?.user;
|
|
355
|
+
this.streaming = fields?.streaming ?? false;
|
|
356
|
+
if (this.azureOpenAIApiKey) {
|
|
357
|
+
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
358
|
+
throw new Error("Azure OpenAI API instance name not found");
|
|
359
|
+
}
|
|
360
|
+
if (!this.azureOpenAIApiDeploymentName) {
|
|
361
|
+
throw new Error("Azure OpenAI API deployment name not found");
|
|
362
|
+
}
|
|
363
|
+
if (!this.azureOpenAIApiVersion) {
|
|
364
|
+
throw new Error("Azure OpenAI API version not found");
|
|
365
|
+
}
|
|
366
|
+
this.openAIApiKey = this.openAIApiKey ?? "";
|
|
367
|
+
}
|
|
368
|
+
this.clientConfig = {
|
|
369
|
+
apiKey: this.openAIApiKey,
|
|
370
|
+
organization: this.organization,
|
|
371
|
+
baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
|
|
372
|
+
dangerouslyAllowBrowser: true,
|
|
373
|
+
defaultHeaders: configuration?.baseOptions?.headers ??
|
|
374
|
+
fields?.configuration?.baseOptions?.headers,
|
|
375
|
+
defaultQuery: configuration?.baseOptions?.params ??
|
|
376
|
+
fields?.configuration?.baseOptions?.params,
|
|
377
|
+
...configuration,
|
|
378
|
+
...fields?.configuration,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Get the parameters used to invoke the model
|
|
383
|
+
*/
|
|
384
|
+
invocationParams(options) {
|
|
385
|
+
function isStructuredToolArray(tools) {
|
|
386
|
+
return (tools !== undefined &&
|
|
387
|
+
tools.every((tool) => Array.isArray(tool.lc_namespace)));
|
|
388
|
+
}
|
|
389
|
+
const params = {
|
|
390
|
+
model: this.modelName,
|
|
391
|
+
temperature: this.temperature,
|
|
392
|
+
top_p: this.topP,
|
|
393
|
+
frequency_penalty: this.frequencyPenalty,
|
|
394
|
+
presence_penalty: this.presencePenalty,
|
|
395
|
+
max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
|
|
396
|
+
n: this.n,
|
|
397
|
+
logit_bias: this.logitBias,
|
|
398
|
+
stop: options?.stop ?? this.stop,
|
|
399
|
+
user: this.user,
|
|
400
|
+
stream: this.streaming,
|
|
401
|
+
functions: options?.functions,
|
|
402
|
+
function_call: options?.function_call,
|
|
403
|
+
tools: isStructuredToolArray(options?.tools)
|
|
404
|
+
? options?.tools.map(formatToOpenAITool)
|
|
405
|
+
: options?.tools,
|
|
406
|
+
tool_choice: options?.tool_choice,
|
|
407
|
+
response_format: options?.response_format,
|
|
408
|
+
seed: options?.seed,
|
|
409
|
+
...this.modelKwargs,
|
|
410
|
+
};
|
|
411
|
+
return params;
|
|
412
|
+
}
|
|
413
|
+
/** @ignore */
|
|
414
|
+
_identifyingParams() {
|
|
415
|
+
return {
|
|
416
|
+
model_name: this.modelName,
|
|
417
|
+
...this.invocationParams(),
|
|
418
|
+
...this.clientConfig,
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
422
|
+
const messagesMapped = convertMessagesToOpenAIParams(messages);
|
|
423
|
+
const params = {
|
|
424
|
+
...this.invocationParams(options),
|
|
425
|
+
messages: messagesMapped,
|
|
426
|
+
stream: true,
|
|
427
|
+
};
|
|
428
|
+
let defaultRole;
|
|
429
|
+
const streamIterable = await this.completionWithRetry(params, options);
|
|
430
|
+
for await (const data of streamIterable) {
|
|
431
|
+
const choice = data?.choices[0];
|
|
432
|
+
if (!choice) {
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const { delta } = choice;
|
|
436
|
+
if (!delta) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
const chunk = _convertDeltaToMessageChunk(delta, defaultRole);
|
|
440
|
+
defaultRole = delta.role ?? defaultRole;
|
|
441
|
+
const newTokenIndices = {
|
|
442
|
+
prompt: options.promptIndex ?? 0,
|
|
443
|
+
completion: choice.index ?? 0,
|
|
444
|
+
};
|
|
445
|
+
if (typeof chunk.content !== "string") {
|
|
446
|
+
console.log("[WARNING]: Received non-string content from OpenAI. This is currently not supported.");
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
const generationChunk = new ChatGenerationChunk({
|
|
450
|
+
message: chunk,
|
|
451
|
+
text: chunk.content,
|
|
452
|
+
generationInfo: newTokenIndices,
|
|
453
|
+
});
|
|
454
|
+
yield generationChunk;
|
|
455
|
+
// eslint-disable-next-line no-void
|
|
456
|
+
void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
|
|
457
|
+
}
|
|
458
|
+
if (options.signal?.aborted) {
|
|
459
|
+
throw new Error("AbortError");
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Get the identifying parameters for the model
|
|
464
|
+
*
|
|
465
|
+
*/
|
|
466
|
+
identifyingParams() {
|
|
467
|
+
return this._identifyingParams();
|
|
468
|
+
}
|
|
469
|
+
/** @ignore */
|
|
470
|
+
async _generate(messages, options, runManager) {
|
|
471
|
+
const tokenUsage = {};
|
|
472
|
+
const params = this.invocationParams(options);
|
|
473
|
+
const messagesMapped = convertMessagesToOpenAIParams(messages);
|
|
474
|
+
if (params.stream) {
|
|
475
|
+
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
476
|
+
const finalChunks = {};
|
|
477
|
+
for await (const chunk of stream) {
|
|
478
|
+
const index = chunk.generationInfo?.completion ?? 0;
|
|
479
|
+
if (finalChunks[index] === undefined) {
|
|
480
|
+
finalChunks[index] = chunk;
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
finalChunks[index] = finalChunks[index].concat(chunk);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
const generations = Object.entries(finalChunks)
|
|
487
|
+
.sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10))
|
|
488
|
+
.map(([_, value]) => value);
|
|
489
|
+
const { functions, function_call } = this.invocationParams(options);
|
|
490
|
+
// OpenAI does not support token usage report under stream mode,
|
|
491
|
+
// fallback to estimation.
|
|
492
|
+
const promptTokenUsage = await this.getEstimatedTokenCountFromPrompt(messages, functions, function_call);
|
|
493
|
+
const completionTokenUsage = await this.getNumTokensFromGenerations(generations);
|
|
494
|
+
tokenUsage.promptTokens = promptTokenUsage;
|
|
495
|
+
tokenUsage.completionTokens = completionTokenUsage;
|
|
496
|
+
tokenUsage.totalTokens = promptTokenUsage + completionTokenUsage;
|
|
497
|
+
return { generations, llmOutput: { estimatedTokenUsage: tokenUsage } };
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
const data = await this.completionWithRetry({
|
|
501
|
+
...params,
|
|
502
|
+
stream: false,
|
|
503
|
+
messages: messagesMapped,
|
|
504
|
+
}, {
|
|
505
|
+
signal: options?.signal,
|
|
506
|
+
...options?.options,
|
|
507
|
+
});
|
|
508
|
+
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, } = data?.usage ?? {};
|
|
509
|
+
if (completionTokens) {
|
|
510
|
+
tokenUsage.completionTokens =
|
|
511
|
+
(tokenUsage.completionTokens ?? 0) + completionTokens;
|
|
512
|
+
}
|
|
513
|
+
if (promptTokens) {
|
|
514
|
+
tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
|
|
515
|
+
}
|
|
516
|
+
if (totalTokens) {
|
|
517
|
+
tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
|
|
518
|
+
}
|
|
519
|
+
const generations = [];
|
|
520
|
+
for (const part of data?.choices ?? []) {
|
|
521
|
+
const text = part.message?.content ?? "";
|
|
522
|
+
const generation = {
|
|
523
|
+
text,
|
|
524
|
+
message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }),
|
|
525
|
+
};
|
|
526
|
+
if (part.finish_reason) {
|
|
527
|
+
generation.generationInfo = { finish_reason: part.finish_reason };
|
|
528
|
+
}
|
|
529
|
+
generations.push(generation);
|
|
530
|
+
}
|
|
531
|
+
return {
|
|
532
|
+
generations,
|
|
533
|
+
llmOutput: { tokenUsage },
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Estimate the number of tokens a prompt will use.
|
|
539
|
+
* Modified from: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts
|
|
540
|
+
*/
|
|
541
|
+
async getEstimatedTokenCountFromPrompt(messages, functions, function_call) {
|
|
542
|
+
// It appears that if functions are present, the first system message is padded with a trailing newline. This
|
|
543
|
+
// was inferred by trying lots of combinations of messages and functions and seeing what the token counts were.
|
|
544
|
+
let tokens = (await this.getNumTokensFromMessages(messages)).totalCount;
|
|
545
|
+
// If there are functions, add the function definitions as they count towards token usage
|
|
546
|
+
if (functions && function_call !== "auto") {
|
|
547
|
+
const promptDefinitions = formatFunctionDefinitions(functions);
|
|
548
|
+
tokens += await this.getNumTokens(promptDefinitions);
|
|
549
|
+
tokens += 9; // Add nine per completion
|
|
550
|
+
}
|
|
551
|
+
// If there's a system message _and_ functions are present, subtract four tokens. I assume this is because
|
|
552
|
+
// functions typically add a system message, but reuse the first one if it's already there. This offsets
|
|
553
|
+
// the extra 9 tokens added by the function definitions.
|
|
554
|
+
if (functions && messages.find((m) => m._getType() === "system")) {
|
|
555
|
+
tokens -= 4;
|
|
556
|
+
}
|
|
557
|
+
// If function_call is 'none', add one token.
|
|
558
|
+
// If it's a FunctionCall object, add 4 + the number of tokens in the function name.
|
|
559
|
+
// If it's undefined or 'auto', don't add anything.
|
|
560
|
+
if (function_call === "none") {
|
|
561
|
+
tokens += 1;
|
|
562
|
+
}
|
|
563
|
+
else if (typeof function_call === "object") {
|
|
564
|
+
tokens += (await this.getNumTokens(function_call.name)) + 4;
|
|
565
|
+
}
|
|
566
|
+
return tokens;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Estimate the number of tokens an array of generations have used.
|
|
570
|
+
*/
|
|
571
|
+
async getNumTokensFromGenerations(generations) {
|
|
572
|
+
const generationUsages = await Promise.all(generations.map(async (generation) => {
|
|
573
|
+
if (generation.message.additional_kwargs?.function_call) {
|
|
574
|
+
return (await this.getNumTokensFromMessages([generation.message]))
|
|
575
|
+
.countPerMessage[0];
|
|
576
|
+
}
|
|
577
|
+
else {
|
|
578
|
+
return await this.getNumTokens(generation.message.content);
|
|
579
|
+
}
|
|
580
|
+
}));
|
|
581
|
+
return generationUsages.reduce((a, b) => a + b, 0);
|
|
582
|
+
}
|
|
583
|
+
async getNumTokensFromMessages(messages) {
|
|
584
|
+
let totalCount = 0;
|
|
585
|
+
let tokensPerMessage = 0;
|
|
586
|
+
let tokensPerName = 0;
|
|
587
|
+
// From: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
|
|
588
|
+
if (this.modelName === "gpt-3.5-turbo-0301") {
|
|
589
|
+
tokensPerMessage = 4;
|
|
590
|
+
tokensPerName = -1;
|
|
591
|
+
}
|
|
592
|
+
else {
|
|
593
|
+
tokensPerMessage = 3;
|
|
594
|
+
tokensPerName = 1;
|
|
595
|
+
}
|
|
596
|
+
const countPerMessage = await Promise.all(messages.map(async (message) => {
|
|
597
|
+
const textCount = await this.getNumTokens(message.content);
|
|
598
|
+
const roleCount = await this.getNumTokens(messageToOpenAIRole(message));
|
|
599
|
+
const nameCount = message.name !== undefined
|
|
600
|
+
? tokensPerName + (await this.getNumTokens(message.name))
|
|
601
|
+
: 0;
|
|
602
|
+
let count = textCount + tokensPerMessage + roleCount + nameCount;
|
|
603
|
+
// From: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts messageTokenEstimate
|
|
604
|
+
const openAIMessage = message;
|
|
605
|
+
if (openAIMessage._getType() === "function") {
|
|
606
|
+
count -= 2;
|
|
607
|
+
}
|
|
608
|
+
if (openAIMessage.additional_kwargs?.function_call) {
|
|
609
|
+
count += 3;
|
|
610
|
+
}
|
|
611
|
+
if (openAIMessage?.additional_kwargs.function_call?.name) {
|
|
612
|
+
count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
|
|
613
|
+
}
|
|
614
|
+
if (openAIMessage.additional_kwargs.function_call?.arguments) {
|
|
615
|
+
count += await this.getNumTokens(
|
|
616
|
+
// Remove newlines and spaces
|
|
617
|
+
JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
|
|
618
|
+
}
|
|
619
|
+
totalCount += count;
|
|
620
|
+
return count;
|
|
621
|
+
}));
|
|
622
|
+
totalCount += 3; // every reply is primed with <|start|>assistant<|message|>
|
|
623
|
+
return { totalCount, countPerMessage };
|
|
624
|
+
}
|
|
625
|
+
async completionWithRetry(request, options) {
|
|
626
|
+
const requestOptions = this._getClientOptions(options);
|
|
627
|
+
return this.caller.call(async () => {
|
|
628
|
+
try {
|
|
629
|
+
const res = await this.client.chat.completions.create(request, requestOptions);
|
|
630
|
+
return res;
|
|
631
|
+
}
|
|
632
|
+
catch (e) {
|
|
633
|
+
const error = wrapOpenAIClientError(e);
|
|
634
|
+
throw error;
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
_getClientOptions(options) {
|
|
639
|
+
if (!this.client) {
|
|
640
|
+
const openAIEndpointConfig = {
|
|
641
|
+
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
642
|
+
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
643
|
+
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
644
|
+
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
645
|
+
baseURL: this.clientConfig.baseURL,
|
|
646
|
+
};
|
|
647
|
+
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
648
|
+
const params = {
|
|
649
|
+
...this.clientConfig,
|
|
650
|
+
baseURL: endpoint,
|
|
651
|
+
timeout: this.timeout,
|
|
652
|
+
maxRetries: 0,
|
|
653
|
+
};
|
|
654
|
+
if (!params.baseURL) {
|
|
655
|
+
delete params.baseURL;
|
|
656
|
+
}
|
|
657
|
+
this.client = new OpenAIClient(params);
|
|
658
|
+
}
|
|
659
|
+
const requestOptions = {
|
|
660
|
+
...this.clientConfig,
|
|
661
|
+
...options,
|
|
662
|
+
};
|
|
663
|
+
if (this.azureOpenAIApiKey) {
|
|
664
|
+
requestOptions.headers = {
|
|
665
|
+
"api-key": this.azureOpenAIApiKey,
|
|
666
|
+
...requestOptions.headers,
|
|
667
|
+
};
|
|
668
|
+
requestOptions.query = {
|
|
669
|
+
"api-version": this.azureOpenAIApiVersion,
|
|
670
|
+
...requestOptions.query,
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
return requestOptions;
|
|
674
|
+
}
|
|
675
|
+
_llmType() {
|
|
676
|
+
return "openai";
|
|
677
|
+
}
|
|
678
|
+
/** @ignore */
|
|
679
|
+
_combineLLMOutput(...llmOutputs) {
|
|
680
|
+
return llmOutputs.reduce((acc, llmOutput) => {
|
|
681
|
+
if (llmOutput && llmOutput.tokenUsage) {
|
|
682
|
+
acc.tokenUsage.completionTokens +=
|
|
683
|
+
llmOutput.tokenUsage.completionTokens ?? 0;
|
|
684
|
+
acc.tokenUsage.promptTokens += llmOutput.tokenUsage.promptTokens ?? 0;
|
|
685
|
+
acc.tokenUsage.totalTokens += llmOutput.tokenUsage.totalTokens ?? 0;
|
|
686
|
+
}
|
|
687
|
+
return acc;
|
|
688
|
+
}, {
|
|
689
|
+
tokenUsage: {
|
|
690
|
+
completionTokens: 0,
|
|
691
|
+
promptTokens: 0,
|
|
692
|
+
totalTokens: 0,
|
|
693
|
+
},
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
}
|
|
4
697
|
export class PromptLayerChatOpenAI extends ChatOpenAI {
|
|
5
698
|
constructor(fields) {
|
|
6
699
|
super(fields);
|