langchain 0.0.189 → 0.0.191

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.
@@ -62,6 +62,10 @@ class HttpResponseOutputParser extends output_parser_js_1.BaseTransformOutputPar
62
62
  */
63
63
  async parse(text) {
64
64
  const chunk = await this.outputParser.parse(text);
65
+ const encoder = new TextEncoder();
66
+ if (this.contentType === "text/event-stream") {
67
+ return encoder.encode(`event: data\ndata: ${JSON.stringify(chunk)}\n\n`);
68
+ }
65
69
  let parsedChunk;
66
70
  if (typeof chunk === "string") {
67
71
  parsedChunk = chunk;
@@ -69,10 +73,6 @@ class HttpResponseOutputParser extends output_parser_js_1.BaseTransformOutputPar
69
73
  else {
70
74
  parsedChunk = JSON.stringify(chunk);
71
75
  }
72
- const encoder = new TextEncoder();
73
- if (this.contentType === "text/event-stream") {
74
- return encoder.encode(`event: data\ndata: ${parsedChunk}\n\n`);
75
- }
76
76
  return encoder.encode(parsedChunk);
77
77
  }
78
78
  getFormatInstructions() {
@@ -59,6 +59,10 @@ export class HttpResponseOutputParser extends BaseTransformOutputParser {
59
59
  */
60
60
  async parse(text) {
61
61
  const chunk = await this.outputParser.parse(text);
62
+ const encoder = new TextEncoder();
63
+ if (this.contentType === "text/event-stream") {
64
+ return encoder.encode(`event: data\ndata: ${JSON.stringify(chunk)}\n\n`);
65
+ }
62
66
  let parsedChunk;
63
67
  if (typeof chunk === "string") {
64
68
  parsedChunk = chunk;
@@ -66,10 +70,6 @@ export class HttpResponseOutputParser extends BaseTransformOutputParser {
66
70
  else {
67
71
  parsedChunk = JSON.stringify(chunk);
68
72
  }
69
- const encoder = new TextEncoder();
70
- if (this.contentType === "text/event-stream") {
71
- return encoder.encode(`event: data\ndata: ${parsedChunk}\n\n`);
72
- }
73
73
  return encoder.encode(parsedChunk);
74
74
  }
75
75
  getFormatInstructions() {
@@ -32,7 +32,30 @@ class RunnableAssign extends base_js_1.Runnable {
32
32
  }
33
33
  exports.RunnableAssign = RunnableAssign;
34
34
  /**
35
- * A runnable that passes through the input.
35
+ * A runnable to passthrough inputs unchanged or with additional keys.
36
+ *
37
+ * This runnable behaves almost like the identity function, except that it
38
+ * can be configured to add additional keys to the output, if the input is
39
+ * an object.
40
+ *
41
+ * The example below demonstrates how to use `RunnablePassthrough to
42
+ * passthrough the input from the `.invoke()`
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * const chain = RunnableSequence.from([
47
+ * {
48
+ * question: new RunnablePassthrough(),
49
+ * context: async () => loadContextFromStore(),
50
+ * },
51
+ * prompt,
52
+ * llm,
53
+ * outputParser,
54
+ * ]);
55
+ * const response = await chain.invoke(
56
+ * "I can pass a single string instead of an object since I'm using `RunnablePassthrough`."
57
+ * );
58
+ * ```
36
59
  */
37
60
  class RunnablePassthrough extends base_js_1.Runnable {
38
61
  constructor() {
@@ -56,6 +79,34 @@ class RunnablePassthrough extends base_js_1.Runnable {
56
79
  async invoke(input, options) {
57
80
  return this._callWithConfig((input) => Promise.resolve(input), input, options);
58
81
  }
82
+ /**
83
+ * A runnable that assigns key-value pairs to the input.
84
+ *
85
+ * The example below shows how you could use it with an inline function.
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * const prompt =
90
+ * PromptTemplate.fromTemplate(`Write a SQL query to answer the question using the following schema: {schema}
91
+ * Question: {question}
92
+ * SQL Query:`);
93
+ *
94
+ * // The `RunnablePassthrough.assign()` is used here to passthrough the input from the `.invoke()`
95
+ * // call (in this example it's the question), along with any inputs passed to the `.assign()` method.
96
+ * // In this case, we're passing the schema.
97
+ * const sqlQueryGeneratorChain = RunnableSequence.from([
98
+ * RunnablePassthrough.assign({
99
+ * schema: async () => db.getTableInfo(),
100
+ * }),
101
+ * prompt,
102
+ * new ChatOpenAI({}).bind({ stop: ["\nSQLResult:"] }),
103
+ * new StringOutputParser(),
104
+ * ]);
105
+ * const result = await sqlQueryGeneratorChain.invoke({
106
+ * question: "How many employees are there?",
107
+ * });
108
+ * ```
109
+ */
59
110
  static assign(
60
111
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
112
  mapping) {
@@ -10,12 +10,63 @@ export declare class RunnableAssign<RunInput extends Record<string, any> = any,
10
10
  invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
11
11
  }
12
12
  /**
13
- * A runnable that passes through the input.
13
+ * A runnable to passthrough inputs unchanged or with additional keys.
14
+ *
15
+ * This runnable behaves almost like the identity function, except that it
16
+ * can be configured to add additional keys to the output, if the input is
17
+ * an object.
18
+ *
19
+ * The example below demonstrates how to use `RunnablePassthrough to
20
+ * passthrough the input from the `.invoke()`
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const chain = RunnableSequence.from([
25
+ * {
26
+ * question: new RunnablePassthrough(),
27
+ * context: async () => loadContextFromStore(),
28
+ * },
29
+ * prompt,
30
+ * llm,
31
+ * outputParser,
32
+ * ]);
33
+ * const response = await chain.invoke(
34
+ * "I can pass a single string instead of an object since I'm using `RunnablePassthrough`."
35
+ * );
36
+ * ```
14
37
  */
15
38
  export declare class RunnablePassthrough<RunInput> extends Runnable<RunInput, RunInput> {
16
39
  static lc_name(): string;
17
40
  lc_namespace: string[];
18
41
  lc_serializable: boolean;
19
42
  invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunInput>;
43
+ /**
44
+ * A runnable that assigns key-value pairs to the input.
45
+ *
46
+ * The example below shows how you could use it with an inline function.
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const prompt =
51
+ * PromptTemplate.fromTemplate(`Write a SQL query to answer the question using the following schema: {schema}
52
+ * Question: {question}
53
+ * SQL Query:`);
54
+ *
55
+ * // The `RunnablePassthrough.assign()` is used here to passthrough the input from the `.invoke()`
56
+ * // call (in this example it's the question), along with any inputs passed to the `.assign()` method.
57
+ * // In this case, we're passing the schema.
58
+ * const sqlQueryGeneratorChain = RunnableSequence.from([
59
+ * RunnablePassthrough.assign({
60
+ * schema: async () => db.getTableInfo(),
61
+ * }),
62
+ * prompt,
63
+ * new ChatOpenAI({}).bind({ stop: ["\nSQLResult:"] }),
64
+ * new StringOutputParser(),
65
+ * ]);
66
+ * const result = await sqlQueryGeneratorChain.invoke({
67
+ * question: "How many employees are there?",
68
+ * });
69
+ * ```
70
+ */
20
71
  static assign(mapping: Record<string, RunnableLike<Record<string, unknown>, any>>): RunnableAssign<Record<string, unknown>, Record<string, unknown>>;
21
72
  }
@@ -28,7 +28,30 @@ export class RunnableAssign extends Runnable {
28
28
  }
29
29
  }
30
30
  /**
31
- * A runnable that passes through the input.
31
+ * A runnable to passthrough inputs unchanged or with additional keys.
32
+ *
33
+ * This runnable behaves almost like the identity function, except that it
34
+ * can be configured to add additional keys to the output, if the input is
35
+ * an object.
36
+ *
37
+ * The example below demonstrates how to use `RunnablePassthrough to
38
+ * passthrough the input from the `.invoke()`
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const chain = RunnableSequence.from([
43
+ * {
44
+ * question: new RunnablePassthrough(),
45
+ * context: async () => loadContextFromStore(),
46
+ * },
47
+ * prompt,
48
+ * llm,
49
+ * outputParser,
50
+ * ]);
51
+ * const response = await chain.invoke(
52
+ * "I can pass a single string instead of an object since I'm using `RunnablePassthrough`."
53
+ * );
54
+ * ```
32
55
  */
33
56
  export class RunnablePassthrough extends Runnable {
34
57
  constructor() {
@@ -52,6 +75,34 @@ export class RunnablePassthrough extends Runnable {
52
75
  async invoke(input, options) {
53
76
  return this._callWithConfig((input) => Promise.resolve(input), input, options);
54
77
  }
78
+ /**
79
+ * A runnable that assigns key-value pairs to the input.
80
+ *
81
+ * The example below shows how you could use it with an inline function.
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * const prompt =
86
+ * PromptTemplate.fromTemplate(`Write a SQL query to answer the question using the following schema: {schema}
87
+ * Question: {question}
88
+ * SQL Query:`);
89
+ *
90
+ * // The `RunnablePassthrough.assign()` is used here to passthrough the input from the `.invoke()`
91
+ * // call (in this example it's the question), along with any inputs passed to the `.assign()` method.
92
+ * // In this case, we're passing the schema.
93
+ * const sqlQueryGeneratorChain = RunnableSequence.from([
94
+ * RunnablePassthrough.assign({
95
+ * schema: async () => db.getTableInfo(),
96
+ * }),
97
+ * prompt,
98
+ * new ChatOpenAI({}).bind({ stop: ["\nSQLResult:"] }),
99
+ * new StringOutputParser(),
100
+ * ]);
101
+ * const result = await sqlQueryGeneratorChain.invoke({
102
+ * question: "How many employees are there?",
103
+ * });
104
+ * ```
105
+ */
55
106
  static assign(
56
107
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
108
  mapping) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.189",
3
+ "version": "0.0.191",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -809,7 +809,7 @@
809
809
  "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rimraf dist/tests dist/**/tests",
810
810
  "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && node scripts/move-cjs-to-dist.js && rimraf dist-cjs",
811
811
  "build:watch": "node scripts/create-entrypoints.js && tsc --outDir dist/ --watch",
812
- "build:scripts": "node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js && yarn conditional:api_refs && node scripts/generate-docs-llm-compatibility-table",
812
+ "build:scripts": "node scripts/create-entrypoints.js && node scripts/check-tree-shaking.js",
813
813
  "conditional:api_refs": "bash scripts/build-api-refs.sh",
814
814
  "lint": "NODE_OPTIONS=--max-old-space-size=4096 eslint src && dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
815
815
  "lint:fix": "yarn lint --fix",