Your Simple Guide to Installing and Using ChatGPT Locally with Python

Your Simple Guide to Installing and Using ChatGPT Locally with Python

Mar 1, 2024
article
artificial intelligence, chatgpt, python

ChatGPT is a state-of-the-art language model developed by OpenAI. It serves many purposes in natural language processing, including language translation, chatbots, and story writing, among others.

This guide describes how to set up ChatGPT locally and utilize it through the OpenAI API service on macOS operating systems. Additionally, it provides instructions for locally integrating ChatGPT with Python.


Use ChatGPT Locally

To set up ChatGPT locally using the terminal, follow these instructions:

  1. Set up an API Key Sign up to OpenAI and follow the indications to create a new secret key. All your API requests must include this API Key in the Authorization HTTP header of the request.
Important:
Keep in a safe location your new secret key. For security reasons, you won’t be able to retrieve it again; thus, you’ll need to generate a new one.
  1. Open terminal

  2. Start making some requests using the OpenAI API client and your API Key. For further information, refer to Making Requests.

    To request all available models, use the following request:

    curl https://api.openai.com/v1/models \
    -H "Authorization: Bearer <YOUR_API_KEY>"
    

    To retrieve information about the text-davinci-003 model, use the following request:

    curl https://api.openai.com/v1/models/text-davinci-003 \
    -H "Authorization: Bearer <YOUR_API_KEY>"
    

    To make a completion, use the following request:

    curl https://api.openai.com/v1/completions \   
    -H "Content-Type: application/json" \   
    -H "Authorization: Bearer <YOUR_API_KEY>" \   
    -d '{
        "model": "text-davinci-003",
       "prompt": "Say this is a test",
       "max_tokens": 7,
       "temperature": 0
     }'
    

Use ChatGPT with Python Locally

This section describes how to set up ChatGPT and use it in your Python scripts.

Prerequisites

Ensure you comply with the following requirements before you continue:

  • Set up an API Key
  • Install Python version 2.7 or later To check your current Python version, type the following command in the terminal python3-version

Set up ChatGPT with Python

To integrate ChatGPT with your Python scripts:

  1. Open terminal

  2. Create a new empty project folder:

    mkdir chatgpt-python
    cd chatgpt-python
    
  3. Install the OpenAI Python library:

    pip3 install openai
    
  4. Create a new file chat.pyin the project folder and edit its content:

    touch chat.py 
    nano chat.py
    
  5. Create a completion request sample using the following template:

    import openai
    
    # Set up OpenAI API client
    openai.api_key = "<YOUR_API_KEY>"
    
    # Set up OpenAI model and prompt
    model = "text-davinci-003"
    prompt = "Hello, how are you today?"
    
    # Generate a request
    completion = openai.Completion.create(
        engine=model,
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0,
    )
    
    response = completion.choices[0].text
    print(response)
    
  6. Save the chat.pyfile.

  7. Make the completion request by running the chat.pyscript.

    python3 chat.py
    

    You get the following sample response:

    I am doing well, thank you. How about you?
    

    Refer to Making Requests for additional information.


Trusting this comes in handy for you!