Skip to main content

How to convert tools to OpenAI Functions

This notebook goes over how to use LangChain tools as OpenAI functions.

yarn add @langchain/community @langchain/openai
import { Calculator } from "@langchain/community/tools/calculator";
import { HumanMessage } from "@langchain/core/messages";
import { convertToOpenAIFunction } from "@langchain/core/utils/function_calling";
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "gpt-3.5-turbo",
});
const tools = [new Calculator()];
const functions = [convertToOpenAIFunction(tools[0])];
functions[0];
{
name: "calculator",
description: "Useful for getting the result of a math expression. The input to this tool should be a valid mathema"... 63 more characters,
parameters: {
type: "object",
properties: { input: { type: "string" } },
additionalProperties: false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
}
const modelWithTools = model.bind({
functions,
});
const message = await modelWithTools.invoke([
new HumanMessage("use a calculator to add 2 and 3"),
]);
message;
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: "",
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: {
function_call: { name: "calculator", arguments: '{"input":"2 + 3"}' },
tool_calls: undefined
},
response_metadata: {}
},
lc_namespace: [ "langchain_core", "messages" ],
content: "",
name: undefined,
additional_kwargs: {
function_call: { name: "calculator", arguments: '{"input":"2 + 3"}' },
tool_calls: undefined
},
response_metadata: {
tokenUsage: { completionTokens: 17, promptTokens: 77, totalTokens: 94 },
finish_reason: "function_call"
},
tool_calls: [],
invalid_tool_calls: []
}
message.additional_kwargs["function_call"];
{ name: "calculator", arguments: '{"input":"2 + 3"}' }

Or we can use the update OpenAI API that uses tools and tool_choice instead of functions and function_call by using ChatOpenAI.bind({ tools }):

const modelWithTools = model.bind({
tools,
});
await modelWithTools.invoke([
new HumanMessage("use a calculator to add 2 and 3"),
]);
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: "",
tool_calls: [
{
name: "calculator",
args: { input: "2 + 3" },
id: "call_R2WXRRIbgh2qn1XBBu8P91DH"
}
],
invalid_tool_calls: [],
additional_kwargs: {
function_call: undefined,
tool_calls: [
{
id: "call_R2WXRRIbgh2qn1XBBu8P91DH",
type: "function",
function: [Object]
}
]
},
response_metadata: {}
},
lc_namespace: [ "langchain_core", "messages" ],
content: "",
name: undefined,
additional_kwargs: {
function_call: undefined,
tool_calls: [
{
id: "call_R2WXRRIbgh2qn1XBBu8P91DH",
type: "function",
function: { name: "calculator", arguments: '{"input":"2 + 3"}' }
}
]
},
response_metadata: {
tokenUsage: { completionTokens: 17, promptTokens: 77, totalTokens: 94 },
finish_reason: "tool_calls"
},
tool_calls: [
{
name: "calculator",
args: { input: "2 + 3" },
id: "call_R2WXRRIbgh2qn1XBBu8P91DH"
}
],
invalid_tool_calls: []
}

Was this page helpful?


You can leave detailed feedback on GitHub.