It can be hard to know where to jump in when you’re learning how to use or build with AI like GPTs (Generative Pre-training Transformers). This tutorial is designed to help you get started with creating your own GPT-powered chat bot. It will walk you through the process of creating a bot, making it live by deploying it with Netlify, and customizing it to make it your own.
#TL;DR
Our amazingly talented dev, Matt Kane, has made it oh so very easy for you by creating Daneel, a chat bot building template to help you hit the ground running. If you have a fast-approaching deadline, learn better by moving fast and coding things, or just loathe helpful, witty, step-by-step tutorials, here’s the fast path:
- Sign up for an OpenAI account and get your API key
- Click the button below and follow the prompts to deploy your bot to Netlify, pasting the API key when prompted
- Customize your bot by pulling down the code from your newly made repo and push new changes to redeploy
#Step-by-step
For the rest of us, we’re going to make a custom prompt, a few sample questions for our bot and then add a little styling. Here’s what it looks like and some resources:
#Our very own bot
There are a few ways to use this repo to make a custom chatbot. All of the approaches require an OpenAI account, API key, and a copy of the project code.
#Keyed in
We can sign up for an OpenAI account here. Once signed up, from the dashboard, click on the API Keys
tab, create, and copy a new key. We’ll need it later and will not be able to view it again. Like the ephemeral days of youth, we can’t revisit it, so make sure to copy and paste it somewhere safe.
#Deploying to Netlify
Now, it’s time to share our bot with the world. We have 3 different options on how to deploy our bot code:
#Option 1: Deploy to Netlify Button
The easiest way to get started is to click the button below and follow the prompts to clone the repo, add the OpenAI API key, and deploy your bot to Netlify:
#Option 2: Clone and deploy through the terminal
We can also do all the lifting from the command line. Here are the steps to do just that:
- Install the Netlify CLI with
npm install netlify-cli -g
- Create a new repo and site using the
netlify sites:create
command passing in the Daneel repo link (Thesites:create
command will output a link to your new site. We can also find it in the Netlify UI under theSites
tab.) - Clone the new repo with
git clone <your-repo-link>
- Add the OpenAI API key to the site’s environment variables with the
netlify env:set VAR_NAME value
and passing inOPENAI_API_KEY
as the variable name and the API key we copied earlier
#Option 3: Fork and deploy through the UI
Seeing is believing, or something catchy to transition us to a UI-based deployment. We can also use the Daneel repo as a template to create a new repo by clicking the ‘Use this template’ button to clone it to your own GitHub account. Then, we can deploy it to Netlify by clicking the ‘New site from Git’ button in the Netlify UI and following the prompts.
We’ll need to add the OpenAI API key to the site’s environment variables. We can do this by clicking on the Site settings
tab, scrolling down to the Build & deploy
section, and clicking on the Environment
tab. Then, we can click on the Edit variables
button and add the OPENAI_API_KEY
variable with the API key we copied earlier.
#Customizing your bot
There are a ton of possibilities for what kind of chatbot you can have since there was so much data used to train the model. To choose the right prompt, we need to be very intentional, detailed and specific. Or be ridiculous and see what happens. If it’s hilarious please let me know, if it’s bad, please forget my name and clear your browser history.
#Unique prompt
The prompt for the chatbot gives it its own personalty and specialty. We can change this by making a new prompt file in the prompts
folder. Each prompt file is a TypeScript file that has two exported constants:
prompt
- the prompt textsamplePhrases
- an array of sample phrases or questions users can click to send to the bot
Here’s an example of a prompt file for when I need a positive and helpful chatbot to counter my inner, defeatist voice:
Here’s some tips from the insightful author of the Daneel repo, Matt Kane:
The important parts in this prompt are:
- Who the bot is and what it should do. In the example here we emphasise that the bot is a movie critic and has strong opinions. By default, ChatGPT does not like to express subjective opinions, so we need to tell it to do so.
- Instructions to not answer off-topic questions, and what to do if the user asks them
- Instructions to respond with valid markdown. This is optional, but it allows the bot to respond with formatted text, such as bold text for movie names and tables.
- Contextual information about the date and user location. These are optional but can help the bot to be more accurate.
In order to have the chatbot use our new prompt, we need to update the config.edge.ts
file to use our new prompt file.
And to use the new sample phrases, we’ll need to update the config.browser.ts
file.
#Welcome Message
If you would like to add a welcome message to your chatbot, you can do so by updating the Welcome.tsx
file in the src/components
folder. Here’s an example of a welcome message:
At a loss for words? You can always just ask the chatbot to tell you about itself.
#Let’s take a peek
Want to have a lil’ chat to see how your prompt and sample phrases are working? We can push our code up and that will trigger a new build and deploy. Then you can either run the netlify open
command to open the site in your browser or navigate to the site dashboard in the Netlify UI under ‘Site overview’ and click on the live URL.
#The aesthetics
If we want are chatbot to be smart and pretty we can tweak the styling with Tailwind CSS classes. Specifically in these files:
src/routes/index.tsx
- the main chat interfacesrc/components/Welcome.tsx
- the welcome screensrc/components/ChatMessage.tsx
- the chat message component
We can add or take away any classes to style the site. For example if we want to change the apperance of the chat we can change these lines in the ChatMessage component:
Here’s a list of all the Tailwind classes to help you get those creative juices flowing.
#We did it!
We now have a chatbot to help us with our life coaching needs and whatever else you decided to create. We can customize it to our heart’s content and deploy it to Netlify to share with the world. If you have any questions, comments, or just want to say hi, please reach out to us on Twitter and share what you’ve made with the other builders in the Daneel repo.
We can’t wait to see what you make! Happy coding 👩🏻💻!