{"id":22963663,"url":"https://github.com/half0wl/openai-python-cache","last_synced_at":"2026-05-19T03:07:13.103Z","repository":{"id":142641893,"uuid":"608983649","full_name":"half0wl/openai-python-cache","owner":"half0wl","description":"A thin wrapper around the OpenAI Python bindings for caching responses.","archived":false,"fork":false,"pushed_at":"2023-03-07T14:04:22.000Z","size":27,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-24T10:38:31.581Z","etag":null,"topics":["chatgpt","chatgpt-api","chatgpt3","openai","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/openai-python-cache/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/half0wl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-03T05:59:01.000Z","updated_at":"2025-04-07T12:10:24.000Z","dependencies_parsed_at":"2024-06-28T15:49:40.243Z","dependency_job_id":null,"html_url":"https://github.com/half0wl/openai-python-cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/half0wl/openai-python-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/half0wl%2Fopenai-python-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/half0wl%2Fopenai-python-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/half0wl%2Fopenai-python-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/half0wl%2Fopenai-python-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/half0wl","download_url":"https://codeload.github.com/half0wl/openai-python-cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/half0wl%2Fopenai-python-cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33200159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-18T09:27:30.708Z","status":"online","status_checked_at":"2026-05-19T02:00:06.763Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatgpt","chatgpt-api","chatgpt3","openai","python"],"created_at":"2024-12-14T19:37:27.765Z","updated_at":"2026-05-19T03:07:13.085Z","avatar_url":"https://github.com/half0wl.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🍰 openai-python-cache\n\nA thin wrapper around the OpenAI Python bindings for caching responses.\n\n## Motivation\n\nI'm experimenting with a large-ish dataset locally that gets injected into GPT\nprompts. Responses are not perfect, and occassionally I have to tweak some\nof my data. This means that I'm making API calls for results that are okay,\nbecause it's iterating over the entire dataset.\n\nThis solves the issue by cache-ing OpenAI responses in a local SQLite3 database.\n**Only ChatCompletion is supported** at this time because it's the only API I use.\n\nThis is a quick and dirty solution. I'd go a level lower and inject this\nbehaviour directly in the requestor, but I don't have time to figure that\npart out (yet?)!\n\n## Installation\n\n```sh\n# Using pip:\n$ pip install openai-python-cache\n\n# Using poetry:\n$ poetry add openai-python-cache\n```\n\n## Usage\n\n```python\nimport os\nimport openai\nfrom openai_python_cache.api import ChatCompletion\nfrom openai_python_cache.provider import Sqlite3CacheProvider\n\nopenai.api_key = os.environ.get(\"OPENAI_API_KEY\")\n\n# Create a cache provider\ncache_provider = Sqlite3CacheProvider()\n\n# Use the ChatCompletion class from `openai_python_cache`\ncompletion = ChatCompletion.create(\n    model=\"gpt-3.5-turbo\",\n    messages=[\n        {\n            \"role\": \"user\",\n            \"content\": \"Tell the world about the ChatGPT API in the style of a pirate.\",\n        }\n    ],\n    # Inject the cache provider. Requests will NOT be cached if this is not\n    # provided!\n    cache_provider=cache_provider,\n)\n\nprint(completion)\n```\n\n## Demo\n\n```python\nimport os\nimport time\nimport openai\nfrom openai_python_cache.api import ChatCompletion\nfrom openai_python_cache.provider import Sqlite3CacheProvider\n\nopenai.api_key = os.environ.get(\"OPENAI_API_KEY\")\n\ncache_provider = Sqlite3CacheProvider()\n\nparams = {\n    'model': \"gpt-3.5-turbo\",\n    'messages': [\n        {\n            \"role\": \"user\",\n            \"content\": \"Testing cache!\",\n        }\n    ]\n}\n\n# First request, cache miss. This will result in an API call to OpenAI, and\n# the response will be saved to cache.\nc0start = time.time()\nChatCompletion.create(cache_provider, **params)\nc0end = time.time() - c0start\nprint(f\"First request is a cache miss. It took {c0end} seconds!\")\n# \u003e\u003e\u003e First request is a cache miss. It took 1.7009928226470947 seconds!\n\n# Second request, cache hit. This will NOT result in an API call to OpenAI.\n# The response will be served from cache.\nc1start = time.time()\nChatCompletion.create(cache_provider, **params)\nc1end = time.time() - c1start\nprint(f\"Second request is a cache hit. It took {c1end} seconds!\")\n# \u003e\u003e\u003e Second request is a cache hit. It took 0.00015616416931152344 seconds!\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalf0wl%2Fopenai-python-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhalf0wl%2Fopenai-python-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalf0wl%2Fopenai-python-cache/lists"}