GPT-3 Programming 101
Here is a primer on how to get started using OpenAI GPT-3 API programmaticaly. OpenAI provides a Python API client, but other clients can be easy build on other languages like Node.
Basic Python Setup
This summary was extracted from many of Twilio’s great GPT-3 examples.
Create working folder.
$ mkdir my-gpt3-app $ cd my-gpt3-app
Setup a virtual environment to install some Python packages in a sandbox.
$ python3 -m venv my-gpt3-venv $ source my-gpt3-venv/bin/activate
Install required packages via pip (the Python package installer):
openai
: The OpenAI Python client library, to send requests to the OpenAI GPT-3 engine.
(my-gpt3-venv) $ pip install openai
Using a lightweight API client
I’ll be using Minimaxir/gpt-3-experiments python client for this example. See below for other client implementation and other languages. I picked this one since it has already build in saving API responses to formatted markdown files.
Install packages:
httpx
: Httpx · Pypi — A next-generation HTTP client for Python.pyyaml
: Pyyaml · Pypi — YAML parser and emitter for Python.fire
: Fire · Pypi – Automatically generate command line interfaces (CLIs) with a single line of code.tqdm
: Tqdm · Pypi— Instantly make your loops show a smart progress meter
$ source my-gpt3-venv/bin/activate (my-gpt3-venv) $ pip3 install httpx pyyaml fire tqdm
Setup configuration in config.yml
Usage (this will generate a file for each specified temperature
in the config.yml
.):
(my-gpt3-venv) $ python3 openai_api.py "Once upon a time" (my-gpt3-venv) $ python3 openai_api.py "prompt.txt"
(Bonus) Saving OpenAI Secret key in an environment variable
Install packages:
python-dotenv
: The python-dotenv package, to read a.env
configuration file.
(my-gpt3-venv) $ pip install python-dotenv
Setup OpenAI API key. Create a .env
file in your project’s root directory
OPENAI_KEY= <SECRET-OPENAI-KEY>
Resources
Libraries
See a full list at OpenAI API
Python:
- Openai/openai-python — OpenAI Python Library
- Minimaxir/gpt-3-experiments — Test Prompts For Openai’s Gpt-3 Api And The Resulting Ai-generated Texts.
- Shreyashankar/gpt3-sandbox — Turn your ideas into demos in a matter of minutes.
Node:
- Njerschow/openai-api — A Tiny Client Module For The Openai Api
- Erlapso/openai-api-node — A Simple Node Wrapper For The Openai Api