Skip to content

Commit

Permalink
Add more docs for calling chatopenai with functions (langchain-ai#1646)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos authored Jun 14, 2023
1 parent 6e7017f commit 6e9d342
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions examples/src/models/chat/integration_openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ 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({
modelName: "gpt-4-0613",
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: '',
Expand All @@ -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}'
}
}
}
*/

0 comments on commit 6e9d342

Please sign in to comment.