Skip to content

Django Restful API that communicates with OpenAI API via POST

Notifications You must be signed in to change notification settings

mesax1/openai_restapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

openai_restapi

Django Restful API that communicates with OpenAI API via POST

A Restful API written in Django that communicates with OpenAI API via POST

Overall Installation

  • 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
  • Dependencies installation

    1. Cd into your cloned repo:
          cd openai_restapi
    2. Create and activate a virtual environment:
    • On Windows
          virtualenv  venv -p python3
          .\venv\Scripts\activate
    • On Linux
          virtualenv  venv -p python3
          source venv/bin/activate
    1. Install all the required dependencies:
          pip install -r requirements.txt
    2. Run the Django migrations of the project
          python manage.py makemigrations
          python manage.py migrate
  • Generate and use your own OpenAI API Key

    Login into your OpenAI account (or create an account if you don't have one yet)

    https://platform.openai.com/

    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.

    • On Windows

    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%
    • On Linux

    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 API and execution

  • Launch the server using:

        python manage.py runserver

    Access the API service on your browser by using

        http://localhost:8000/chat-free/
    
  • Make a POST request

    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
    }
    

About

Django Restful API that communicates with OpenAI API via POST

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages