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