Maximize Your Productivity with Node.js and ChatGPT: A Guide to Building an Efficient Email Writing Assistant
Madhur Rastogi
Node JS |?| JavaScript |?| Git |?| ElasticSearch |?| MongoDB |?| SQL |?| NEXTJS |?| HTML |?| GraphQL |?|
I hope you’re all doing well. As you know, ChatGPT is currently a hot topic in the market. It’s becoming a key tool in many IT companies to boost productivity. While ChatGPT is indeed transforming our IT infrastructure, it can also enhance your personal productivity if you understand how to use its models effectively.
Recently, I decided to create an email generator using ChatGPT to help me focus on other tasks. Having worked as a software developer for the past five years, I know firsthand the numerous responsibilities we juggle, including sending follow-ups to clients and coordinating with internal teams. This tool is designed to streamline those tasks and free up more of your time.
A word about the API
OpenAI, the company behind ChatGPT, offers a straightforward API for us to use. While you can also access DALL-E through this API, we'll focus on ChatGPT for now.
Unlike its public interface, this API doesn’t allow for unlimited requests. The free tier is quite limited, but it should be adequate for our example.
If you plan to use it for more extensive purposes, I recommend adding your credit card information. The costs are generally low unless you create a highly utilized API. The pricing model is pay-as-you-go, so the more you use the API, the more you pay.
Additionally, I'll be providing all my examples in JavaScript, but you can easily adapt them to your preferred programming language. As you'll see, the API is quite simple.
Building our assistant
In this article, I'll demonstrate how to create a simple command-line tool that prompts you to specify the recipient of the email and provide a brief description of its content.
That's all there is to it. The result will be a fully written email generated by ChatGPT.
Along the way, you'll gain insight into some of the key parameters that govern this model and learn how to adjust them to suit your preferences.
What we’ll need
The API operates by receiving a prompt, similar to how you interact with the public ChatGPT interface.
To have it write an email for us, we’ll give it a clear instruction along with additional details.
Specifically, we’ll provide the name of the email recipient, the subject of the email, and your own name.
The final output should appear as follows:
Here is what we need:
And that’s it!
领英推荐
Putting it all together
The first thing to do is to get your API key, for that you’ll need to sign up for an account on the OpenAI website.
Once you have your account, which should be newer than 3 months (otherwise your free credit would have expired), visit the API Keys section of your profile.
Click on the “Create new secret key” button, and make sure you copy the key you’re shown right after because you won’t be able to get it again.
Once you have the key, save it inside a .env file in a variable called OPENAI_API_KEY . We’ll use it in a minute.
With the key saved, install both packages with npm install openai dotenv . That will give you all the extra tools you need.
Then proceed to write this code:
const { Configuration, OpenAIApi } = require("openai");
require("dotenv").config();
const readline = require("node:readline/promises").createInterface({
input: process.stdin,
output: process.stdout,
});
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
async function writeEmail(name, topic, destination) {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `Write an email for ${destination}, ${topic}, my name is ${name}`,
max_tokens: 200,
temperature: 0.7,
});
console.log(response.data.choices[0].text);
}
(async () => {
let destination = await readline.question(
"Who is the email for? (let us know if this person knows you) "
);
let name = await readline.question("What's your name? ");
let topic = await readline.question("What is the email about? ");
await writeEmail(name, topic, destination);
readline.close();
})();
We’re also using the readline package that comes with Node. That package simplifies the process of asking for user input in the terminal, so we’ll use it on lines 27 to 29 with the question method.
We’re creating the OpenAI client on line 13 with the configuration object, that in our case, only contains the API key from before (thanks to line 2 where we use the dotenv package).
Finally, inside the writeEmail function we’ll use the OpenAI client to request a text completion. This is where you give the AI a prompt and it gives you back some text.
In our case, the prompt can be found on line 18, where I create a string using the three parameters I request from the user.
You can change that template to whatever you want, but I found good results asking it to write an email like that. You could use variations like:
`Write an informal email to ${destination}, ${topic}, my name is ${name}`
`Write an email to ${destination}, ${topic}, make sure they understand it's important and that my name is ${name}`
`Write a corporate-sounding email to ${destination}, ${topic}, my name is ${name}`
`Write a formal email to my boss, ${destination}, about ${topic}, and sign it as ${name}`
Understanding the response
The response from the API call includes a lot of extraneous information, such as HTTP-related data. To get only the relevant information, you should focus on the data attribute.
In the code, I directly access the first choice from the data property and then retrieve the text attribute from it. If you need additional information, be sure to examine the full response.
And that’s all there is to it!
The "magic" might seem less mysterious once you see how the internals work, but the results are impressive. With this setup, you can quickly generate well-written text, like emails, in just seconds—especially handy for those days when inspiration is in short supply.
Have you used the OpenAI API before? What have you built with it?
Senior Managing Director
7 个月Madhur Rastogi Very insightful. Thank you for sharing