@langchain/openai 0.0.23 → 0.0.25
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/chat_models.cjs
CHANGED
package/dist/chat_models.js
CHANGED
|
@@ -146,6 +146,43 @@ Respond with a JSON object containing three keys:
|
|
|
146
146
|
expect("number1" in result).toBe(true);
|
|
147
147
|
expect("number2" in result).toBe(true);
|
|
148
148
|
});
|
|
149
|
+
test("withStructuredOutput JSON schema", async () => {
|
|
150
|
+
const model = new ChatOpenAI({
|
|
151
|
+
temperature: 0,
|
|
152
|
+
modelName: "gpt-4-turbo-preview",
|
|
153
|
+
});
|
|
154
|
+
const jsonSchema = {
|
|
155
|
+
title: "calculator",
|
|
156
|
+
description: "A simple calculator",
|
|
157
|
+
type: "object",
|
|
158
|
+
properties: {
|
|
159
|
+
operation: {
|
|
160
|
+
type: "string",
|
|
161
|
+
enum: ["add", "subtract", "multiply", "divide"],
|
|
162
|
+
},
|
|
163
|
+
number1: { type: "number" },
|
|
164
|
+
number2: { type: "number" },
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
const modelWithStructuredOutput = model.withStructuredOutput(jsonSchema);
|
|
168
|
+
const prompt = ChatPromptTemplate.fromMessages([
|
|
169
|
+
"system",
|
|
170
|
+
`You are VERY bad at math and must always use a calculator.
|
|
171
|
+
Respond with a JSON object containing three keys:
|
|
172
|
+
'operation': the type of operation to execute, either 'add', 'subtract', 'multiply' or 'divide',
|
|
173
|
+
'number1': the first number to operate on,
|
|
174
|
+
'number2': the second number to operate on.
|
|
175
|
+
`,
|
|
176
|
+
"human",
|
|
177
|
+
"Please help me!! What is 2 + 2?",
|
|
178
|
+
]);
|
|
179
|
+
const chain = prompt.pipe(modelWithStructuredOutput);
|
|
180
|
+
const result = await chain.invoke({});
|
|
181
|
+
console.log(result);
|
|
182
|
+
expect("operation" in result).toBe(true);
|
|
183
|
+
expect("number1" in result).toBe(true);
|
|
184
|
+
expect("number2" in result).toBe(true);
|
|
185
|
+
});
|
|
149
186
|
test("withStructuredOutput includeRaw true", async () => {
|
|
150
187
|
const model = new ChatOpenAI({
|
|
151
188
|
temperature: 0,
|