Created
June 16, 2023 00:38
-
-
Save wrannaman/71f6285a0ec791c55fc63b935e384a4a to your computer and use it in GitHub Desktop.
example function in node js to call the chat gpt api with a function call
This file contains hidden or 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
const config = require('./config') | |
const { Configuration, OpenAIApi } = require("openai"); | |
const configuration = new Configuration({ | |
apiKey: config.openai, | |
}); | |
const openai = new OpenAIApi(configuration); | |
const businessType = async (businessType) => { | |
console.log("businessType:", businessType) | |
return 'cybersecurity' | |
} | |
setTimeout(async () => { | |
const text = `please respond to my email` | |
const output = await openai.createChatCompletion({ | |
model: "gpt-3.5-turbo-0613", | |
messages: [ | |
{ role: 'system', content: `Write an email subject given the business type. you may use emoji.` }, | |
{ role: 'user', content: `business type is cybersecurity` } | |
], | |
functions: [{ | |
name: 'businessType', | |
description: 'get the email context like the type of business we are writing the email for', | |
parameters: { | |
type: "object", | |
properties: { | |
businessType: { | |
type: "string", | |
description: "the type of business we are writing the email for", | |
} | |
}, | |
required: ["businessType"] | |
} | |
}], | |
function_call: 'auto', | |
temperature: 1, | |
max_tokens: 200, | |
top_p: 1, | |
frequency_penalty: 0, | |
presence_penalty: 0, | |
}); | |
const out = output?.data?.choices[0] | |
if (out.finish_reason === 'function_call') { | |
if (out?.message?.function_call?.name === 'businessType') { | |
const j = JSON.parse(out?.message?.function_call?.arguments) | |
console.log("j:", j) | |
} | |
} else { | |
console.log("no function call:", out.message); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment