langchain 0.0.195 → 0.0.196

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 (216) hide show
  1. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  2. package/dist/base_language/count_tokens.cjs +5 -70
  3. package/dist/base_language/count_tokens.d.ts +1 -10
  4. package/dist/base_language/count_tokens.js +1 -65
  5. package/dist/base_language/index.cjs +6 -196
  6. package/dist/base_language/index.d.ts +1 -111
  7. package/dist/base_language/index.js +1 -191
  8. package/dist/cache/base.cjs +15 -37
  9. package/dist/cache/base.d.ts +1 -20
  10. package/dist/cache/base.js +1 -33
  11. package/dist/cache/index.cjs +2 -46
  12. package/dist/cache/index.d.ts +1 -29
  13. package/dist/cache/index.js +1 -45
  14. package/dist/callbacks/base.cjs +3 -139
  15. package/dist/callbacks/base.d.ts +1 -266
  16. package/dist/callbacks/base.js +1 -126
  17. package/dist/callbacks/handlers/console.cjs +14 -221
  18. package/dist/callbacks/handlers/console.d.ts +1 -117
  19. package/dist/callbacks/handlers/console.js +1 -217
  20. package/dist/callbacks/handlers/initialize.cjs +15 -30
  21. package/dist/callbacks/handlers/initialize.d.ts +1 -16
  22. package/dist/callbacks/handlers/initialize.js +1 -27
  23. package/dist/callbacks/handlers/log_stream.cjs +15 -293
  24. package/dist/callbacks/handlers/log_stream.d.ts +1 -100
  25. package/dist/callbacks/handlers/log_stream.js +1 -289
  26. package/dist/callbacks/handlers/run_collector.cjs +15 -48
  27. package/dist/callbacks/handlers/run_collector.d.ts +1 -26
  28. package/dist/callbacks/handlers/run_collector.js +1 -46
  29. package/dist/callbacks/handlers/tracer.cjs +15 -375
  30. package/dist/callbacks/handlers/tracer.d.ts +1 -70
  31. package/dist/callbacks/handlers/tracer.js +1 -373
  32. package/dist/callbacks/handlers/tracer_langchain.cjs +15 -104
  33. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -41
  34. package/dist/callbacks/handlers/tracer_langchain.js +1 -102
  35. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +15 -197
  36. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -57
  37. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -195
  38. package/dist/callbacks/manager.cjs +15 -676
  39. package/dist/callbacks/manager.d.ts +1 -180
  40. package/dist/callbacks/manager.js +1 -666
  41. package/dist/callbacks/promises.cjs +14 -42
  42. package/dist/callbacks/promises.d.ts +1 -11
  43. package/dist/callbacks/promises.js +1 -37
  44. package/dist/chains/graph_qa/prompts.d.ts +1 -1
  45. package/dist/chains/openai_functions/structured_output.cjs +2 -2
  46. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  47. package/dist/chains/openai_functions/structured_output.js +1 -1
  48. package/dist/chat_models/baiduwenxin.d.ts +1 -1
  49. package/dist/chat_models/base.cjs +15 -296
  50. package/dist/chat_models/base.d.ts +1 -122
  51. package/dist/chat_models/base.js +1 -292
  52. package/dist/chat_models/bedrock/web.d.ts +1 -1
  53. package/dist/chat_models/fireworks.d.ts +1 -1
  54. package/dist/document.cjs +2 -24
  55. package/dist/document.d.ts +1 -12
  56. package/dist/document.js +1 -23
  57. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  58. package/dist/document_loaders/web/s3.d.ts +1 -1
  59. package/dist/embeddings/base.cjs +15 -22
  60. package/dist/embeddings/base.d.ts +1 -33
  61. package/dist/embeddings/base.js +1 -20
  62. package/dist/embeddings/cache_backed.cjs +2 -2
  63. package/dist/embeddings/cache_backed.js +1 -1
  64. package/dist/evaluation/agents/trajectory.d.ts +1 -1
  65. package/dist/evaluation/criteria/prompt.d.ts +2 -2
  66. package/dist/evaluation/qa/prompt.d.ts +2 -2
  67. package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -1
  68. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  69. package/dist/llms/base.cjs +15 -278
  70. package/dist/llms/base.d.ts +1 -115
  71. package/dist/llms/base.js +1 -275
  72. package/dist/llms/bedrock/web.d.ts +1 -1
  73. package/dist/llms/fireworks.d.ts +1 -1
  74. package/dist/load/import_map.cjs +2 -1
  75. package/dist/load/import_map.d.ts +1 -0
  76. package/dist/load/import_map.js +1 -0
  77. package/dist/load/index.cjs +7 -148
  78. package/dist/load/index.js +7 -148
  79. package/dist/load/map_keys.cjs +0 -24
  80. package/dist/load/map_keys.d.ts +0 -6
  81. package/dist/load/map_keys.js +1 -17
  82. package/dist/load/serializable.cjs +15 -178
  83. package/dist/load/serializable.d.ts +1 -66
  84. package/dist/load/serializable.js +1 -175
  85. package/dist/memory/base.cjs +17 -92
  86. package/dist/memory/base.d.ts +2 -68
  87. package/dist/memory/base.js +2 -87
  88. package/dist/output_parsers/openai_functions.cjs +2 -2
  89. package/dist/output_parsers/openai_functions.d.ts +1 -1
  90. package/dist/output_parsers/openai_functions.js +1 -1
  91. package/dist/output_parsers/regex.d.ts +1 -1
  92. package/dist/output_parsers/structured.d.ts +1 -1
  93. package/dist/prompts/base.cjs +8 -183
  94. package/dist/prompts/base.d.ts +3 -132
  95. package/dist/prompts/base.js +3 -178
  96. package/dist/prompts/chat.cjs +15 -477
  97. package/dist/prompts/chat.d.ts +1 -219
  98. package/dist/prompts/chat.js +1 -466
  99. package/dist/prompts/few_shot.cjs +15 -353
  100. package/dist/prompts/few_shot.d.ts +1 -192
  101. package/dist/prompts/few_shot.js +1 -350
  102. package/dist/prompts/index.cjs +3 -2
  103. package/dist/prompts/index.d.ts +2 -1
  104. package/dist/prompts/index.js +2 -1
  105. package/dist/prompts/pipeline.cjs +15 -142
  106. package/dist/prompts/pipeline.d.ts +1 -98
  107. package/dist/prompts/pipeline.js +1 -140
  108. package/dist/prompts/prompt.cjs +15 -146
  109. package/dist/prompts/prompt.d.ts +1 -92
  110. package/dist/prompts/prompt.js +1 -144
  111. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +15 -148
  112. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -89
  113. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -146
  114. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +15 -137
  115. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -91
  116. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -135
  117. package/dist/prompts/selectors/conditional.cjs +15 -74
  118. package/dist/prompts/selectors/conditional.d.ts +1 -63
  119. package/dist/prompts/selectors/conditional.js +1 -69
  120. package/dist/prompts/serde.cjs +15 -0
  121. package/dist/prompts/serde.d.ts +1 -43
  122. package/dist/prompts/serde.js +1 -1
  123. package/dist/prompts/template.cjs +14 -88
  124. package/dist/prompts/template.d.ts +1 -36
  125. package/dist/prompts/template.js +1 -83
  126. package/dist/{util/@cfworker/json-schema → runnables}/index.cjs +1 -1
  127. package/dist/runnables/index.d.ts +1 -0
  128. package/dist/runnables/index.js +1 -0
  129. package/dist/schema/document.cjs +3 -34
  130. package/dist/schema/document.d.ts +2 -29
  131. package/dist/schema/document.js +2 -32
  132. package/dist/schema/index.cjs +37 -612
  133. package/dist/schema/index.d.ts +11 -311
  134. package/dist/schema/index.js +8 -583
  135. package/dist/schema/output_parser.cjs +15 -309
  136. package/dist/schema/output_parser.d.ts +1 -173
  137. package/dist/schema/output_parser.js +1 -301
  138. package/dist/schema/retriever.cjs +15 -77
  139. package/dist/schema/retriever.d.ts +1 -43
  140. package/dist/schema/retriever.js +1 -75
  141. package/dist/schema/runnable/base.cjs +10 -1072
  142. package/dist/schema/runnable/base.d.ts +1 -356
  143. package/dist/schema/runnable/base.js +1 -1060
  144. package/dist/schema/runnable/branch.cjs +2 -131
  145. package/dist/schema/runnable/branch.d.ts +1 -94
  146. package/dist/schema/runnable/branch.js +1 -130
  147. package/dist/schema/runnable/config.cjs +0 -6
  148. package/dist/schema/runnable/config.d.ts +1 -3
  149. package/dist/schema/runnable/config.js +1 -4
  150. package/dist/schema/runnable/index.cjs +15 -16
  151. package/dist/schema/runnable/index.d.ts +1 -5
  152. package/dist/schema/runnable/index.js +1 -4
  153. package/dist/schema/runnable/passthrough.cjs +3 -113
  154. package/dist/schema/runnable/passthrough.d.ts +1 -72
  155. package/dist/schema/runnable/passthrough.js +1 -111
  156. package/dist/schema/runnable/router.cjs +2 -71
  157. package/dist/schema/runnable/router.d.ts +1 -29
  158. package/dist/schema/runnable/router.js +1 -70
  159. package/dist/schema/storage.cjs +15 -8
  160. package/dist/schema/storage.d.ts +1 -57
  161. package/dist/schema/storage.js +1 -6
  162. package/dist/tools/bingserpapi.d.ts +1 -1
  163. package/dist/tools/searchapi.d.ts +1 -1
  164. package/dist/tools/serpapi.d.ts +1 -1
  165. package/dist/tools/serper.d.ts +1 -1
  166. package/dist/util/async_caller.cjs +14 -128
  167. package/dist/util/async_caller.d.ts +1 -45
  168. package/dist/util/async_caller.js +1 -124
  169. package/package.json +11 -5
  170. package/runnables.cjs +1 -0
  171. package/runnables.d.ts +1 -0
  172. package/runnables.js +1 -0
  173. package/dist/util/@cfworker/json-schema/index.d.ts +0 -1
  174. package/dist/util/@cfworker/json-schema/index.js +0 -1
  175. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  176. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  177. package/dist/util/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  178. package/dist/util/@cfworker/json-schema/src/dereference.cjs +0 -169
  179. package/dist/util/@cfworker/json-schema/src/dereference.d.ts +0 -12
  180. package/dist/util/@cfworker/json-schema/src/dereference.js +0 -165
  181. package/dist/util/@cfworker/json-schema/src/format.cjs +0 -139
  182. package/dist/util/@cfworker/json-schema/src/format.d.ts +0 -2
  183. package/dist/util/@cfworker/json-schema/src/format.js +0 -136
  184. package/dist/util/@cfworker/json-schema/src/index.cjs +0 -24
  185. package/dist/util/@cfworker/json-schema/src/index.d.ts +0 -8
  186. package/dist/util/@cfworker/json-schema/src/index.js +0 -8
  187. package/dist/util/@cfworker/json-schema/src/pointer.cjs +0 -11
  188. package/dist/util/@cfworker/json-schema/src/pointer.d.ts +0 -2
  189. package/dist/util/@cfworker/json-schema/src/pointer.js +0 -6
  190. package/dist/util/@cfworker/json-schema/src/types.cjs +0 -2
  191. package/dist/util/@cfworker/json-schema/src/types.d.ts +0 -72
  192. package/dist/util/@cfworker/json-schema/src/types.js +0 -1
  193. package/dist/util/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  194. package/dist/util/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  195. package/dist/util/@cfworker/json-schema/src/ucs2-length.js +0 -24
  196. package/dist/util/@cfworker/json-schema/src/validate.cjs +0 -808
  197. package/dist/util/@cfworker/json-schema/src/validate.d.ts +0 -3
  198. package/dist/util/@cfworker/json-schema/src/validate.js +0 -804
  199. package/dist/util/@cfworker/json-schema/src/validator.cjs +0 -44
  200. package/dist/util/@cfworker/json-schema/src/validator.d.ts +0 -10
  201. package/dist/util/@cfworker/json-schema/src/validator.js +0 -40
  202. package/dist/util/fast-json-patch/index.cjs +0 -49
  203. package/dist/util/fast-json-patch/index.d.ts +0 -22
  204. package/dist/util/fast-json-patch/index.js +0 -16
  205. package/dist/util/fast-json-patch/src/core.cjs +0 -469
  206. package/dist/util/fast-json-patch/src/core.d.ts +0 -111
  207. package/dist/util/fast-json-patch/src/core.js +0 -459
  208. package/dist/util/fast-json-patch/src/duplex.cjs +0 -237
  209. package/dist/util/fast-json-patch/src/duplex.d.ts +0 -23
  210. package/dist/util/fast-json-patch/src/duplex.js +0 -230
  211. package/dist/util/fast-json-patch/src/helpers.cjs +0 -194
  212. package/dist/util/fast-json-patch/src/helpers.d.ts +0 -36
  213. package/dist/util/fast-json-patch/src/helpers.js +0 -181
  214. package/dist/util/js-sha1/hash.cjs +0 -358
  215. package/dist/util/js-sha1/hash.d.ts +0 -1
  216. package/dist/util/js-sha1/hash.js +0 -355
@@ -1,45 +1,17 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
4
15
  };
5
16
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.awaitAllCallbacks = exports.consumeCallback = void 0;
7
- const p_queue_1 = __importDefault(require("p-queue"));
8
- let queue;
9
- /**
10
- * Creates a queue using the p-queue library. The queue is configured to
11
- * auto-start and has a concurrency of 1, meaning it will process tasks
12
- * one at a time.
13
- */
14
- function createQueue() {
15
- const PQueue = "default" in p_queue_1.default ? p_queue_1.default.default : p_queue_1.default;
16
- return new PQueue({
17
- autoStart: true,
18
- concurrency: 1,
19
- });
20
- }
21
- /**
22
- * Consume a promise, either adding it to the queue or waiting for it to resolve
23
- * @param promise Promise to consume
24
- * @param wait Whether to wait for the promise to resolve or resolve immediately
25
- */
26
- async function consumeCallback(promiseFn, wait) {
27
- if (wait === true) {
28
- await promiseFn();
29
- }
30
- else {
31
- if (typeof queue === "undefined") {
32
- queue = createQueue();
33
- }
34
- void queue.add(promiseFn);
35
- }
36
- }
37
- exports.consumeCallback = consumeCallback;
38
- /**
39
- * Waits for all promises in the queue to resolve. If the queue is
40
- * undefined, it immediately resolves a promise.
41
- */
42
- function awaitAllCallbacks() {
43
- return typeof queue !== "undefined" ? queue.onIdle() : Promise.resolve();
44
- }
45
- exports.awaitAllCallbacks = awaitAllCallbacks;
17
+ __exportStar(require("langchain-core/callbacks/promises"), exports);
@@ -1,11 +1 @@
1
- /**
2
- * Consume a promise, either adding it to the queue or waiting for it to resolve
3
- * @param promise Promise to consume
4
- * @param wait Whether to wait for the promise to resolve or resolve immediately
5
- */
6
- export declare function consumeCallback<T>(promiseFn: () => Promise<T> | T | void, wait: boolean): Promise<void>;
7
- /**
8
- * Waits for all promises in the queue to resolve. If the queue is
9
- * undefined, it immediately resolves a promise.
10
- */
11
- export declare function awaitAllCallbacks(): Promise<void>;
1
+ export * from "langchain-core/callbacks/promises";
@@ -1,37 +1 @@
1
- import PQueueMod from "p-queue";
2
- let queue;
3
- /**
4
- * Creates a queue using the p-queue library. The queue is configured to
5
- * auto-start and has a concurrency of 1, meaning it will process tasks
6
- * one at a time.
7
- */
8
- function createQueue() {
9
- const PQueue = "default" in PQueueMod ? PQueueMod.default : PQueueMod;
10
- return new PQueue({
11
- autoStart: true,
12
- concurrency: 1,
13
- });
14
- }
15
- /**
16
- * Consume a promise, either adding it to the queue or waiting for it to resolve
17
- * @param promise Promise to consume
18
- * @param wait Whether to wait for the promise to resolve or resolve immediately
19
- */
20
- export async function consumeCallback(promiseFn, wait) {
21
- if (wait === true) {
22
- await promiseFn();
23
- }
24
- else {
25
- if (typeof queue === "undefined") {
26
- queue = createQueue();
27
- }
28
- void queue.add(promiseFn);
29
- }
30
- }
31
- /**
32
- * Waits for all promises in the queue to resolve. If the queue is
33
- * undefined, it immediately resolves a promise.
34
- */
35
- export function awaitAllCallbacks() {
36
- return typeof queue !== "undefined" ? queue.onIdle() : Promise.resolve();
37
- }
1
+ export * from "langchain-core/callbacks/promises";
@@ -1,7 +1,7 @@
1
1
  import { PromptTemplate } from "../../prompts/prompt.js";
2
2
  export declare const CYPHER_GENERATION_PROMPT: PromptTemplate<{
3
- schema: any;
4
3
  question: any;
4
+ schema: any;
5
5
  }, any>;
6
6
  export declare const CYPHER_QA_PROMPT: PromptTemplate<{
7
7
  context: any;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createStructuredOutputChainFromZod = exports.createStructuredOutputChain = exports.FunctionCallStructuredOutputParser = void 0;
4
4
  const zod_to_json_schema_1 = require("zod-to-json-schema");
5
- const index_js_1 = require("../../util/@cfworker/json-schema/index.cjs");
5
+ const json_schema_1 = require("langchain-core/util/json_schema");
6
6
  const llm_chain_js_1 = require("../llm_chain.cjs");
7
7
  const openai_js_1 = require("../../chat_models/openai.cjs");
8
8
  const output_parser_js_1 = require("../../schema/output_parser.cjs");
@@ -38,7 +38,7 @@ class FunctionCallStructuredOutputParser extends output_parser_js_1.BaseLLMOutpu
38
38
  writable: true,
39
39
  value: void 0
40
40
  });
41
- this.jsonSchemaValidator = new index_js_1.Validator(schema, "7");
41
+ this.jsonSchemaValidator = new json_schema_1.Validator(schema, "7");
42
42
  }
