Products:
Redis
Stack:
Flask
Use Case:
Ratelimit, OpenAI
Languages:
Python
Platforms:
Publisher:

Rate Limiting OpenAI Requests

Getting Started

Prerequisites

In order to run this locally, you will need python installed and an Upstash Redis database. If you don't have a database ready, you can learn how to do it here

Quickstart

  1. Clone the repo

    $ git clone https://github.com/upstash/examples.git
    $ cd examples/ratelimiting-python-openai
    
  2. Install the requirements

    $ pip install -r requirements.txt
    
  3. Create the environment variables

    $ export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
    $ export UPSTASH_REDIS_REST_URL=<YOUR_UPSTASH_REDIS_REST_URL>
    $ export UPSTASH_REDIS_REST_TOKEN=<YOUR_UPSTASH_REDIS_REST_TOKEN>
    
  4. Run the Flask server

    $ cd api
    $ flask --app server run
    
  5. Send requests repeatedly and test the rate limit

    $ curl http://127.0.0.1:5000/openai
    

Learn More

To learn more about Upstash and its services, check out the following resources: