@langchain/core 0.1.35 → 0.1.37
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.
- package/dist/language_models/base.cjs +0 -16
- package/dist/language_models/base.d.ts +36 -7
- package/dist/language_models/base.js +0 -16
- package/dist/output_parsers/openai_tools/index.cjs +1 -0
- package/dist/output_parsers/openai_tools/index.d.ts +1 -0
- package/dist/output_parsers/openai_tools/index.js +1 -0
- package/dist/output_parsers/openai_tools/json_output_functions_parsers.cjs +207 -0
- package/dist/output_parsers/openai_tools/json_output_functions_parsers.d.ts +80 -0
- package/dist/output_parsers/openai_tools/json_output_functions_parsers.js +201 -0
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +0 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +0 -1
- package/dist/runnables/base.cjs +13 -6
- package/dist/runnables/base.js +13 -6
- package/dist/types/type-utils.cjs +2 -0
- package/dist/types/type-utils.d.ts +1 -0
- package/dist/types/type-utils.js +1 -0
- package/package.json +2 -2
|
@@ -253,21 +253,5 @@ class BaseLanguageModel extends BaseLangChain {
|
|
|
253
253
|
static async deserialize(_data) {
|
|
254
254
|
throw new Error("Use .toJSON() instead");
|
|
255
255
|
}
|
|
256
|
-
/**
|
|
257
|
-
* Return a new runnable which calls an LLM with structured output.
|
|
258
|
-
* Only available for LLMs that support structured output.
|
|
259
|
-
*
|
|
260
|
-
* @template {any} RunInput The input type for the Runnable.
|
|
261
|
-
* @template {z.ZodObject<any, any, any, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.
|
|
262
|
-
*
|
|
263
|
-
* @param {z.ZodEffects<RunOutput> | Record<string, any>} schema The schema for the structured output. Either as a ZOD class or a valid JSON schema object.
|
|
264
|
-
* @param {string} name The name of the function to call.
|
|
265
|
-
* @returns {Runnable<RunInput, RunOutput>} A new runnable that calls the LLM with structured output.
|
|
266
|
-
*/
|
|
267
|
-
withStructuredOutput(
|
|
268
|
-
// @ts-expect-error Var is unused in this base method implementation.
|
|
269
|
-
{ schema, name, }) {
|
|
270
|
-
throw new Error("Method not implemented.");
|
|
271
|
-
}
|
|
272
256
|
}
|
|
273
257
|
exports.BaseLanguageModel = BaseLanguageModel;
|
|
@@ -177,21 +177,50 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
|
|
|
177
177
|
* Load an LLM from a json-like object describing it.
|
|
178
178
|
*/
|
|
179
179
|
static deserialize(_data: SerializedLLM): Promise<BaseLanguageModel>;
|
|
180
|
+
withStructuredOutput?<RunInput = BaseLanguageModelInput, RunOutput extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>>({ schema, name, method, includeRaw, }: {
|
|
181
|
+
schema: z.ZodEffects<RunOutput> | Record<string, any>;
|
|
182
|
+
name: string;
|
|
183
|
+
method?: "functionCalling" | "jsonMode";
|
|
184
|
+
includeRaw: true;
|
|
185
|
+
}): Runnable<RunInput, {
|
|
186
|
+
raw: BaseMessage;
|
|
187
|
+
parsed: RunOutput;
|
|
188
|
+
}>;
|
|
189
|
+
withStructuredOutput?<RunInput = BaseLanguageModelInput, RunOutput extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>>({ schema, name, method, includeRaw, }: {
|
|
190
|
+
schema: z.ZodEffects<RunOutput> | Record<string, any>;
|
|
191
|
+
name: string;
|
|
192
|
+
method?: "functionCalling" | "jsonMode";
|
|
193
|
+
includeRaw?: false;
|
|
194
|
+
}): Runnable<RunInput, RunOutput>;
|
|
180
195
|
/**
|
|
181
|
-
*
|
|
182
|
-
* Only available for LLMs that support structured output.
|
|
196
|
+
* Model wrapper that returns outputs formatted to match the given schema.
|
|
183
197
|
*
|
|
184
|
-
* @template {
|
|
198
|
+
* @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.
|
|
185
199
|
* @template {z.ZodObject<any, any, any, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.
|
|
186
200
|
*
|
|
187
|
-
* @param {z.ZodEffects<RunOutput>
|
|
201
|
+
* @param {z.ZodEffects<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.
|
|
188
202
|
* @param {string} name The name of the function to call.
|
|
189
|
-
* @
|
|
203
|
+
* @param {"functionCalling" | "jsonMode"} [method=functionCalling] The method to use for getting the structured output. Defaults to "functionCalling".
|
|
204
|
+
* @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.
|
|
205
|
+
* @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.
|
|
190
206
|
*/
|
|
191
|
-
withStructuredOutput
|
|
207
|
+
withStructuredOutput?<RunInput extends BaseLanguageModelInput = BaseLanguageModelInput, RunOutput extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>>({ schema, name,
|
|
208
|
+
/**
|
|
209
|
+
* @default functionCalling
|
|
210
|
+
*/
|
|
211
|
+
method,
|
|
212
|
+
/**
|
|
213
|
+
* @default false
|
|
214
|
+
*/
|
|
215
|
+
includeRaw, }: {
|
|
192
216
|
schema: z.ZodEffects<RunOutput> | Record<string, any>;
|
|
193
217
|
name: string;
|
|
194
|
-
|
|
218
|
+
method?: "functionCalling" | "jsonMode";
|
|
219
|
+
includeRaw?: boolean;
|
|
220
|
+
}): Runnable<RunInput, RunOutput> | Runnable<RunInput, {
|
|
221
|
+
raw: BaseMessage;
|
|
222
|
+
parsed: RunOutput;
|
|
223
|
+
}>;
|
|
195
224
|
}
|
|
196
225
|
/**
|
|
197
226
|
* Shared interface for token usage
|
|
@@ -245,20 +245,4 @@ export class BaseLanguageModel extends BaseLangChain {
|
|
|
245
245
|
static async deserialize(_data) {
|
|
246
246
|
throw new Error("Use .toJSON() instead");
|
|
247
247
|
}
|
|
248
|
-
/**
|
|
249
|
-
* Return a new runnable which calls an LLM with structured output.
|
|
250
|
-
* Only available for LLMs that support structured output.
|
|
251
|
-
*
|
|
252
|
-
* @template {any} RunInput The input type for the Runnable.
|
|
253
|
-
* @template {z.ZodObject<any, any, any, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.
|
|
254
|
-
*
|
|
255
|
-
* @param {z.ZodEffects<RunOutput> | Record<string, any>} schema The schema for the structured output. Either as a ZOD class or a valid JSON schema object.
|
|
256
|
-
* @param {string} name The name of the function to call.
|
|
257
|
-
* @returns {Runnable<RunInput, RunOutput>} A new runnable that calls the LLM with structured output.
|
|
258
|
-
*/
|
|
259
|
-
withStructuredOutput(
|
|
260
|
-
// @ts-expect-error Var is unused in this base method implementation.
|
|
261
|
-
{ schema, name, }) {
|
|
262
|
-
throw new Error("Method not implemented.");
|
|
263
|
-
}
|
|
264
248
|
}
|
|
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./json_output_tools_parsers.cjs"), exports);
|
|
18
|
+
__exportStar(require("./json_output_functions_parsers.cjs"), exports);
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonKeyOutputFunctionsParser = exports.JsonOutputFunctionsParser = exports.OutputFunctionsParser = void 0;
|
|
4
|
+
const base_js_1 = require("../base.cjs");
|
|
5
|
+
const json_js_1 = require("../json.cjs");
|
|
6
|
+
const transform_js_1 = require("../transform.cjs");
|
|
7
|
+
const json_patch_js_1 = require("../../utils/json_patch.cjs");
|
|
8
|
+
/**
|
|
9
|
+
* Class for parsing the output of an LLM. Can be configured to return
|
|
10
|
+
* only the arguments of the function call in the output.
|
|
11
|
+
*/
|
|
12
|
+
class OutputFunctionsParser extends base_js_1.BaseLLMOutputParser {
|
|
13
|
+
static lc_name() {
|
|
14
|
+
return "OutputFunctionsParser";
|
|
15
|
+
}
|
|
16
|
+
constructor(config) {
|
|
17
|
+
super();
|
|
18
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: ["langchain", "output_parsers", "openai_functions"]
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: true
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(this, "argsOnly", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: true
|
|
35
|
+
});
|
|
36
|
+
this.argsOnly = config?.argsOnly ?? this.argsOnly;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Parses the output and returns a string representation of the function
|
|
40
|
+
* call or its arguments.
|
|
41
|
+
* @param generations The output of the LLM to parse.
|
|
42
|
+
* @returns A string representation of the function call or its arguments.
|
|
43
|
+
*/
|
|
44
|
+
async parseResult(generations) {
|
|
45
|
+
if ("message" in generations[0]) {
|
|
46
|
+
const gen = generations[0];
|
|
47
|
+
const functionCall = gen.message.additional_kwargs.function_call;
|
|
48
|
+
if (!functionCall) {
|
|
49
|
+
throw new Error(`No function_call in message ${JSON.stringify(generations)}`);
|
|
50
|
+
}
|
|
51
|
+
if (!functionCall.arguments) {
|
|
52
|
+
throw new Error(`No arguments in function_call ${JSON.stringify(generations)}`);
|
|
53
|
+
}
|
|
54
|
+
if (this.argsOnly) {
|
|
55
|
+
return functionCall.arguments;
|
|
56
|
+
}
|
|
57
|
+
return JSON.stringify(functionCall);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
throw new Error(`No message in generations ${JSON.stringify(generations)}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.OutputFunctionsParser = OutputFunctionsParser;
|
|
65
|
+
/**
|
|
66
|
+
* Class for parsing the output of an LLM into a JSON object. Uses an
|
|
67
|
+
* instance of `OutputFunctionsParser` to parse the output.
|
|
68
|
+
*/
|
|
69
|
+
class JsonOutputFunctionsParser extends transform_js_1.BaseCumulativeTransformOutputParser {
|
|
70
|
+
static lc_name() {
|
|
71
|
+
return "JsonOutputFunctionsParser";
|
|
72
|
+
}
|
|
73
|
+
constructor(config) {
|
|
74
|
+
super(config);
|
|
75
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
configurable: true,
|
|
78
|
+
writable: true,
|
|
79
|
+
value: ["langchain", "output_parsers", "openai_functions"]
|
|
80
|
+
});
|
|
81
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
configurable: true,
|
|
84
|
+
writable: true,
|
|
85
|
+
value: true
|
|
86
|
+
});
|
|
87
|
+
Object.defineProperty(this, "outputParser", {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
configurable: true,
|
|
90
|
+
writable: true,
|
|
91
|
+
value: void 0
|
|
92
|
+
});
|
|
93
|
+
Object.defineProperty(this, "argsOnly", {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
configurable: true,
|
|
96
|
+
writable: true,
|
|
97
|
+
value: true
|
|
98
|
+
});
|
|
99
|
+
this.argsOnly = config?.argsOnly ?? this.argsOnly;
|
|
100
|
+
this.outputParser = new OutputFunctionsParser(config);
|
|
101
|
+
}
|
|
102
|
+
_diff(prev, next) {
|
|
103
|
+
if (!next) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
const ops = (0, json_patch_js_1.compare)(prev ?? {}, next);
|
|
107
|
+
return ops;
|
|
108
|
+
}
|
|
109
|
+
async parsePartialResult(generations) {
|
|
110
|
+
const generation = generations[0];
|
|
111
|
+
if (!generation.message) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
const { message } = generation;
|
|
115
|
+
const functionCall = message.additional_kwargs.function_call;
|
|
116
|
+
if (!functionCall) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
if (this.argsOnly) {
|
|
120
|
+
return (0, json_js_1.parsePartialJson)(functionCall.arguments);
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
...functionCall,
|
|
124
|
+
arguments: (0, json_js_1.parsePartialJson)(functionCall.arguments),
|
|
125
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
130
|
+
* only the arguments of the function call are returned.
|
|
131
|
+
* @param generations The output of the LLM to parse.
|
|
132
|
+
* @returns A JSON object representation of the function call or its arguments.
|
|
133
|
+
*/
|
|
134
|
+
async parseResult(generations) {
|
|
135
|
+
const result = await this.outputParser.parseResult(generations);
|
|
136
|
+
if (!result) {
|
|
137
|
+
throw new Error(`No result from "OutputFunctionsParser" ${JSON.stringify(generations)}`);
|
|
138
|
+
}
|
|
139
|
+
return this.parse(result);
|
|
140
|
+
}
|
|
141
|
+
async parse(text) {
|
|
142
|
+
const parsedResult = JSON.parse(text);
|
|
143
|
+
if (this.argsOnly) {
|
|
144
|
+
return parsedResult;
|
|
145
|
+
}
|
|
146
|
+
parsedResult.arguments = JSON.parse(parsedResult.arguments);
|
|
147
|
+
return parsedResult;
|
|
148
|
+
}
|
|
149
|
+
getFormatInstructions() {
|
|
150
|
+
return "";
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
exports.JsonOutputFunctionsParser = JsonOutputFunctionsParser;
|
|
154
|
+
/**
|
|
155
|
+
* Class for parsing the output of an LLM into a JSON object and returning
|
|
156
|
+
* a specific attribute. Uses an instance of `JsonOutputFunctionsParser`
|
|
157
|
+
* to parse the output.
|
|
158
|
+
*/
|
|
159
|
+
class JsonKeyOutputFunctionsParser extends base_js_1.BaseLLMOutputParser {
|
|
160
|
+
static lc_name() {
|
|
161
|
+
return "JsonKeyOutputFunctionsParser";
|
|
162
|
+
}
|
|
163
|
+
get lc_aliases() {
|
|
164
|
+
return {
|
|
165
|
+
attrName: "key_name",
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
constructor(fields) {
|
|
169
|
+
super(fields);
|
|
170
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
171
|
+
enumerable: true,
|
|
172
|
+
configurable: true,
|
|
173
|
+
writable: true,
|
|
174
|
+
value: ["langchain", "output_parsers", "openai_functions"]
|
|
175
|
+
});
|
|
176
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
177
|
+
enumerable: true,
|
|
178
|
+
configurable: true,
|
|
179
|
+
writable: true,
|
|
180
|
+
value: true
|
|
181
|
+
});
|
|
182
|
+
Object.defineProperty(this, "outputParser", {
|
|
183
|
+
enumerable: true,
|
|
184
|
+
configurable: true,
|
|
185
|
+
writable: true,
|
|
186
|
+
value: new JsonOutputFunctionsParser()
|
|
187
|
+
});
|
|
188
|
+
Object.defineProperty(this, "attrName", {
|
|
189
|
+
enumerable: true,
|
|
190
|
+
configurable: true,
|
|
191
|
+
writable: true,
|
|
192
|
+
value: void 0
|
|
193
|
+
});
|
|
194
|
+
this.attrName = fields.attrName;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Parses the output and returns a specific attribute of the parsed JSON
|
|
198
|
+
* object.
|
|
199
|
+
* @param generations The output of the LLM to parse.
|
|
200
|
+
* @returns The value of a specific attribute of the parsed JSON object.
|
|
201
|
+
*/
|
|
202
|
+
async parseResult(generations) {
|
|
203
|
+
const result = await this.outputParser.parseResult(generations);
|
|
204
|
+
return result[this.attrName];
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
exports.JsonKeyOutputFunctionsParser = JsonKeyOutputFunctionsParser;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { JsonSchema7ObjectType } from "zod-to-json-schema";
|
|
2
|
+
import { Optional } from "../../types/type-utils.js";
|
|
3
|
+
import { Generation, ChatGeneration } from "../../outputs.js";
|
|
4
|
+
import { BaseLLMOutputParser } from "../base.js";
|
|
5
|
+
import { BaseCumulativeTransformOutputParser, BaseCumulativeTransformOutputParserInput } from "../transform.js";
|
|
6
|
+
import { type Operation as JSONPatchOperation } from "../../utils/json_patch.js";
|
|
7
|
+
/**
|
|
8
|
+
* Represents optional parameters for a function in a JSON Schema.
|
|
9
|
+
*/
|
|
10
|
+
export type FunctionParameters = Optional<JsonSchema7ObjectType, "additionalProperties">;
|
|
11
|
+
/**
|
|
12
|
+
* Class for parsing the output of an LLM. Can be configured to return
|
|
13
|
+
* only the arguments of the function call in the output.
|
|
14
|
+
*/
|
|
15
|
+
export declare class OutputFunctionsParser extends BaseLLMOutputParser<string> {
|
|
16
|
+
static lc_name(): string;
|
|
17
|
+
lc_namespace: string[];
|
|
18
|
+
lc_serializable: boolean;
|
|
19
|
+
argsOnly: boolean;
|
|
20
|
+
constructor(config?: {
|
|
21
|
+
argsOnly?: boolean;
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Parses the output and returns a string representation of the function
|
|
25
|
+
* call or its arguments.
|
|
26
|
+
* @param generations The output of the LLM to parse.
|
|
27
|
+
* @returns A string representation of the function call or its arguments.
|
|
28
|
+
*/
|
|
29
|
+
parseResult(generations: Generation[] | ChatGeneration[]): Promise<string>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Class for parsing the output of an LLM into a JSON object. Uses an
|
|
33
|
+
* instance of `OutputFunctionsParser` to parse the output.
|
|
34
|
+
*/
|
|
35
|
+
export declare class JsonOutputFunctionsParser<Output extends Record<string, any> = Record<string, any>> extends BaseCumulativeTransformOutputParser<Output> {
|
|
36
|
+
static lc_name(): string;
|
|
37
|
+
lc_namespace: string[];
|
|
38
|
+
lc_serializable: boolean;
|
|
39
|
+
outputParser: OutputFunctionsParser;
|
|
40
|
+
argsOnly: boolean;
|
|
41
|
+
constructor(config?: {
|
|
42
|
+
argsOnly?: boolean;
|
|
43
|
+
} & BaseCumulativeTransformOutputParserInput);
|
|
44
|
+
protected _diff(prev: unknown | undefined, next: unknown): JSONPatchOperation[] | undefined;
|
|
45
|
+
parsePartialResult(generations: ChatGeneration[]): Promise<Output | undefined>;
|
|
46
|
+
/**
|
|
47
|
+
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
48
|
+
* only the arguments of the function call are returned.
|
|
49
|
+
* @param generations The output of the LLM to parse.
|
|
50
|
+
* @returns A JSON object representation of the function call or its arguments.
|
|
51
|
+
*/
|
|
52
|
+
parseResult(generations: Generation[] | ChatGeneration[]): Promise<Output>;
|
|
53
|
+
parse(text: string): Promise<Output>;
|
|
54
|
+
getFormatInstructions(): string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Class for parsing the output of an LLM into a JSON object and returning
|
|
58
|
+
* a specific attribute. Uses an instance of `JsonOutputFunctionsParser`
|
|
59
|
+
* to parse the output.
|
|
60
|
+
*/
|
|
61
|
+
export declare class JsonKeyOutputFunctionsParser<T extends Record<string, any> = Record<string, any>> extends BaseLLMOutputParser<T> {
|
|
62
|
+
static lc_name(): string;
|
|
63
|
+
lc_namespace: string[];
|
|
64
|
+
lc_serializable: boolean;
|
|
65
|
+
outputParser: JsonOutputFunctionsParser<Record<string, any>>;
|
|
66
|
+
attrName: string;
|
|
67
|
+
get lc_aliases(): {
|
|
68
|
+
attrName: string;
|
|
69
|
+
};
|
|
70
|
+
constructor(fields: {
|
|
71
|
+
attrName: string;
|
|
72
|
+
});
|
|
73
|
+
/**
|
|
74
|
+
* Parses the output and returns a specific attribute of the parsed JSON
|
|
75
|
+
* object.
|
|
76
|
+
* @param generations The output of the LLM to parse.
|
|
77
|
+
* @returns The value of a specific attribute of the parsed JSON object.
|
|
78
|
+
*/
|
|
79
|
+
parseResult(generations: Generation[] | ChatGeneration[]): Promise<T>;
|
|
80
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { BaseLLMOutputParser } from "../base.js";
|
|
2
|
+
import { parsePartialJson } from "../json.js";
|
|
3
|
+
import { BaseCumulativeTransformOutputParser, } from "../transform.js";
|
|
4
|
+
import { compare, } from "../../utils/json_patch.js";
|
|
5
|
+
/**
|
|
6
|
+
* Class for parsing the output of an LLM. Can be configured to return
|
|
7
|
+
* only the arguments of the function call in the output.
|
|
8
|
+
*/
|
|
9
|
+
export class OutputFunctionsParser extends BaseLLMOutputParser {
|
|
10
|
+
static lc_name() {
|
|
11
|
+
return "OutputFunctionsParser";
|
|
12
|
+
}
|
|
13
|
+
constructor(config) {
|
|
14
|
+
super();
|
|
15
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: ["langchain", "output_parsers", "openai_functions"]
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: true
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "argsOnly", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: true
|
|
32
|
+
});
|
|
33
|
+
this.argsOnly = config?.argsOnly ?? this.argsOnly;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Parses the output and returns a string representation of the function
|
|
37
|
+
* call or its arguments.
|
|
38
|
+
* @param generations The output of the LLM to parse.
|
|
39
|
+
* @returns A string representation of the function call or its arguments.
|
|
40
|
+
*/
|
|
41
|
+
async parseResult(generations) {
|
|
42
|
+
if ("message" in generations[0]) {
|
|
43
|
+
const gen = generations[0];
|
|
44
|
+
const functionCall = gen.message.additional_kwargs.function_call;
|
|
45
|
+
if (!functionCall) {
|
|
46
|
+
throw new Error(`No function_call in message ${JSON.stringify(generations)}`);
|
|
47
|
+
}
|
|
48
|
+
if (!functionCall.arguments) {
|
|
49
|
+
throw new Error(`No arguments in function_call ${JSON.stringify(generations)}`);
|
|
50
|
+
}
|
|
51
|
+
if (this.argsOnly) {
|
|
52
|
+
return functionCall.arguments;
|
|
53
|
+
}
|
|
54
|
+
return JSON.stringify(functionCall);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
throw new Error(`No message in generations ${JSON.stringify(generations)}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Class for parsing the output of an LLM into a JSON object. Uses an
|
|
63
|
+
* instance of `OutputFunctionsParser` to parse the output.
|
|
64
|
+
*/
|
|
65
|
+
export class JsonOutputFunctionsParser extends BaseCumulativeTransformOutputParser {
|
|
66
|
+
static lc_name() {
|
|
67
|
+
return "JsonOutputFunctionsParser";
|
|
68
|
+
}
|
|
69
|
+
constructor(config) {
|
|
70
|
+
super(config);
|
|
71
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
configurable: true,
|
|
74
|
+
writable: true,
|
|
75
|
+
value: ["langchain", "output_parsers", "openai_functions"]
|
|
76
|
+
});
|
|
77
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true,
|
|
81
|
+
value: true
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(this, "outputParser", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: void 0
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(this, "argsOnly", {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
configurable: true,
|
|
92
|
+
writable: true,
|
|
93
|
+
value: true
|
|
94
|
+
});
|
|
95
|
+
this.argsOnly = config?.argsOnly ?? this.argsOnly;
|
|
96
|
+
this.outputParser = new OutputFunctionsParser(config);
|
|
97
|
+
}
|
|
98
|
+
_diff(prev, next) {
|
|
99
|
+
if (!next) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
const ops = compare(prev ?? {}, next);
|
|
103
|
+
return ops;
|
|
104
|
+
}
|
|
105
|
+
async parsePartialResult(generations) {
|
|
106
|
+
const generation = generations[0];
|
|
107
|
+
if (!generation.message) {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
const { message } = generation;
|
|
111
|
+
const functionCall = message.additional_kwargs.function_call;
|
|
112
|
+
if (!functionCall) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
if (this.argsOnly) {
|
|
116
|
+
return parsePartialJson(functionCall.arguments);
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
...functionCall,
|
|
120
|
+
arguments: parsePartialJson(functionCall.arguments),
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
126
|
+
* only the arguments of the function call are returned.
|
|
127
|
+
* @param generations The output of the LLM to parse.
|
|
128
|
+
* @returns A JSON object representation of the function call or its arguments.
|
|
129
|
+
*/
|
|
130
|
+
async parseResult(generations) {
|
|
131
|
+
const result = await this.outputParser.parseResult(generations);
|
|
132
|
+
if (!result) {
|
|
133
|
+
throw new Error(`No result from "OutputFunctionsParser" ${JSON.stringify(generations)}`);
|
|
134
|
+
}
|
|
135
|
+
return this.parse(result);
|
|
136
|
+
}
|
|
137
|
+
async parse(text) {
|
|
138
|
+
const parsedResult = JSON.parse(text);
|
|
139
|
+
if (this.argsOnly) {
|
|
140
|
+
return parsedResult;
|
|
141
|
+
}
|
|
142
|
+
parsedResult.arguments = JSON.parse(parsedResult.arguments);
|
|
143
|
+
return parsedResult;
|
|
144
|
+
}
|
|
145
|
+
getFormatInstructions() {
|
|
146
|
+
return "";
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Class for parsing the output of an LLM into a JSON object and returning
|
|
151
|
+
* a specific attribute. Uses an instance of `JsonOutputFunctionsParser`
|
|
152
|
+
* to parse the output.
|
|
153
|
+
*/
|
|
154
|
+
export class JsonKeyOutputFunctionsParser extends BaseLLMOutputParser {
|
|
155
|
+
static lc_name() {
|
|
156
|
+
return "JsonKeyOutputFunctionsParser";
|
|
157
|
+
}
|
|
158
|
+
get lc_aliases() {
|
|
159
|
+
return {
|
|
160
|
+
attrName: "key_name",
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
constructor(fields) {
|
|
164
|
+
super(fields);
|
|
165
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
166
|
+
enumerable: true,
|
|
167
|
+
configurable: true,
|
|
168
|
+
writable: true,
|
|
169
|
+
value: ["langchain", "output_parsers", "openai_functions"]
|
|
170
|
+
});
|
|
171
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
172
|
+
enumerable: true,
|
|
173
|
+
configurable: true,
|
|
174
|
+
writable: true,
|
|
175
|
+
value: true
|
|
176
|
+
});
|
|
177
|
+
Object.defineProperty(this, "outputParser", {
|
|
178
|
+
enumerable: true,
|
|
179
|
+
configurable: true,
|
|
180
|
+
writable: true,
|
|
181
|
+
value: new JsonOutputFunctionsParser()
|
|
182
|
+
});
|
|
183
|
+
Object.defineProperty(this, "attrName", {
|
|
184
|
+
enumerable: true,
|
|
185
|
+
configurable: true,
|
|
186
|
+
writable: true,
|
|
187
|
+
value: void 0
|
|
188
|
+
});
|
|
189
|
+
this.attrName = fields.attrName;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Parses the output and returns a specific attribute of the parsed JSON
|
|
193
|
+
* object.
|
|
194
|
+
* @param generations The output of the LLM to parse.
|
|
195
|
+
* @returns The value of a specific attribute of the parsed JSON object.
|
|
196
|
+
*/
|
|
197
|
+
async parseResult(generations) {
|
|
198
|
+
const result = await this.outputParser.parseResult(generations);
|
|
199
|
+
return result[this.attrName];
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -77,7 +77,6 @@ exports.JsonOutputToolsParser = JsonOutputToolsParser;
|
|
|
77
77
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
78
78
|
* expecting only a single tool to be called.
|
|
79
79
|
*/
|
|
80
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
80
|
class JsonOutputKeyToolsParser extends base_js_1.BaseLLMOutputParser {
|
|
82
81
|
static lc_name() {
|
|
83
82
|
return "JsonOutputKeyToolsParser";
|
|
@@ -40,7 +40,7 @@ export type JsonOutputKeyToolsParserParams = {
|
|
|
40
40
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
41
41
|
* expecting only a single tool to be called.
|
|
42
42
|
*/
|
|
43
|
-
export declare class JsonOutputKeyToolsParser extends
|
|
43
|
+
export declare class JsonOutputKeyToolsParser<T extends Record<string, any> = Record<string, any>> extends BaseLLMOutputParser<T> {
|
|
44
44
|
static lc_name(): string;
|
|
45
45
|
lc_namespace: string[];
|
|
46
46
|
lc_serializable: boolean;
|
|
@@ -73,7 +73,6 @@ export class JsonOutputToolsParser extends BaseLLMOutputParser {
|
|
|
73
73
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
74
74
|
* expecting only a single tool to be called.
|
|
75
75
|
*/
|
|
76
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
76
|
export class JsonOutputKeyToolsParser extends BaseLLMOutputParser {
|
|
78
77
|
static lc_name() {
|
|
79
78
|
return "JsonOutputKeyToolsParser";
|
package/dist/runnables/base.cjs
CHANGED
|
@@ -1283,17 +1283,24 @@ class RunnableLambda extends Runnable {
|
|
|
1283
1283
|
}
|
|
1284
1284
|
async _invoke(input, config, runManager) {
|
|
1285
1285
|
return new Promise((resolve, reject) => {
|
|
1286
|
-
|
|
1286
|
+
const childConfig = (0, config_js_1.patchConfig)(config, {
|
|
1287
|
+
callbacks: runManager?.getChild(),
|
|
1288
|
+
recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
|
|
1289
|
+
});
|
|
1290
|
+
void index_js_1.AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
|
|
1287
1291
|
try {
|
|
1288
|
-
let output = await this.func(input, {
|
|
1292
|
+
let output = await this.func(input, {
|
|
1293
|
+
...childConfig,
|
|
1294
|
+
config: childConfig,
|
|
1295
|
+
});
|
|
1289
1296
|
if (output && Runnable.isRunnable(output)) {
|
|
1290
1297
|
if (config?.recursionLimit === 0) {
|
|
1291
1298
|
throw new Error("Recursion limit reached.");
|
|
1292
1299
|
}
|
|
1293
|
-
output = await output.invoke(input,
|
|
1294
|
-
|
|
1295
|
-
recursionLimit: (
|
|
1296
|
-
})
|
|
1300
|
+
output = await output.invoke(input, {
|
|
1301
|
+
...childConfig,
|
|
1302
|
+
recursionLimit: (childConfig.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
|
|
1303
|
+
});
|
|
1297
1304
|
}
|
|
1298
1305
|
resolve(output);
|
|
1299
1306
|
}
|
package/dist/runnables/base.js
CHANGED
|
@@ -1270,17 +1270,24 @@ export class RunnableLambda extends Runnable {
|
|
|
1270
1270
|
}
|
|
1271
1271
|
async _invoke(input, config, runManager) {
|
|
1272
1272
|
return new Promise((resolve, reject) => {
|
|
1273
|
-
|
|
1273
|
+
const childConfig = patchConfig(config, {
|
|
1274
|
+
callbacks: runManager?.getChild(),
|
|
1275
|
+
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
|
|
1276
|
+
});
|
|
1277
|
+
void AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
|
|
1274
1278
|
try {
|
|
1275
|
-
let output = await this.func(input, {
|
|
1279
|
+
let output = await this.func(input, {
|
|
1280
|
+
...childConfig,
|
|
1281
|
+
config: childConfig,
|
|
1282
|
+
});
|
|
1276
1283
|
if (output && Runnable.isRunnable(output)) {
|
|
1277
1284
|
if (config?.recursionLimit === 0) {
|
|
1278
1285
|
throw new Error("Recursion limit reached.");
|
|
1279
1286
|
}
|
|
1280
|
-
output = await output.invoke(input,
|
|
1281
|
-
|
|
1282
|
-
recursionLimit: (
|
|
1283
|
-
})
|
|
1287
|
+
output = await output.invoke(input, {
|
|
1288
|
+
...childConfig,
|
|
1289
|
+
recursionLimit: (childConfig.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
|
|
1290
|
+
});
|
|
1284
1291
|
}
|
|
1285
1292
|
resolve(output);
|
|
1286
1293
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.37",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"camelcase": "6",
|
|
44
44
|
"decamelize": "1.2.0",
|
|
45
45
|
"js-tiktoken": "^1.0.8",
|
|
46
|
-
"langsmith": "~0.1.
|
|
46
|
+
"langsmith": "~0.1.7",
|
|
47
47
|
"ml-distance": "^4.0.0",
|
|
48
48
|
"p-queue": "^6.6.2",
|
|
49
49
|
"p-retry": "4",
|