Generate Summary using ChatGpt
2 min readMay 28, 2023
To Generate a summary about any topic using ChatGpt
- Use OpenAi API to use chatGPT.
- Do SignUp at https://openai.com/ and get the API key.
Let’s set up the node js project.
- Run npm init. which will create a package.json file.
- Install the OpenAI package using this command.
npm install openai
3. install the Axios npm plugin to do API calls.
npm install axios
4. Create the below file as an index.js in the root folder. Add the following code:
const axios = require('axios');
const apiUrl = 'https://api.openai.com/v1/chat/completions';
const apiKey = 'OPENAI_KEY'; // @Please add here your openai key.
async function fetchChatCompletion(messageContent) {
try {
const response = await axios.post(
apiUrl,
{
messages: [
{ role: 'system', content: messageContent },
],
model: 'gpt-3.5-turbo', // Specify the model to use
},
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
}
);
console.log('Chat completion response:', response.data.choices);
return response.data.choices;
} catch (error) {
console.error('Error:', error.response.data);
}
}
module.exports = {
fetchChatCompletion
}
5. Above method is used for calling the Chatgpt Api, It will take the message content and then it will summarise then return the summary.
6. Use like this:
const message = fetchChatCompletion("Can you please tell how to do node js setup?");
7. Here is a working example: https://github.com/sahilpandya/confluenceGPT