diff --git a/examples/src/models/chat/integration_openai.ts b/examples/src/models/chat/integration_openai.ts index bb13c3601c99..caff387ef8d9 100644 --- a/examples/src/models/chat/integration_openai.ts +++ b/examples/src/models/chat/integration_openai.ts @@ -7,7 +7,7 @@ const model = new ChatOpenAI({ openAIApiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.OPENAI_API_KEY }); -// You can also pass functions to the model, learn more here +// You can also pass tools or functions to the model, learn more here // https://platform.openai.com/docs/guides/gpt/function-calling const modelForFunctionCalling = new ChatOpenAI({ @@ -15,12 +15,11 @@ const modelForFunctionCalling = new ChatOpenAI({ temperature: 0, }); -const result = await modelForFunctionCalling.predictMessages( +await modelForFunctionCalling.predictMessages( [new HumanChatMessage("What is the weather in New York?")], { tools: [new SerpAPI()] } + // Tools will be automatically formatted as functions in the OpenAI format ); - -console.log(result); /* AIChatMessage { text: '', @@ -33,3 +32,42 @@ AIChatMessage { } } */ + +await modelForFunctionCalling.predictMessages( + [new HumanChatMessage("What is the weather in New York?")], + { + functions: [ + { + name: "get_current_weather", + description: "Get the current weather in a given location", + parameters: { + type: "object", + properties: { + location: { + type: "string", + description: "The city and state, e.g. San Francisco, CA", + }, + unit: { type: "string", enum: ["celsius", "fahrenheit"] }, + }, + required: ["location"], + }, + }, + ], + // You can set the `function_call` arg to force the model to use a function + function_call: { + name: "get_current_weather", + }, + } +); +/* +AIChatMessage { + text: '', + name: undefined, + additional_kwargs: { + function_call: { + name: 'get_current_weather', + arguments: '{\n "location": "New York"\n}' + } + } +} +*/