@langchain/openai 0.0.20 → 0.0.21
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
|
@@ -762,7 +762,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
762
762
|
}
|
|
763
763
|
}
|
|
764
764
|
else {
|
|
765
|
-
|
|
765
|
+
let functionName = name ?? "extract";
|
|
766
766
|
// Is function calling
|
|
767
767
|
if (isZodSchema(schema)) {
|
|
768
768
|
const asJsonSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(schema);
|
|
@@ -791,15 +791,25 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
|
|
|
791
791
|
});
|
|
792
792
|
}
|
|
793
793
|
else {
|
|
794
|
+
let openAIFunctionDefinition;
|
|
795
|
+
if (typeof schema.name === "string" &&
|
|
796
|
+
typeof schema.parameters === "object" &&
|
|
797
|
+
schema.parameters != null) {
|
|
798
|
+
openAIFunctionDefinition = schema;
|
|
799
|
+
functionName = schema.name;
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
openAIFunctionDefinition = {
|
|
803
|
+
name: functionName,
|
|
804
|
+
description: schema.description ?? "",
|
|
805
|
+
parameters: schema,
|
|
806
|
+
};
|
|
807
|
+
}
|
|
794
808
|
llm = this.bind({
|
|
795
809
|
tools: [
|
|
796
810
|
{
|
|
797
811
|
type: "function",
|
|
798
|
-
function:
|
|
799
|
-
name: functionName,
|
|
800
|
-
description: schema.description,
|
|
801
|
-
parameters: schema,
|
|
802
|
-
},
|
|
812
|
+
function: openAIFunctionDefinition,
|
|
803
813
|
},
|
|
804
814
|
],
|
|
805
815
|
tool_choice: {
|
package/dist/chat_models.js
CHANGED
|
@@ -758,7 +758,7 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
758
758
|
}
|
|
759
759
|
}
|
|
760
760
|
else {
|
|
761
|
-
|
|
761
|
+
let functionName = name ?? "extract";
|
|
762
762
|
// Is function calling
|
|
763
763
|
if (isZodSchema(schema)) {
|
|
764
764
|
const asJsonSchema = zodToJsonSchema(schema);
|
|
@@ -787,15 +787,25 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
787
787
|
});
|
|
788
788
|
}
|
|
789
789
|
else {
|
|
790
|
+
let openAIFunctionDefinition;
|
|
791
|
+
if (typeof schema.name === "string" &&
|
|
792
|
+
typeof schema.parameters === "object" &&
|
|
793
|
+
schema.parameters != null) {
|
|
794
|
+
openAIFunctionDefinition = schema;
|
|
795
|
+
functionName = schema.name;
|
|
796
|
+
}
|
|
797
|
+
else {
|
|
798
|
+
openAIFunctionDefinition = {
|
|
799
|
+
name: functionName,
|
|
800
|
+
description: schema.description ?? "",
|
|
801
|
+
parameters: schema,
|
|
802
|
+
};
|
|
803
|
+
}
|
|
790
804
|
llm = this.bind({
|
|
791
805
|
tools: [
|
|
792
806
|
{
|
|
793
807
|
type: "function",
|
|
794
|
-
function:
|
|
795
|
-
name: functionName,
|
|
796
|
-
description: schema.description,
|
|
797
|
-
parameters: schema,
|
|
798
|
-
},
|
|
808
|
+
function: openAIFunctionDefinition,
|
|
799
809
|
},
|
|
800
810
|
],
|
|
801
811
|
tool_choice: {
|
|
@@ -87,6 +87,33 @@ test("withStructuredOutput JSON schema function calling", async () => {
|
|
|
87
87
|
expect("number1" in result).toBe(true);
|
|
88
88
|
expect("number2" in result).toBe(true);
|
|
89
89
|
});
|
|
90
|
+
test("withStructuredOutput OpenAI function definition function calling", async () => {
|
|
91
|
+
const model = new ChatOpenAI({
|
|
92
|
+
temperature: 0,
|
|
93
|
+
modelName: "gpt-4-turbo-preview",
|
|
94
|
+
});
|
|
95
|
+
const calculatorSchema = z.object({
|
|
96
|
+
operation: z.enum(["add", "subtract", "multiply", "divide"]),
|
|
97
|
+
number1: z.number(),
|
|
98
|
+
number2: z.number(),
|
|
99
|
+
});
|
|
100
|
+
const modelWithStructuredOutput = model.withStructuredOutput({
|
|
101
|
+
name: "calculator",
|
|
102
|
+
parameters: zodToJsonSchema(calculatorSchema),
|
|
103
|
+
});
|
|
104
|
+
const prompt = ChatPromptTemplate.fromMessages([
|
|
105
|
+
"system",
|
|
106
|
+
`You are VERY bad at math and must always use a calculator.`,
|
|
107
|
+
"human",
|
|
108
|
+
"Please help me!! What is 2 + 2?",
|
|
109
|
+
]);
|
|
110
|
+
const chain = prompt.pipe(modelWithStructuredOutput);
|
|
111
|
+
const result = await chain.invoke({});
|
|
112
|
+
console.log(result);
|
|
113
|
+
expect("operation" in result).toBe(true);
|
|
114
|
+
expect("number1" in result).toBe(true);
|
|
115
|
+
expect("number2" in result).toBe(true);
|
|
116
|
+
});
|
|
90
117
|
test("withStructuredOutput JSON schema JSON mode", async () => {
|
|
91
118
|
const model = new ChatOpenAI({
|
|
92
119
|
temperature: 0,
|