43
43
  /**
44
44
  * Method to parse the result of chat generations. It first parses the
@@ -1,6 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { JsonSchema7Type } from "zod-to-json-schema/src/parseDef.js";
3
- import { Validator } from "../../util/@cfworker/json-schema/index.js";
3
+ import { Validator } from "langchain-core/util/json_schema";
4
4
  import { LLMChain, LLMChainInput } from "../llm_chain.js";
5
5
  import { ChatOpenAI } from "../../chat_models/openai.js";
6
6
  import { BasePromptTemplate } from "../../prompts/index.js";
@@ -1,5 +1,5 @@
1
1
  import { zodToJsonSchema } from "zod-to-json-schema";
2
- import { Validator } from "../../util/@cfworker/json-schema/index.js";
2
+ import { Validator } from "langchain-core/util/json_schema";
3
3
  import { LLMChain } from "../llm_chain.js";
4
4
  import { ChatOpenAI } from "../../chat_models/openai.js";
5
5
  import { BaseLLMOutputParser, OutputParserException, } from "../../schema/output_parser.js";
@@ -131,8 +131,8 @@ export declare class ChatBaiduWenxin extends BaseChatModel implements BaiduWenxi
131
131
  * Get the identifying parameters for the model
132
132
  */
133
133
  identifyingParams(): {
134
- system?: string | undefined;
135
134
  stream?: boolean | undefined;
135
+ system?: string | undefined;
136
136
  temperature?: number | undefined;
137
137
  top_p?: number | undefined;
138
138
  user_id?: string | undefined;
@@ -1,298 +1,17 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SimpleChatModel = exports.BaseChatModel = exports.createChatMessageChunkEncoderStream = void 0;
4
- const index_js_1 = require("../schema/index.cjs");
5
- const index_js_2 = require("../base_language/index.cjs");
6
- const manager_js_1 = require("../callbacks/manager.cjs");
7
- /**
8
- * Creates a transform stream for encoding chat message chunks.
9
- * @deprecated Use {@link BytesOutputParser} instead
10
- * @returns A TransformStream instance that encodes chat message chunks.
11
- */
12
- function createChatMessageChunkEncoderStream() {
13
- const textEncoder = new TextEncoder();
14
- return new TransformStream({
15
- transform(chunk, controller) {
16
- controller.enqueue(textEncoder.encode(typeof chunk.content === "string"
17
- ? chunk.content
18
- : JSON.stringify(chunk.content)));
19
- },
20
- });
21
- }
22
- exports.createChatMessageChunkEncoderStream = createChatMessageChunkEncoderStream;
23
- /**
24
- * Base class for chat models. It extends the BaseLanguageModel class and
25
- * provides methods for generating chat based on input messages.
26
- */
27
- class BaseChatModel extends index_js_2.BaseLanguageModel {
28
- constructor(fields) {
29
- super(fields);
30
- Object.defineProperty(this, "lc_namespace", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: ["langchain", "chat_models", this._llmType()]
35
- });
36
- }
37
- _separateRunnableConfigFromCallOptions(options) {
38
- const [runnableConfig, callOptions] = super._separateRunnableConfigFromCallOptions(options);
39
- if (callOptions?.timeout && !callOptions.signal) {
40
- callOptions.signal = AbortSignal.timeout(callOptions.timeout);
41
- }
42
- return [runnableConfig, callOptions];
43
- }
44
- /**
45
- * Invokes the chat model with a single input.
46
- * @param input The input for the language model.
47
- * @param options The call options.
48
- * @returns A Promise that resolves to a BaseMessageChunk.
49
- */
50
- async invoke(input, options) {
51
- const promptValue = BaseChatModel._convertInputToPromptValue(input);
52
- const result = await this.generatePrompt([promptValue], options, options?.callbacks);
53
- const chatGeneration = result.generations[0][0];
54
- // TODO: Remove cast after figuring out inheritance
55
- return chatGeneration.message;
56
- }
57
- // eslint-disable-next-line require-yield
58
- async *_streamResponseChunks(_messages, _options, _runManager) {
59
- throw new Error("Not implemented.");
60
- }
61
- async *_streamIterator(input, options) {
62
- // Subclass check required to avoid double callbacks with default implementation
63
- if (this._streamResponseChunks ===
64
- BaseChatModel.prototype._streamResponseChunks) {
65
- yield this.invoke(input, options);
66
- }
67
- else {
68
- const prompt = BaseChatModel._convertInputToPromptValue(input);
69
- const messages = prompt.toChatMessages();
70
- const [runnableConfig, callOptions] = this._separateRunnableConfigFromCallOptions(options);
71
- const callbackManager_ = await manager_js_1.CallbackManager.configure(runnableConfig.callbacks, this.callbacks, runnableConfig.tags, this.tags, runnableConfig.metadata, this.metadata, { verbose: this.verbose });
72
- const extra = {
73
- options: callOptions,
74
- invocation_params: this?.invocationParams(callOptions),
75
- batch_size: 1,
76
- };
77
- const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), [messages], undefined, undefined, extra, undefined, undefined, runnableConfig.runName);
78
- let generationChunk;
79
- try {
80
- for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) {
81
- yield chunk.message;
82
- if (!generationChunk) {
83
- generationChunk = chunk;
84
- }
85
- else {
86
- generationChunk = generationChunk.concat(chunk);
87
- }
88
- }
89
- }
90
- catch (err) {
91
- await Promise.all((runManagers ?? []).map((runManager) => runManager?.handleLLMError(err)));
92
- throw err;
93
- }
94
- await Promise.all((runManagers ?? []).map((runManager) => runManager?.handleLLMEnd({
95
- // TODO: Remove cast after figuring out inheritance
96
- generations: [[generationChunk]],
97
- })));
98
- }
99
- }
100
- /** @ignore */
101
- async _generateUncached(messages, parsedOptions, handledOptions) {
102
- const baseMessages = messages.map((messageList) => messageList.map(index_js_1.coerceMessageLikeToMessage));
103
- // create callback manager and start run
104
- const callbackManager_ = await manager_js_1.CallbackManager.configure(handledOptions.callbacks, this.callbacks, handledOptions.tags, this.tags, handledOptions.metadata, this.metadata, { verbose: this.verbose });
105
- const extra = {
106
- options: parsedOptions,
107
- invocation_params: this?.invocationParams(parsedOptions),
108
- batch_size: 1,
109
- };
110
- const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra, undefined, undefined, handledOptions.runName);
111
- // generate results
112
- const results = await Promise.allSettled(baseMessages.map((messageList, i) => this._generate(messageList, { ...parsedOptions, promptIndex: i }, runManagers?.[i])));
113
- // handle results
114
- const generations = [];
115
- const llmOutputs = [];
116
- await Promise.all(results.map(async (pResult, i) => {
117
- if (pResult.status === "fulfilled") {
118
- const result = pResult.value;
119
- generations[i] = result.generations;
120
- llmOutputs[i] = result.llmOutput;
121
- return runManagers?.[i]?.handleLLMEnd({
122
- generations: [result.generations],
123
- llmOutput: result.llmOutput,
124
- });
125
- }
126
- else {
127
- // status === "rejected"
128
- await runManagers?.[i]?.handleLLMError(pResult.reason);
129
- return Promise.reject(pResult.reason);
130
- }
131
- }));
132
- // create combined output
133
- const output = {
134
- generations,
135
- llmOutput: llmOutputs.length
136
- ? this._combineLLMOutput?.(...llmOutputs)
137
- : undefined,
138
- };
139
- Object.defineProperty(output, index_js_1.RUN_KEY, {
140
- value: runManagers
141
- ? { runIds: runManagers?.map((manager) => manager.runId) }
142
- : undefined,
143
- configurable: true,
144
- });
145
- return output;
146
- }
147
- /**
148
- * Generates chat based on the input messages.
149
- * @param messages An array of arrays of BaseMessage instances.
150
- * @param options The call options or an array of stop sequences.
151
- * @param callbacks The callbacks for the language model.
152
- * @returns A Promise that resolves to an LLMResult.
153
- */
154
- async generate(messages, options, callbacks) {
155
- // parse call options
156
- let parsedOptions;
157
- if (Array.isArray(options)) {
158
- parsedOptions = { stop: options };
159
- }
160
- else {
161
- parsedOptions = options;
162
- }
163
- const baseMessages = messages.map((messageList) => messageList.map(index_js_1.coerceMessageLikeToMessage));
164
- const [runnableConfig, callOptions] = this._separateRunnableConfigFromCallOptions(parsedOptions);
165
- runnableConfig.callbacks = runnableConfig.callbacks ?? callbacks;
166
- if (!this.cache) {
167
- return this._generateUncached(baseMessages, callOptions, runnableConfig);
168
- }
169
- const { cache } = this;
170
- const llmStringKey = this._getSerializedCacheKeyParametersForCall(callOptions);
171
- const missingPromptIndices = [];
172
- const generations = await Promise.all(baseMessages.map(async (baseMessage, index) => {
173
- // Join all content into one string for the prompt index
174
- const prompt = BaseChatModel._convertInputToPromptValue(baseMessage).toString();
175
- const result = await cache.lookup(prompt, llmStringKey);
176
- if (!result) {
177
- missingPromptIndices.push(index);
178
- }
179
- return result;
180
- }));
181
- let llmOutput = {};
182
- if (missingPromptIndices.length > 0) {
183
- const results = await this._generateUncached(missingPromptIndices.map((i) => baseMessages[i]), callOptions, runnableConfig);
184
- await Promise.all(results.generations.map(async (generation, index) => {
185
- const promptIndex = missingPromptIndices[index];
186
- generations[promptIndex] = generation;
187
- // Join all content into one string for the prompt index
188
- const prompt = BaseChatModel._convertInputToPromptValue(baseMessages[promptIndex]).toString();
189
- return cache.update(prompt, llmStringKey, generation);
190
- }));
191
- llmOutput = results.llmOutput ?? {};
192
- }
193
- return { generations, llmOutput };
194
- }
195
- /**
196
- * Get the parameters used to invoke the model
197
- */
198
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
199
- invocationParams(_options) {
200
- return {};
201
- }
202
- _modelType() {
203
- return "base_chat_model";
204
- }
205
- /**
206
- * @deprecated
207
- * Return a json-like object representing this LLM.
208
- */
209
- serialize() {
210
- return {
211
- ...this.invocationParams(),
212
- _type: this._llmType(),
213
- _model: this._modelType(),
214
- };
215
- }
216
- /**
217
- * Generates a prompt based on the input prompt values.
218
- * @param promptValues An array of BasePromptValue instances.
219
- * @param options The call options or an array of stop sequences.
220
- * @param callbacks The callbacks for the language model.
221
- * @returns A Promise that resolves to an LLMResult.
222
- */
223
- async generatePrompt(promptValues, options, callbacks) {
224
- const promptMessages = promptValues.map((promptValue) => promptValue.toChatMessages());
225
- return this.generate(promptMessages, options, callbacks);
226
- }
227
- /**
228
- * Makes a single call to the chat model.
229
- * @param messages An array of BaseMessage instances.
230
- * @param options The call options or an array of stop sequences.
231
- * @param callbacks The callbacks for the language model.
232
- * @returns A Promise that resolves to a BaseMessage.
233
- */
234
- async call(messages, options, callbacks) {
235
- const result = await this.generate([messages.map(index_js_1.coerceMessageLikeToMessage)], options, callbacks);
236
- const generations = result.generations;
237
- return generations[0][0].message;
238
- }
239
- /**
240
- * Makes a single call to the chat model with a prompt value.
241
- * @param promptValue The value of the prompt.
242
- * @param options The call options or an array of stop sequences.
243
- * @param callbacks The callbacks for the language model.
244
- * @returns A Promise that resolves to a BaseMessage.
245
- */
246
- async callPrompt(promptValue, options, callbacks) {
247
- const promptMessages = promptValue.toChatMessages();
248
- return this.call(promptMessages, options, callbacks);
249
- }
250
- /**
251
- * Predicts the next message based on the input messages.
252
- * @param messages An array of BaseMessage instances.
253
- * @param options The call options or an array of stop sequences.
254
- * @param callbacks The callbacks for the language model.
255
- * @returns A Promise that resolves to a BaseMessage.
256
- */
257
- async predictMessages(messages, options, callbacks) {
258
- return this.call(messages, options, callbacks);
259
- }
260
- /**
261
- * Predicts the next message based on a text input.
262
- * @param text The text input.
263
- * @param options The call options or an array of stop sequences.
264
- * @param callbacks The callbacks for the language model.
265
- * @returns A Promise that resolves to a string.
266
- */
267
- async predict(text, options, callbacks) {
268
- const message = new index_js_1.HumanMessage(text);
269
- const result = await this.call([message], options, callbacks);
270
- if (typeof result.content !== "string") {
271
- throw new Error("Cannot use predict when output is not a string.");
272
- }
273
- return result.content;
274
- }
275
- }
276
- exports.BaseChatModel = BaseChatModel;
277
- /**
278
- * An abstract class that extends BaseChatModel and provides a simple
279
- * implementation of _generate.
280
- */
281
- class SimpleChatModel extends BaseChatModel {
282
- async _generate(messages, options, runManager) {
283
- const text = await this._call(messages, options, runManager);
284
- const message = new index_js_1.AIMessage(text);
285
- if (typeof message.content !== "string") {
286
- throw new Error("Cannot generate with a simple chat model when output is not a string.");
287
- }
288
- return {
289
- generations: [
290
- {
291
- text: message.content,
292
- message,
293
- },
294
- ],
295
- };
296
- }
297
- }
298
- exports.SimpleChatModel = SimpleChatModel;
17
+ __exportStar(require("langchain-core/chat_model"), exports);
@@ -1,122 +1 @@
1
- import { BaseMessage, BaseMessageChunk, BaseMessageLike, BasePromptValue, ChatGenerationChunk, ChatResult, LLMResult } from "../schema/index.js";
2
- import { BaseLanguageModel, BaseLanguageModelCallOptions, BaseLanguageModelInput, BaseLanguageModelParams } from "../base_language/index.js";
3
- import { CallbackManagerForLLMRun, Callbacks } from "../callbacks/manager.js";
4
- import { RunnableConfig } from "../schema/runnable/config.js";
5
- /**
6
- * Represents a serialized chat model.
7
- */
8
- export type SerializedChatModel = {
9
- _model: string;
10
- _type: string;
11
- } & Record<string, any>;
12
- /**
13
- * Represents a serialized large language model.
14
- */
15
- export type SerializedLLM = {
16
- _model: string;
17
- _type: string;
18
- } & Record<string, any>;
19
- /**
20
- * Represents the parameters for a base chat model.
21
- */
22
- export type BaseChatModelParams = BaseLanguageModelParams;
23
- /**
24
- * Represents the call options for a base chat model.
25
- */
26
- export type BaseChatModelCallOptions = BaseLanguageModelCallOptions;
27
- /**
28
- * Creates a transform stream for encoding chat message chunks.
29
- * @deprecated Use {@link BytesOutputParser} instead
30
- * @returns A TransformStream instance that encodes chat message chunks.
31
- */
32
- export declare function createChatMessageChunkEncoderStream(): TransformStream<BaseMessageChunk, any>;
33
- /**
34
- * Base class for chat models. It extends the BaseLanguageModel class and
35
- * provides methods for generating chat based on input messages.
36
- */
37
- export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions> extends BaseLanguageModel<BaseMessageChunk, CallOptions> {
38
- ParsedCallOptions: Omit<CallOptions, keyof RunnableConfig & "timeout">;
39
- lc_namespace: string[];
40
- constructor(fields: BaseChatModelParams);
41
- abstract _combineLLMOutput?(...llmOutputs: LLMResult["llmOutput"][]): LLMResult["llmOutput"];
42
- protected _separateRunnableConfigFromCallOptions(options?: Partial<CallOptions>): [RunnableConfig, this["ParsedCallOptions"]];
43
- /**
44
- * Invokes the chat model with a single input.
45
- * @param input The input for the language model.
46
- * @param options The call options.
47
- * @returns A Promise that resolves to a BaseMessageChunk.
48
- */
49
- invoke(input: BaseLanguageModelInput, options?: CallOptions): Promise<BaseMessageChunk>;
50
- _streamResponseChunks(_messages: BaseMessage[], _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
51
- _streamIterator(input: BaseLanguageModelInput, options?: CallOptions): AsyncGenerator<BaseMessageChunk>;
52
- /** @ignore */
53
- _generateUncached(messages: BaseMessageLike[][], parsedOptions: this["ParsedCallOptions"], handledOptions: RunnableConfig): Promise<LLMResult>;
54
- /**
55
- * Generates chat based on the input messages.
56
- * @param messages An array of arrays of BaseMessage instances.
57
- * @param options The call options or an array of stop sequences.
58
- * @param callbacks The callbacks for the language model.
59
- * @returns A Promise that resolves to an LLMResult.
60
- */
61
- generate(messages: BaseMessageLike[][], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;
62
- /**
63
- * Get the parameters used to invoke the model
64
- */
65
- invocationParams(_options?: this["ParsedCallOptions"]): any;
66
- _modelType(): string;
67
- abstract _llmType(): string;
68
- /**
69
- * @deprecated
70
- * Return a json-like object representing this LLM.
71
- */
72
- serialize(): SerializedLLM;
73
- /**
74
- * Generates a prompt based on the input prompt values.
75
- * @param promptValues An array of BasePromptValue instances.
76
- * @param options The call options or an array of stop sequences.
77
- * @param callbacks The callbacks for the language model.
78
- * @returns A Promise that resolves to an LLMResult.
79
- */
80
- generatePrompt(promptValues: BasePromptValue[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;
81
- abstract _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
82
- /**
83
- * Makes a single call to the chat model.
84
- * @param messages An array of BaseMessage instances.
85
- * @param options The call options or an array of stop sequences.
86
- * @param callbacks The callbacks for the language model.
87
- * @returns A Promise that resolves to a BaseMessage.
88
- */
89
- call(messages: BaseMessageLike[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<BaseMessage>;
90
- /**
91
- * Makes a single call to the chat model with a prompt value.
92
- * @param promptValue The value of the prompt.
93
- * @param options The call options or an array of stop sequences.
94
- * @param callbacks The callbacks for the language model.
95
- * @returns A Promise that resolves to a BaseMessage.
96
- */
97
- callPrompt(promptValue: BasePromptValue, options?: string[] | CallOptions, callbacks?: Callbacks): Promise<BaseMessage>;
98
- /**
99
- * Predicts the next message based on the input messages.
100
- * @param messages An array of BaseMessage instances.
101
- * @param options The call options or an array of stop sequences.
102
- * @param callbacks The callbacks for the language model.
103
- * @returns A Promise that resolves to a BaseMessage.
104
- */
105
- predictMessages(messages: BaseMessage[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<BaseMessage>;
106
- /**
107
- * Predicts the next message based on a text input.
108
- * @param text The text input.
109
- * @param options The call options or an array of stop sequences.
110
- * @param callbacks The callbacks for the language model.
111
- * @returns A Promise that resolves to a string.
112
- */
113
- predict(text: string, options?: string[] | CallOptions, callbacks?: Callbacks): Promise<string>;
114
- }
115
- /**
116
- * An abstract class that extends BaseChatModel and provides a simple
117
- * implementation of _generate.
118
- */
119
- export declare abstract class SimpleChatModel<CallOptions extends BaseChatModelCallOptions = BaseChatModelCallOptions> extends BaseChatModel<CallOptions> {
120
- abstract _call(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
121
- _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
122
- }
1
+ export * from "langchain-core/chat_model";