Create cool projects with Python & ChatGPT!
Today’s blog will more creative twist on my previous blog. This will be a short explanation of how to use Python and ChatGPT, but to have more fun! There are several prompts we can experiment with but I have something delicious in mind!
Step #1 — API Keys
To access OpenAI’s API, you need to create an account if you haven’t already done so.
Home page of OpenAI
Once created go to the top right corner of your computer screen, and select ‘View API Keys’. Create an API Key or copy an existing API key then save it somewhere you will not forget!
DO NOT SHARE YOUR API KEY WITH ANYONE OR ON GITHUB!
Click ‘ View API Keys’
Step #2- OPENAI API & Python
Go to your desired directory and create your project folder. Using the Command line, follow the steps below.
$ mkdir python-play
$ cd python-play
Next, install the OPENAI API client library for Python. You will need to have Python and PIP, installed on your computer. To install the terminal follow the command below:
$ pip install openai
Once the installation is complete, you will import the library with the command above.
Step #3 — Connect to the API
To generate anything we need to first set up the OpenAI API client, model engine, and prompt with the following parameters:
openai.api_key = “YOUR_API_KEY”
model_engine = “text-davinci-003”
prompt = “Make a dessert. I have only an air fryer as my cooking method.
Ingredients: 2 Waffles, Nutella, cinnamon, frozen banana, peanut Butter
Cool Whip, syrup”
completion = openai.Completion.create(
If you’re using visual code as your code editor, you can create a file called play.py and structure it as follows:
#play py file works with the config.py file
from config import OPENAI_API_KEY
openai.api_key = OPENAI_API_KEY
response = openai.Completion.create(
prompt=”1.Create a dessert.2.Ingredients: Nutella, 2 frozen waffles, Frozen banana, Peanut Butter, Oreos, butter, marshmallows.3.Cooking Method: Air Fryer.”,
You will need to create another file that holds your API KEY. If you’re running this on your local machine and do not plan on uploading this play file to GitHub or public access you can skip this step. But it is good practice to conceal your API Key in case you want to push anything to production.
##OPEN API STUFF
OPENAI_API_KEY = “YOUR_API_KEY”
This is a basic template for anyone who wants to get started playing around with the OPENAI API using Python. Remember, you can customize the behavior of the model by adjusting the temperature parameter, which controls the level of randomness in the output. There is also the stop parameter which can be used to specify a string or sequence of strings that will cause the model to stop generating further text.
Step — 4 RUN THE CODE
Using the terminal in VisualCode run:
$ python play.py
Just like that, we made an awesome dessert using the ChatGPT engine! Feel free to customize it for your favorite sandwich, or homemade treat or ask for a meal plan, the options are endless. I hope you had a fun time learning about this real-world use case for ChatGPT, you never know when you’ll need a last-minute recipe to satisfy your sweet tooth!
OpenAI’s API provides an easy way to start using ChatGPT to assist in your day-to-day life as a type of productivity hack. As you saw in the blog above, ChatGPT can be used to create simple prompts or solve real-world tasks like finding a late-night snack. 🙂
With amazing tools like ChatGPT, you can use it for the simple or creative tasks you do every day such as creating a dessert for the family or a meal plan for the week.
Are you looking to stay up-to-date with my writing and projects? Connect with me on Linkedin and follow the links below to learn more!
Check out my website — cover letter builder — and read my official blog post to unlock the power of automated background removal with AI.