Django Restful API that communicates with OpenAI API via POST
A Restful API written in Django that communicates with OpenAI API via POST
-
First ensure you have python 3 installed. If not, get python here. Preferably Python 3.10.
-
Install virtualenv:
pip install virtualenv
-
Then, Git clone this repo to your PC
git clone https://github.com/mesax1/openai_restapi.git
-
- Cd into your cloned repo:
cd openai_restapi
- Create and activate a virtual environment:
-
virtualenv venv -p python3 .\venv\Scripts\activate
-
virtualenv venv -p python3 source venv/bin/activate
- Install all the required dependencies:
pip install -r requirements.txt
- Run the Django migrations of the project
python manage.py makemigrations python manage.py migrate
- Cd into your cloned repo:
-
Login into your OpenAI account (or create an account if you don't have one yet)
Access your API keys
https://platform.openai.com/account/api-keys
Click on + Create new secret key
Copy the API key temporarily into a .txt file or somewhere where you can copy it later.
Run the following in the cmd prompt, replacing with your OpenAI API key:
setx OPENAI_API_KEY <yourkey>
Open a new cmd prompt, validate that the environment variable has been set by typing
echo %OPENAI_API_KEY%
Run the following in the terminal, replacing with your OpenAI API key:
echo "export OPENAI_API_KEY='yourkey'" >> ~/.zshrc
Update the shell variable
source ~/.zshrc
Open a new terminal, validate that the environment variable has been set by typing
echo $OPENAI_API_KEY
-
Launch the server using:
python manage.py runserver
Access the API service on your browser by using
http://localhost:8000/chat-free/
-
The POST request requires at least 2 inputs: model and prompt.
- model can be either
"gpt-3.5-turbo-0301"
or
"text-davinci-003"
- prompt would be the question that you would like to ask to the AI
"¿Que precio tiene el kellogs?"
*Some additional parameters can be sent in the POST request, that affect the execution of the AI model.
- Example of a POST request with multiple optional parameters
{ "model": "gpt-3.5-turbo-0301", "prompt": "Respondeme cualquier cosa en francés, en 1 párrafo", "temperature": 1.0, "max_tokens": 200, "frequency_penalty": 1, "presence_penalty": 1 }