@jeromeetienne/openai-cache 1.0.2 → 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 CHANGED
@@ -7,11 +7,23 @@ You can use any Keyv storage backend (like Redis, filesystem, etc) to store the
7
7
  See the [Keyv documentation](https://keyv.org/docs/) for more details on available storage options and how to set them up.
8
8
  In the example below, we use a SQLite database to persist the cache.
9
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
+ ```
21
+
10
22
  ## Usage
11
23
 
12
24
  ```ts
13
25
  import OpenAI from "openai";
14
- import { OpenAICache } from "@jeromeetienne/openai-cache";
26
+ import OpenAICache from "@jeromeetienne/openai-cache";
15
27
  import KeyvSqlite from '@keyv/sqlite';
16
28
  import { Cacheable } from "cacheable";
17
29
 
@@ -42,14 +54,14 @@ console.log(response.output_text);
42
54
  - **PRO**: Reduces redundant API calls, saving time and costs.
43
55
  data.
44
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.
45
58
 
46
59
 
47
60
  ## Possible improvements
48
61
  - dont cache if temporature > 0 or top_p < 1, You’ll freeze randomness if cached
49
62
  - NOTE: do that on options
50
- - check errors, and dont cache if error
51
- - 429/500 errors should not be cached, but other errors (like invalid request) could be cached to prevent repeated bad requests
52
- - tools requests errors should not be cached
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
53
65
 
54
66
  ## Developper Notes
55
67
 
@@ -60,4 +72,9 @@ A. Do the following steps:
60
72
  npm run version:patch && npm run publish:all
61
73
  ```
62
74
 
63
- Lots of trouble with the 2fa system
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
@@ -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
- await this._cache.set(cacheKey, {
77
- status: clonedResponse.status,
78
- headers: normalizedHeaders,
79
- body: responseBuffer.toString("base64"),
80
- bodyEncoding: "base64",
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.2",
3
+ "version": "1.0.3",
4
4
  "main": "dist/openai_cache.js",
5
5
  "types": "dist/openai_cache.d.ts",
6
6
  "exports": {