🍰 openai-python-cache
A thin wrapper around the OpenAI Python bindings for caching responses.
Motivation
I'm experimenting with a large-ish dataset locally that gets injected into GPT prompts. Responses are not perfect, and occassionally I have to tweak some of my data. This means that I'm making API calls for results that are okay, because it's iterating over the entire dataset.
This solves the issue by cache-ing OpenAI responses in a local SQLite3 database. Only ChatCompletion is supported at this time because it's the only API I use.
This is a quick and dirty solution. I'd go a level lower and inject this behaviour directly in the requestor, but I don't have time to figure that part out (yet?)!
Installation
# Using pip:
$ pip install openai-python-cache
# Using poetry:
$ poetry add openai-python-cache
Usage
import os
import openai
from openai_python_cache.api import ChatCompletion
from openai_python_cache.provider import Sqlite3CacheProvider
openai.api_key = os.environ.get("OPENAI_API_KEY")
# Create a cache provider
cache_provider = Sqlite3CacheProvider()
# Use the ChatCompletion class from `openai_python_cache`
completion = ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "Tell the world about the ChatGPT API in the style of a pirate.",
}
],
# Inject the cache provider. Requests will NOT be cached if this is not
# provided!
cache_provider=cache_provider,
)
print(completion)
Demo
import os
import time
import openai
from openai_python_cache.api import ChatCompletion
from openai_python_cache.provider import Sqlite3CacheProvider
openai.api_key = os.environ.get("OPENAI_API_KEY")
cache_provider = Sqlite3CacheProvider()
params = {
'model': "gpt-3.5-turbo",
'messages': [
{
"role": "user",
"content": "Testing cache!",
}
]
}
# First request, cache miss. This will result in an API call to OpenAI, and
# the response will be saved to cache.
c0start = time.time()
ChatCompletion.create(cache_provider, **params)
c0end = time.time() - c0start
print(f"First request is a cache miss. It took {c0end} seconds!")
# >>> First request is a cache miss. It took 1.7009928226470947 seconds!
# Second request, cache hit. This will NOT result in an API call to OpenAI.
# The response will be served from cache.
c1start = time.time()
ChatCompletion.create(cache_provider, **params)
c1end = time.time() - c1start
print(f"Second request is a cache hit. It took {c1end} seconds!")
# >>> Second request is a cache hit. It took 0.00015616416931152344 seconds!
Metadata
Release files for openai-python-cache 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| openai_python_cache-0.5.0.tar.gz | 4.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| openai_python_cache-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.1 kB
Release files / openai_python_cache-0.5.0.tar.gz
| Download URL | openai_python_cache-0.5.0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8452705777a58364b80e57d4bf44e9158096d8982c3a55ddc9d89589e9fa859f
|
|
BLAKE2b-256 checksum How to use checksums |
fc660b289f24705a267763d82b7b19e54d91b9c397261fdcd9da193cd81bfaa6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.11.1 Darwin/22.3.0
|
Release files / openai_python_cache-0.5.0-py3-none-any.whl
| Download URL | openai_python_cache-0.5.0-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4763b9983e617809f59f24eff4e0ee6b6488f5d9c1c6953476619035805db7ea
|
|
BLAKE2b-256 checksum How to use checksums |
c8a829aece884db2fa749136910a41d076f03c84dfdde2f4a9422b2fbccf5cb3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.11.1 Darwin/22.3.0
|
Security for openai-python-cache 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.