langchain 0.0.197-rc.0 → 0.0.197-rc.1

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 (202) hide show
  1. package/dist/agents/executor.cjs +351 -1
  2. package/dist/agents/executor.d.ts +61 -3
  3. package/dist/agents/executor.js +350 -1
  4. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  5. package/dist/base_language/count_tokens.cjs +1 -1
  6. package/dist/base_language/count_tokens.d.ts +1 -1
  7. package/dist/base_language/count_tokens.js +1 -1
  8. package/dist/base_language/index.cjs +1 -1
  9. package/dist/base_language/index.d.ts +1 -1
  10. package/dist/base_language/index.js +1 -1
  11. package/dist/cache/base.cjs +1 -1
  12. package/dist/cache/base.d.ts +1 -1
  13. package/dist/cache/base.js +1 -1
  14. package/dist/cache/index.cjs +1 -1
  15. package/dist/cache/index.d.ts +1 -1
  16. package/dist/cache/index.js +1 -1
  17. package/dist/callbacks/base.cjs +1 -1
  18. package/dist/callbacks/base.d.ts +1 -1
  19. package/dist/callbacks/base.js +1 -1
  20. package/dist/callbacks/handlers/console.cjs +1 -1
  21. package/dist/callbacks/handlers/console.d.ts +1 -1
  22. package/dist/callbacks/handlers/console.js +1 -1
  23. package/dist/callbacks/handlers/initialize.cjs +1 -1
  24. package/dist/callbacks/handlers/initialize.d.ts +1 -1
  25. package/dist/callbacks/handlers/initialize.js +1 -1
  26. package/dist/callbacks/handlers/log_stream.cjs +1 -1
  27. package/dist/callbacks/handlers/log_stream.d.ts +1 -1
  28. package/dist/callbacks/handlers/log_stream.js +1 -1
  29. package/dist/callbacks/handlers/run_collector.cjs +1 -1
  30. package/dist/callbacks/handlers/run_collector.d.ts +1 -1
  31. package/dist/callbacks/handlers/run_collector.js +1 -1
  32. package/dist/callbacks/handlers/tracer.cjs +1 -1
  33. package/dist/callbacks/handlers/tracer.d.ts +1 -1
  34. package/dist/callbacks/handlers/tracer.js +1 -1
  35. package/dist/callbacks/handlers/tracer_langchain.cjs +1 -1
  36. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -1
  37. package/dist/callbacks/handlers/tracer_langchain.js +1 -1
  38. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +1 -1
  39. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -1
  40. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -1
  41. package/dist/callbacks/manager.cjs +1 -1
  42. package/dist/callbacks/manager.d.ts +1 -1
  43. package/dist/callbacks/manager.js +1 -1
  44. package/dist/callbacks/promises.cjs +1 -1
  45. package/dist/callbacks/promises.d.ts +1 -1
  46. package/dist/callbacks/promises.js +1 -1
  47. package/dist/chains/base.cjs +16 -0
  48. package/dist/chains/base.d.ts +2 -0
  49. package/dist/chains/base.js +16 -0
  50. package/dist/chains/openai_functions/structured_output.cjs +1 -1
  51. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  52. package/dist/chains/openai_functions/structured_output.js +1 -1
  53. package/dist/chains/openai_moderation.cjs +2 -2
  54. package/dist/chains/openai_moderation.d.ts +1 -1
  55. package/dist/chains/openai_moderation.js +1 -1
  56. package/dist/chat_models/base.cjs +1 -1
  57. package/dist/chat_models/base.d.ts +1 -1
  58. package/dist/chat_models/base.js +1 -1
  59. package/dist/chat_models/fireworks.d.ts +2 -2
  60. package/dist/chat_models/iflytek_xinghuo/common.d.ts +1 -1
  61. package/dist/chat_models/minimax.d.ts +2 -2
  62. package/dist/chat_models/openai.cjs +4 -698
  63. package/dist/chat_models/openai.d.ts +4 -137
  64. package/dist/chat_models/openai.js +2 -695
  65. package/dist/document.cjs +1 -1
  66. package/dist/document.d.ts +1 -1
  67. package/dist/document.js +1 -1
  68. package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
  69. package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
  70. package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
  71. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  72. package/dist/document_loaders/web/s3.d.ts +1 -1
  73. package/dist/embeddings/base.cjs +1 -1
  74. package/dist/embeddings/base.d.ts +1 -1
  75. package/dist/embeddings/base.js +1 -1
  76. package/dist/embeddings/cache_backed.cjs +1 -1
  77. package/dist/embeddings/cache_backed.js +1 -1
  78. package/dist/embeddings/openai.cjs +2 -240
  79. package/dist/embeddings/openai.d.ts +1 -82
  80. package/dist/embeddings/openai.js +1 -239
  81. package/dist/experimental/openai_assistant/index.cjs +3 -3
  82. package/dist/experimental/openai_assistant/index.d.ts +1 -1
  83. package/dist/experimental/openai_assistant/index.js +1 -1
  84. package/dist/experimental/openai_assistant/schema.d.ts +1 -1
  85. package/dist/experimental/openai_files/index.cjs +2 -2
  86. package/dist/experimental/openai_files/index.d.ts +1 -1
  87. package/dist/experimental/openai_files/index.js +1 -1
  88. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  89. package/dist/llms/base.cjs +1 -1
  90. package/dist/llms/base.d.ts +1 -1
  91. package/dist/llms/base.js +1 -1
  92. package/dist/llms/fireworks.d.ts +2 -2
  93. package/dist/llms/openai-chat.cjs +3 -445
  94. package/dist/llms/openai-chat.d.ts +4 -123
  95. package/dist/llms/openai-chat.js +2 -443
  96. package/dist/llms/openai.cjs +6 -530
  97. package/dist/llms/openai.d.ts +4 -123
  98. package/dist/llms/openai.js +2 -525
  99. package/dist/load/index.cjs +4 -2
  100. package/dist/load/index.d.ts +2 -2
  101. package/dist/load/index.js +4 -2
  102. package/dist/load/serializable.cjs +1 -1
  103. package/dist/load/serializable.d.ts +1 -1
  104. package/dist/load/serializable.js +1 -1
  105. package/dist/memory/base.cjs +2 -2
  106. package/dist/memory/base.d.ts +2 -2
  107. package/dist/memory/base.js +2 -2
  108. package/dist/output_parsers/list.cjs +1 -1
  109. package/dist/output_parsers/list.d.ts +1 -1
  110. package/dist/output_parsers/list.js +1 -1
  111. package/dist/output_parsers/openai_functions.cjs +1 -1
  112. package/dist/output_parsers/openai_functions.d.ts +1 -1
  113. package/dist/output_parsers/openai_functions.js +1 -1
  114. package/dist/output_parsers/structured.d.ts +1 -1
  115. package/dist/prompts/base.cjs +3 -3
  116. package/dist/prompts/base.d.ts +3 -3
  117. package/dist/prompts/base.js +3 -3
  118. package/dist/prompts/chat.cjs +2 -2
  119. package/dist/prompts/chat.d.ts +2 -2
  120. package/dist/prompts/chat.js +2 -2
  121. package/dist/prompts/few_shot.cjs +1 -1
  122. package/dist/prompts/few_shot.d.ts +1 -1
  123. package/dist/prompts/few_shot.js +1 -1
  124. package/dist/prompts/index.cjs +1 -1
  125. package/dist/prompts/index.d.ts +1 -1
  126. package/dist/prompts/index.js +1 -1
  127. package/dist/prompts/pipeline.cjs +1 -1
  128. package/dist/prompts/pipeline.d.ts +1 -1
  129. package/dist/prompts/pipeline.js +1 -1
  130. package/dist/prompts/prompt.cjs +1 -1
  131. package/dist/prompts/prompt.d.ts +1 -1
  132. package/dist/prompts/prompt.js +1 -1
  133. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +1 -1
  134. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -1
  135. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -1
  136. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +1 -1
  137. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -1
  138. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -1
  139. package/dist/prompts/selectors/conditional.cjs +1 -1
  140. package/dist/prompts/selectors/conditional.d.ts +1 -1
  141. package/dist/prompts/selectors/conditional.js +1 -1
  142. package/dist/prompts/serde.d.ts +1 -1
  143. package/dist/prompts/template.cjs +1 -1
  144. package/dist/prompts/template.d.ts +1 -1
  145. package/dist/prompts/template.js +1 -1
  146. package/dist/runnables/index.cjs +1 -1
  147. package/dist/runnables/index.d.ts +1 -1
  148. package/dist/runnables/index.js +1 -1
  149. package/dist/schema/document.cjs +1 -1
  150. package/dist/schema/document.d.ts +1 -1
  151. package/dist/schema/document.js +1 -1
  152. package/dist/schema/index.cjs +7 -7
  153. package/dist/schema/index.d.ts +11 -11
  154. package/dist/schema/index.js +7 -7
  155. package/dist/schema/output_parser.cjs +1 -1
  156. package/dist/schema/output_parser.d.ts +1 -1
  157. package/dist/schema/output_parser.js +1 -1
  158. package/dist/schema/retriever.cjs +1 -1
  159. package/dist/schema/retriever.d.ts +1 -1
  160. package/dist/schema/retriever.js +1 -1
  161. package/dist/schema/runnable/base.cjs +1 -1
  162. package/dist/schema/runnable/base.d.ts +1 -1
  163. package/dist/schema/runnable/base.js +1 -1
  164. package/dist/schema/runnable/branch.cjs +1 -1
  165. package/dist/schema/runnable/branch.d.ts +1 -1
  166. package/dist/schema/runnable/branch.js +1 -1
  167. package/dist/schema/runnable/config.d.ts +1 -1
  168. package/dist/schema/runnable/index.cjs +1 -1
  169. package/dist/schema/runnable/index.d.ts +1 -1
  170. package/dist/schema/runnable/index.js +1 -1
  171. package/dist/schema/runnable/passthrough.cjs +1 -1
  172. package/dist/schema/runnable/passthrough.d.ts +1 -1
  173. package/dist/schema/runnable/passthrough.js +1 -1
  174. package/dist/schema/runnable/router.cjs +1 -1
  175. package/dist/schema/runnable/router.d.ts +1 -1
  176. package/dist/schema/runnable/router.js +1 -1
  177. package/dist/schema/storage.cjs +1 -1
  178. package/dist/schema/storage.d.ts +1 -1
  179. package/dist/schema/storage.js +1 -1
  180. package/dist/tools/base.cjs +4 -108
  181. package/dist/tools/base.d.ts +1 -72
  182. package/dist/tools/base.js +1 -105
  183. package/dist/tools/bingserpapi.d.ts +1 -1
  184. package/dist/tools/convert_to_openai.cjs +4 -38
  185. package/dist/tools/convert_to_openai.d.ts +1 -11
  186. package/dist/tools/convert_to_openai.js +1 -35
  187. package/dist/tools/searchapi.d.ts +1 -1
  188. package/dist/tools/serpapi.d.ts +1 -1
  189. package/dist/tools/serper.d.ts +1 -1
  190. package/dist/types/openai-types.d.ts +1 -133
  191. package/dist/util/async_caller.cjs +1 -1
  192. package/dist/util/async_caller.d.ts +1 -1
  193. package/dist/util/async_caller.js +1 -1
  194. package/dist/util/openai.cjs +2 -18
  195. package/dist/util/openai.d.ts +1 -1
  196. package/dist/util/openai.js +1 -17
  197. package/dist/util/openapi.d.ts +2 -2
  198. package/dist/util/prompt-layer.d.ts +1 -1
  199. package/package.json +6 -5
  200. package/dist/util/openai-format-fndef.cjs +0 -81
  201. package/dist/util/openai-format-fndef.d.ts +0 -44
  202. package/dist/util/openai-format-fndef.js +0 -77
