@langchain/openai
Advanced tools
Comparing version 0.0.20 to 0.0.21
@@ -761,3 +761,3 @@ import { OpenAI as OpenAIClient } from "openai"; | ||
else { | ||
const functionName = name ?? "extract"; | ||
let functionName = name ?? "extract"; | ||
// Is function calling | ||
@@ -791,2 +791,16 @@ if (isZodSchema(schema)) { | ||
else { | ||
let openAIFunctionDefinition; | ||
if (typeof schema.name === "string" && | ||
typeof schema.parameters === "object" && | ||
schema.parameters != null) { | ||
openAIFunctionDefinition = schema; | ||
functionName = schema.name; | ||
} | ||
else { | ||
openAIFunctionDefinition = { | ||
name: functionName, | ||
description: schema.description ?? "", | ||
parameters: schema, | ||
}; | ||
} | ||
llm = this.bind({ | ||
@@ -796,7 +810,3 @@ tools: [ | ||
type: "function", | ||
function: { | ||
name: functionName, | ||
description: schema.description, | ||
parameters: schema, | ||
}, | ||
function: openAIFunctionDefinition, | ||
}, | ||
@@ -803,0 +813,0 @@ ], |
@@ -90,2 +90,29 @@ import { z } from "zod"; | ||
}); | ||
test("withStructuredOutput OpenAI function definition function calling", async () => { | ||
const model = new ChatOpenAI({ | ||
temperature: 0, | ||
modelName: "gpt-4-turbo-preview", | ||
}); | ||
const calculatorSchema = z.object({ | ||
operation: z.enum(["add", "subtract", "multiply", "divide"]), | ||
number1: z.number(), | ||
number2: z.number(), | ||
}); | ||
const modelWithStructuredOutput = model.withStructuredOutput({ | ||
name: "calculator", | ||
parameters: zodToJsonSchema(calculatorSchema), | ||
}); | ||
const prompt = ChatPromptTemplate.fromMessages([ | ||
"system", | ||
`You are VERY bad at math and must always use a calculator.`, | ||
"human", | ||
"Please help me!! What is 2 + 2?", | ||
]); | ||
const chain = prompt.pipe(modelWithStructuredOutput); | ||
const result = await chain.invoke({}); | ||
console.log(result); | ||
expect("operation" in result).toBe(true); | ||
expect("number1" in result).toBe(true); | ||
expect("number2" in result).toBe(true); | ||
}); | ||
test("withStructuredOutput JSON schema JSON mode", async () => { | ||
@@ -92,0 +119,0 @@ const model = new ChatOpenAI({ |
{ | ||
"name": "@langchain/openai", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"description": "OpenAI integrations for LangChain.js", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
287137
7389