Get started building with Generative AI

Step by step guide making a GPT-3 AI chat

As an aside, for a general overview of the generative AI space and possibilities in it, check out this twitter thread.

Overview

Here’s what we’ll make today. It’s got all your AI chat essentials.

Step 1: Everything comes down to Prompt Design

Our models can do everything from generating original stories to performing complex text analysis. Because they can do so many things, you have to be explicit in describing what you want. Showing, not just telling, is often the secret to a good prompt.

The secret to writing good prompts is understanding what GPT-3 knows about the world and how to get the model to use that information to generate useful results. In a game of charades, our goal is to give players just enough information to figure out the right word using their intelligence. In the same way, we must give GPT-3 just enough context in the form of a training prompt for the model to figure out the patterns and perform the task. We don’t want to interrupt the natural intelligence flow of the model by overloading it with information, but giving it too little can lead to inaccurate results.

Dialogue with ChatGPT
The prompt with the returned completion. The completion is highlighted in green.

Step 2: Set up the app

Step 3: Make it your own

const pre_prompt = `
You support me in identifying gratitude in my life.
You share examples of gratitude, and you also share reasons why recognizing gratitude
can improve one's wellbeing. You help me find gratitude. Your language is simple, clear,
and you are enthusiastic, compassionate, and caring.
An example of this is "I'm curious, what do you feel grateful for today?"
or "I'd love to know what you feel thankful for."
or "Is there anything that comes to mind today that filled you with gratitude?"
Your presence fills me with calm. You're jovial.
Limit the questions in each message and don't be too repetitive.
Gently introduce the idea of gratitude in our conversation.

Start with a quick greeting, and succinctly give me an example thing i can be thankful for.
Share this example gratitude in the first person.
Here is an example of how to start the conversation:
"Hi! I'm glad we can talk today. One thing I've been grateful for lately is the sound of the wind in the trees. It's beautiful."
`;

function generatePrompt(chat) {
let messages = "";
chat.map((message) => {
const m = message.name + ": " + message.message + "\n";
messages += m;
});

const prompt = pre_prompt + messages + "AI:";

return prompt;
}
      const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: generatePrompt(chat),
temperature: 0.9,
max_tokens: 250,
presence_penalty: 0.6,
stop: ["AI:", "Me:"],
});
res.status(200).json({ result: completion.data.choices[0].text });
function getGreeting() {
const greetings = [
"Hi there! How's your day going? I've been feeling particularly grateful for the delicious meals I've been able to enjoy lately. How about you?",
"Good morning! I hope you're having a great start to your day. I'm feeling grateful for the beautiful nature around me, it always helps me to feel at peace. What are you thankful for today?",
"Hello! I'm grateful for the laughter and joy that my loved ones bring me. What are you grateful for today?",
"Hey, How's it going? Today, I'm grateful for the simple things in life like a warm bed and a good book. What are you grateful for today?",
"Hi, how are you? I'm feeling grateful for the memories I've made with friends and family. Is there anything you're grateful for today?",
];
const index = Math.floor(greetings.length * Math.random());
return greetings[index];
}
const response = await fetch("/api/generate", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
chat: [...messages, { name: "Me", message: sentInput }],
}),
});
const data = await response.json();
if (response.status !== 200) {
throw (
data.error ||
new Error(`Request failed with status ${response.status}`)
);
}

setMessages((prevMessages) => {
const newMessages = [
...prevMessages,
{ name: "AI", message: data.result },
];
return newMessages;
});

Step 4: Deploy

--

--

Building. Author of “Feeling Great About My Butt.” Previously: Creators @Medium, Product @embedly, Research @NECSI. http://whichlight.com.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kawandeep Virdee

Building. Author of “Feeling Great About My Butt.” Previously: Creators @Medium, Product @embedly, Research @NECSI. http://whichlight.com.