-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
structured_output.ts
200 lines (186 loc) Β· 6.55 KB
/
structured_output.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { z } from "zod";
import { zodToJsonSchema, JsonSchema7Type } from "zod-to-json-schema";
import { Validator } from "@langchain/core/utils/json_schema";
import { ChatOpenAI } from "@langchain/openai";
import { BasePromptTemplate } from "@langchain/core/prompts";
import {
BaseLLMOutputParser,
OutputParserException,
} from "@langchain/core/output_parsers";
import { ChatGeneration } from "@langchain/core/outputs";
import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
import type { BaseFunctionCallOptions } from "@langchain/core/language_models/base";
import { LLMChain, type LLMChainInput } from "../llm_chain.js";
import { OutputFunctionsParser } from "../../output_parsers/openai_functions.js";
/**
* Type representing the input for creating a structured output chain. It
* extends the LLMChainInput type and includes an additional
* 'outputSchema' field representing the JSON schema for the expected
* output.
*/
export type StructuredOutputChainInput<
T extends z.AnyZodObject = z.AnyZodObject
> = Omit<LLMChainInput, "outputParser" | "llm"> & {
outputSchema?: JsonSchema7Type;
prompt: BasePromptTemplate;
llm?: BaseChatModel<BaseFunctionCallOptions>;
zodSchema?: T;
};
export type FunctionCallStructuredOutputParserFields<
T extends z.AnyZodObject = z.AnyZodObject
> = {
jsonSchema?: JsonSchema7Type;
zodSchema?: T;
};
function isJsonSchema7Type(
x: JsonSchema7Type | FunctionCallStructuredOutputParserFields
): x is JsonSchema7Type {
return (
(x as FunctionCallStructuredOutputParserFields).jsonSchema === undefined &&
(x as FunctionCallStructuredOutputParserFields).zodSchema === undefined
);
}
/**
* Class that extends the BaseLLMOutputParser class. It provides
* functionality for parsing the structured output based on a JSON schema.
*/
export class FunctionCallStructuredOutputParser<
T extends z.AnyZodObject
> extends BaseLLMOutputParser<z.infer<T>> {
lc_namespace = ["langchain", "chains", "openai_functions"];
protected functionOutputParser = new OutputFunctionsParser();
protected jsonSchemaValidator?: Validator;
protected zodSchema?: T;
constructor(fieldsOrSchema: JsonSchema7Type);
constructor(fieldsOrSchema: FunctionCallStructuredOutputParserFields<T>);
constructor(
fieldsOrSchema:
| JsonSchema7Type
| FunctionCallStructuredOutputParserFields<T>
) {
let fields;
if (isJsonSchema7Type(fieldsOrSchema)) {
fields = { jsonSchema: fieldsOrSchema };
} else {
fields = fieldsOrSchema;
}
if (fields.jsonSchema === undefined && fields.zodSchema === undefined) {
throw new Error(
`Must provide at least one of "jsonSchema" or "zodSchema".`
);
}
super(fields);
if (fields.jsonSchema !== undefined) {
this.jsonSchemaValidator = new Validator(fields.jsonSchema, "7");
}
if (fields.zodSchema !== undefined) {
this.zodSchema = fields.zodSchema;
}
}
/**
* Method to parse the result of chat generations. It first parses the
* result using the functionOutputParser, then parses the result against a
* zod schema if the zod schema is available which allows the result to undergo
* Zod preprocessing, then it parses that result against the JSON schema.
* If the result is valid, it returns the parsed result. Otherwise, it throws
* an OutputParserException.
* @param generations Array of ChatGeneration instances to be parsed.
* @returns The parsed result if it is valid according to the JSON schema.
*/
async parseResult(generations: ChatGeneration[]) {
const initialResult = await this.functionOutputParser.parseResult(
generations
);
const parsedResult = JSON.parse(initialResult, (_, value) => {
if (value === null) {
return undefined;
}
return value;
});
if (this.zodSchema) {
const zodParsedResult = this.zodSchema.safeParse(parsedResult);
if (zodParsedResult.success) {
return zodParsedResult.data;
} else {
throw new OutputParserException(
`Failed to parse. Text: "${initialResult}". Error: ${JSON.stringify(
zodParsedResult.error.errors
)}`,
initialResult
);
}
} else if (this.jsonSchemaValidator !== undefined) {
const result = this.jsonSchemaValidator.validate(parsedResult);
if (result.valid) {
return parsedResult;
} else {
throw new OutputParserException(
`Failed to parse. Text: "${initialResult}". Error: ${JSON.stringify(
result.errors
)}`,
initialResult
);
}
} else {
throw new Error(
"This parser requires an input JSON Schema or an input Zod schema."
);
}
}
}
/**
* @deprecated Use {@link https://api.js.langchain.com/functions/langchain.chains_openai_functions.createStructuredOutputRunnable.html | createStructuredOutputRunnable} instead
* Create a chain that returns output matching a JSON Schema.
* @param input Object that includes all LLMChainInput fields except "outputParser"
* as well as an additional required "outputSchema" JSON Schema object.
* @returns OpenAPIChain
*/
export function createStructuredOutputChain<
T extends z.AnyZodObject = z.AnyZodObject
>(input: StructuredOutputChainInput<T>) {
const {
outputSchema,
llm = new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613", temperature: 0 }),
outputKey = "output",
llmKwargs = {},
zodSchema,
...rest
} = input;
if (outputSchema === undefined && zodSchema === undefined) {
throw new Error(`Must provide one of "outputSchema" or "zodSchema".`);
}
const functionName = "output_formatter";
return new LLMChain({
llm,
llmKwargs: {
...llmKwargs,
functions: [
{
name: functionName,
description: `Output formatter. Should always be used to format your response to the user.`,
parameters: outputSchema,
},
],
function_call: {
name: functionName,
},
},
outputKey,
outputParser: new FunctionCallStructuredOutputParser<T>({
jsonSchema: outputSchema,
zodSchema,
}),
...rest,
});
}
/** @deprecated Use {@link https://api.js.langchain.com/functions/langchain.chains_openai_functions.createStructuredOutputRunnable.html | createStructuredOutputRunnable} instead */
export function createStructuredOutputChainFromZod<T extends z.AnyZodObject>(
zodSchema: T,
input: Omit<StructuredOutputChainInput<T>, "outputSchema">
) {
return createStructuredOutputChain<T>({
...input,
outputSchema: zodToJsonSchema(zodSchema),
zodSchema,
});
}