How To Build a GPT-3 Web App with Python

Become unstoppable at Codenames with the help of OpenAI’s GPT-3

What is Codenames?

Requirements

Getting Started

$ git clone https://github.com/shreyashankar/gpt3-sandbox.git
$ python -m venv venv
$ source venv/bin/activate
pip install <package>
(venv) $ pip install openai
(venv) $ pip install python-dotenv
pip install -r api/requirements.txt
npm install yarn
brew install yarn
export OPENAI_KEY=your-super-secret-api-key

Intro to GPT3

Creating our Codenames Web App

# load dotenv to reference .env
from dotenv import load_dotenv
import os
load_dotenv()
KEY_NAME = os.getenv(‘OPENAI_KEY’)
set_openai_key(KEY_NAME)
def submit_request(self, prompt):
“””Calls the OpenAI API with the specified parameters.”””
response = openai.Completion.create(engine=self.get_engine(),
prompt=self.craft_query(prompt),
max_tokens=self.get_max_tokens(),
temperature=self.get_temperature(),
top_p=1,
n=1,
stream=False,
stop=self.stop)
return response
# Construct GPT object and show some examples
gpt = GPT(engine=”davinci”,
temperature=0.7,
max_tokens=10)
class Example:
“””Stores an input, output pair and formats it to prime the model.”””
def __init__(self, inp, out):
self.input = inp
self.output = out
self.id = uuid.uuid4().hex
def get_input(self):
“””Returns the input of the example.”””
return self.input
def get_output(self):
“””Returns the intended output of the example.”””
return self.output
def get_id(self):
“””Returns the unique ID of the example.”””
return self.id
def as_dict(self):
return {
“input”: self.get_input(),
“output”: self.get_output(),
“id”: self.get_id(),
}
gpt.add_example(Example(“answers: nut, bark”,
“clue: tree”))
gpt.add_example(Example(“answers: trick, spells, wizard, enchanted, mystic”,
“clue: magic”))
gpt.add_example(Example(“answers: football, soccer, rugby, tennis”,
“clue: sport”))
gpt.add_example(Example(“answers: yarn, shoe, knit, stocking”,
“clue: sock”))
question_prefix = “answers: “
answer_prefix = “clue: “
gpt = GPT(engine=”davinci”,
temperature=0.7,
max_tokens=10,
input_prefix=question_prefix,
output_prefix=answer_prefix,
append_output_prefix_to_query=False)
# Define UI configuration
config = UIConfig(description=”Use GPT3 to Dominate Codenames”,
button_text=”Generate Clue”,
placeholder=”Add your spymaster words separated by commas”)
demo_web_app(gpt, config)
* Serving Flask app “api.demo_web_app” (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
yarn run v1.22.10
warning ../../package.json: No license field
$ react-scripts start
ℹ 「wds」: Project is running at http://192.168.86.107/
ℹ 「wds」: webpack output is served from
ℹ 「wds」: Content not from webpack is served from /Users/jabe/Projects/nba_playground/gpt3-sql/gpt3-sandbox/public
ℹ 「wds」: 404s will fallback to /
Starting the development server…
Browserslist: caniuse-lite is outdated. Please run the following command: `npx browserslist — update-db`
Compiled successfully!
You can now view gpt3-sandbox in the browser.Local: http://localhost:3000
On Your Network: http://192.168.86.107:3000
Note that the development build is not optimized.
To create a production build, use yarn build.
127.0.0.1 — — [31/May/2021 18:16:36] “GET /params HTTP/1.1” 200 -

Adding to Our Github Repo

Conclusion

Helpful Resources

--

--

May or May Not Work for the CIA.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store