This content originally appeared on Bits and Pieces - Medium and was authored by Fernando Doglio
Boost Your Productivity with Node.js and ChatGPT: How to Create an Email Writing Assistant and Save Time
A quick introduction to how to use the OpenAI API for your own benefits
ChatGPT has been a hot topic lately, and with good reason, after all, it’s doing a great job at fulfilling all of our requests.
And while it’s not the perfect code-generation tool as I’ve explained before, it definitely does a fantastic job if you ask it to write some words in plain English.
However, in this article I’m going to show you how to take advantage of its API to create an e-mail writing assistant. Essentially, you’ll give it a couple of important details, and this tool will give you a fully written email for you to send.
Let’s see how we can do that!
A word about the API
OpenAI, the company behind ChatGPT, provides a very simple API for us to use it. In fact, we can also use Dall-E through it, but we’ll focus on ChatGPT for the time being.
Of course, unlike with its public interface, we can’t just send unlimited requests to this API, the FREE tier is quite limited, but for our example it will do just fine.
If you’re going to be using it for something more, I’d recommend you enter your credit card, it’s definitely not expensive unless you create a massively used API (it’s the classic pay-as-you-go model, so the more you use the API the more you pay).
Also, I’ll be writing all my examples using JavaScript, but you can easily port them to the language of your choice, as you’re about to see, the API is not complex.
Building our assistant
For this article, I’m going to build a simple command line tool that will ask you who the email is for, and for you to enter a small description of what the email should say.
That’s it, as an output, you’ll get a fully written email from ChatGPT.
In the process, you’ll learn a little bit about some of the governing parameters of this model and how to tweak it based on your own preferences.
What we’ll need
The way the API works, is by providing it with a prompt, much like you do with the public UI of ChatGPT.
So if we want it to write an email for us, we’ll ask it exactly that and on top of it, we’ll provide a few more data points.
In particular, we’ll request the name of the person the email is for, the topic of the email, and finally, your own name.
The final result should look like this:
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.
It looks like this:
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, installed both packages with npm install openai dotenv . That will give you all the extra tools you need.
Then proceed to write this code:
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:
The other parameters are:
- The ML model to use. In my case I’m using the Davinci model, which is the most complex one (also the most expensive one to use if you’re paying for it).
- The Max number of tokens, which signals how long can the answer be. Tokens are a unit of measure that OpenAI uses to control how much can you use the API. Tokens translate to money, and for a new account, you’ll have 18 USD for free. It’s hard to mentally measure how many tokens you’ve used, so make sure you visit the Usage section of your profile often, it’ll show you how much you have left. If you want longer emails, increase this number.
- The temperature. This is a number between 0 and 1, the higher it is, the more “interesting” the answer will be. If you keep it at 0, then you’ll get a somewhat standard answer. Of course, the higher the number the crazier the result can be as well, so keep that in mind.
There are more optional attributes that you can use, including asking for multiple responses for you to choose from or if you want the response to be streamed or given all together once it’s ready.
Make sure you read the docs to understand what else you can do with this endpoint.
Understanding the response
The response you get from the API call will contain a lot of unnecessary information since it contains all the HTTP-related data. However, if you narrow down and focus on the data attribute, you’ll get all the info you need.
That is why I directly get the first choice from the data property, and from it I get the text attribute. Make sure to examine the response if you want to get extra information.
And that’s all there is to it!
The “magic” is gone, I know, it happens when you show the internals of such tools. But the results are great, now you can quickly get a well-written email in a few seconds, especially useful for those days when you’re not feeling inspired.
Have you used the OpenAI API before? What did you build with it?
Build Apps with reusable components, just like Lego
Bit’s open-source tool help 250,000+ devs to build apps with components.
Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.
Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:
→ Micro-Frontends
→ Design System
→ Code-Sharing and reuse
→ Monorepo
Learn more
- How We Build Micro Frontends
- How we Build a Component Design System
- Bit - Component driven development
- 5 Ways to Build a React Monorepo
- How to Create a Composable React App with Bit
- Sharing JavaScript Utility Functions Across Projects
Write an e-mail assistant with Node.js and ChatGPT and Improve your Productivity was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Fernando Doglio
Fernando Doglio | Sciencx (2023-02-05T07:07:29+00:00) Write an e-mail assistant with Node.js and ChatGPT and Improve your Productivity. Retrieved from https://www.scien.cx/2023/02/05/write-an-e-mail-assistant-with-node-js-and-chatgpt-and-improve-your-productivity/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.