package/dist/document.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Document = void 0;
4
- var documents_1 = require("langchain-core/documents");
4
+ var documents_1 = require("@langchain/core/documents");
5
5
  Object.defineProperty(exports, "Document", { enumerable: true, get: function () { return documents_1.Document; } });
@@ -1 +1 @@
1
- export { type DocumentInput, Document } from "langchain-core/documents";
1
+ export { type DocumentInput, Document } from "@langchain/core/documents";
package/dist/document.js CHANGED
@@ -1 +1 @@
1
- export { Document } from "langchain-core/documents";
1
+ export { Document } from "@langchain/core/documents";
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OpenAIWhisperAudio = void 0;
4
- const openai_1 = require("openai");
4
+ const openai_1 = require("@langchain/openai");
5
5
  const document_js_1 = require("../../document.cjs");
6
6
  const buffer_js_1 = require("./buffer.cjs");
7
7
  const MODEL_NAME = "whisper-1";
@@ -24,7 +24,7 @@ class OpenAIWhisperAudio extends buffer_js_1.BufferLoader {
24
24
  writable: true,
25
25
  value: void 0
26
26
  });
27
- this.openAIClient = new openai_1.OpenAI(fields?.clientOptions);
27
+ this.openAIClient = new openai_1.OpenAIClient(fields?.clientOptions);
28
28
  }
