langchain 0.0.195 → 0.0.197-rc.0

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 (236) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agents/openai/index.cjs +6 -2
  3. package/dist/agents/openai/index.js +6 -2
  4. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  5. package/dist/base_language/count_tokens.cjs +5 -70
  6. package/dist/base_language/count_tokens.d.ts +1 -10
  7. package/dist/base_language/count_tokens.js +1 -65
  8. package/dist/base_language/index.cjs +6 -196
  9. package/dist/base_language/index.d.ts +1 -111
  10. package/dist/base_language/index.js +1 -191
  11. package/dist/cache/base.cjs +15 -37
  12. package/dist/cache/base.d.ts +1 -20
  13. package/dist/cache/base.js +1 -33
  14. package/dist/cache/index.cjs +2 -46
  15. package/dist/cache/index.d.ts +1 -29
  16. package/dist/cache/index.js +1 -45
  17. package/dist/callbacks/base.cjs +3 -139
  18. package/dist/callbacks/base.d.ts +1 -266
  19. package/dist/callbacks/base.js +1 -126
  20. package/dist/callbacks/handlers/console.cjs +14 -221
  21. package/dist/callbacks/handlers/console.d.ts +1 -117
  22. package/dist/callbacks/handlers/console.js +1 -217
  23. package/dist/callbacks/handlers/initialize.cjs +15 -30
  24. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  25. package/dist/callbacks/handlers/initialize.js +1 -27
  26. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  27. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  28. package/dist/callbacks/handlers/log_stream.js +1 -289
  29. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  30. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  31. package/dist/callbacks/handlers/run_collector.js +1 -46
  32. package/dist/callbacks/handlers/tracer.cjs +15 -375
  33. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  34. package/dist/callbacks/handlers/tracer.js +1 -373
  35. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  36. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  37. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  38. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  39. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  40. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  41. package/dist/callbacks/manager.cjs +15 -676
  42. package/dist/callbacks/manager.d.ts +1 -180
  43. package/dist/callbacks/manager.js +1 -666
  44. package/dist/callbacks/promises.cjs +14 -42
  45. package/dist/callbacks/promises.d.ts +1 -11
  46. package/dist/callbacks/promises.js +1 -37
  47. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  48. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  49. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  50. package/dist/chains/openai_functions/structured_output.js +1 -1
  51. package/dist/chat_models/anthropic.cjs +15 -348
  52. package/dist/chat_models/anthropic.d.ts +1 -156
  53. package/dist/chat_models/anthropic.js +1 -346
  54. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  55. package/dist/chat_models/base.cjs +15 -296
  56. package/dist/chat_models/base.d.ts +1 -122
  57. package/dist/chat_models/base.js +1 -292
  58. package/dist/chat_models/bedrock/web.cjs +21 -1
  59. package/dist/chat_models/bedrock/web.d.ts +2 -2
  60. package/dist/chat_models/bedrock/web.js +21 -1
  61. package/dist/chat_models/fireworks.d.ts +1 -1
  62. package/dist/document.cjs +2 -24
  63. package/dist/document.d.ts +1 -12
  64. package/dist/document.js +1 -23
  65. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  66. package/dist/document_loaders/web/github.cjs +105 -0
  67. package/dist/document_loaders/web/github.d.ts +26 -0
  68. package/dist/document_loaders/web/github.js +105 -0
  69. package/dist/document_loaders/web/s3.d.ts +1 -1
  70. package/dist/embeddings/base.cjs +15 -22
  71. package/dist/embeddings/base.d.ts +1 -33
  72. package/dist/embeddings/base.js +1 -20
  73. package/dist/embeddings/cache_backed.cjs +2 -2
  74. package/dist/embeddings/cache_backed.js +1 -1
  75. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  76. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  77. package/dist/evaluation/qa/prompt.d.ts +2 -2
  78. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  79. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  80. package/dist/llms/base.cjs +15 -278
  81. package/dist/llms/base.d.ts +1 -115
  82. package/dist/llms/base.js +1 -275
  83. package/dist/llms/bedrock/web.cjs +21 -1
  84. package/dist/llms/bedrock/web.d.ts +2 -2
  85. package/dist/llms/bedrock/web.js +21 -1
  86. package/dist/llms/fireworks.d.ts +1 -1
  87. package/dist/load/import_map.cjs +2 -1
  88. package/dist/load/import_map.d.ts +1 -0
  89. package/dist/load/import_map.js +1 -0
  90. package/dist/load/index.cjs +7 -148
  91. package/dist/load/index.js +7 -148
  92. package/dist/load/map_keys.cjs +0 -24
  93. package/dist/load/map_keys.d.ts +0 -6
  94. package/dist/load/map_keys.js +1 -17
  95. package/dist/load/serializable.cjs +15 -178
  96. package/dist/load/serializable.d.ts +1 -66
  97. package/dist/load/serializable.js +1 -175
  98. package/dist/memory/base.cjs +17 -92
  99. package/dist/memory/base.d.ts +2 -68
  100. package/dist/memory/base.js +2 -87
  101. package/dist/output_parsers/list.cjs +4 -122
  102. package/dist/output_parsers/list.d.ts +1 -57
  103. package/dist/output_parsers/list.js +1 -119
  104. package/dist/output_parsers/openai_functions.cjs +2 -2
  105. package/dist/output_parsers/openai_functions.d.ts +1 -1
  106. package/dist/output_parsers/openai_functions.js +1 -1
  107. package/dist/output_parsers/regex.d.ts +1 -1
  108. package/dist/output_parsers/structured.d.ts +1 -1
  109. package/dist/prompts/base.cjs +8 -183
  110. package/dist/prompts/base.d.ts +3 -132
  111. package/dist/prompts/base.js +3 -178
  112. package/dist/prompts/chat.cjs +13 -477
  113. package/dist/prompts/chat.d.ts +2 -219
  114. package/dist/prompts/chat.js +2 -466
  115. package/dist/prompts/few_shot.cjs +3 -352
  116. package/dist/prompts/few_shot.d.ts +1 -192
  117. package/dist/prompts/few_shot.js +1 -350
  118. package/dist/prompts/index.cjs +3 -2
  119. package/dist/prompts/index.d.ts +2 -1
  120. package/dist/prompts/index.js +2 -1
  121. package/dist/prompts/pipeline.cjs +2 -141
  122. package/dist/prompts/pipeline.d.ts +1 -98
  123. package/dist/prompts/pipeline.js +1 -140
  124. package/dist/prompts/prompt.cjs +2 -145
  125. package/dist/prompts/prompt.d.ts +1 -92
  126. package/dist/prompts/prompt.js +1 -144
  127. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +2 -147
  128. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  129. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  130. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  131. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  132. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  133. package/dist/prompts/selectors/conditional.cjs +5 -73
  134. package/dist/prompts/selectors/conditional.d.ts +1 -63
  135. package/dist/prompts/selectors/conditional.js +1 -69
  136. package/dist/prompts/serde.d.ts +1 -43
  137. package/dist/prompts/template.cjs +8 -88
  138. package/dist/prompts/template.d.ts +1 -36
  139. package/dist/prompts/template.js +1 -83
  140. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  141. package/dist/runnables/index.d.ts +1 -0
  142. package/dist/runnables/index.js +1 -0
  143. package/dist/schema/document.cjs +3 -34
  144. package/dist/schema/document.d.ts +2 -29
  145. package/dist/schema/document.js +2 -32
  146. package/dist/schema/index.cjs +37 -612
  147. package/dist/schema/index.d.ts +11 -311
  148. package/dist/schema/index.js +8 -583
  149. package/dist/schema/output_parser.cjs +15 -309
  150. package/dist/schema/output_parser.d.ts +1 -173
  151. package/dist/schema/output_parser.js +1 -301
  152. package/dist/schema/retriever.cjs +15 -77
  153. package/dist/schema/retriever.d.ts +1 -43
  154. package/dist/schema/retriever.js +1 -75
  155. package/dist/schema/runnable/base.cjs +10 -1072
  156. package/dist/schema/runnable/base.d.ts +1 -356
  157. package/dist/schema/runnable/base.js +1 -1060
  158. package/dist/schema/runnable/branch.cjs +2 -131
  159. package/dist/schema/runnable/branch.d.ts +1 -94
  160. package/dist/schema/runnable/branch.js +1 -130
  161. package/dist/schema/runnable/config.cjs +0 -6
  162. package/dist/schema/runnable/config.d.ts +1 -3
  163. package/dist/schema/runnable/config.js +1 -4
  164. package/dist/schema/runnable/index.cjs +15 -16
  165. package/dist/schema/runnable/index.d.ts +1 -5
  166. package/dist/schema/runnable/index.js +1 -4
  167. package/dist/schema/runnable/passthrough.cjs +3 -113
  168. package/dist/schema/runnable/passthrough.d.ts +1 -72
  169. package/dist/schema/runnable/passthrough.js +1 -111
  170. package/dist/schema/runnable/router.cjs +2 -71
  171. package/dist/schema/runnable/router.d.ts +1 -29
  172. package/dist/schema/runnable/router.js +1 -70
  173. package/dist/schema/storage.cjs +15 -8
  174. package/dist/schema/storage.d.ts +1 -57
  175. package/dist/schema/storage.js +1 -6
  176. package/dist/tools/bingserpapi.d.ts +1 -1
  177. package/dist/tools/searchapi.d.ts +1 -1
  178. package/dist/tools/serpapi.d.ts +1 -1
  179. package/dist/tools/serper.d.ts +1 -1
  180. package/dist/util/async_caller.cjs +14 -128
  181. package/dist/util/async_caller.d.ts +1 -45
  182. package/dist/util/async_caller.js +1 -124
  183. package/dist/vectorstores/momento_vector_index.cjs +39 -0
  184. package/dist/vectorstores/momento_vector_index.d.ts +17 -1
  185. package/dist/vectorstores/momento_vector_index.js +40 -1
  186. package/dist/vectorstores/mongodb_atlas.cjs +22 -2
  187. package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
  188. package/dist/vectorstores/mongodb_atlas.js +22 -2
  189. package/package.json +18 -11
  190. package/runnables.cjs +1 -0
  191. package/runnables.d.ts +1 -0
  192. package/runnables.js +1 -0
  193. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  194. package/dist/util/@cfworker/json-schema/index.js +0 -1
  195. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  196. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  197. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  198. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  199. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  200. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  201. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  202. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  203. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  204. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  205. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  206. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  207. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  208. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  209. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  210. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  211. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  212. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  213. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  214. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  215. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  216. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  217. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  218. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  219. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  220. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  221. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  222. package/dist/util/fast-json-patch/index.cjs +0 -49
  223. package/dist/util/fast-json-patch/index.d.ts +0 -22
  224. package/dist/util/fast-json-patch/index.js +0 -16
  225. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  226. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  227. package/dist/util/fast-json-patch/src/core.js +0 -459
  228. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  229. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  230. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  231. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  232. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  233. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  234. package/dist/util/js-sha1/hash.cjs +0 -358
  235. package/dist/util/js-sha1/hash.d.ts +0 -1
  236. package/dist/util/js-sha1/hash.js +0 -355
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Harrison Chase
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -146,12 +146,16 @@ class OpenAIAgent extends agent_js_1.Agent {
146
146
  const callKeys = "callKeys" in this.llmChain.llm ? this.llmChain.llm.callKeys : [];
147
147
  for (const key of callKeys) {
148
148
  if (key in inputs) {
149
- valuesForLLM[key] = inputs[key];
149
+ valuesForLLM[key] =
150
+ inputs[key];
150
151
  delete valuesForPrompt[key];
151
152
  }
152
153
  }
153
154
  const promptValue = await this.llmChain.prompt.formatPromptValue(valuesForPrompt);
154
- const message = await llm.predictMessages(promptValue.toChatMessages(), valuesForLLM, callbackManager);
155
+ const message = await llm.invoke(promptValue.toChatMessages(), {
156
+ ...valuesForLLM,
157
+ callbacks: callbackManager,
158
+ });
155
159
  return this.outputParser.parseAIMessage(message);
156
160
  }
