Exploring the OpenAI GPT-3 API
October 11, 2022
0. Why This Guide Exists
Everybody on campus keeps asking the same thing:
"Show me GPT-3 without the playground."
The completions endpoint is live, the docs are thin, and the possibilities feel endless. I built GPT-3 Academy—a single-file Streamlit app—to demo the engine and took notes along the way. This page is a distilled, hands-on guide so you can recreate (or remix) the magic yourself.
1. Sign Up and Grab Your Key
- Create an OpenAI account.
- Open View API Keys in the dashboard.
- Click Create secret key and copy it once.
- Store it safely—environment variables keep the key out of source control.
export OPENAI_API_KEY="sk-…your-long-string…"
2. Install the SDK
pip install openai==0.27.0 # current latest
pip install streamlit
The openai client hides HTTP plumbing so you speak plain Python.
3. Hello Completion
import openai, os
openai.api_key = os.getenv("OPENAI_API_KEY")
resp = openai.Completion.create(
model="text-davinci-002",
prompt="Write a haiku about autumn",
max_tokens=32,
temperature=0.8
)
print(resp["choices"][0]["text"].strip())
Key Parameters
Field
Plain-English Effect
modelWhich brain (
text-davinci-002 tops the list)promptEverything the model sees before it starts typing
max_tokensHard ceiling on output length
temperature0 = laser-focused, 1 = let it riff
top_pAlternative to
temperature for nucleus samplingfrequency_penaltyPenalize token repetition
presence_penaltyEncourage the model to introduce new topics
4. Prompt Recipes to Try
-
Summaries
Summarize the following for a second-grade reader: <your text> -
Outline generator
Make a bullet outline covering the key ideas: <your text> -
Fact check
List factual claims in the text, rate their plausibility 1-5, and cite sources if possible. -
Quiz builder
Create five multiple-choice questions that test understanding of: <topic>
5. Keep the Bill Down
@st.cache # Streamlit memoizes identical calls
def call_openai(prompt):
return openai.Completion.create(
model="text-davinci-002",
prompt=prompt,
max_tokens=256,
temperature=0.7
)["choices"][0]["text"]
Caching saves credits while you tweak the UI (and impress friends).
6. Wrap It in Streamlit
import streamlit as st, openai, os
openai.api_key = os.getenv("OPENAI_API_KEY")
st.set_page_config(layout="centered", page_title="GPT-3 Academy")
st.header("GPT-3 Academy")
text = st.text_area("Paste or write text")
task = st.selectbox(
"Choose capability",
("Summarize for a 2nd grader", "Make an outline",
"Fact Check", "Make a quiz")
)
if st.button("Run AI"):
if not text:
st.warning("Enter some text")
else:
prompt = f"{task}:\n{text[:1500]}" # 1500-char guard
st.write(call_openai(prompt))
Why Streamlit? One file, instant widgets, no front-end hassle—perfect for quick demos.
7. What GPT-3 Does Well (October 2022 Snapshot)
Capability
Real-world Note
Summarization
Excellent on articles under ~2k tokens
Light reasoning
Good if you guide with clear chain-of-thought cues
Creative writing
Poems, jokes, short fiction feel human-ish
Translation
Strong for high-resource languages
Code snippets
Short Python functions okay; long blocks need careful prompts
Factual recall
Mixed accuracy—add "If unsure, say 'I don't know'" to reduce hallucinations
8. Common Pitfalls
- Token limit: prompt + completion must stay under 4096 tokens.
- High temperature: entertaining but can spiral into nonsense.
- Missing newlines: dense prompts sometimes yield run-on gibberish.
- Rate limits: starter accounts sit at 60 requests per minute.
9. Next Steps
- Expose
temperatureandmax_tokensas UI sliders. - Log every prompt–response pair to SQLite for later study.
- Benchmark cheaper models (
curie,babbage) against Davinci. - Watch the changelog—rumor says a chat-style endpoint is brewing.