Purpose of the guide
Use this guide if you faced any issues with setting up the environment for openai CLI / toolbox. openai API reference
Setup
- Install the openai package with dependencies:
pip install --upgrade openai
you may also install it using npm, brew or other package managers.
- Set your OPENAI_API_KEY environment variable :
export OPENAI_API_KEY="<OPENAI_API_KEY>"
- Now in order to run openai script as a CLI tool we need to:
- add shortcut to the bin
- make the script executable (for unix/mac)
python3 -m site --user-site
That will give you path to the user-site packages directory (for me’/home/vzhukov/.local/lib/python3.8/site-packages’). Add ‘openai/_openai_scripts.py’ and run the following command:
sudo chmod +x /home/vzhukov/.local/lib/python3.8/site-packages/openai/_openai_scripts.py sudo sh -c 'echo python3 /home/vzhukov/.local/lib/python3.8/site-packages/openai/_openai_scripts.py \$\@ > /usr/local/bin/openai'
Here the first command will make script executable and the second will create link to ‘openai’ call.
From now on you can use openai as a CLI tool, for example:openai api fine_tunes.list
P.S. curl could be also handy when you are working with OpenAI API:
curl https://api.openai.com/v1/completions \
-H "Authorization: Bearer <OPENAI_API_KEY>" \
-H "Content-Type: application/json" \
-d '{"prompt": "Why do I weven write these guides? :)", "model": "curie:ft-responder-2023-06-28-04-36-35"}'