157
161
  }
@@ -142,12 +142,16 @@ export class OpenAIAgent extends Agent {
142
142
  const callKeys = "callKeys" in this.llmChain.llm ? this.llmChain.llm.callKeys : [];
143
143
  for (const key of callKeys) {
144
144
  if (key in inputs) {
145
- valuesForLLM[key] = inputs[key];
145
+ valuesForLLM[key] =
146
+ inputs[key];
146
147
  delete valuesForPrompt[key];
147
148
  }
148
149
  }
149
150
  const promptValue = await this.llmChain.prompt.formatPromptValue(valuesForPrompt);
150
- const message = await llm.predictMessages(promptValue.toChatMessages(), valuesForLLM, callbackManager);
151
+ const message = await llm.invoke(promptValue.toChatMessages(), {
152
+ ...valuesForLLM,
153
+ callbacks: callbackManager,
154
+ });
151
155
  return this.outputParser.parseAIMessage(message);
152
156
  }
153
157
  }
@@ -33,7 +33,7 @@ export declare class OpenAIAgentTokenBufferMemory extends BaseChatMemory {
33
33
  * Retrieves the messages from the chat history.
34
34
  * @returns Promise that resolves with the messages from the chat history.
35
35
  */
36
- getMessages(): Promise<import("../../../schema/index.js").BaseMessage[]>;
36
+ getMessages(): Promise<import("langchain-core/messages").BaseMessage[]>;
37
37
  /**
38
38
  * Loads memory variables from the input values.
39
39
  * @param _values Input values.
@@ -1,72 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateMaxTokens = exports.getModelContextSize = exports.getEmbeddingContextSize = exports.getModelNameForTiktoken = void 0;
4
- const tiktoken_js_1 = require("../util/tiktoken.cjs");
5
- // https://www.npmjs.com/package/js-tiktoken
6
- const getModelNameForTiktoken = (modelName) => {
7
- if (modelName.startsWith("gpt-3.5-turbo-16k")) {
8
- return "gpt-3.5-turbo-16k";
9
- }
10
- if (modelName.startsWith("gpt-3.5-turbo-")) {
11
- return "gpt-3.5-turbo";
12
- }
13
- if (modelName.startsWith("gpt-4-32k")) {
14
- return "gpt-4-32k";
15
- }
16
- if (modelName.startsWith("gpt-4-")) {
17
- return "gpt-4";
18
- }
19
- return modelName;
20
- };
21
- exports.getModelNameForTiktoken = getModelNameForTiktoken;
22
- const getEmbeddingContextSize = (modelName) => {
23
- switch (modelName) {
24
- case "text-embedding-ada-002":
25
- return 8191;
26
- default:
27
- return 2046;
28
- }
29
- };
30
- exports.getEmbeddingContextSize = getEmbeddingContextSize;
31
- const getModelContextSize = (modelName) => {
32
- switch ((0, exports.getModelNameForTiktoken)(modelName)) {
33
- case "gpt-3.5-turbo-16k":
34
- return 16384;
35
- case "gpt-3.5-turbo":
36
- return 4096;
37
- case "gpt-4-32k":
38
- return 32768;
39
- case "gpt-4":
40
- return 8192;
41
- case "text-davinci-003":
42
- return 4097;
43
- case "text-curie-001":
44
- return 2048;
45
- case "text-babbage-001":
46
- return 2048;
47
- case "text-ada-001":
48
- return 2048;
49
- case "code-davinci-002":
50
- return 8000;
51
- case "code-cushman-001":
52
- return 2048;
53
- default:
54
- return 4097;
55
- }
56
- };
57
- exports.getModelContextSize = getModelContextSize;
58
- const calculateMaxTokens = async ({ prompt, modelName, }) => {
59
- let numTokens;
60
- try {
61
- numTokens = (await (0, tiktoken_js_1.encodingForModel)((0, exports.getModelNameForTiktoken)(modelName))).encode(prompt).length;
62
- }
63
- catch (error) {
64
- console.warn("Failed to calculate number of tokens, falling back to approximate count");
65
- // fallback to approximate calculation if tiktoken is not available
66
- // each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#
67
- numTokens = Math.ceil(prompt.length / 4);
68
- }
69
- const maxTokens = (0, exports.getModelContextSize)(modelName);
70
- return maxTokens - numTokens;
71
- };
72
- exports.calculateMaxTokens = calculateMaxTokens;
3
+ exports.getEmbeddingContextSize = exports.getModelContextSize = exports.calculateMaxTokens = void 0;
4
+ var base_1 = require("langchain-core/language_models/base");
5
+ Object.defineProperty(exports, "calculateMaxTokens", { enumerable: true, get: function () { return base_1.calculateMaxTokens; } });
6
+ Object.defineProperty(exports, "getModelContextSize", { enumerable: true, get: function () { return base_1.getModelContextSize; } });
7
+ Object.defineProperty(exports, "getEmbeddingContextSize", { enumerable: true, get: function () { return base_1.getEmbeddingContextSize; } });
@@ -1,10 +1 @@
1
- import { type TiktokenModel } from "js-tiktoken/lite";
2
- export declare const getModelNameForTiktoken: (modelName: string) => TiktokenModel;
3
- export declare const getEmbeddingContextSize: (modelName?: string) => number;
4
- export declare const getModelContextSize: (modelName: string) => number;
5
- interface CalculateMaxTokenProps {
6
- prompt: string;
7
- modelName: TiktokenModel;
8
- }
9
- export declare const calculateMaxTokens: ({ prompt, modelName, }: CalculateMaxTokenProps) => Promise<number>;
10
- export {};
1
+ export { calculateMaxTokens, getModelContextSize, getEmbeddingContextSize, } from "langchain-core/language_models/base";
@@ -1,65 +1 @@
1
- import { encodingForModel } from "../util/tiktoken.js";
2
- // https://www.npmjs.com/package/js-tiktoken
3
- export const getModelNameForTiktoken = (modelName) => {
4
- if (modelName.startsWith("gpt-3.5-turbo-16k")) {
5
- return "gpt-3.5-turbo-16k";
6
- }
7
- if (modelName.startsWith("gpt-3.5-turbo-")) {
8
- return "gpt-3.5-turbo";
9
- }
10
- if (modelName.startsWith("gpt-4-32k")) {
11
- return "gpt-4-32k";
12
- }
13
- if (modelName.startsWith("gpt-4-")) {
14
- return "gpt-4";
15
- }
16
- return modelName;
17
- };
18
- export const getEmbeddingContextSize = (modelName) => {
19
- switch (modelName) {
20
- case "text-embedding-ada-002":
21
- return 8191;
22
- default:
23
- return 2046;
24
- }
25
- };
26
- export const getModelContextSize = (modelName) => {
27
- switch (getModelNameForTiktoken(modelName)) {
28
- case "gpt-3.5-turbo-16k":
29
- return 16384;
30
- case "gpt-3.5-turbo":
31
- return 4096;
32
- case "gpt-4-32k":
33
- return 32768;
34
- case "gpt-4":
35
- return 8192;
36
- case "text-davinci-003":
37
- return 4097;
38
- case "text-curie-001":
39
- return 2048;
40
- case "text-babbage-001":
41
- return 2048;
42
- case "text-ada-001":
43
- return 2048;
44
- case "code-davinci-002":
45
- return 8000;
46
- case "code-cushman-001":
47
- return 2048;
48
- default:
49
- return 4097;
50
- }
51
- };
52
- export const calculateMaxTokens = async ({ prompt, modelName, }) => {
53
- let numTokens;
54
- try {
55
- numTokens = (await encodingForModel(getModelNameForTiktoken(modelName))).encode(prompt).length;
56
- }
57
- catch (error) {
58
- console.warn("Failed to calculate number of tokens, falling back to approximate count");
59
- // fallback to approximate calculation if tiktoken is not available
60
- // each token is ~4 characters: https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them#
61
- numTokens = Math.ceil(prompt.length / 4);
62
- }
63
- const maxTokens = getModelContextSize(modelName);
64
- return maxTokens - numTokens;
65
- };
1
+ export { calculateMaxTokens, getModelContextSize, getEmbeddingContextSize, } from "langchain-core/language_models/base";
@@ -1,204 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getModelContextSize = exports.calculateMaxTokens = exports.BaseLanguageModel = exports.BaseLangChain = void 0;
4
- const index_js_1 = require("../schema/index.cjs");
5
- const async_caller_js_1 = require("../util/async_caller.cjs");
6
- const count_tokens_js_1 = require("./count_tokens.cjs");
7
- const tiktoken_js_1 = require("../util/tiktoken.cjs");
8
- const index_js_2 = require("../schema/runnable/index.cjs");
9
- const base_js_1 = require("../prompts/base.cjs");
10
- const chat_js_1 = require("../prompts/chat.cjs");
11
- const index_js_3 = require("../cache/index.cjs");
12
- const getVerbosity = () => false;
13
- /**
14
- * Base class for language models, chains, tools.
15
- */
16
- class BaseLangChain extends index_js_2.Runnable {
17
- get lc_attributes() {
18
- return {
19
- callbacks: undefined,
20
- verbose: undefined,
21
- };
22
- }
23
- constructor(params) {
24
- super(params);
25
- /**
26
- * Whether to print out response text.
27
- */
28
- Object.defineProperty(this, "verbose", {
29
- enumerable: true,
30
- configurable: true,
31
- writable: true,
32
- value: void 0
33
- });
34
- Object.defineProperty(this, "callbacks", {
35
- enumerable: true,
36
- configurable: true,
37
- writable: true,
38
- value: void 0
39
- });
40
- Object.defineProperty(this, "tags", {
41
- enumerable: true,
42
- configurable: true,
43
- writable: true,
44
- value: void 0
45
- });
46
- Object.defineProperty(this, "metadata", {
47
- enumerable: true,
48
- configurable: true,
49
- writable: true,
50
- value: void 0
51
- });
52
- this.verbose = params.verbose ?? getVerbosity();
53
- this.callbacks = params.callbacks;
54
- this.tags = params.tags ?? [];
55
- this.metadata = params.metadata ?? {};
56
- }
57
- }
58
- exports.BaseLangChain = BaseLangChain;
59
- /**
60
- * Base class for language models.
61
- */
62
- class BaseLanguageModel extends BaseLangChain {
63
- /**
64
- * Keys that the language model accepts as call options.
65
- */
66
- get callKeys() {
67
- return ["stop", "timeout", "signal", "tags", "metadata", "callbacks"];
68
- }
69
- constructor({ callbacks, callbackManager, ...params }) {
70
- super({
71
- callbacks: callbacks ?? callbackManager,
72
- ...params,
73
- });
74
- /**
75
- * The async caller should be used by subclasses to make any async calls,
76
- * which will thus benefit from the concurrency and retry logic.
77
- */
78
- Object.defineProperty(this, "caller", {
79
- enumerable: true,
80
- configurable: true,
81
- writable: true,
82
- value: void 0
83
- });
84
- Object.defineProperty(this, "cache", {
85
- enumerable: true,
86
- configurable: true,
87
- writable: true,
88
- value: void 0
89
- });
90
- Object.defineProperty(this, "_encoding", {
91
- enumerable: true,
92
- configurable: true,
93
- writable: true,
94
- value: void 0
95
- });
96
- if (typeof params.cache === "object") {
97
- this.cache = params.cache;
98
- }
99
- else if (params.cache) {
100
- this.cache = index_js_3.InMemoryCache.global();
101
- }
102
- else {
103
- this.cache = undefined;
104
- }
105
- this.caller = new async_caller_js_1.AsyncCaller(params ?? {});
106
- }
107
- async getNumTokens(content) {
108
- // TODO: Figure out correct value.
109
- if (typeof content !== "string") {
110
- return 0;
111
- }
112
- // fallback to approximate calculation if tiktoken is not available
113
- let numTokens = Math.ceil(content.length / 4);
114
- if (!this._encoding) {
115
- try {
116
- this._encoding = await (0, tiktoken_js_1.encodingForModel)("modelName" in this
117
- ? (0, count_tokens_js_1.getModelNameForTiktoken)(this.modelName)
118
- : "gpt2");
119
- }
120
- catch (error) {
121
- console.warn("Failed to calculate number of tokens, falling back to approximate count", error);
122
- }
123
- }
124
- if (this._encoding) {
125
- numTokens = this._encoding.encode(content).length;
126
- }
127
- return numTokens;
128
- }
129
- static _convertInputToPromptValue(input) {
130
- if (typeof input === "string") {
131
- return new base_js_1.StringPromptValue(input);
132
- }
133
- else if (Array.isArray(input)) {
134
- return new chat_js_1.ChatPromptValue(input.map(index_js_1.coerceMessageLikeToMessage));
135
- }
136
- else {
137
- return input;
138
- }
139
- }
140
- /**
141
- * Get the identifying parameters of the LLM.
142
- */
143
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
144
- _identifyingParams() {
145
- return {};
146
- }
147
- /**
148
- * Create a unique cache key for a specific call to a specific language model.
149
- * @param callOptions Call options for the model
150
- * @returns A unique cache key.
151
- */
152
- _getSerializedCacheKeyParametersForCall(callOptions) {
153
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
154
- const params = {
155
- ...this._identifyingParams(),
156
- ...callOptions,
157
- _type: this._llmType(),
158
- _model: this._modelType(),
159
- };
160
- const filteredEntries = Object.entries(params).filter(([_, value]) => value !== undefined);
161
- const serializedEntries = filteredEntries
162
- .map(([key, value]) => `${key}:${JSON.stringify(value)}`)
163
- .sort()
164
- .join(",");
165
- return serializedEntries;
166
- }
167
- /**
168
- * @deprecated
169
- * Return a json-like object representing this LLM.
170
- */
171
- serialize() {
172
- return {
173
- ...this._identifyingParams(),
174
- _type: this._llmType(),
175
- _model: this._modelType(),
176
- };
177
- }
178
- /**
179
- * @deprecated
180
- * Load an LLM from a json-like object describing it.
181
- */
182
- static async deserialize(data) {
183
- const { _type, _model, ...rest } = data;
184
- if (_model && _model !== "base_chat_model") {
185
- throw new Error(`Cannot load LLM with model ${_model}`);
186
- }
187
- const Cls = {
188
- openai: (await import("../chat_models/openai.js")).ChatOpenAI,
189
- }[_type];
190
- if (Cls === undefined) {
191
- throw new Error(`Cannot load LLM with type ${_type}`);
192
- }
193
- return new Cls(rest);
194
- }
195
- }
196
- exports.BaseLanguageModel = BaseLanguageModel;
4
+ var base_1 = require("langchain-core/language_models/base");
5
+ Object.defineProperty(exports, "BaseLangChain", { enumerable: true, get: function () { return base_1.BaseLangChain; } });
6
+ Object.defineProperty(exports, "BaseLanguageModel", { enumerable: true, get: function () { return base_1.BaseLanguageModel; } });
197
7
  /*
198
8
  * Export utility functions for token calculations:
199
9
  * - calculateMaxTokens: Calculate max tokens for a given model and prompt (the model context size - tokens in prompt).
200
10
  * - getModelContextSize: Get the context size for a specific model.
201
11
  */
202
- var count_tokens_js_2 = require("./count_tokens.cjs");
203
- Object.defineProperty(exports, "calculateMaxTokens", { enumerable: true, get: function () { return count_tokens_js_2.calculateMaxTokens; } });
204
- Object.defineProperty(exports, "getModelContextSize", { enumerable: true, get: function () { return count_tokens_js_2.getModelContextSize; } });
12
+ var count_tokens_js_1 = require("./count_tokens.cjs");
13
+ Object.defineProperty(exports, "calculateMaxTokens", { enumerable: true, get: function () { return count_tokens_js_1.calculateMaxTokens; } });
14
+ Object.defineProperty(exports, "getModelContextSize", { enumerable: true, get: function () { return count_tokens_js_1.getModelContextSize; } });
@@ -1,112 +1,2 @@
1
- import type { OpenAI as OpenAIClient } from "openai";
2
- import { BaseCache, BaseMessage, BaseMessageLike, BasePromptValue, LLMResult, MessageContent } from "../schema/index.js";
3
- import { BaseCallbackConfig, CallbackManager, Callbacks } from "../callbacks/manager.js";
4
- import { AsyncCaller, AsyncCallerParams } from "../util/async_caller.js";
5
- import { Runnable } from "../schema/runnable/index.js";
6
- import { RunnableConfig } from "../schema/runnable/config.js";
7
- export type SerializedLLM = {
8
- _model: string;
9
- _type: string;
10
- } & Record<string, any>;
11
- export interface BaseLangChainParams {
12
- verbose?: boolean;
13
- callbacks?: Callbacks;
14
- tags?: string[];
15
- metadata?: Record<string, unknown>;
16
- }
17
- /**
18
- * Base class for language models, chains, tools.
19
- */
20
- export declare abstract class BaseLangChain<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> implements BaseLangChainParams {
21
- /**
22
- * Whether to print out response text.
23
- */
24
- verbose: boolean;
25
- callbacks?: Callbacks;
26
- tags?: string[];
27
- metadata?: Record<string, unknown>;
28
- get lc_attributes(): {
29
- [key: string]: undefined;
30
- } | undefined;
31
- constructor(params: BaseLangChainParams);
32
- }
33
- /**
34
- * Base interface for language model parameters.
35
- * A subclass of {@link BaseLanguageModel} should have a constructor that
36
- * takes in a parameter that extends this interface.
37
- */
38
- export interface BaseLanguageModelParams extends AsyncCallerParams, BaseLangChainParams {
39
- /**
40
- * @deprecated Use `callbacks` instead
41
- */
42
- callbackManager?: CallbackManager;
43
- cache?: BaseCache | boolean;
44
- }
45
- export interface BaseLanguageModelCallOptions extends BaseCallbackConfig {
46
- /**
47
- * Stop tokens to use for this call.
48
- * If not provided, the default stop tokens for the model will be used.
49
- */
50
- stop?: string[];
51
- /**
52
- * Timeout for this call in milliseconds.
53
- */
54
- timeout?: number;
55
- /**
56
- * Abort signal for this call.
57
- * If provided, the call will be aborted when the signal is aborted.
58
- * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
59
- */
60
- signal?: AbortSignal;
61
- }
62
- export interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {
63
- function_call?: OpenAIClient.Chat.ChatCompletionFunctionCallOption;
64
- functions?: OpenAIClient.Chat.ChatCompletionCreateParams.Function[];
65
- }
66
- export type BaseLanguageModelInput = BasePromptValue | string | BaseMessageLike[];
67
- /**
68
- * Base class for language models.
69
- */
70
- export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions extends BaseLanguageModelCallOptions = BaseLanguageModelCallOptions> extends BaseLangChain<BaseLanguageModelInput, RunOutput, CallOptions> implements BaseLanguageModelParams {
71
- CallOptions: CallOptions;
72
- /**
73
- * Keys that the language model accepts as call options.
74
- */
75
- get callKeys(): string[];
76
- /**
77
- * The async caller should be used by subclasses to make any async calls,
78
- * which will thus benefit from the concurrency and retry logic.
79
- */
80
- caller: AsyncCaller;
81
- cache?: BaseCache;
82
- constructor({ callbacks, callbackManager, ...params }: BaseLanguageModelParams);
83
- abstract generatePrompt(promptValues: BasePromptValue[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;
84
- abstract predict(text: string, options?: string[] | CallOptions, callbacks?: Callbacks): Promise<string>;
85
- abstract predictMessages(messages: BaseMessage[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<BaseMessage>;
86
- abstract _modelType(): string;
87
- abstract _llmType(): string;
88
- private _encoding?;
89
- getNumTokens(content: MessageContent): Promise<number>;
90
- protected static _convertInputToPromptValue(input: BaseLanguageModelInput): BasePromptValue;
91
- /**
92
- * Get the identifying parameters of the LLM.
93
- */
94
- _identifyingParams(): Record<string, any>;
95
- /**
96
- * Create a unique cache key for a specific call to a specific language model.
97
- * @param callOptions Call options for the model
98
- * @returns A unique cache key.
99
- */
100
- protected _getSerializedCacheKeyParametersForCall(callOptions: CallOptions): string;
101
- /**
102
- * @deprecated
103
- * Return a json-like object representing this LLM.
104
- */
105
- serialize(): SerializedLLM;
106
- /**
107
- * @deprecated
108
- * Load an LLM from a json-like object describing it.
109
- */
110
- static deserialize(data: SerializedLLM): Promise<BaseLanguageModel>;
111
- }
1
+ export { type SerializedLLM, type BaseLangChainParams, BaseLangChain, type BaseLanguageModelParams, type BaseLanguageModelCallOptions, type BaseFunctionCallOptions, type BaseLanguageModelInput, BaseLanguageModel, } from "langchain-core/language_models/base";
112
2
  export { calculateMaxTokens, getModelContextSize } from "./count_tokens.js";