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,7 +1,448 @@
1
- import { OpenAIChat } from "@langchain/openai";
1
+ import { OpenAI as OpenAIClient } from "openai";
2
+ import { GenerationChunk } from "../schema/index.js";
3
+ import { getEndpoint } from "../util/azure.js";
2
4
  import { getEnvironmentVariable } from "../util/env.js";
3
5
  import { promptLayerTrackRequest } from "../util/prompt-layer.js";
4
- export { OpenAIChat };
6
+ import { LLM } from "./base.js";
7
+ import { wrapOpenAIClientError } from "../util/openai.js";
8
+ /**
9
+ * Wrapper around OpenAI large language models that use the Chat endpoint.
10
+ *
11
+ * To use you should have the `openai` package installed, with the
12
+ * `OPENAI_API_KEY` environment variable set.
13
+ *
14
+ * To use with Azure you should have the `openai` package installed, with the
15
+ * `AZURE_OPENAI_API_KEY`,
16
+ * `AZURE_OPENAI_API_INSTANCE_NAME`,
17
+ * `AZURE_OPENAI_API_DEPLOYMENT_NAME`
18
+ * and `AZURE_OPENAI_API_VERSION` environment variable set.
19
+ *
20
+ * @remarks
21
+ * Any parameters that are valid to be passed to {@link
22
+ * https://platform.openai.com/docs/api-reference/chat/create |
23
+ * `openai.createCompletion`} can be passed through {@link modelKwargs}, even
24
+ * if not explicitly available on this class.
25
+ *
26
+ * @augments BaseLLM
27
+ * @augments OpenAIInput
28
+ * @augments AzureOpenAIChatInput
29
+ * @example
30
+ * ```typescript
31
+ * const model = new OpenAIChat({
32
+ * prefixMessages: [
33
+ * {
34
+ * role: "system",
35
+ * content: "You are a helpful assistant that answers in pirate language",
36
+ * },
37
+ * ],
38
+ * maxTokens: 50,
39
+ * });
40
+ *
41
+ * const res = await model.call(
42
+ * "What would be a good company name for a company that makes colorful socks?"
43
+ * );
44
+ * console.log({ res });
45
+ * ```
46
+ */
47
+ export class OpenAIChat extends LLM {
48
+ static lc_name() {
49
+ return "OpenAIChat";
50
+ }
51
+ get callKeys() {
52
+ return [...super.callKeys, "options", "promptIndex"];
53
+ }
54
+ get lc_secrets() {
55
+ return {
56
+ openAIApiKey: "OPENAI_API_KEY",
57
+ azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
58
+ organization: "OPENAI_ORGANIZATION",
59
+ };
60
+ }
61
+ get lc_aliases() {
62
+ return {
63
+ modelName: "model",
64
+ openAIApiKey: "openai_api_key",
65
+ azureOpenAIApiVersion: "azure_openai_api_version",
66
+ azureOpenAIApiKey: "azure_openai_api_key",
67
+ azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
68
+ azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
69
+ };
70
+ }
71
+ constructor(fields,
72
+ /** @deprecated */
73
+ configuration) {
74
+ super(fields ?? {});
75
+ Object.defineProperty(this, "lc_serializable", {
76
+ enumerable: true,
77
+ configurable: true,
78
+ writable: true,
79
+ value: true
80
+ });
81
+ Object.defineProperty(this, "temperature", {
82
+ enumerable: true,
83
+ configurable: true,
84
+ writable: true,
85
+ value: 1
86
+ });
87
+ Object.defineProperty(this, "topP", {
88
+ enumerable: true,
89
+ configurable: true,
90
+ writable: true,
91
+ value: 1
92
+ });
93
+ Object.defineProperty(this, "frequencyPenalty", {
94
+ enumerable: true,
95
+ configurable: true,
96
+ writable: true,
97
+ value: 0
98
+ });
99
+ Object.defineProperty(this, "presencePenalty", {
100
+ enumerable: true,
101
+ configurable: true,
102
+ writable: true,
103
+ value: 0
104
+ });
105
+ Object.defineProperty(this, "n", {
106
+ enumerable: true,
107
+ configurable: true,
108
+ writable: true,
109
+ value: 1
110
+ });
111
+ Object.defineProperty(this, "logitBias", {
112
+ enumerable: true,
113
+ configurable: true,
114
+ writable: true,
115
+ value: void 0
116
+ });
117
+ Object.defineProperty(this, "maxTokens", {
118
+ enumerable: true,
119
+ configurable: true,
120
+ writable: true,
121
+ value: void 0
122
+ });
123
+ Object.defineProperty(this, "modelName", {
124
+ enumerable: true,
125
+ configurable: true,
126
+ writable: true,
127
+ value: "gpt-3.5-turbo"
128
+ });
129
+ Object.defineProperty(this, "prefixMessages", {
130
+ enumerable: true,
131
+ configurable: true,
132
+ writable: true,
133
+ value: void 0
134
+ });
135
+ Object.defineProperty(this, "modelKwargs", {
136
+ enumerable: true,
137
+ configurable: true,
138
+ writable: true,
139
+ value: void 0
140
+ });
141
+ Object.defineProperty(this, "timeout", {
142
+ enumerable: true,
143
+ configurable: true,
144
+ writable: true,
145
+ value: void 0
146
+ });
147
+ Object.defineProperty(this, "stop", {
148
+ enumerable: true,
149
+ configurable: true,
150
+ writable: true,
151
+ value: void 0
152
+ });
153
+ Object.defineProperty(this, "user", {
154
+ enumerable: true,
155
+ configurable: true,
156
+ writable: true,
157
+ value: void 0
158
+ });
159
+ Object.defineProperty(this, "streaming", {
160
+ enumerable: true,
161
+ configurable: true,
162
+ writable: true,
163
+ value: false
164
+ });
165
+ Object.defineProperty(this, "openAIApiKey", {
166
+ enumerable: true,
167
+ configurable: true,
168
+ writable: true,
169
+ value: void 0
170
+ });
171
+ Object.defineProperty(this, "azureOpenAIApiVersion", {
172
+ enumerable: true,
173
+ configurable: true,
174
+ writable: true,
175
+ value: void 0
176
+ });
177
+ Object.defineProperty(this, "azureOpenAIApiKey", {
178
+ enumerable: true,
179
+ configurable: true,
180
+ writable: true,
181
+ value: void 0
182
+ });
183
+ Object.defineProperty(this, "azureOpenAIApiInstanceName", {
184
+ enumerable: true,
185
+ configurable: true,
186
+ writable: true,
187
+ value: void 0
188
+ });
189
+ Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
190
+ enumerable: true,
191
+ configurable: true,
192
+ writable: true,
193
+ value: void 0
194
+ });
195
+ Object.defineProperty(this, "azureOpenAIBasePath", {
196
+ enumerable: true,
197
+ configurable: true,
198
+ writable: true,
199
+ value: void 0
200
+ });
201
+ Object.defineProperty(this, "organization", {
202
+ enumerable: true,
203
+ configurable: true,
204
+ writable: true,
205
+ value: void 0
206
+ });
207
+ Object.defineProperty(this, "client", {
208
+ enumerable: true,
209
+ configurable: true,
210
+ writable: true,
211
+ value: void 0
212
+ });
213
+ Object.defineProperty(this, "clientConfig", {
214
+ enumerable: true,
215
+ configurable: true,
216
+ writable: true,
217
+ value: void 0
218
+ });
219
+ this.openAIApiKey =
220
+ fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY");
221
+ this.azureOpenAIApiKey =
222
+ fields?.azureOpenAIApiKey ??
223
+ getEnvironmentVariable("AZURE_OPENAI_API_KEY");
224
+ if (!this.azureOpenAIApiKey && !this.openAIApiKey) {
225
+ throw new Error("OpenAI or Azure OpenAI API key not found");
226
+ }
227
+ this.azureOpenAIApiInstanceName =
228
+ fields?.azureOpenAIApiInstanceName ??
229
+ getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
230
+ this.azureOpenAIApiDeploymentName =
231
+ (fields?.azureOpenAIApiCompletionsDeploymentName ||
232
+ fields?.azureOpenAIApiDeploymentName) ??
233
+ (getEnvironmentVariable("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
234
+ getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
235
+ this.azureOpenAIApiVersion =
236
+ fields?.azureOpenAIApiVersion ??
237
+ getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
238
+ this.azureOpenAIBasePath =
239
+ fields?.azureOpenAIBasePath ??
240
+ getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
241
+ this.organization =
242
+ fields?.configuration?.organization ??
243
+ getEnvironmentVariable("OPENAI_ORGANIZATION");
244
+ this.modelName = fields?.modelName ?? this.modelName;
245
+ this.prefixMessages = fields?.prefixMessages ?? this.prefixMessages;
246
+ this.modelKwargs = fields?.modelKwargs ?? {};
247
+ this.timeout = fields?.timeout;
248
+ this.temperature = fields?.temperature ?? this.temperature;
249
+ this.topP = fields?.topP ?? this.topP;
250
+ this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
251
+ this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
252
+ this.n = fields?.n ?? this.n;
253
+ this.logitBias = fields?.logitBias;
254
+ this.maxTokens = fields?.maxTokens;
255
+ this.stop = fields?.stop;
256
+ this.user = fields?.user;
257
+ this.streaming = fields?.streaming ?? false;
258
+ if (this.n > 1) {
259
+ throw new Error("Cannot use n > 1 in OpenAIChat LLM. Use ChatOpenAI Chat Model instead.");
260
+ }
261
+ if (this.azureOpenAIApiKey) {
262
+ if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
263
+ throw new Error("Azure OpenAI API instance name not found");
264
+ }
265
+ if (!this.azureOpenAIApiDeploymentName) {
266
+ throw new Error("Azure OpenAI API deployment name not found");
267
+ }
268
+ if (!this.azureOpenAIApiVersion) {
269
+ throw new Error("Azure OpenAI API version not found");
270
+ }
271
+ this.openAIApiKey = this.openAIApiKey ?? "";
272
+ }
273
+ this.clientConfig = {
274
+ apiKey: this.openAIApiKey,
275
+ organization: this.organization,
276
+ baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
277
+ dangerouslyAllowBrowser: true,
278
+ defaultHeaders: configuration?.baseOptions?.headers ??
279
+ fields?.configuration?.baseOptions?.headers,
280
+ defaultQuery: configuration?.baseOptions?.params ??
281
+ fields?.configuration?.baseOptions?.params,
282
+ ...configuration,
283
+ ...fields?.configuration,
284
+ };
285
+ }
286
+ /**
287
+ * Get the parameters used to invoke the model
288
+ */
289
+ invocationParams(options) {
290
+ return {
291
+ model: this.modelName,
292
+ temperature: this.temperature,
293
+ top_p: this.topP,
294
+ frequency_penalty: this.frequencyPenalty,
295
+ presence_penalty: this.presencePenalty,
296
+ n: this.n,
297
+ logit_bias: this.logitBias,
298
+ max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
299
+ stop: options?.stop ?? this.stop,
300
+ user: this.user,
301
+ stream: this.streaming,
302
+ ...this.modelKwargs,
303
+ };
304
+ }
305
+ /** @ignore */
306
+ _identifyingParams() {
307
+ return {
308
+ model_name: this.modelName,
309
+ ...this.invocationParams(),
310
+ ...this.clientConfig,
311
+ };
312
+ }
313
+ /**
314
+ * Get the identifying parameters for the model
315
+ */
316
+ identifyingParams() {
317
+ return {
318
+ model_name: this.modelName,
319
+ ...this.invocationParams(),
320
+ ...this.clientConfig,
321
+ };
322
+ }
323
+ /**
324
+ * Formats the messages for the OpenAI API.
325
+ * @param prompt The prompt to be formatted.
326
+ * @returns Array of formatted messages.
327
+ */
328
+ formatMessages(prompt) {
329
+ const message = {
330
+ role: "user",
331
+ content: prompt,
332
+ };
333
+ return this.prefixMessages ? [...this.prefixMessages, message] : [message];
334
+ }
335
+ async *_streamResponseChunks(prompt, options, runManager) {
336
+ const params = {
337
+ ...this.invocationParams(options),
338
+ messages: this.formatMessages(prompt),
339
+ stream: true,
340
+ };
341
+ const stream = await this.completionWithRetry(params, options);
342
+ for await (const data of stream) {
343
+ const choice = data?.choices[0];
344
+ if (!choice) {
345
+ continue;
346
+ }
347
+ const { delta } = choice;
348
+ const generationChunk = new GenerationChunk({
349
+ text: delta.content ?? "",
350
+ });
351
+ yield generationChunk;
352
+ const newTokenIndices = {
353
+ prompt: options.promptIndex ?? 0,
354
+ completion: choice.index ?? 0,
355
+ };
356
+ // eslint-disable-next-line no-void
357
+ void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices);
358
+ }
359
+ if (options.signal?.aborted) {
360
+ throw new Error("AbortError");
361
+ }
362
+ }
363
+ /** @ignore */
364
+ async _call(prompt, options, runManager) {
365
+ const params = this.invocationParams(options);
366
+ if (params.stream) {
367
+ const stream = await this._streamResponseChunks(prompt, options, runManager);
368
+ let finalChunk;
369
+ for await (const chunk of stream) {
370
+ if (finalChunk === undefined) {
371
+ finalChunk = chunk;
372
+ }
373
+ else {
374
+ finalChunk = finalChunk.concat(chunk);
375
+ }
376
+ }
377
+ return finalChunk?.text ?? "";
378
+ }
379
+ else {
380
+ const response = await this.completionWithRetry({
381
+ ...params,
382
+ stream: false,
383
+ messages: this.formatMessages(prompt),
384
+ }, {
385
+ signal: options.signal,
386
+ ...options.options,
387
+ });
388
+ return response?.choices[0]?.message?.content ?? "";
389
+ }
390
+ }
391
+ async completionWithRetry(request, options) {
392
+ const requestOptions = this._getClientOptions(options);
393
+ return this.caller.call(async () => {
394
+ try {
395
+ const res = await this.client.chat.completions.create(request, requestOptions);
396
+ return res;
397
+ }
398
+ catch (e) {
399
+ const error = wrapOpenAIClientError(e);
400
+ throw error;
401
+ }
402
+ });
403
+ }
404
+ /** @ignore */
405
+ _getClientOptions(options) {
406
+ if (!this.client) {
407
+ const openAIEndpointConfig = {
408
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
409
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
410
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
411
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
412
+ baseURL: this.clientConfig.baseURL,
413
+ };
414
+ const endpoint = getEndpoint(openAIEndpointConfig);
415
+ const params = {
416
+ ...this.clientConfig,
417
+ baseURL: endpoint,
418
+ timeout: this.timeout,
419
+ maxRetries: 0,
420
+ };
421
+ if (!params.baseURL) {
422
+ delete params.baseURL;
423
+ }
424
+ this.client = new OpenAIClient(params);
425
+ }
426
+ const requestOptions = {
427
+ ...this.clientConfig,
428
+ ...options,
429
+ };
430
+ if (this.azureOpenAIApiKey) {
431
+ requestOptions.headers = {
432
+ "api-key": this.azureOpenAIApiKey,
433
+ ...requestOptions.headers,
434
+ };
435
+ requestOptions.query = {
436
+ "api-version": this.azureOpenAIApiVersion,
437
+ ...requestOptions.query,
438
+ };
439
+ }
440
+ return requestOptions;
441
+ }
442
+ _llmType() {
443
+ return "openai";
444
+ }
445
+ }
5
446
  /**
6
447
  * PromptLayer wrapper to OpenAIChat
7
448
  */