@langchain/core 0.3.56 → 0.3.58-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.
- package/dist/language_models/base.d.ts +14 -7
- package/dist/language_models/chat_models.cjs +5 -5
- package/dist/language_models/chat_models.d.ts +9 -3
- package/dist/language_models/chat_models.js +5 -5
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.ts +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +3 -2
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +19 -5
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +3 -2
- package/dist/output_parsers/structured.cjs +8 -7
- package/dist/output_parsers/structured.d.ts +10 -9
- package/dist/output_parsers/structured.js +6 -5
- package/dist/prompts/structured.cjs +7 -9
- package/dist/prompts/structured.js +7 -9
- package/dist/runnables/base.cjs +35 -19
- package/dist/runnables/base.d.ts +33 -13
- package/dist/runnables/base.js +32 -16
- package/dist/runnables/graph.cjs +2 -2
- package/dist/runnables/graph.js +2 -2
- package/dist/runnables/passthrough.cjs +1 -1
- package/dist/runnables/passthrough.d.ts +1 -1
- package/dist/runnables/passthrough.js +1 -1
- package/dist/runnables/types.d.ts +2 -2
- package/dist/tools/index.cjs +10 -12
- package/dist/tools/index.d.ts +11 -8
- package/dist/tools/index.js +8 -10
- package/dist/tools/types.cjs +2 -2
- package/dist/tools/types.d.ts +11 -13
- package/dist/tools/types.js +2 -2
- package/dist/utils/function_calling.cjs +4 -6
- package/dist/utils/function_calling.d.ts +4 -6
- package/dist/utils/function_calling.js +4 -6
- package/dist/utils/json_schema.cjs +6 -2
- package/dist/utils/json_schema.d.ts +3 -2
- package/dist/utils/json_schema.js +6 -2
- package/dist/utils/testing/index.cjs +1 -1
- package/dist/utils/testing/index.d.ts +8 -7
- package/dist/utils/testing/index.js +1 -1
- package/dist/utils/types/index.cjs +1 -1
- package/dist/utils/types/index.d.ts +1 -1
- package/dist/utils/types/index.js +1 -1
- package/dist/utils/types/zod.cjs +426 -0
- package/dist/utils/types/zod.d.ts +173 -0
- package/dist/utils/types/zod.js +406 -0
- package/package.json +2 -2
- package/dist/utils/types/is_zod_schema.cjs +0 -38
- package/dist/utils/types/is_zod_schema.d.ts +0 -8
- package/dist/utils/types/is_zod_schema.js +0 -34
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { TiktokenModel } from "js-tiktoken/lite";
|
|
2
|
-
import {
|
|
2
|
+
import type { ZodType as ZodTypeV3 } from "zod/v3";
|
|
3
|
+
import type { $ZodType as ZodTypeV4 } from "zod/v4/core";
|
|
3
4
|
import { type BaseCache } from "../caches/base.js";
|
|
4
5
|
import { type BasePromptValueInterface } from "../prompt_values.js";
|
|
5
6
|
import { type BaseMessage, type BaseMessageLike, type MessageContent } from "../messages/base.js";
|
|
@@ -9,6 +10,7 @@ import { AsyncCaller, AsyncCallerParams } from "../utils/async_caller.js";
|
|
|
9
10
|
import { Runnable, type RunnableInterface } from "../runnables/base.js";
|
|
10
11
|
import { RunnableConfig } from "../runnables/config.js";
|
|
11
12
|
import { JSONSchema } from "../utils/json_schema.js";
|
|
13
|
+
import { InferInteropZodOutput, InteropZodObject, InteropZodType } from "../utils/types/zod.js";
|
|
12
14
|
export declare const getModelNameForTiktoken: (modelName: string) => TiktokenModel;
|
|
13
15
|
export declare const getEmbeddingContextSize: (modelName?: string) => number;
|
|
14
16
|
export declare const getModelContextSize: (modelName: string) => number;
|
|
@@ -103,7 +105,7 @@ export interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {
|
|
|
103
105
|
functions?: FunctionDefinition[];
|
|
104
106
|
}
|
|
105
107
|
export type BaseLanguageModelInput = BasePromptValueInterface | string | BaseMessageLike[];
|
|
106
|
-
export type StructuredOutputType =
|
|
108
|
+
export type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;
|
|
107
109
|
export type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> = {
|
|
108
110
|
name?: string;
|
|
109
111
|
method?: "functionCalling" | "jsonMode" | "jsonSchema" | string;
|
|
@@ -114,7 +116,7 @@ export type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =
|
|
|
114
116
|
/** @deprecated Use StructuredOutputMethodOptions instead */
|
|
115
117
|
export type StructuredOutputMethodParams<RunOutput, IncludeRaw extends boolean = false> = {
|
|
116
118
|
/** @deprecated Pass schema in as the first argument */
|
|
117
|
-
schema:
|
|
119
|
+
schema: InteropZodType<RunOutput> | Record<string, any>;
|
|
118
120
|
name?: string;
|
|
119
121
|
method?: "functionCalling" | "jsonMode";
|
|
120
122
|
includeRaw?: IncludeRaw;
|
|
@@ -192,8 +194,13 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
|
|
|
192
194
|
* Load an LLM from a json-like object describing it.
|
|
193
195
|
*/
|
|
194
196
|
static deserialize(_data: SerializedLLM): Promise<BaseLanguageModel>;
|
|
195
|
-
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema:
|
|
196
|
-
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema:
|
|
197
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
198
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
199
|
+
raw: BaseMessage;
|
|
200
|
+
parsed: RunOutput;
|
|
201
|
+
}>;
|
|
202
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
203
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
197
204
|
raw: BaseMessage;
|
|
198
205
|
parsed: RunOutput;
|
|
199
206
|
}>;
|
|
@@ -203,14 +210,14 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
|
|
|
203
210
|
* @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.
|
|
204
211
|
* @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.
|
|
205
212
|
*
|
|
206
|
-
* @param {
|
|
213
|
+
* @param {InteropZodType<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.
|
|
207
214
|
* If a Zod schema is passed, the returned attributes will be validated, whereas with JSON schema they will not be.
|
|
208
215
|
* @param {string} name The name of the function to call.
|
|
209
216
|
* @param {"functionCalling" | "jsonMode"} [method=functionCalling] The method to use for getting the structured output. Defaults to "functionCalling".
|
|
210
217
|
* @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.
|
|
211
218
|
* @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.
|
|
212
219
|
*/
|
|
213
|
-
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema:
|
|
220
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
|
|
214
221
|
raw: BaseMessage;
|
|
215
222
|
parsed: RunOutput;
|
|
216
223
|
}>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SimpleChatModel = exports.BaseChatModel = exports.createChatMessageChunkEncoderStream = void 0;
|
|
4
|
-
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
5
4
|
const index_js_1 = require("../messages/index.cjs");
|
|
6
5
|
const outputs_js_1 = require("../outputs.cjs");
|
|
7
6
|
const base_js_1 = require("./base.cjs");
|
|
@@ -9,8 +8,9 @@ const manager_js_1 = require("../callbacks/manager.cjs");
|
|
|
9
8
|
const base_js_2 = require("../runnables/base.cjs");
|
|
10
9
|
const stream_js_1 = require("../utils/stream.cjs");
|
|
11
10
|
const passthrough_js_1 = require("../runnables/passthrough.cjs");
|
|
12
|
-
const
|
|
11
|
+
const zod_js_1 = require("../utils/types/zod.cjs");
|
|
13
12
|
const base_js_3 = require("../callbacks/base.cjs");
|
|
13
|
+
const json_schema_js_1 = require("../utils/json_schema.cjs");
|
|
14
14
|
/**
|
|
15
15
|
* Creates a transform stream for encoding chat message chunks.
|
|
16
16
|
* @deprecated Use {@link BytesOutputParser} instead
|
|
@@ -534,7 +534,7 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
534
534
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
535
535
|
const schema = outputSchema;
|
|
536
536
|
const name = config?.name;
|
|
537
|
-
const description = schema
|
|
537
|
+
const description = (0, zod_js_1.getSchemaDescription)(schema) ?? "A function available to call.";
|
|
538
538
|
const method = config?.method;
|
|
539
539
|
const includeRaw = config?.includeRaw;
|
|
540
540
|
if (method === "jsonMode") {
|
|
@@ -542,14 +542,14 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
542
542
|
}
|
|
543
543
|
let functionName = name ?? "extract";
|
|
544
544
|
let tools;
|
|
545
|
-
if ((0,
|
|
545
|
+
if ((0, zod_js_1.isInteropZodSchema)(schema)) {
|
|
546
546
|
tools = [
|
|
547
547
|
{
|
|
548
548
|
type: "function",
|
|
549
549
|
function: {
|
|
550
550
|
name: functionName,
|
|
551
551
|
description,
|
|
552
|
-
parameters: (0,
|
|
552
|
+
parameters: (0, json_schema_js_1.toJsonSchema)(schema),
|
|
553
553
|
},
|
|
554
554
|
},
|
|
555
555
|
];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ZodType as ZodTypeV3 } from "zod/v3";
|
|
2
|
+
import type { $ZodType as ZodTypeV4 } from "zod/v4/core";
|
|
2
3
|
import { type BaseMessage, BaseMessageChunk, type BaseMessageLike, AIMessageChunk } from "../messages/index.js";
|
|
3
4
|
import type { BasePromptValueInterface } from "../prompt_values.js";
|
|
4
5
|
import { LLMResult, ChatGenerationChunk, type ChatResult, type Generation } from "../outputs.js";
|
|
@@ -183,8 +184,13 @@ export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCal
|
|
|
183
184
|
* @returns A Promise that resolves to a string.
|
|
184
185
|
*/
|
|
185
186
|
predict(text: string, options?: string[] | CallOptions, callbacks?: Callbacks): Promise<string>;
|
|
186
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema:
|
|
187
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema:
|
|
187
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
188
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
189
|
+
raw: BaseMessage;
|
|
190
|
+
parsed: RunOutput;
|
|
191
|
+
}>;
|
|
192
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
193
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
188
194
|
raw: BaseMessage;
|
|
189
195
|
parsed: RunOutput;
|
|
190
196
|
}>;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
1
|
import { AIMessage, HumanMessage, coerceMessageLikeToMessage, isAIMessageChunk, isBaseMessage, isAIMessage, convertToOpenAIImageBlock, isURLContentBlock, isBase64ContentBlock, } from "../messages/index.js";
|
|
3
2
|
import { RUN_KEY, } from "../outputs.js";
|
|
4
3
|
import { BaseLanguageModel, } from "./base.js";
|
|
@@ -6,8 +5,9 @@ import { CallbackManager, } from "../callbacks/manager.js";
|
|
|
6
5
|
import { RunnableLambda, RunnableSequence, } from "../runnables/base.js";
|
|
7
6
|
import { concat } from "../utils/stream.js";
|
|
8
7
|
import { RunnablePassthrough } from "../runnables/passthrough.js";
|
|
9
|
-
import {
|
|
8
|
+
import { getSchemaDescription, isInteropZodSchema, } from "../utils/types/zod.js";
|
|
10
9
|
import { callbackHandlerPrefersStreaming } from "../callbacks/base.js";
|
|
10
|
+
import { toJsonSchema } from "../utils/json_schema.js";
|
|
11
11
|
/**
|
|
12
12
|
* Creates a transform stream for encoding chat message chunks.
|
|
13
13
|
* @deprecated Use {@link BytesOutputParser} instead
|
|
@@ -530,7 +530,7 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
530
530
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
531
531
|
const schema = outputSchema;
|
|
532
532
|
const name = config?.name;
|
|
533
|
-
const description = schema
|
|
533
|
+
const description = getSchemaDescription(schema) ?? "A function available to call.";
|
|
534
534
|
const method = config?.method;
|
|
535
535
|
const includeRaw = config?.includeRaw;
|
|
536
536
|
if (method === "jsonMode") {
|
|
@@ -538,14 +538,14 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
538
538
|
}
|
|
539
539
|
let functionName = name ?? "extract";
|
|
540
540
|
let tools;
|
|
541
|
-
if (
|
|
541
|
+
if (isInteropZodSchema(schema)) {
|
|
542
542
|
tools = [
|
|
543
543
|
{
|
|
544
544
|
type: "function",
|
|
545
545
|
function: {
|
|
546
546
|
name: functionName,
|
|
547
547
|
description,
|
|
548
|
-
parameters:
|
|
548
|
+
parameters: toJsonSchema(schema),
|
|
549
549
|
},
|
|
550
550
|
},
|
|
551
551
|
];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { JsonSchema7ObjectType } from "zod-to-json-schema";
|
|
2
1
|
import { Optional } from "../../types/type-utils.js";
|
|
3
2
|
import { Generation, ChatGeneration } from "../../outputs.js";
|
|
4
3
|
import { BaseLLMOutputParser } from "../base.js";
|
|
5
4
|
import { BaseCumulativeTransformOutputParser, BaseCumulativeTransformOutputParserInput } from "../transform.js";
|
|
5
|
+
import { JsonSchema7ObjectType } from "../../utils/json_schema.js";
|
|
6
6
|
import { type Operation as JSONPatchOperation } from "../../utils/json_patch.js";
|
|
7
7
|
/**
|
|
8
8
|
* Represents optional parameters for a function in a JSON Schema.
|
|
@@ -5,6 +5,7 @@ const base_js_1 = require("../base.cjs");
|
|
|
5
5
|
const json_js_1 = require("../json.cjs");
|
|
6
6
|
const transform_js_1 = require("../transform.cjs");
|
|
7
7
|
const ai_js_1 = require("../../messages/ai.cjs");
|
|
8
|
+
const zod_js_1 = require("../../utils/types/zod.cjs");
|
|
8
9
|
function parseToolCall(
|
|
9
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
11
|
rawToolCall, options) {
|
|
@@ -215,12 +216,12 @@ class JsonOutputKeyToolsParser extends JsonOutputToolsParser {
|
|
|
215
216
|
if (this.zodSchema === undefined) {
|
|
216
217
|
return result;
|
|
217
218
|
}
|
|
218
|
-
const zodParsedResult = await this.zodSchema
|
|
219
|
+
const zodParsedResult = await (0, zod_js_1.interopSafeParseAsync)(this.zodSchema, result);
|
|
219
220
|
if (zodParsedResult.success) {
|
|
220
221
|
return zodParsedResult.data;
|
|
221
222
|
}
|
|
222
223
|
else {
|
|
223
|
-
throw new base_js_1.OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error
|
|
224
|
+
throw new base_js_1.OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error?.issues)}`, JSON.stringify(result, null, 2));
|
|
224
225
|
}
|
|
225
226
|
}
|
|
226
227
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type * as z3 from "zod/v3";
|
|
2
|
+
import type * as z4 from "zod/v4/core";
|
|
2
3
|
import { ChatGeneration, ChatGenerationChunk } from "../../outputs.js";
|
|
3
4
|
import { InvalidToolCall, ToolCall } from "../../messages/tool.js";
|
|
4
5
|
import { BaseCumulativeTransformOutputParser, BaseCumulativeTransformOutputParserInput } from "../transform.js";
|
|
6
|
+
import { type InteropZodType } from "../../utils/types/zod.js";
|
|
5
7
|
export type ParsedToolCall = {
|
|
6
8
|
id?: string;
|
|
7
9
|
type: string;
|
|
@@ -52,11 +54,20 @@ export declare class JsonOutputToolsParser<T> extends BaseCumulativeTransformOut
|
|
|
52
54
|
*/
|
|
53
55
|
parsePartialResult(generations: ChatGenerationChunk[] | ChatGeneration[], partial?: boolean): Promise<any>;
|
|
54
56
|
}
|
|
55
|
-
|
|
57
|
+
type JsonOutputKeyToolsParserParamsBase = {
|
|
56
58
|
keyName: string;
|
|
57
59
|
returnSingle?: boolean;
|
|
58
|
-
zodSchema?: z.ZodType<T>;
|
|
59
60
|
} & JsonOutputToolsParserParams;
|
|
61
|
+
type JsonOutputKeyToolsParserParamsV3<T extends Record<string, any> = Record<string, any>> = {
|
|
62
|
+
zodSchema?: z3.ZodType<T>;
|
|
63
|
+
} & JsonOutputKeyToolsParserParamsBase;
|
|
64
|
+
type JsonOutputKeyToolsParserParamsV4<T extends Record<string, any> = Record<string, any>> = {
|
|
65
|
+
zodSchema?: z4.$ZodType<T, T>;
|
|
66
|
+
} & JsonOutputKeyToolsParserParamsBase;
|
|
67
|
+
export type JsonOutputKeyToolsParserParamsInterop<T extends Record<string, any> = Record<string, any>> = {
|
|
68
|
+
zodSchema?: InteropZodType<T>;
|
|
69
|
+
} & JsonOutputKeyToolsParserParamsBase;
|
|
70
|
+
export type JsonOutputKeyToolsParserParams<T extends Record<string, any> = Record<string, any>> = JsonOutputKeyToolsParserParamsV3<T>;
|
|
60
71
|
/**
|
|
61
72
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
62
73
|
* expecting only a single tool to be called.
|
|
@@ -70,9 +81,12 @@ export declare class JsonOutputKeyToolsParser<T extends Record<string, any> = Re
|
|
|
70
81
|
keyName: string;
|
|
71
82
|
/** Whether to return only the first tool call. */
|
|
72
83
|
returnSingle: boolean;
|
|
73
|
-
zodSchema?:
|
|
74
|
-
constructor(params:
|
|
84
|
+
zodSchema?: InteropZodType<T>;
|
|
85
|
+
constructor(params: JsonOutputKeyToolsParserParamsV3<T>);
|
|
86
|
+
constructor(params: JsonOutputKeyToolsParserParamsV4<T>);
|
|
87
|
+
constructor(params: JsonOutputKeyToolsParserParamsInterop<T>);
|
|
75
88
|
protected _validateResult(result: unknown): Promise<T>;
|
|
76
89
|
parsePartialResult(generations: ChatGeneration[]): Promise<any>;
|
|
77
90
|
parseResult(generations: ChatGeneration[]): Promise<any>;
|
|
78
91
|
}
|
|
92
|
+
export {};
|
|
@@ -2,6 +2,7 @@ import { OutputParserException } from "../base.js";
|
|
|
2
2
|
import { parsePartialJson } from "../json.js";
|
|
3
3
|
import { BaseCumulativeTransformOutputParser, } from "../transform.js";
|
|
4
4
|
import { isAIMessage } from "../../messages/ai.js";
|
|
5
|
+
import { interopSafeParseAsync, } from "../../utils/types/zod.js";
|
|
5
6
|
export function parseToolCall(
|
|
6
7
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
8
|
rawToolCall, options) {
|
|
@@ -208,12 +209,12 @@ export class JsonOutputKeyToolsParser extends JsonOutputToolsParser {
|
|
|
208
209
|
if (this.zodSchema === undefined) {
|
|
209
210
|
return result;
|
|
210
211
|
}
|
|
211
|
-
const zodParsedResult = await this.zodSchema
|
|
212
|
+
const zodParsedResult = await interopSafeParseAsync(this.zodSchema, result);
|
|
212
213
|
if (zodParsedResult.success) {
|
|
213
214
|
return zodParsedResult.data;
|
|
214
215
|
}
|
|
215
216
|
else {
|
|
216
|
-
throw new OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error
|
|
217
|
+
throw new OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error?.issues)}`, JSON.stringify(result, null, 2));
|
|
217
218
|
}
|
|
218
219
|
}
|
|
219
220
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AsymmetricStructuredOutputParser = exports.JsonMarkdownStructuredOutputParser = exports.StructuredOutputParser = void 0;
|
|
4
|
-
const
|
|
5
|
-
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
4
|
+
const v3_1 = require("zod/v3");
|
|
6
5
|
const base_js_1 = require("./base.cjs");
|
|
6
|
+
const zod_js_1 = require("../utils/types/zod.cjs");
|
|
7
|
+
const json_schema_js_1 = require("../utils/json_schema.cjs");
|
|
7
8
|
class StructuredOutputParser extends base_js_1.BaseOutputParser {
|
|
8
9
|
static lc_name() {
|
|
9
10
|
return "StructuredOutputParser";
|
|
@@ -41,7 +42,7 @@ class StructuredOutputParser extends base_js_1.BaseOutputParser {
|
|
|
41
42
|
* @returns A new instance of StructuredOutputParser.
|
|
42
43
|
*/
|
|
43
44
|
static fromNamesAndDescriptions(schemas) {
|
|
44
|
-
const zodSchema =
|
|
45
|
+
const zodSchema = v3_1.z.object(Object.fromEntries(Object.entries(schemas).map(([name, description]) => [name, v3_1.z.string().describe(description)])));
|
|
45
46
|
return new this(zodSchema);
|
|
46
47
|
}
|
|
47
48
|
/**
|
|
@@ -63,7 +64,7 @@ Your output will be parsed and type-checked according to the provided schema ins
|
|
|
63
64
|
|
|
64
65
|
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
|
|
65
66
|
\`\`\`json
|
|
66
|
-
${JSON.stringify((0,
|
|
67
|
+
${JSON.stringify((0, json_schema_js_1.toJsonSchema)(this.schema))}
|
|
67
68
|
\`\`\`
|
|
68
69
|
`;
|
|
69
70
|
}
|
|
@@ -83,7 +84,7 @@ ${JSON.stringify((0, zod_to_json_schema_1.zodToJsonSchema)(this.schema))}
|
|
|
83
84
|
return `"${escapedInsideQuotes}"`;
|
|
84
85
|
})
|
|
85
86
|
.replace(/\n/g, "");
|
|
86
|
-
return await this.schema
|
|
87
|
+
return await (0, zod_js_1.interopParseAsync)(this.schema, JSON.parse(escapedJson));
|
|
87
88
|
}
|
|
88
89
|
catch (e) {
|
|
89
90
|
throw new base_js_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
|
|
@@ -104,7 +105,7 @@ class JsonMarkdownStructuredOutputParser extends StructuredOutputParser {
|
|
|
104
105
|
if (interpolationDepth < 1) {
|
|
105
106
|
throw new Error("f string interpolation depth must be at least 1");
|
|
106
107
|
}
|
|
107
|
-
return `Return a markdown code snippet with a JSON object formatted to look like:\n\`\`\`json\n${this._schemaToInstruction((0,
|
|
108
|
+
return `Return a markdown code snippet with a JSON object formatted to look like:\n\`\`\`json\n${this._schemaToInstruction((0, json_schema_js_1.toJsonSchema)(this.schema))
|
|
108
109
|
.replaceAll("{", "{".repeat(interpolationDepth))
|
|
109
110
|
.replaceAll("}", "}".repeat(interpolationDepth))}\n\`\`\``;
|
|
110
111
|
}
|
|
@@ -159,7 +160,7 @@ class JsonMarkdownStructuredOutputParser extends StructuredOutputParser {
|
|
|
159
160
|
return new this(schema);
|
|
160
161
|
}
|
|
161
162
|
static fromNamesAndDescriptions(schemas) {
|
|
162
|
-
const zodSchema =
|
|
163
|
+
const zodSchema = v3_1.z.object(Object.fromEntries(Object.entries(schemas).map(([name, description]) => [name, v3_1.z.string().describe(description)])));
|
|
163
164
|
return new this(zodSchema);
|
|
164
165
|
}
|
|
165
166
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
1
|
+
import { z } from "zod/v3";
|
|
2
2
|
import { BaseOutputParser, FormatInstructionsOptions } from "./base.js";
|
|
3
|
+
import { type InteropZodType, type InferInteropZodOutput } from "../utils/types/zod.js";
|
|
3
4
|
export type JsonMarkdownStructuredOutputParserInput = {
|
|
4
5
|
interpolationDepth?: number;
|
|
5
6
|
};
|
|
6
7
|
export interface JsonMarkdownFormatInstructionsOptions extends FormatInstructionsOptions {
|
|
7
8
|
interpolationDepth?: number;
|
|
8
9
|
}
|
|
9
|
-
export declare class StructuredOutputParser<T extends
|
|
10
|
+
export declare class StructuredOutputParser<T extends InteropZodType> extends BaseOutputParser<InferInteropZodOutput<T>> {
|
|
10
11
|
schema: T;
|
|
11
12
|
static lc_name(): string;
|
|
12
13
|
lc_namespace: string[];
|
|
@@ -17,7 +18,7 @@ export declare class StructuredOutputParser<T extends z.ZodTypeAny> extends Base
|
|
|
17
18
|
* @param schema The Zod schema which the output should match
|
|
18
19
|
* @returns A new instance of StructuredOutputParser.
|
|
19
20
|
*/
|
|
20
|
-
static fromZodSchema<T extends
|
|
21
|
+
static fromZodSchema<T extends InteropZodType>(schema: T): StructuredOutputParser<T>;
|
|
21
22
|
/**
|
|
22
23
|
* Creates a new StructuredOutputParser from a set of names and
|
|
23
24
|
* descriptions.
|
|
@@ -45,17 +46,17 @@ export declare class StructuredOutputParser<T extends z.ZodTypeAny> extends Base
|
|
|
45
46
|
* @param text The text to parse
|
|
46
47
|
* @returns The parsed output.
|
|
47
48
|
*/
|
|
48
|
-
parse(text: string): Promise<
|
|
49
|
+
parse(text: string): Promise<InferInteropZodOutput<T>>;
|
|
49
50
|
}
|
|
50
51
|
/**
|
|
51
52
|
* A specific type of `StructuredOutputParser` that parses JSON data
|
|
52
53
|
* formatted as a markdown code snippet.
|
|
53
54
|
*/
|
|
54
|
-
export declare class JsonMarkdownStructuredOutputParser<T extends
|
|
55
|
+
export declare class JsonMarkdownStructuredOutputParser<T extends InteropZodType> extends StructuredOutputParser<T> {
|
|
55
56
|
static lc_name(): string;
|
|
56
57
|
getFormatInstructions(options?: JsonMarkdownFormatInstructionsOptions): string;
|
|
57
58
|
private _schemaToInstruction;
|
|
58
|
-
static fromZodSchema<T extends
|
|
59
|
+
static fromZodSchema<T extends InteropZodType>(schema: T): JsonMarkdownStructuredOutputParser<T>;
|
|
59
60
|
static fromNamesAndDescriptions<S extends {
|
|
60
61
|
[key: string]: string;
|
|
61
62
|
}>(schemas: S): JsonMarkdownStructuredOutputParser<z.ZodObject<{
|
|
@@ -66,14 +67,14 @@ export declare class JsonMarkdownStructuredOutputParser<T extends z.ZodTypeAny>
|
|
|
66
67
|
[x: string]: string;
|
|
67
68
|
}>>;
|
|
68
69
|
}
|
|
69
|
-
export interface AsymmetricStructuredOutputParserFields<T extends
|
|
70
|
+
export interface AsymmetricStructuredOutputParserFields<T extends InteropZodType> {
|
|
70
71
|
inputSchema: T;
|
|
71
72
|
}
|
|
72
73
|
/**
|
|
73
74
|
* A type of `StructuredOutputParser` that handles asymmetric input and
|
|
74
75
|
* output schemas.
|
|
75
76
|
*/
|
|
76
|
-
export declare abstract class AsymmetricStructuredOutputParser<T extends
|
|
77
|
+
export declare abstract class AsymmetricStructuredOutputParser<T extends InteropZodType, Y = unknown> extends BaseOutputParser<Y> {
|
|
77
78
|
private structuredInputParser;
|
|
78
79
|
constructor({ inputSchema }: AsymmetricStructuredOutputParserFields<T>);
|
|
79
80
|
/**
|
|
@@ -82,7 +83,7 @@ export declare abstract class AsymmetricStructuredOutputParser<T extends z.ZodTy
|
|
|
82
83
|
* @param input The parsed input
|
|
83
84
|
* @returns The processed output.
|
|
84
85
|
*/
|
|
85
|
-
abstract outputProcessor(input:
|
|
86
|
+
abstract outputProcessor(input: InferInteropZodOutput<T>): Promise<Y>;
|
|
86
87
|
parse(text: string): Promise<Y>;
|
|
87
88
|
getFormatInstructions(): string;
|
|
88
89
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { zodToJsonSchema, } from "zod-to-json-schema";
|
|
1
|
+
import { z } from "zod/v3";
|
|
3
2
|
import { BaseOutputParser, OutputParserException, } from "./base.js";
|
|
3
|
+
import { interopParseAsync, } from "../utils/types/zod.js";
|
|
4
|
+
import { toJsonSchema, } from "../utils/json_schema.js";
|
|
4
5
|
export class StructuredOutputParser extends BaseOutputParser {
|
|
5
6
|
static lc_name() {
|
|
6
7
|
return "StructuredOutputParser";
|
|
@@ -60,7 +61,7 @@ Your output will be parsed and type-checked according to the provided schema ins
|
|
|
60
61
|
|
|
61
62
|
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
|
|
62
63
|
\`\`\`json
|
|
63
|
-
${JSON.stringify(
|
|
64
|
+
${JSON.stringify(toJsonSchema(this.schema))}
|
|
64
65
|
\`\`\`
|
|
65
66
|
`;
|
|
66
67
|
}
|
|
@@ -80,7 +81,7 @@ ${JSON.stringify(zodToJsonSchema(this.schema))}
|
|
|
80
81
|
return `"${escapedInsideQuotes}"`;
|
|
81
82
|
})
|
|
82
83
|
.replace(/\n/g, "");
|
|
83
|
-
return await this.schema
|
|
84
|
+
return await interopParseAsync(this.schema, JSON.parse(escapedJson));
|
|
84
85
|
}
|
|
85
86
|
catch (e) {
|
|
86
87
|
throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
|
|
@@ -100,7 +101,7 @@ export class JsonMarkdownStructuredOutputParser extends StructuredOutputParser {
|
|
|
100
101
|
if (interpolationDepth < 1) {
|
|
101
102
|
throw new Error("f string interpolation depth must be at least 1");
|
|
102
103
|
}
|
|
103
|
-
return `Return a markdown code snippet with a JSON object formatted to look like:\n\`\`\`json\n${this._schemaToInstruction(
|
|
104
|
+
return `Return a markdown code snippet with a JSON object formatted to look like:\n\`\`\`json\n${this._schemaToInstruction(toJsonSchema(this.schema))
|
|
104
105
|
.replaceAll("{", "{".repeat(interpolationDepth))
|
|
105
106
|
.replaceAll("}", "}".repeat(interpolationDepth))}\n\`\`\``;
|
|
106
107
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StructuredPrompt = void 0;
|
|
4
|
+
const base_js_1 = require("../runnables/base.cjs");
|
|
4
5
|
const chat_js_1 = require("./chat.cjs");
|
|
5
6
|
function isWithStructuredOutput(x
|
|
6
7
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
@@ -54,15 +55,12 @@ class StructuredPrompt extends chat_js_1.ChatPromptTemplate {
|
|
|
54
55
|
}
|
|
55
56
|
if (isRunnableBinding(coerceable) &&
|
|
56
57
|
isWithStructuredOutput(coerceable.bound)) {
|
|
57
|
-
return super.pipe(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.withStructuredOutput(this.schema)
|
|
64
|
-
.bind(coerceable.kwargs ?? {})
|
|
65
|
-
.withConfig(coerceable.config));
|
|
58
|
+
return super.pipe(new base_js_1.RunnableBinding({
|
|
59
|
+
bound: coerceable.bound.withStructuredOutput(this.schema, ...(this.method ? [{ method: this.method }] : [])),
|
|
60
|
+
kwargs: coerceable.kwargs ?? {},
|
|
61
|
+
config: coerceable.config,
|
|
62
|
+
configFactories: coerceable.configFactories,
|
|
63
|
+
}));
|
|
66
64
|
}
|
|
67
65
|
throw new Error(`Structured prompts need to be piped to a language model that supports the "withStructuredOutput()" method.`);
|
|
68
66
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RunnableBinding } from "../runnables/base.js";
|
|
1
2
|
import { ChatPromptTemplate, } from "./chat.js";
|
|
2
3
|
function isWithStructuredOutput(x
|
|
3
4
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
@@ -51,15 +52,12 @@ export class StructuredPrompt extends ChatPromptTemplate {
|
|
|
51
52
|
}
|
|
52
53
|
if (isRunnableBinding(coerceable) &&
|
|
53
54
|
isWithStructuredOutput(coerceable.bound)) {
|
|
54
|
-
return super.pipe(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
.withStructuredOutput(this.schema)
|
|
61
|
-
.bind(coerceable.kwargs ?? {})
|
|
62
|
-
.withConfig(coerceable.config));
|
|
55
|
+
return super.pipe(new RunnableBinding({
|
|
56
|
+
bound: coerceable.bound.withStructuredOutput(this.schema, ...(this.method ? [{ method: this.method }] : [])),
|
|
57
|
+
kwargs: coerceable.kwargs ?? {},
|
|
58
|
+
config: coerceable.config,
|
|
59
|
+
configFactories: coerceable.configFactories,
|
|
60
|
+
}));
|
|
63
61
|
}
|
|
64
62
|
throw new Error(`Structured prompts need to be piped to a language model that supports the "withStructuredOutput()" method.`);
|
|
65
63
|
}
|