Building an AI chatbot for beginners: part 0

Posted on 19 March 2023 in Programming, Python, AI

[Note that this series kind of dried up; when I started the series, I knew that I knew very little about the subject, but I was hoping to learn better by learning in public. However, as time went by it turned out that this wasn't working. There are a lot of better tutorials out there!]

Like a lot of people, I've been blown away by the capabilities of Large Language Model (LLM) based systems over the last few months. I'm using ChatGPT regularly for all kinds of things, from generating basic code to debugging errors to writing emails.

I wanted to understand more about how these tools worked, and feel strongly that there's no better way to learn something than by doing it. Building an LLM is, at least right now, super-expensive -- in the millions of dollars (although maybe that will be coming down fast?). It also requires a lot of deep knowledge to get to something interesting. Perhaps something to try in the future, but not right now.

However, using LLMs to create something interesting -- that's much easier, especially because OpenAI have a powerful API, which provides ways to do all kinds of stuff. Most relevantly, they provide access to a Completion API. That, as I understand it, is the lowest-level way of interacting with an LLM, so building something out of it is probably the best bang for the buck for learning.

Over the last few weeks I've put together a bunch of things I found interesting, and have learned a lot. But it occurred to me that an even better way to learn stuff than by building it is to build it, and then explain it to someone else, even if that person is an abstract persona for "someone out there on the Internet". So: time for a LLM chatbot tutorial!

I'm writing this addressing myself as I was a few months back, so if it's of interest to anyone, it'll be for people who know Python reasonably well, but who haven't spent much or any time coding for LLMs. If you already know AI or Natural Language Processing well, you probably won't find anything new here (though I would be very grateful for any corrections!).

My goal is to keep things as simple as possible. The bots I've built in the past have been web-based or use Telegram to interact with the user. That adds on unnecessary complexity from the viewpoint of a tutorial, so I'll show how to build a basic chatbot that runs in a console as a single process. You'll run

python bot.py

...and be able to type in your messages. The bot will respond, using OpenAI as a backend.

So far I've planned out the steps required to go from a basic framework that bounces stuff off OpenAI, to something that can actually have a conversation, to something that avoids the obvious forms of prompt injection, to something that can have long-running conversations and understand the context, even if a user's message refers to something they said some time ago.

I'll be posting these as I write them. Hopefully there are people out there who'll find them useful!