-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openai[minor],docs[minor]: Add parallel tool calls call option and do…
…cs (#5717) * openai[minor],docs[minor]: Add parallel tool calls call option and docs * chore: lint files * chore: lint files
- Loading branch information
1 parent
ef9b85c
commit c338b38
Showing
6 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
examples/src/models/chat/integration_openai_parallel_tool_calls.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { ChatOpenAI } from "@langchain/openai"; | ||
import { z } from "zod"; | ||
import { zodToJsonSchema } from "zod-to-json-schema"; | ||
|
||
const model = new ChatOpenAI({ | ||
temperature: 0, | ||
model: "gpt-4o", | ||
}); | ||
|
||
// Define your tools | ||
const calculatorSchema = z | ||
.object({ | ||
operation: z.enum(["add", "subtract", "multiply", "divide"]), | ||
number1: z.number(), | ||
number2: z.number(), | ||
}) | ||
.describe("A tool to perform basic arithmetic operations"); | ||
const weatherSchema = z | ||
.object({ | ||
city: z.enum(["add", "subtract", "multiply", "divide"]), | ||
}) | ||
.describe("A tool to get the weather in a city"); | ||
|
||
// Bind tools to the model | ||
const modelWithTools = model.bindTools([ | ||
{ | ||
type: "function", | ||
function: { | ||
name: "calculator", | ||
description: calculatorSchema.description, | ||
parameters: zodToJsonSchema(calculatorSchema), | ||
}, | ||
}, | ||
{ | ||
type: "function", | ||
function: { | ||
name: "weather", | ||
description: weatherSchema.description, | ||
parameters: zodToJsonSchema(weatherSchema), | ||
}, | ||
}, | ||
]); | ||
|
||
// Invoke the model with `parallel_tool_calls` set to `true` | ||
const response = await modelWithTools.invoke( | ||
["What is the weather in san francisco and what is 23716 times 27342?"], | ||
{ | ||
parallel_tool_calls: true, | ||
} | ||
); | ||
console.log(response.tool_calls); | ||
// We can see it called two tools | ||
/* | ||
[ | ||
{ | ||
name: 'weather', | ||
args: { city: 'san francisco' }, | ||
id: 'call_c1KymEIix7mdlFtgLSnTXmDc' | ||
}, | ||
{ | ||
name: 'calculator', | ||
args: { operation: 'multiply', number1: 23716, number2: 27342 }, | ||
id: 'call_ANLYclAmXQ4TwUCLXakbPr3Z' | ||
} | ||
] | ||
*/ | ||
|
||
// Invoke the model with `parallel_tool_calls` set to `false` | ||
const response2 = await modelWithTools.invoke( | ||
["What is the weather in san francisco and what is 23716 times 27342?"], | ||
{ | ||
parallel_tool_calls: false, | ||
} | ||
); | ||
console.log(response2.tool_calls); | ||
// We can see it called one tool | ||
/* | ||
[ | ||
{ | ||
name: 'weather', | ||
args: { city: 'san francisco' }, | ||
id: 'call_Rk34XffawJjgZ2BCK9E4CwlT' | ||
} | ||
] | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters