In this article, we will use OpenAI’s API to build a simple diet bot, a virtual assistant that can guide users through their dietary needs and choices.
Setting up OpenAI API

To get started, first, you need to create an account on OpenAI. Here’s how you do it:
- Visit the OpenAI platform’s website.
- Create an account by providing the necessary details.
- Once you’ve signed up, navigate to your profile on the top right side of the platform.
- Click on ‘View API keys’ from the drop-down menu.
- Generate a new secret API key by clicking on ‘Create new secret key’.
- Store the generated API key safely in a notepad file 😂, as you’ll need it later.
💡 Recommended: OpenAI Python API – A Helpful Illustrated Guide in 5 Steps
Developing the Diet Bot using OpenAI’s API

We’ll use Google Colab to develop our chatbot as it is a user-friendly and efficient platform.
Start by installing OpenAI’s Python client library using the command !pip install openai
in a Jupyter notebook and pip install openai
in your normal Python environment (not Jupyter).
💡 Recommended: How to Install OpenAI
Now, we can proceed with the code (thanks to Ashutosh Roy for providing an initial version of this code ♥️):
import openai openai.api_key = "YOUR API KEY" # Substitute "YOUR API KEY" with the key you generated earlier def dietchatbot(): messages = [ {"role": "system", "content": "You are a diet planner."}, ] while True: user_message = input("User: ") if user_message.lower() == "quit": break messages.append({"role": "user", "content": user_message}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) result = response['choices'][0]['message']['content'] print(f"Diet_Bot: {result}") messages.append({"role": "assistant", "content": result}) print("Chatting with the Diet bot") dietchatbot()
Let’s delve into how this code works:
- We start by importing the
openai
module and initializing our OpenAI API key. - We define a function
dietchatbot()
, which houses the main logic for our chatbot. - The
messages
list is initialized with a system message defining the bot’s role as a diet planner. This serves as the bot’s initial understanding and sets the context for all future interactions. The list will eventually store all the dialog exchanges between the user and the bot. - We then enter a
while
loop, which continues running until the user types'quit'
. Inside the loop, the bot takes the user’s input, appends it to themessages
list, and passes the updated list to the model. - The
openai.ChatCompletion.create()
function then generates a response based on the user’s query and the previously recorded messages. This function uses the GPT-3.5-turbo model. - The response is extracted from the
response
object, displayed to the user, and appended back to themessages
list.
In summary, this simple diet bot takes in user queries, processes them using OpenAI’s ChatGPT model, and provides appropriate dietary advice.
The conversation continues until the user decides to exit by typing 'quit'
. The code is quite flexible and can be easily customized to develop chatbots for different roles by changing the initial system message.
More Ideas for Initial System Messages (Alternative Bots)

The initial system message sets the context and role for the AI model. Here are five different ideas for the initial system message that can result in entirely different chatbots:
Personal Fitness Trainer
{"role": "system", "content": "You are a personal fitness trainer."}
This sets up the model as a fitness trainer, giving advice on exercise routines, workout plans, and general fitness guidance.
Mental Health Counselor
{"role": "system", "content": "You are a mental health counselor."}
This would prompt the model to act as a mental health counselor, providing help for stress management, mindfulness techniques, and positive thinking.
Financial Advisor
{"role": "system", "content": "You are a financial advisor."}
With this, the bot would offer advice on financial matters, such as investments, budgeting, retirement planning, and more.
Recipe Advisor
{"role": "system", "content": "You are a recipe advisor."}
This context would enable the chatbot to offer culinary advice, suggest recipes, and provide tips on cooking and baking.
Language Tutor
{"role": "system", "content": "You are a language tutor."}
The bot would act as a language tutor in this case, assisting with vocabulary, grammar rules, and language learning tips.
Also check out our free Finxter cheat sheet on OpenAI API: 👇
🔗 Recommended: Python OpenAI API Cheat Sheet (Free)

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.