Cheapi (cheap API)
Lightweight wrapper to cache expensive function results in-memory or on disk.
Installation
pip install cheapi
Usage
from cheapi import CachingWrapper
import openai
def _ask_chatgpt(prompt):
res = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant for a software engineer."},
{"role": "user", "content": prompt}
],
temperature=0,
timeout=10,
)
choice, *_ = res["choices"]
usage = res["usage"]["total_tokens"]
print(f"OpenAI usage: {usage}")
result = choice["message"]["content"]
return result
ash_chatgpt = CachingWrapper(_ask_chatgpt, cache_backend="memory")
for i in range(2):
print(ash_chatgpt("Write a short joke about caching?") + "\n")
or even simpler:
from cheapi import cached
@cached(cache_backend="memory")
def ask_chatgpt(prompt):
...
for i in range(2):
print(ask_chatgpt("Write a short joke about caching?") + "\n")
Caching backends
Using memory backend is mostly equivalent to using functools.lru_cache but with more configuration.
SQLite backend is useful for persistent caching, and is similar to joblib.Memory.
Cloud backend is being dreamed of, but not implemented yet.
Why?
Metadata
Release files for cheapi 0.1.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 | |
|---|---|---|---|
| cheapi-0.1.0.tar.gz | 4.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cheapi-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.0 kB
Release files / cheapi-0.1.0.tar.gz
| Download URL | cheapi-0.1.0.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4168452a5211974c164e590b52bed04ab625592955a4798f85a5e79cf216b993
|
|
BLAKE2b-256 checksum How to use checksums |
7c87ce7cb4190c4a2fea5159068b1fe0819d59d4fcad9ec27f7d1e449acd0ceb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.9.11 Darwin/22.2.0
|
Release files / cheapi-0.1.0-py3-none-any.whl
| Download URL | cheapi-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d5e4a8687305d525c18f1369bc655df08db95f444ef767e0611561873b75fca5
|
|
BLAKE2b-256 checksum How to use checksums |
27b2ebc34ce8436e5e5450888aee224beeb20986491f23e860b0ee475f72a7f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.9.11 Darwin/22.2.0
|