Skip to main content

OpenAI-Manager

Speed up your OpenAI requests by balancing prompts to multiple API keys. Quite useful if you are playing with code-davinci-002 endpoint.

If you seldomly trigger rate limit errors, it is unnecessary to use this package.

Disclaimer

Before using this tool, you are required to read the EULA and ToS of OpenAI L.P. carefully. Actions that violate the OpenAI user agreement may result in the API Key and associated account being suspended. The author shall not be held liable for any consequential damages.

Design

TL;DR: this package helps you manage rate limit (both request-level and token-level) for each api_key for maximum number of requests to OpenAI API.

This is extremely helpful if you use CODEX endpoint or you have a handful of free-trial accounts due to limited budget. Free-trial accounts apply strict rate limit.

Quickstart

  1. Install openai-manager on PyPI.

    pip install openai-manager
    
  2. Prepare your OpenAI credentials in

    1. Environmental Varibles: any envvars beginning with OPENAI_API_KEY will be used to initialized the manager. Best practice to load your api keys is to prepare a .env file like:
    OPENAI_API_KEY_1=sk-Nxo******
    OPENAI_API_KEY_2=sk-TG2******
    OPENAI_API_KEY_3=sk-Kpt******
    # You can set a global proxy for all api_keys
    OPENAI_API_PROXY=http://127.0.0.1:7890
    # You can also append proxy to each api_key. 
    # Make sure the indices match.
    OPENAI_API_PROXY_1=http://127.0.0.1:7890
    OPENAI_API_PROXY_2=http://127.0.0.1:7890
    OPENAI_API_PROXY_3=http://127.0.0.1:7890
    

    Then load your environmental varibles before running any scripts:

    export $(grep -v '^#' .env | xargs)
    
    1. YAML config file: you can add more fine-grained restrictions on each API key if you know the ratelimit for each key in advance. (WIP)
  3. Run this minimal running example to see how to boost your OpenAI completions. (more interfaces coming!)

    import openai as official_openai
    import openai_manager
    
    @timeit
    def test_official_separate():
        for i in range(10):
            prompt = "Once upon a time, "
            response = official_openai.Completion.create(
                model="code-davinci-002",
                prompt=prompt,
                max_tokens=20,
            )
            print("Answer {}: {}".format(i, response["choices"][0]["text"]))
    
    @timeit
    def test_manager():
        prompt = "Once upon a time, "
        prompts = [prompt] * 10
        responses = openai_manager.Completion.create(
            model="code-davinci-002",
            prompt=prompts,
            max_tokens=20,
        )
        assert len(responses) == 10
        for i, response in enumerate(responses):
            print("Answer {}: {}".format(i, response["choices"][0]["text"]))
    

Performance Assessment

WIP

Frequently Asked Questions

  1. Q: Why don't we just use official batching function?

     prompt = "Once upon a time, "
     prompts = [prompt] * 10
     response = openai.Completion.create(
         model="code-davinci-002",
         prompt=prompts,  # official batching allows multiple prompts in one request
         max_tokens=20,
     )
     assert len(response["choices"]) == 10
     for i, answer in enumerate(response["choices"]):
         print("Answer {}: {}".format(i, answer["text"]))
    

    A: code-davinci-002 or other similar OpenAI endpoints apply strict token-level rate limit, even if you upgrade to pay-as-you-go user. Simple batching would not solve this.

Acknowledgement

openai-cookbook

openai-python

TODO

  • Support all functions in OpenAI Python API.
    • Completions
    • Embeddings
    • Generations
    • ChatCompletions
  • Better back-off strategy for maximum throughput.
  • Properly handling exceptions raised by OpenAI API.
  • Automatic rotation of tons of OpenAI API Keys. (Removing invaild, adding new, etc.)

Donation

If this package helps your research, consider making a donation via GitHub!

Release files for openai-manager 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-manager 0.0.1
File Size Uploaded
openai-manager-0.0.1.tar.gz 13.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-manager 0.0.1
File Interpreter ABI Platform
openai_manager-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 42.4 kB

Release files / openai-manager-0.0.1.tar.gz

Download URL openai-manager-0.0.1.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
76083ad9c97f336435f9a12a83ea74c23bbea8262bc51be800c6dcbc637bd1b7
BLAKE2b-256 checksum
How to use checksums
47a7bedda988024c18a26076a27827107dfedb659b6620dcd79d632838bc4455
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / openai_manager-0.0.1-py3-none-any.whl

Download URL openai_manager-0.0.1-py3-none-any.whl
Size 28.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b790240c6b599cd4fcf5fbfb1b209a8b35aef9af3f8b43f77af2ebb2bc84b77c
BLAKE2b-256 checksum
How to use checksums
10c6d1a10c19e7fd9ddaabe0a25a931b8609514d66b0f4287f128b7ee9e69daa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

1.0.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page