Skip to content

Commit

Permalink
fix[openai]: add a test, make sure title is propagated to tool_choice (
Browse files Browse the repository at this point in the history
  • Loading branch information
dqbd authored Mar 29, 2024
1 parent 69ad164 commit 4a9e11e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libs/langchain-openai/src/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,9 @@ export class ChatOpenAI<
openAIFunctionDefinition = schema as FunctionDefinition;
functionName = schema.name;
} else {
functionName = schema.title ?? functionName;
openAIFunctionDefinition = {
name: schema.title ?? functionName,
name: functionName,
description: schema.description ?? "",
parameters: schema,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,46 @@ Respond with a JSON object containing three keys:
expect("number2" in result).toBe(true);
});

test("withStructuredOutput JSON schema", async () => {
const model = new ChatOpenAI({
temperature: 0,
modelName: "gpt-4-turbo-preview",
});

const jsonSchema = {
title: "calculator",
description: "A simple calculator",
type: "object",
properties: {
operation: {
type: "string",
enum: ["add", "subtract", "multiply", "divide"],
},
number1: { type: "number" },
number2: { type: "number" },
},
};
const modelWithStructuredOutput = model.withStructuredOutput(jsonSchema);

const prompt = ChatPromptTemplate.fromMessages([
"system",
`You are VERY bad at math and must always use a calculator.
Respond with a JSON object containing three keys:
'operation': the type of operation to execute, either 'add', 'subtract', 'multiply' or 'divide',
'number1': the first number to operate on,
'number2': the second number to operate on.
`,
"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 includeRaw true", async () => {
const model = new ChatOpenAI({
temperature: 0,
Expand Down

0 comments on commit 4a9e11e

Please sign in to comment.