@langchain/openai 0.0.24 → 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.
@@ -817,8 +817,9 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
817
817
  functionName = schema.name;
818
818
  }
819
819
  else {
820
+ functionName = schema.title ?? functionName;
820
821
  openAIFunctionDefinition = {
821
- name: schema.title ?? functionName,
822
+ name: functionName,
822
823
  description: schema.description ?? "",
823
824
  parameters: schema,
824
825
  };
@@ -813,8 +813,9 @@ export class ChatOpenAI extends BaseChatModel {
813
813
  functionName = schema.name;
814
814
  }
815
815
  else {
816
+ functionName = schema.title ?? functionName;
816
817
  openAIFunctionDefinition = {
817
- name: schema.title ?? functionName,
818
+ name: functionName,
818
819
  description: schema.description ?? "",
819
820
  parameters: schema,
820
821
  };
@@ -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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.0.24",
3
+ "version": "0.0.25",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {