OpenAI APIs
OpenAI API is a simple restful api. But the have Python and Node.js packages built on top of the api. We are using the Python package.
Installation and Setup
- Python:
- Windows & MacOS: Go to Python3.11 and find the link in the page and follow the installer.
- WSL2 & Ubuntu:
- run
sudo apt update && sudo apt upgrade. - Check the Python version:
python -V. - If you need to update the python, run
sudo apt upgrade python3. - run
- Virtual Environment:
- Create a venv:
python -m venv path-to-venv
venv stands for Virtual Environment. 2. Activate the venv:
Linux & MacOS:source path-to-venv/bin/activate. Windows:path-to-venv\Scripts\activate. - VS Code:
- Install Python Extension:
Extensions > search Python > Install microsoft.com Python extension.
2. Choose the Python interpreter from your venv:
Settings > Command Palette > Python: Select interpreter > Enter interpreter path > Find > Path-to-env/bin/activate. 3. Choose the kernel for notebooks. - Set up API Key: We use
dotenvpackage and.envfile. This way can work for all similar APIs.- In each venv install
dotenvpackage bypip install dotenv. - In each project folder create
.envfile. In this file add the following line:OPENAI_API_KEY=key. - At the top of the main file of your project, add the following two lines:
from dotenv import load_dotenv
load_dotenv() - In each venv install
Models
- API Structure: The API interface is very simple. You just need a client to call any of the models. The following two lines import the needed class and create an instance of it:
from openai import OpenAI
client = OpenAI()
- Models List
- GPT-4 Turbo accessed by handle
gpt-4-turbo-previewor any of the following handles.gpt-4-0125-previewContext Window: 128 k, Training Data: Up to Dec 2023.gpt-4-1106-previewContext Window: 128 k, Training Data: Up to Apr 2023.gpt-4-1106-vision-previewContext Window: 128 k, Training Data: Up to Apr 2023. Understands images.
- GPT-3.5 Turbo accessed by handle
gpt-3.5-turbogpt-3.5-turbo-0125Context Window: 32 k, Training Data: Up to Sep 2021.
- DALL-E
dall-e-3released Nov 2023dall-e-2released Nov 2022
- TTS Text to natural sounding spoken text.
tts-1tts-1-hd
- Whisper general-purpose speech recognition model.
- Embeddings numerical representation of text for semantic similarity and semantic search.
text-embeddings-3-largeOutput dimension: 3072text-embeddings-3-smallOutput dimension: 1536text-embeddings-ada-002Output dimension: 1536 (oldest)
- GPT-4 Turbo accessed by handle