Introduction
Welcome to my first article on the ever-evolving world of AI. We’re starting with a hands-on exploration of the OpenAI API, offering a glimpse into the vast potential of AI. This is just the beginning of a series where we’ll uncover more about AI’s capabilities and applications. Stay tuned!
We will proceed based on this tutorial.
Quickstart tutorial – OpenAI API
Be sure to read Introduction, Switch with an instruction, Add some examples, Adjust you settings.
Environment
- Ubuntu 22.04 LTS
- python 3
- git
Let’s start
Add the following code to ~/.bashrc to run the command :
alias python="python3"
alias pip="pip3"
Then, source reflect it in bash using the command.
source ~/.bashrc
Start tutorial
Copy the repository with git
Now, let’s move on to the Build of your application section.
First, create a directory with any name in your home directory and move to the current directory.
$ cd ~
$ mkdir OpenAI
$ cd OpenAI
Clone the tutorial repository with git.
$ git clone https://github.com/openai/openai-quickstart-python.git
Get API key
Access the OpenAI API page , select Personal at the top rleft of the screen, and click [View API keys] to display the API keys screen.

Press the Create new secret key button to get your API key.

You will only see your API key once, so be sure to write it down. Also, please do not share your API key with anyone or publish it in a public github repository.
Set the API key in your environment
Copy the environment file called .env.example as an .env file.
cd openai-quickstart-python
cp .env.example .env
Please change the .env file as below.
FLASK_APP=app
FLASK_ENV=development
# Once you add your API key below, make sure to not share it with anyone! The API key should remain private.
OPENAI_API_KEY=sk-XXXXXXXXXX>
Normally I don’t want to hardcode it, but for now I’m just going to proceed as is.
Run the application with python
Create a virtual environment, install dependencies, and run your flask application.
python -m venv venv
. venv/bin/activate
pip install -r requirements.txt
flask run
The application will launch at http://127.0.0.1:5000/, so access it.
It looks like a web application that generates names for your pet. I immediately tried typing dog and cat.


He suggested the name » Super Paws, Mighty Mutt, The Mighty Hound » for dog, and » Super Whiskers, Furry Fury, The Mighty Purr-fect » for cat.
Let’s take a quick look at how the application works.
The app.py file was as follows.
import os
import openai
from flask import Flask, redirect, render_template, request, url_for
app = Flask(__name__)
openai.api_key = os.getenv("OPENAI_API_KEY")
@app.route("/", methods=("GET", "POST"))
def index():
if request.method == "POST":
animal = request.form["animal"]
response = openai.Completion.create(
model="text-davinci-003",
prompt=generate_prompt(animal),
temperature=0.6,
)
return redirect(url_for("index", result=response.choices[0].text))
result = request.args.get("result")
return render_template("index.html", result=result)
def generate_prompt(animal):
return """Suggest three names for an animal that is a superhero.
Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: {}
Names:""".format(
animal.capitalize()
)
A prompt has already been written in the generate_prompt function.
It suggests names for animals like superheroes, with examples for cats and examples for dogs.
I also thought that the suggested name sounded like a hero, but it was specified in the prompt.
By changing this, the results will likely change. It looks fun when you play around with it.
Summary
I briefly followed the OpenAI API Quickstart tutorial in a python environment.
There may be a charge , so please use it wisely.

Une réflexion sur “[For Beginner] I tried the QuickStart tutorial of OpenAI API”