Add more docs for calling chatopenai with functions (#1646)

This commit is contained in:
Nuno Campos
2023-06-14 11:43:34 +01:00
committed by GitHub
parent 6e7017f423
commit 6e9d342a6e
+42 -4
View File
@@ -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}'
}
}
}
*/