I built a Lenny chatbot using GPT-3. Here’s how to build your own.
I built a Lenny chatbot using GPT-3. Here’s how to build your own.A step-by-step guide to building an AI chatbot👋 Hey, Lenny here! Welcome to this month’s ✨ free edition ✨ of Lenny’s Newsletter. Each week I humbly tackle reader questions about building product, driving growth, and working with humans. If you’re not a subscriber, here’s what you missed this month: Subscribe to get access to these posts, and every post. Last month, Dan Shipper (co-founder and CEO of Every) launched a chatbot trained on the Huberman Lab podcast, and I was blown away. I pinged Dan to see what it would take to build something like this for my newsletter content (as I was super-curious about the process, plus readers have been asking for it over the past couple of months, including just yesterday!), and by the next morning, Dan had a functioning chatbot 🤯 Not only did Dan offer to build and launch the chatbot, he suggested that we use this opportunity to help people learn how to build their own. And that’s exactly what you’ll find below. Dan walks us through the basics of AI and GPT-3, how to set up an environment to play with the API, and how to train it on your own data. No coding background necessary. I found this incredibly interesting, and, as Dan points out below, “the best way to prepare for this fast-approaching future is to dive in and get your hands dirty.” Let’s dig in. You can find Dan Shipper on Twitter, LinkedIn, and especially make sure to subscribe to Every, where he shares his ongoing research into all things AI, tech, and personal development. How I built a Lenny chatbotby Dan Shipper Lenny’s Newsletter is great, but it’s inherently one-sided. It talks to you, but you can’t talk back. Wouldn’t it be awesome if you could ask Lenny’s Newsletter a question? Well, now that’s possible. Over the course of last week I built an AI-powered chatbot for Lenny that uses his entire newsletter archive to answer any question you have about product, growth, and startups. It’s built with GPT-3 and it took a couple hours to do, end to end. In this post, I’ll break down exactly how the Lenny Bot works so you can learn to build one yourself. You can also use it right now 👇 AI technologies like GPT-3 are still in their infancy, but they’re going to be everywhere soon. Staying on top of how they work is going to be crucial to your career in tech, and especially in building product. The best way to prepare for a fast-approaching future is to dive in and get your hands dirty. That’s what I’ve been doing over at Every—I’m writing weekly about my experiments with using GPT-3 to build chatbots, understand patterns in my thinking, and organize my notes. I’m trying to understand these technologies as deeply as possible, and I want to share with you what I’ve learned. It might seem intimidating to get started, especially if you don’t have a technical background. But I’m going to start at the very beginning. You’ll be able to understand what I’m talking about and begin using it yourself, no programming required. (And if you have any questions, you can always paste them into ChatGPT—it’ll give you good responses ;) Preamble: GPT-3 vs. ChatGPTYou’ve probably heard of both GPT-3 and ChatGPT. Maybe you use those terms interchangeably, or maybe you’re not really sure what the difference is. It’s worth taking a minute to understand how they differ. GPT-3 and ChatGPT are both “large language models.” These are machine-learning models that are very good at generating natural-sounding text, code, and more. They’re trained using large data sets of text, and this helps them get very good at lots of natural-language tasks, like answering questions, writing marketing copy, and holding conversations. So what’s the difference between them? And why is it important? GPT-3 is a general-purpose language model—it can hold conversations, write code, complete a blog post, do translation tasks, and more. You can think of it like a flexible know-it-all that can expound on any topic you want. ChatGPT is a version of GPT-3 that’s been turned into a friendly, inoffensive extrovert. Basically, it’s been specifically trained to be good at holding conversations. OpenAI does this by repeatedly holding conversations with the model and then rewarding it for good responses and punishing it for bad ones—a process called Reinforcement Learning from Human Feedback. You’d think since we’re building a chatbot we’d use ChatGPT, right? Unfortunately not. OpenAI hasn’t created a way for us to interact with the ChatGPT model directly—you can only use it through the ChatGPT web app. So it’s not suitable for our purposes. We want to be able to interact with the model directly, not through an intervening app. So instead we’ll use GPT-3 for our explorations. It’ll give us all the power and flexibility we need to build a chatbot. We’ll do it in two ways: using OpenAI’s Playground to start, and with a little bit of code after that. The Playground is a web app that lets you prompt GPT-3 directly and get responses back, making it a great place for us to experiment. Let’s start there and see how things go. The basics of GPT-3The really basic way to explain GPT-3 is that it likes to finish your sentences for you. You provide it with a starting set of words, and it tries to figure out the most likely set of words that follow from your input. You can provide any string of words. It’s very flexible and can talk about anything you want, from product management to astrophysics. The set of words you provide is called a prompt, and the answer you get back from GPT-3 is called a completion. Below is a simple example in the GPT-3 Playground. The non-green text is what I typed in as a prompt, and the green text is what GPT-3 returned as the completion: You can see that GPT-3 performs well on a simple completion like this. But it performs well even when the prompts get more complicated. You can, for example, prompt it to define product-market fit: That’s not bad! Since it can already answer product questions, this looks like it will be useful for our Lenny Chatbot out of the box. You might assume that on the back end, GPT-3 has some compendium of concepts that it’s using to understand your sentence and generate the right completion. But in reality, it’s a probability engine. It’s just very good at, given a prompt, finding the words that are most likely to follow it. It can do this because it’s been trained by analyzing the statistical probabilities of sentences from basically the entire internet, so it has a lot of data to learn from. (All those Medium posts about product-market fit are good for something!) If you want to learn more about how this works from a technical perspective, I recommend checking out Andrej Karpathy’s videos. Turning GPT-3 into a chatbotRight now we have the bot answering questions, but how can we get it to actually chat with us? Ideally we want it to get messages from the user and give responses back. And we want to give it a little bit of personality. It would be great if it sounded something like Lenny himself—warm, friendly, and smart. That’s pretty simple to do with GPT-3 as well. We can ask it to behave in this way in our prompt: As you can see, GPT-3 has read enough chatbot transcripts and product management posts to be able to start a conversation with us based on this kind of prompt. We can continue our conversation with it by writing more of the transcript: Notice what we’re doing—every time we run the model, we feed it the entire transcript of what came before in the conversation, and that guides its responses: Success! It’s chatting with us at a high level about product management questions, like how to build a roadmap. But what if we want to get responses to questions that are harder to answer? For example, one of the biggest values of Lenny’s Newsletter is the amount of benchmark data he provides so that you can measure how well you’re doing against the best in the business. If we go back through Lenny’s archive, we find in his post “What is a good activation rate” that the average activation rate across different kinds of products is about 34% and the median is 25%. Let’s ask GPT-3 and see whether it knows this: Not bad! It’s in the right ballpark, but its estimate for a good activation rate is a little lower than Lenny’s data says is the average. Ideally, since it’s a Lenny chatbot, we want it to return the benchmark he provides in his article. Once we start really probing the bot, this kind of problem only gets bigger. For example, if we ask it who Substack’s first publisher was—a topic Lenny covered in “How to kickstart and scale a consumer business, Step 4”—it will say it was Andrew Sullivan: This answer sounds confident, but it is incorrect. (The correct answer is Bill Bishop.) This isn’t an isolated incident. For example, if I ask, “Is it best for consumer startup ideas to come from founders who are trying to solve their own problems?” it replies: This is confident—and also wrong. As Lenny covered in “How to kickstart and scale a consumer business,” less than a third of consumer startup ideas came from founders solving their own problems. So it’s not “absolutely” a best practice. What’s going on here? There are two intertwined problems:
So how could we construct a chatbot with GPT-3 that solves these problems? Ideally we want to feed GPT-3 the information it needs to answer questions on the fly. That way it will have the right information available and will be less likely to make things up. There’s a pretty easy way to do that. Stuffing context into the promptWhen I was in high school, I had a physics teacher who allowed open-book tests. He would allow you to bring a single index card to the test with any of the formulas that you thought you needed to answer the questions. The idea was that memorizing the formulas didn’t matter so much. What mattered most was using your reasoning abilities to turn the formulas into the correct answer. People would come to the test with microscopic handwriting covering every inch of their notecard. It turns out that this was helpful! The formulas gave you the context you needed to think through the answers to the questions on the tests, so the tests became less about your memory and more about how well you understood the topic. (I got a B in that class, so my understanding was pretty average.) You can work with GPT-3 in a similar way. If, in your prompt, you include the equivalent of a notecard with context to help it answer the question, it will often get it right. (Its reasoning capabilities are better than mine.) Let’s go back to an example GPT-3 failed on earlier and see if we can correct it with this technique. As I mentioned above, in “How to kickstart a consumer business,” Lenny notes that less than a third of the founders got their idea from trying to solve their own problem: Last time, when we asked GPT-3 if it was best for consumer business founders to try to solve their own problem, it responded, “Absolutely!” Which, given what’s in Lenny’s article, is wrong. Let’s ask GPT-3 this question again—but let’s give it a little help. We’ll feed it the equivalent of a notecard. Written on the notecard will be the section of Lenny’s article that contains the answer. Then we’ll see if it can get it right. To make this fair, we won’t give it just the text containing the answer. We’ll give it a bunch of the surrounding text in the article as well to see how it does. Let’s see if it works: Success! Now it tells us that less than a third of founders were trying to solve their own problem. This is great—all we have to do is write all of Lenny’s posts on a little notecard and feed it into the model along with any question we have, and it will answer based on what he’s written. But this introduces another problem: space limitations. The notecard analogy is apt because there’s limited space in the prompt—right now, about 4,000 tokens (each token is the equivalent of ¾ of a word). So we can’t feed in Lenny’s entire archive on every question. We have to be very choosy about what we put in there. Let’s talk about how to solve this. Embedding Lenny’s archiveAt this point we’re going to have to move out of manual interactions with GPT-3’s Playground and start using chunks of code that work directly with the GPT-3 API. The code we’re building is going to do the following tasks:
This is simple to do with a library called GPT Index, an open-source library created by Jerry Liu. It’s completely separate from OpenAI but built to help with tasks like this. Here’s how it works:
Note: This is about to get a little bit more complicated and technical. If you’re interested in that, read on to get an explanation of exactly what’s happening here! You can access and run the code from this article in a Google Colab file here. Colab is a cloud-based programming environment that will let you run everything right from your browser. (If you have questions about any of this, reach out to me on Twitter.) If you’re not interested in the technical details, skip to the end to try out the chatbot for yourself. Still here? Great. Let’s start with index construction. Constructing our indexThe first thing we need to do is construct our index. You can think of an index as like a database: it stores a collection of pieces of text in a way that makes them easily searchable. To construct the index, we first collect Lenny’s entire newsletter archive into a folder. Then we ask GPT Index to take all of the files in the folder and break each file into small, sequential pieces. Then we store those pieces in a searchable format. The code looks like this: When we run this function, we’ll have created a file called index.json that contains chunks of Lenny’s articles converted into a format that makes them easily searchable. These are called embeddings—a condensed mathematical representation of each chunk of text. Just like latitude and longitude can help you tell how close two cities are on a map, embeddings do the same kind of thing for text chunks. If you want to know if two pieces of text are similar, you just calculate the embeddings for them and compare. Text chunks with embeddings that are “closer” together are similar. Embeddings are handy for us, because when a user asks a question, they’ll make it easy for us to search Lenny’s archive and find articles that are most likely to answer our question. With that in mind, let’s run the code and see what happens. Success! The Lenny’s archive is fully indexed, and we can query it to find relevant chunks of documents and use those chunks to answer our questions. (Be careful if you do this with big documents, as embeddings cost $0.0004 for every 1,000 tokens.) Asking our questionTo query the index we created in the last section, all we have to do is paste a question into GPT Index. It will then:
Here’s what the code looks like: And if I ask it, “What is good retention for a consumer social product?” it says: “25% is good.” If I ask it now, “Is it best for consumer startup ideas to come from founders who are trying to solve their own problem?” it automatically comes back with the right answer: “Based on the research, it appears that it is a good idea for consumer startup ideas to come from founders who are trying to solve their own problem, as this was the strategy used by around 30% of the most successful consumer companies. However, it is not necessarily the best strategy, as other strategies such as paying attention to curiosity, what’s already working, and paradigm shifts were also used by a significant number of successful companies.” We now have an end-to-end solution to turn questions into answers that are based on Lenny’s archive. And it only took a few lines of code! If you want to see the results in action, you should check out the bot here: You can also access the full source code for this article in this Colab notebook. What all of this meansIt’s important to keep in mind: this is just the beginning. The horizon of possibility is shifting almost every day with these technologies. What’s hard to do today will be easy in a matter of months. We’re just beginning to unravel the implications of this technology, but here are a few of the most relevant possibilities. Content creators get a new formatEvery newsletter, book, blog, and podcast that’s used as evergreen reference information by its audience can now be repackaged as a chatbot. This is great for audiences because it means that any time you want to know what Lenny (or any other creator) says about a topic, you’re not going to have to sort through an archive of articles or podcast episodes in order to get their answer to your question. Instead, you’ll just be able to use Lenny’s chatbot to get his answer instantly—and then maybe later read the article in full if you want more details. This is also great for content creators. They now get the ability to monetize the content they’ve already created in new ways, and lessen the amount of repetitive questions they have to answer. This will (hopefully) give them more time and money to create great content. A new class of content creators will learn to create compelling chatbot experiences that combine their personality and worldview for their niche audience in the same way that some creators learned to create compelling YouTube videos, newsletter articles, or TikTok clips. Organizing your notes is overYou’re not going to need a fancy filing system to make sure that you remember what got said in a meeting, or what you learned from the latest book you read. Instead, you’ll be able to ask a chatbot that sits on top of all your notes and returns the right answer to you. This type of chatbot will be able to act like a personal research assistant that can detect patterns in what you’re writing down and synthesize information to help you understand what you think about a topic. Enterprise knowledge management is changing foreverChatbots aren’t just valuable for content creators and personal knowledge management. They’re also a big deal for companies. Every company currently struggles with making sure all of the knowledge in their organization is written down, up to date, and searchable. In the future, chatbots could serve as a kind of automated librarian for company knowledge. They’d be responsible for answering questions by sourcing information from the right person or document. They could also be used to proactively record tacit knowledge into living documents by periodically interviewing key people about their progress on company initiatives. They’ll eliminate the repetitive questions that get asked inside organizations and make it easier for larger companies to collaborate effectively. Wrapping upAll of this is just the start. Ultimately, the limit for these technologies is our imagination and our desire to improve them. If you use Lenny’s chatbot or follow the code samples, you’ll see that it’s very promising but not yet perfect. There are tremendous returns available to the individuals or groups who learn to make these types of experiences truly incredible for users. I hope this inspires you to embark on that journey. If you’re really into this and want to go deeper, I write a column over at Every where I unpack my AI experiments and explorations. Check it out here. 📚 Further studyThanks, Dan! Have a fulfilling and productive week 🙏 📣 Join Lenny’s Talent Collective 📣If you’re hiring, join Lenny’s Talent Collective to start getting weekly drops of world-class product and growth people who are passively open to new opportunities. I hand-review every application and accept less than 10% of candidates who apply. If you’re looking for a new gig, apply to join! You’ll get personalized opportunities from hand-selected companies. You can join anonymously, hide yourself from companies, and leave anytime. ❤️🔥 Featured job opportunities
If you’re finding this newsletter valuable, share it with a friend, and consider subscribing if you haven’t already. Sincerely, Lenny 👋 |
Older messages
AI and product management | Marily Nika (Meta, Google)
Sunday, February 5, 2023
Listen now (48 min) | Brought to you by Amplitude—Build better products: https://amplitude.com/ | Eppo—Run reliable, impactful experiments: https://www.geteppo.com/ | Pando—Always-on employee
How to foster innovation and big thinking | Eeke de Milliano (Retool, Stripe)
Thursday, February 2, 2023
Listen now (63 min) | Brought to you by Miro—A collaborative visual platform where your best work comes to life: https://miro.com/lenny | Notion—One workspace. Every team: https://www.notion.com/
How Coda builds product
Tuesday, January 31, 2023
Lane Shackleton, CPO at Coda, shares his team's actual templates, processes, and many lessons learned
Lessons from Airtable’s unconventional growth strategy | Zoelle Egner
Sunday, January 29, 2023
Listen now (74 min) | Brought to you by OneSchema—import CSV data 10x faster: https://oneschema.co/lenny; Pando—always-on employee progression: https://www.pando.com/lenny; and Lenny's Job Board—
An inside look at Mixpanel’s product journey | Vijay Iyengar (Head of Product)
Thursday, January 26, 2023
Listen now (47 min) | Brought to you by Pando—Always on employee progression (https://www.pando.com/lenny), Notion—One workspace. Every team (https://www.notion.com/lennyspod), and Lemon.io—A
You Might Also Like
🚨 48 hours left - your login info for Alicia’s course
Saturday, November 16, 2024
How to Build a Million Dollar Beauty Brand by Alicia Scott Hey Friend , Only 48 hours left. This is your last chance to get access to How to Build a Million Dollar Beauty Brand with Alicia Scott. After
🚀 SpaceX Seeks $250B Valuation
Friday, November 15, 2024
Plus $ASTS and Blue Origin team up, $RKLB soars, $SPIR and ABL's realignment, Q3 earnings and more! The latest space investing news and updates. View this email in your browser The Space Scoop Week
Small but Mighty AI
Friday, November 15, 2024
Tomasz Tunguz Venture Capitalist If you were forwarded this newsletter, and you'd like to receive it in the future, subscribe here. Small but Mighty AI 77% of enterprise AI usage are using
🗞 What's New: Meet the indie hackers killing it on TikTok
Friday, November 15, 2024
Also: ChatGPT can read code from your computer ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
what’s next for tech in 2025
Friday, November 15, 2024
emerging tech trends to have on your radar heading into the new year Hi there, AI breakthroughs are fueling a new era of tech innovation. Join our expert panel as they go live to unpack the trends that
How to go viral on TikTok (10 actionable tips)
Friday, November 15, 2024
Plus, clever automations to streamline your content creation. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
10words: Top picks from this week
Friday, November 15, 2024
Today's projects: Kaptr.me • Success.ai • MakeCharts.co • Quickads.ai • StoryBee • Subjects • JobSearch.Coach • Directorist • X-Ray.contact • Music-list • Taskheat • SalesDoubler 10words Discover
Issue #130: Building $1K-$10K MRR Micro SaaS Products around AI-Powered Personalised Movies for Kids, SEO Optimisa…
Friday, November 15, 2024
Build Profitable SaaS products!! ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
The Age of the Gatekeeper Is Over — The Bootstrapped Founder 355
Friday, November 15, 2024
YouTube is both the best and the worst source of information — because we can watch it all, but no one will tell us what's REALLY worth watching. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
Who’s working for Mistral?
Friday, November 15, 2024
Plus: Fintech's fight for Gen Z cash; latest deals View in browser Sponsor Card - Up Round-27 Good morning there, Today we're bringing you details on one European startup that's reaching