29
29
  async parse(raw, metadata) {
30
30
  const fileName = metadata.source === "blob" ? metadata.blobType : metadata.source;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- import { type ClientOptions } from "openai";
2
+ import { type ClientOptions } from "@langchain/openai";
3
3
  import { Document } from "../../document.js";
4
4
  import { BufferLoader } from "./buffer.js";
5
5
  /**
@@ -1,4 +1,4 @@
1
- import { OpenAI as OpenAIClient, toFile } from "openai";
1
+ import { OpenAIClient, toFile } from "@langchain/openai";
2
2
  import { Document } from "../../document.js";
3
3
  import { BufferLoader } from "./buffer.js";
4
4
  const MODEL_NAME = "whisper-1";
@@ -47,6 +47,6 @@ export declare class AzureBlobStorageFileLoader extends BaseDocumentLoader {
47
47
  * are returned, and the temporary directory is deleted.
48
48
  * @returns An array of documents loaded from the file in Azure Blob Storage.
49
49
  */
50
- load(): Promise<import("langchain-core/documents").Document<Record<string, any>>[]>;
50
+ load(): Promise<import("@langchain/core/documents").Document<Record<string, any>>[]>;
51
51
  }
52
52
  export {};
@@ -70,5 +70,5 @@ export declare class S3Loader extends BaseDocumentLoader {
70
70
  * and then uses the UnstructuredLoader to load the file as a document.
71
71
  * @returns An array of Document objects representing the loaded documents.
72
72
  */
73
- load(): Promise<import("langchain-core/documents").Document<Record<string, any>>[]>;
73
+ load(): Promise<import("@langchain/core/documents").Document<Record<string, any>>[]>;
74
74
  }
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("langchain-core/embeddings"), exports);
17
+ __exportStar(require("@langchain/core/embeddings"), exports);
@@ -1 +1 @@
1
- export * from "langchain-core/embeddings";
1
+ export * from "@langchain/core/embeddings";
@@ -1 +1 @@
1
- export * from "langchain-core/embeddings";
1
+ export * from "@langchain/core/embeddings";
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CacheBackedEmbeddings = void 0;
4
- const hash_1 = require("langchain-core/utils/hash");
4
+ const hash_1 = require("@langchain/core/utils/hash");
5
5
  const encoder_backed_js_1 = require("../storage/encoder_backed.cjs");
