I coded my own ChatGPT
A dummy-level guide to building your own AI chatbot with GPT-4
Hello there! Long time no hearing from me, since writing has become part of my job at E2B.
My life philosophy is to suck at something new every month.
This time, I am learning to code, so in this post, I’ll be explaining how to create an AI friend that can chat with you on any topic, except for explicit or adult content. Later, I will teach this friend to become a senior programmer as well.
This guide is aimed at beginners and is meant to be super easy to understand. I wrote it as I was learning the basics, so I hope it’s helpful!
Let’s go fight snakes!
A plan
Rather than building the program part by part, I have decided to start with creating a super-simple program and then improving it through iterations. This will enable us to understand the process better.
The iterations
- Create a simple program (= just a bunch of code) that returns everything that I write to it as an input. Like an annoying friend that occasionally starts repeating everything after you
- Add an option for the program to “call AI”, so it gains its own “brain” and can talk to me. Don’t get too excited, since we need to pay for the AI.
- Give my AI friend a memory (before it couldn’t remember what I said a few seconds ago)
- Make the AI friend become a programmer. We will only outline this since it is out of the scope of this beginner text.
👩🏻🍳 Ingredients
These prerequisites you will need if you want to replicate my process.
🥣 IDE for Python
You’ll need a code editor or Integrated Development Environment (IDE) to write and run your Python code. Popular choices include Visual Studio Code, PyCharm, or even the good ol’ trusty Python IDLE.
I stored my program publicly on GitHub, you can access it here.
🫘 Python code
If you also haven’t coded before, I recommend this beginner Python course by Codecademy.
🍤 API key
Adding AI to the program is gonna be the most emotionally demanding step of our journey — because it costs money.
We will need the OpenAI API — a key to using a GPT-4 — one of the currently most popular large language models (LLMs). The OpenAI API will allow us to connect GPT-4 to our program.
Everyone has their unique OpenAI API key, but don’t worry — it shouldn’t cost you more than a few dollars, since it is charged based on how much you use it.
🍱 GitHub account
I put “likes open-source” among my personality traits, so I am using GitHub, and you should too. Also, I found out that for programmers, GitHub profile and contributions can be more important than a CV, LinkedIn.
📜 OpenAI documentation
Since we will use an LLM from OpenAI, our book of wisdom will be their documentation, especially this part. I found it fascinating btw.
The process:
1. Simple program that prints what I tell it.
I will first create an annoying friend that repeats what you tell them. It is very easy.
user_input = input("Enter some text: ")
print(user_input)
This code will repeat my input one time. If I want to do this infinite times, I create an infinite loop that takes the user’s input and prints exactly the same input. The infinite loop is done by “while True”, we could also write for example “while 1 + 1 + 2” or “while ‘whatever’”.
while True:
user_input = input("Enter some text: ")
print(user_input)
2. Add the AI
Now, instead of printing the same thing that I tell the program, it will print what the large language model (LLM) considers the best reaction to my text.
The beauty of LLMs is that they are trained on large datasets to predict the most likely reaction to a given text input, making them suitable for creating AI assistants.
Importing the necessary libraries
First, we import several libraries on the top of the code from part 1. These are “surprise tools that will help us later”, and I explained each of them in the # comments.
# The openai library provides a Python interface for OpenAI API
import openai
# The os library in Python provides a way to interact with the operating system.
# It provides functions for accessing environment variables, working with files and directories, and executing system commands.
import os
# Importing dotenv so I can use my API key from .env file
from dotenv import load_dotenv
while True:
user_input = input("Enter some text: ")
print(user_input)
The third imported library, dotenv, is for hiding my API key in a separate file called .env. This is a usual practice when you want to hide sensitive data from public code. The dotenv allows my Python program access my API key from the .env file without showing it to you (sorry).
Setting up the key
We imported what we needed, now we can call a function that calls environment variables for us. In this example, we only have one such variable — the API key.
# load_dotenv() is a function from the dotenv library that loads environment variables from a .env file
load_dotenv()
# os.environ is a dictionary-like object that represents the current environment variables.
openai.api_key = os.environ["OPENAI_API_KEY"]
This way, the .env file won’t be shown on your GitHub repository.
Making AI think and talk
So far, we have this code
# This is importing necessary libraries and loading the OpenAI API key
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.environ["OPENAI_API_KEY"]
# This is still just printing the same that you write
while True:
user_input = input("Enter some text: ")
print(user_input)
We need the AI to somehow answer. We add “messages”, which is part of “chat completions” — a concept prepared by OpenAI for making AI chatbots like this one.
“Messages” tells us what roles will you and the model play and how could the conversation look like.
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.environ["OPENAI_API_KEY"]
# Specify the role of LLM in the "system" (for example, a helpful assistant)
# Provide example of user's input (e.g. who won the world series)
# Provide example of ideal model's output (the most optimal answer)
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
]
# Inside of the infinite loop, we add also response from the model
while True:
user_input = input("Enter some text: ")
print(user_input)
# This is a form of response from the OpenAI model.
# If you want to understand this part more, please study the Chat Completions in their documentation.
response = openai.ChatCompletion.create(.
model="gpt-4",
messages=messages,
# This takes the right part of model's output and prints it.
choice = response["choices"][0]
content = choice["message"]["content"]
print(content)
3. Adding memory
So far, we can chat with the AI friend, but it doesn’t store the text that you provide to the program.
For that to happen, we need to add memory. This will be done by “append”.
In Python, the append() method appends an element to the end of the list. That’s exactly what we want to do with each input user (me or you) provides and also each output that the AI friend (the LLM model) returns.
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.environ["OPENAI_API_KEY"]
messages = [
{"role": "system", "content": "You are a helpful assistant."}
]
while True:
user_input = input("Enter some text: ")
# First .append: Adding the user input to memory
messages.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
choice = response["choices"][0]
content = choice["message"]["content"]
# Second .append: Adding the LLM's output to memory
messages.append({"role": "assistant", "content": content})
print(content)
The resulting code
We are done. This is the resulting code for you to try. Please let me know how about your own AI friend.
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.environ["OPENAI_API_KEY"]
messages = [
{"role": "system", "content": "You are a helpful assistant."}
]
while True:
user_input = input("Enter some text: ")
messages.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
choice = response["choices"][0]
content = choice["message"]["content"]
messages.append({"role": "assistant", "content": content})
print(content)
Making my AI friend a programmer.
This is next, optional part, in which we will make the AI friend code.
OpenAI Functions
OpenAI introduced “function calling”, where the
“Functions” have two required properties, name and parameters, as well as an optional one, description. `name` corresponds to how we call the function while `description` is used by the LLM to choose when and how to call the function. Parameters is a nested object that has three fields:
The logic is that you describe something you want, e.g. “What is the weather in Prague today”, and LLM recognizes by your talk, that it could help to call a function. It assigns the key terms from your prompt to the function (e.g. “weather” identifies that we want the function, “Prague” specifies the location parameter, and “today” specifies the time parameter.)
The GPT-4 model has been fine-tuned to know WHEN it should call a function and how to provide correct output for functions. Adding functions will have 3 key steps:
1. Describe a function that I want the LLM to use. For this, we will use JSON, a new concept for me.
2. Tell the model what to do if I want to use a function
3. Add the context of the function to the model’s memory.
Useful resources for functions
I will not provide more code here, but for more advanced content, watch the E2B blog.
Until next time!
Happy hacking.