@jeromeetienne/openai-cache 1.0.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -14
- package/dist/openai_cache.js +8 -6
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,24 +1,47 @@
|
|
|
1
1
|
# Cache OpenAI
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
A simple caching layer for [OpenAI API](https://www.npmjs.com/package/openai), designed to reduce redundant API calls and save time and costs. It works by intercepting API requests and storing their responses in a cache. When the same request is made again, the cached response is returned instead of making a new API call.
|
|
3
|
+
|
|
4
|
+
It is based on the [cacheable](https://cacheable.org/docs/) library, which provides a simple interface for caching data with support for various storage backends (like in-memory, Redis, SQLite, etc). This allows you to easily integrate caching into your OpenAI API usage without having to manage the caching logic yourself.
|
|
5
|
+
|
|
6
|
+
You can use any Keyv storage backend (like Redis, filesystem, etc) to store the cached responses.
|
|
7
|
+
See the [Keyv documentation](https://keyv.org/docs/) for more details on available storage options and how to set them up.
|
|
8
|
+
In the example below, we use a SQLite database to persist the cache.
|
|
9
|
+
|
|
10
|
+
# Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @jeromeetienne/openai-cache
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
If you want to use the SQLite storage backend, you also need to install the `@keyv/sqlite` package:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @keyv/sqlite
|
|
20
|
+
```
|
|
4
21
|
|
|
5
22
|
## Usage
|
|
6
23
|
|
|
7
24
|
```ts
|
|
8
25
|
import OpenAI from "openai";
|
|
9
|
-
import
|
|
10
|
-
|
|
26
|
+
import OpenAICache from "@jeromeetienne/openai-cache";
|
|
27
|
+
import KeyvSqlite from '@keyv/sqlite';
|
|
28
|
+
import { Cacheable } from "cacheable";
|
|
11
29
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
});
|
|
30
|
+
// init a cacheable instance
|
|
31
|
+
// - here it is backed by a sqlite database, but you can use any Keyv storage backend (redis, filesystem, etc)
|
|
32
|
+
const sqlitePath = `sqlite://${__dirname}/.openai_cache.sqlite`;
|
|
33
|
+
const sqliteCache = new Cacheable({ secondary: new KeyvSqlite(sqlitePath) });
|
|
34
|
+
|
|
35
|
+
// init the OpenAICache with the cacheable instance
|
|
36
|
+
const openaiCache = new OpenAICache(sqliteCache);
|
|
16
37
|
|
|
38
|
+
// init the OpenAI client with the cache's fetch function
|
|
17
39
|
const client = new OpenAI({
|
|
18
40
|
apiKey: process.env.OPENAI_API_KEY,
|
|
19
41
|
fetch: openaiCache.getFetchFn(),
|
|
20
42
|
});
|
|
21
43
|
|
|
44
|
+
// now use it normally - responses will be cached in the sqlite database
|
|
22
45
|
const response = await client.responses.create({
|
|
23
46
|
model: "gpt-4.1-mini",
|
|
24
47
|
input: "Say hello in one short sentence.",
|
|
@@ -31,13 +54,27 @@ console.log(response.output_text);
|
|
|
31
54
|
- **PRO**: Reduces redundant API calls, saving time and costs.
|
|
32
55
|
data.
|
|
33
56
|
- **NOTE**: When `temperature === 0`, caching works optimally as responses are deterministic. However, with `temperature > 0`, caching may reduce variety across multiple calls since identical prompts will return cached results instead of generating new varied responses.
|
|
57
|
+
- **NOTE**: Only successful responses (`2xx`) are cached. Error responses (`4xx`/`5xx`) are returned normally but are not persisted.
|
|
34
58
|
|
|
35
59
|
|
|
36
60
|
## Possible improvements
|
|
37
61
|
- dont cache if temporature > 0 or top_p < 1, You’ll freeze randomness if cached
|
|
38
|
-
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
62
|
+
- NOTE: do that on options
|
|
63
|
+
- add configurable cache policy for errors (for example, cache selected deterministic `4xx` while never caching `429`/`5xx`)
|
|
64
|
+
- tools requests errors should not be cached
|
|
65
|
+
|
|
66
|
+
## Developper Notes
|
|
67
|
+
|
|
68
|
+
### Q. how to publish the package to npm?
|
|
69
|
+
A. Do the following steps:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npm run version:patch && npm run publish:all
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Lots of trouble with the 2fa system
|
|
76
|
+
|
|
77
|
+
Revevant Documentation:
|
|
78
|
+
- https://docs.npmjs.com/requiring-2fa-for-package-publishing-and-settings-modification
|
|
79
|
+
- https://docs.npmjs.com/trusted-publishers
|
|
80
|
+
- https://docs.npmjs.com/creating-and-viewing-access-tokens
|
package/dist/openai_cache.js
CHANGED
|
@@ -73,12 +73,14 @@ class OpenAICache {
|
|
|
73
73
|
// Collect headers and normalize them
|
|
74
74
|
const headers = Array.from(clonedResponse.headers.entries());
|
|
75
75
|
const normalizedHeaders = OpenAICache._normalizeHeaders(headers, responseBuffer.length);
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
if (response.ok) {
|
|
77
|
+
await this._cache.set(cacheKey, {
|
|
78
|
+
status: clonedResponse.status,
|
|
79
|
+
headers: normalizedHeaders,
|
|
80
|
+
body: responseBuffer.toString("base64"),
|
|
81
|
+
bodyEncoding: "base64",
|
|
82
|
+
});
|
|
83
|
+
}
|
|
82
84
|
// Return live response (body already buffered)
|
|
83
85
|
return new Response(responseBuffer, { status: response.status, headers: normalizedHeaders });
|
|
84
86
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jeromeetienne/openai-cache",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "dist/openai_cache.js",
|
|
5
5
|
"types": "dist/openai_cache.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"prepublishOnly": "npm run test && npm run build",
|
|
26
26
|
"test": "tsx --test ./test/*_test.ts",
|
|
27
27
|
"test:watch": "tsx --test --watch ./test/*_test.ts",
|
|
28
|
+
"publish:all": "npm run build && npm publish --access public",
|
|
28
29
|
"version:patch": "npm version patch",
|
|
29
30
|
"version:minor": "npm version minor",
|
|
30
31
|
"version:major": "npm version major",
|
|
@@ -46,12 +47,12 @@
|
|
|
46
47
|
},
|
|
47
48
|
"repository": {
|
|
48
49
|
"type": "git",
|
|
49
|
-
"url": "git+https://github.com/jeromeetienne/
|
|
50
|
+
"url": "git+https://github.com/jeromeetienne/openai-cache.git",
|
|
50
51
|
"directory": "packages/openai_cache"
|
|
51
52
|
},
|
|
52
|
-
"homepage": "https://github.com/jeromeetienne/
|
|
53
|
+
"homepage": "https://github.com/jeromeetienne/openai-cache/tree/main/#cache-openai",
|
|
53
54
|
"bugs": {
|
|
54
|
-
"url": "https://github.com/jeromeetienne/
|
|
55
|
+
"url": "https://github.com/jeromeetienne/openai-cache/issues"
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|
|
57
58
|
"@keyv/sqlite": "^4.0.8",
|
|
@@ -66,4 +67,4 @@
|
|
|
66
67
|
"dependencies": {
|
|
67
68
|
"cacheable": "^2.3.3"
|
|
68
69
|
}
|
|
69
|
-
}
|
|
70
|
+
}
|