6
6
  const base_js_1 = require("./base.cjs");
7
7
  /**
@@ -1,4 +1,4 @@
1
- import { insecureHash } from "langchain-core/utils/hash";
1
+ import { insecureHash } from "@langchain/core/utils/hash";
2
2
  import { EncoderBackedStore } from "../storage/encoder_backed.js";
3
3
  import { Embeddings } from "./base.js";
4
4
  /**
@@ -1,243 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OpenAIEmbeddings = void 0;
4
- const openai_1 = require("openai");
5
- const env_js_1 = require("../util/env.cjs");
6
- const chunk_js_1 = require("../util/chunk.cjs");
7
- const base_js_1 = require("./base.cjs");
8
- const azure_js_1 = require("../util/azure.cjs");
9
- const openai_js_1 = require("../util/openai.cjs");
10
- /**
11
- * Class for generating embeddings using the OpenAI API. Extends the
12
- * Embeddings class and implements OpenAIEmbeddingsParams and
13
- * AzureOpenAIInput.
14
- * @example
15
- * ```typescript
16
- * // Embed a query using OpenAIEmbeddings to generate embeddings for a given text
17
- * const model = new OpenAIEmbeddings();
18
- * const res = await model.embedQuery(
19
- * "What would be a good company name for a company that makes colorful socks?",
20
- * );
21
- * console.log({ res });
22
- *
23
- * ```
24
- */
25
- class OpenAIEmbeddings extends base_js_1.Embeddings {
26
- constructor(fields, configuration) {
27
- const fieldsWithDefaults = { maxConcurrency: 2, ...fields };
28
- super(fieldsWithDefaults);
29
- Object.defineProperty(this, "modelName", {
30
- enumerable: true,
31
- configurable: true,
32
- writable: true,
33
- value: "text-embedding-ada-002"
34
- });
35
- Object.defineProperty(this, "batchSize", {
36
- enumerable: true,
37
- configurable: true,
38
- writable: true,
39
- value: 512
40
- });
41
- Object.defineProperty(this, "stripNewLines", {
42
- enumerable: true,
43
- configurable: true,
44
- writable: true,
45
- value: true
46
- });
47
- Object.defineProperty(this, "timeout", {
48
- enumerable: true,
49
- configurable: true,
50
- writable: true,
51
- value: void 0
52
- });
53
- Object.defineProperty(this, "azureOpenAIApiVersion", {
54
- enumerable: true,
55
- configurable: true,
56
- writable: true,
57
- value: void 0
58
- });
59
- Object.defineProperty(this, "azureOpenAIApiKey", {
60
- enumerable: true,
61
- configurable: true,
62
- writable: true,
63
- value: void 0
64
- });
65
- Object.defineProperty(this, "azureOpenAIApiInstanceName", {
66
- enumerable: true,
67
- configurable: true,
68
- writable: true,
69
- value: void 0
70
- });
71
- Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
72
- enumerable: true,
73
- configurable: true,
74
- writable: true,
75
- value: void 0
76
- });
77
- Object.defineProperty(this, "azureOpenAIBasePath", {
78
- enumerable: true,
79
- configurable: true,
80
- writable: true,
81
- value: void 0
82
- });
83
- Object.defineProperty(this, "organization", {
84
- enumerable: true,
85
- configurable: true,
86
- writable: true,
87
- value: void 0
88
- });
89
- Object.defineProperty(this, "client", {
90
- enumerable: true,
91
- configurable: true,
92
- writable: true,
93
- value: void 0
94
- });
95
- Object.defineProperty(this, "clientConfig", {
96
- enumerable: true,
97
- configurable: true,
98
- writable: true,
99
- value: void 0
100
- });
101
- let apiKey = fieldsWithDefaults?.openAIApiKey ??
102
- (0, env_js_1.getEnvironmentVariable)("OPENAI_API_KEY");
103
- const azureApiKey = fieldsWithDefaults?.azureOpenAIApiKey ??
104
- (0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
105
- if (!azureApiKey && !apiKey) {
106
- throw new Error("OpenAI or Azure OpenAI API key not found");
107
- }
108
- const azureApiInstanceName = fieldsWithDefaults?.azureOpenAIApiInstanceName ??
109
- (0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME");
110
- const azureApiDeploymentName = (fieldsWithDefaults?.azureOpenAIApiEmbeddingsDeploymentName ||
111
- fieldsWithDefaults?.azureOpenAIApiDeploymentName) ??
112
- ((0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") ||
113
- (0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
114
- const azureApiVersion = fieldsWithDefaults?.azureOpenAIApiVersion ??
115
- (0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_API_VERSION");
116
- this.azureOpenAIBasePath =
117
- fieldsWithDefaults?.azureOpenAIBasePath ??
118
- (0, env_js_1.getEnvironmentVariable)("AZURE_OPENAI_BASE_PATH");
119
- this.organization =
120
- fieldsWithDefaults?.configuration?.organization ??
121
- (0, env_js_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
122
- this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;
123
- this.batchSize =
124
- fieldsWithDefaults?.batchSize ?? (azureApiKey ? 1 : this.batchSize);
125
- this.stripNewLines =
126
- fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;
127
- this.timeout = fieldsWithDefaults?.timeout;
128
- this.azureOpenAIApiVersion = azureApiVersion;
129
- this.azureOpenAIApiKey = azureApiKey;
130
- this.azureOpenAIApiInstanceName = azureApiInstanceName;
131
- this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
132
- if (this.azureOpenAIApiKey) {
133
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
134
- throw new Error("Azure OpenAI API instance name not found");
135
- }
136
- if (!this.azureOpenAIApiDeploymentName) {
137
- throw new Error("Azure OpenAI API deployment name not found");
138
- }
139
- if (!this.azureOpenAIApiVersion) {
140
- throw new Error("Azure OpenAI API version not found");
141
- }
142
- apiKey = apiKey ?? "";
143
- }
144
- this.clientConfig = {
145
- apiKey,
146
- organization: this.organization,
147
- baseURL: configuration?.basePath,
148
- dangerouslyAllowBrowser: true,
149
- defaultHeaders: configuration?.baseOptions?.headers,
150
- defaultQuery: configuration?.baseOptions?.params,
151
- ...configuration,
152
- ...fields?.configuration,
153
- };
154
- }
155
- /**
156
- * Method to generate embeddings for an array of documents. Splits the
157
- * documents into batches and makes requests to the OpenAI API to generate
158
- * embeddings.
159
- * @param texts Array of documents to generate embeddings for.
160
- * @returns Promise that resolves to a 2D array of embeddings for each document.
161
- */
162
- async embedDocuments(texts) {
163
- const batches = (0, chunk_js_1.chunkArray)(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
164
- const batchRequests = batches.map((batch) => this.embeddingWithRetry({
165
- model: this.modelName,
166
- input: batch,
167
- }));
168
- const batchResponses = await Promise.all(batchRequests);
169
- const embeddings = [];
170
- for (let i = 0; i < batchResponses.length; i += 1) {
171
- const batch = batches[i];
172
- const { data: batchResponse } = batchResponses[i];
173
- for (let j = 0; j < batch.length; j += 1) {
174
- embeddings.push(batchResponse[j].embedding);
175
- }
176
- }
177
- return embeddings;
178
- }
179
- /**
180
- * Method to generate an embedding for a single document. Calls the
181
- * embeddingWithRetry method with the document as the input.
182
- * @param text Document to generate an embedding for.
183
- * @returns Promise that resolves to an embedding for the document.
184
- */
185
- async embedQuery(text) {
186
- const { data } = await this.embeddingWithRetry({
187
- model: this.modelName,
188
- input: this.stripNewLines ? text.replace(/\n/g, " ") : text,
189
- });
190
- return data[0].embedding;
191
- }
192
- /**
193
- * Private method to make a request to the OpenAI API to generate
194
- * embeddings. Handles the retry logic and returns the response from the
195
- * API.
196
- * @param request Request to send to the OpenAI API.
197
- * @returns Promise that resolves to the response from the API.
198
- */
199
- async embeddingWithRetry(request) {
200
- if (!this.client) {
201
- const openAIEndpointConfig = {
202
- azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
203
- azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
204
- azureOpenAIApiKey: this.azureOpenAIApiKey,
205
- azureOpenAIBasePath: this.azureOpenAIBasePath,
206
- baseURL: this.clientConfig.baseURL,
207
- };
208
- const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
209
- const params = {
210
- ...this.clientConfig,
211
- baseURL: endpoint,
212
- timeout: this.timeout,
213
- maxRetries: 0,
214
- };
215
- if (!params.baseURL) {
216
- delete params.baseURL;
217
- }
218
- this.client = new openai_1.OpenAI(params);
219
- }
220
- const requestOptions = {};
221
- if (this.azureOpenAIApiKey) {
222
- requestOptions.headers = {
223
- "api-key": this.azureOpenAIApiKey,
224
- ...requestOptions.headers,
225
- };
226
- requestOptions.query = {
227
- "api-version": this.azureOpenAIApiVersion,
228
- ...requestOptions.query,
229
- };
230
- }
231
- return this.caller.call(async () => {
232
- try {
233
- const res = await this.client.embeddings.create(request, requestOptions);
234
- return res;
235
- }
236
- catch (e) {
237
- const error = (0, openai_js_1.wrapOpenAIClientError)(e);
238
- throw error;
239
- }
240
- });
241
- }
242
- }
243
- exports.OpenAIEmbeddings = OpenAIEmbeddings;
4
+ var openai_1 = require("@langchain/openai");
5
+ Object.defineProperty(exports, "OpenAIEmbeddings", { enumerable: true, get: function () { return openai_1.OpenAIEmbeddings; } });
@@ -1,82 +1 @@
1
- import { type ClientOptions } from "openai";
2
- import { AzureOpenAIInput, LegacyOpenAIInput } from "../types/openai-types.js";
3
- import { Embeddings, EmbeddingsParams } from "./base.js";
4
- /**
5
- * Interface for OpenAIEmbeddings parameters. Extends EmbeddingsParams and
6
- * defines additional parameters specific to the OpenAIEmbeddings class.
7
- */
8
- export interface OpenAIEmbeddingsParams extends EmbeddingsParams {
9
- /** Model name to use */
10
- modelName: string;
11
- /**
12
- * Timeout to use when making requests to OpenAI.
13
- */
14
- timeout?: number;
15
- /**
16
- * The maximum number of documents to embed in a single request. This is
17
- * limited by the OpenAI API to a maximum of 2048.
18
- */
19
- batchSize?: number;
20
- /**
21
- * Whether to strip new lines from the input text. This is recommended by
22
- * OpenAI, but may not be suitable for all use cases.
23
- */
24
- stripNewLines?: boolean;
25
- }
26
- /**
27
- * Class for generating embeddings using the OpenAI API. Extends the
28
- * Embeddings class and implements OpenAIEmbeddingsParams and
29
- * AzureOpenAIInput.
30
- * @example
31
- * ```typescript
32
- * // Embed a query using OpenAIEmbeddings to generate embeddings for a given text
33
- * const model = new OpenAIEmbeddings();
34
- * const res = await model.embedQuery(
35
- * "What would be a good company name for a company that makes colorful socks?",
36
- * );
37
- * console.log({ res });
38
- *
39
- * ```
40
- */
41
- export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbeddingsParams, AzureOpenAIInput {
42
- modelName: string;
43
- batchSize: number;
44
- stripNewLines: boolean;
45
- timeout?: number;
46
- azureOpenAIApiVersion?: string;
47
- azureOpenAIApiKey?: string;
48
- azureOpenAIApiInstanceName?: string;
49
- azureOpenAIApiDeploymentName?: string;
50
- azureOpenAIBasePath?: string;
51
- organization?: string;
52
- private client;
53
- private clientConfig;
54
- constructor(fields?: Partial<OpenAIEmbeddingsParams> & Partial<AzureOpenAIInput> & {
55
- verbose?: boolean;
56
- openAIApiKey?: string;
57
- configuration?: ClientOptions;
58
- }, configuration?: ClientOptions & LegacyOpenAIInput);
59
- /**
60
- * Method to generate embeddings for an array of documents. Splits the
61
- * documents into batches and makes requests to the OpenAI API to generate
62
- * embeddings.
63
- * @param texts Array of documents to generate embeddings for.
64
- * @returns Promise that resolves to a 2D array of embeddings for each document.
65
- */
66
- embedDocuments(texts: string[]): Promise<number[][]>;
67
- /**
68
- * Method to generate an embedding for a single document. Calls the
69
- * embeddingWithRetry method with the document as the input.
70
- * @param text Document to generate an embedding for.
71
- * @returns Promise that resolves to an embedding for the document.
72
- */
73
- embedQuery(text: string): Promise<number[]>;
74
- /**
75
- * Private method to make a request to the OpenAI API to generate
76
- * embeddings. Handles the retry logic and returns the response from the
77
- * API.
78
- * @param request Request to send to the OpenAI API.
79
- * @returns Promise that resolves to the response from the API.
80
- */
81
- private embeddingWithRetry;
82
- }
1
+ export { type OpenAIEmbeddingsParams, OpenAIEmbeddings, } from "@langchain/openai";
@@ -1,239 +1 @@
1
- import { OpenAI as OpenAIClient } from "openai";
2
- import { getEnvironmentVariable } from "../util/env.js";
3
- import { chunkArray } from "../util/chunk.js";
4
- import { Embeddings } from "./base.js";
5
- import { getEndpoint } from "../util/azure.js";
6
- import { wrapOpenAIClientError } from "../util/openai.js";
7
- /**
8
- * Class for generating embeddings using the OpenAI API. Extends the
9
- * Embeddings class and implements OpenAIEmbeddingsParams and
10
- * AzureOpenAIInput.
11
- * @example
12
- * ```typescript
13
- * // Embed a query using OpenAIEmbeddings to generate embeddings for a given text
14
- * const model = new OpenAIEmbeddings();
15
- * const res = await model.embedQuery(
16
- * "What would be a good company name for a company that makes colorful socks?",
17
- * );
18
- * console.log({ res });
19
- *
20
- * ```
21
- */
22
- export class OpenAIEmbeddings extends Embeddings {
23
- constructor(fields, configuration) {
24
- const fieldsWithDefaults = { maxConcurrency: 2, ...fields };
25
- super(fieldsWithDefaults);
26
- Object.defineProperty(this, "modelName", {
27
- enumerable: true,
28
- configurable: true,
29
- writable: true,
30
- value: "text-embedding-ada-002"
31
- });
32
- Object.defineProperty(this, "batchSize", {
33
- enumerable: true,
34
- configurable: true,
35
- writable: true,
36
- value: 512
37
- });
38
- Object.defineProperty(this, "stripNewLines", {
39
- enumerable: true,
40
- configurable: true,
41
- writable: true,
42
- value: true
43
- });
44
- Object.defineProperty(this, "timeout", {
45
- enumerable: true,
46
- configurable: true,
47
- writable: true,
48
- value: void 0
49
- });
50
- Object.defineProperty(this, "azureOpenAIApiVersion", {
51
- enumerable: true,
52
- configurable: true,
53
- writable: true,
54
- value: void 0
55
- });
56
- Object.defineProperty(this, "azureOpenAIApiKey", {
57
- enumerable: true,
58
- configurable: true,
59
- writable: true,
60
- value: void 0
61
- });
62
- Object.defineProperty(this, "azureOpenAIApiInstanceName", {
63
- enumerable: true,
64
- configurable: true,
65
- writable: true,
66
- value: void 0
67
- });
68
- Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
69
- enumerable: true,
70
- configurable: true,
71
- writable: true,
72
- value: void 0
73
- });
74
- Object.defineProperty(this, "azureOpenAIBasePath", {
75
- enumerable: true,
76
- configurable: true,
77
- writable: true,
78
- value: void 0
79
- });
80
- Object.defineProperty(this, "organization", {
81
- enumerable: true,
82
- configurable: true,
83
- writable: true,
84
- value: void 0
85
- });
86
- Object.defineProperty(this, "client", {
87
- enumerable: true,
88
- configurable: true,
89
- writable: true,
90
- value: void 0
91
- });
92
- Object.defineProperty(this, "clientConfig", {
93
- enumerable: true,
94
- configurable: true,
95
- writable: true,
96
- value: void 0
97
- });
98
- let apiKey = fieldsWithDefaults?.openAIApiKey ??
99
- getEnvironmentVariable("OPENAI_API_KEY");
100
- const azureApiKey = fieldsWithDefaults?.azureOpenAIApiKey ??
101
- getEnvironmentVariable("AZURE_OPENAI_API_KEY");
102
- if (!azureApiKey && !apiKey) {
103
- throw new Error("OpenAI or Azure OpenAI API key not found");
104
- }
105
- const azureApiInstanceName = fieldsWithDefaults?.azureOpenAIApiInstanceName ??
106
- getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
107
- const azureApiDeploymentName = (fieldsWithDefaults?.azureOpenAIApiEmbeddingsDeploymentName ||
108
- fieldsWithDefaults?.azureOpenAIApiDeploymentName) ??
109
- (getEnvironmentVariable("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") ||
110
- getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
111
- const azureApiVersion = fieldsWithDefaults?.azureOpenAIApiVersion ??
112
- getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
113
- this.azureOpenAIBasePath =
114
- fieldsWithDefaults?.azureOpenAIBasePath ??
115
- getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
116
- this.organization =
117
- fieldsWithDefaults?.configuration?.organization ??
118
- getEnvironmentVariable("OPENAI_ORGANIZATION");
119
- this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;
120
- this.batchSize =
121
- fieldsWithDefaults?.batchSize ?? (azureApiKey ? 1 : this.batchSize);
122
- this.stripNewLines =
123
- fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;
124
- this.timeout = fieldsWithDefaults?.timeout;
125
- this.azureOpenAIApiVersion = azureApiVersion;
126
- this.azureOpenAIApiKey = azureApiKey;
127
- this.azureOpenAIApiInstanceName = azureApiInstanceName;
128
- this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
129
- if (this.azureOpenAIApiKey) {
130
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
131
- throw new Error("Azure OpenAI API instance name not found");
132
- }
133
- if (!this.azureOpenAIApiDeploymentName) {
134
- throw new Error("Azure OpenAI API deployment name not found");
135
- }
136
- if (!this.azureOpenAIApiVersion) {
137
- throw new Error("Azure OpenAI API version not found");
138
- }
139
- apiKey = apiKey ?? "";
140
- }
141
- this.clientConfig = {
142
- apiKey,
143
- organization: this.organization,
144
- baseURL: configuration?.basePath,
145
- dangerouslyAllowBrowser: true,
146
- defaultHeaders: configuration?.baseOptions?.headers,
147
- defaultQuery: configuration?.baseOptions?.params,
148
- ...configuration,
149
- ...fields?.configuration,
150
- };
151
- }
152
- /**
153
- * Method to generate embeddings for an array of documents. Splits the
154
- * documents into batches and makes requests to the OpenAI API to generate
155
- * embeddings.
156
- * @param texts Array of documents to generate embeddings for.
157
- * @returns Promise that resolves to a 2D array of embeddings for each document.
158
- */
159
- async embedDocuments(texts) {
160
- const batches = chunkArray(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
161
- const batchRequests = batches.map((batch) => this.embeddingWithRetry({
162
- model: this.modelName,
163
- input: batch,
164
- }));
165
- const batchResponses = await Promise.all(batchRequests);
166
- const embeddings = [];
167
- for (let i = 0; i < batchResponses.length; i += 1) {
168
- const batch = batches[i];
169
- const { data: batchResponse } = batchResponses[i];
170
- for (let j = 0; j < batch.length; j += 1) {
171
- embeddings.push(batchResponse[j].embedding);
172
- }
173
- }
174
- return embeddings;
175
- }
176
- /**
177
- * Method to generate an embedding for a single document. Calls the
178
- * embeddingWithRetry method with the document as the input.
179
- * @param text Document to generate an embedding for.
180
- * @returns Promise that resolves to an embedding for the document.
181
- */
182
- async embedQuery(text) {
183
- const { data } = await this.embeddingWithRetry({
184
- model: this.modelName,
185
- input: this.stripNewLines ? text.replace(/\n/g, " ") : text,
186
- });
187
- return data[0].embedding;
188
- }
189
- /**
190
- * Private method to make a request to the OpenAI API to generate
191
- * embeddings. Handles the retry logic and returns the response from the
192
- * API.
193
- * @param request Request to send to the OpenAI API.
194
- * @returns Promise that resolves to the response from the API.
195
- */
196
- async embeddingWithRetry(request) {
197
- if (!this.client) {
198
- const openAIEndpointConfig = {
199
- azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
200
- azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
201
- azureOpenAIApiKey: this.azureOpenAIApiKey,
202
- azureOpenAIBasePath: this.azureOpenAIBasePath,
203
- baseURL: this.clientConfig.baseURL,
204
- };
205
- const endpoint = getEndpoint(openAIEndpointConfig);
206
- const params = {
207
- ...this.clientConfig,
208
- baseURL: endpoint,
209
- timeout: this.timeout,
210
- maxRetries: 0,
211
- };
212
- if (!params.baseURL) {
213
- delete params.baseURL;
214
- }
215
- this.client = new OpenAIClient(params);
216
- }
217
- const requestOptions = {};
218
- if (this.azureOpenAIApiKey) {
219
- requestOptions.headers = {
220
- "api-key": this.azureOpenAIApiKey,
221
- ...requestOptions.headers,
222
- };
223
- requestOptions.query = {
224
- "api-version": this.azureOpenAIApiVersion,
225
- ...requestOptions.query,
226
- };
227
- }
228
- return this.caller.call(async () => {
229
- try {
230
- const res = await this.client.embeddings.create(request, requestOptions);
231
- return res;
232
- }
233
- catch (e) {
234
- const error = wrapOpenAIClientError(e);
235
- throw error;
236
- }
237
- });
238
- }
239
- }
1
+ export { OpenAIEmbeddings, } from "@langchain/openai";