langchain 0.0.153 → 0.0.154

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.
@@ -8,6 +8,7 @@ const tiktoken_js_1 = require("../util/tiktoken.cjs");
8
8
  const index_js_2 = require("../schema/runnable/index.cjs");
9
9
  const base_js_1 = require("../prompts/base.cjs");
10
10
  const chat_js_1 = require("../prompts/chat.cjs");
11
+ const index_js_3 = require("../cache/index.cjs");
11
12
  const getVerbosity = () => false;
12
13
  /**
13
14
  * Base class for language models, chains, tools.
@@ -80,12 +81,27 @@ class BaseLanguageModel extends BaseLangChain {
80
81
  writable: true,
81
82
  value: void 0
82
83
  });
84
+ Object.defineProperty(this, "cache", {
85
+ enumerable: true,
86
+ configurable: true,
87
+ writable: true,
88
+ value: void 0
89
+ });
83
90
  Object.defineProperty(this, "_encoding", {
84
91
  enumerable: true,
85
92
  configurable: true,
86
93
  writable: true,
87
94
  value: void 0
88
95
  });
96
+ if (typeof params.cache === "object") {
97
+ this.cache = params.cache;
98
+ }
99
+ else if (params.cache) {
100
+ this.cache = index_js_3.InMemoryCache.global();
101
+ }
102
+ else {
103
+ this.cache = undefined;
104
+ }
89
105
  this.caller = new async_caller_js_1.AsyncCaller(params ?? {});
90
106
  }
91
107
  async getNumTokens(text) {
@@ -124,6 +140,26 @@ class BaseLanguageModel extends BaseLangChain {
124
140
  _identifyingParams() {
125
141
  return {};
126
142
  }
143
+ /**
144
+ * Create a unique cache key for a specific call to a specific language model.
145
+ * @param callOptions Call options for the model
146
+ * @returns A unique cache key.
147
+ */
148
+ _getSerializedCacheKeyParametersForCall(callOptions) {
149
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
150
+ const params = {
151
+ ...this._identifyingParams(),
152
+ ...callOptions,
153
+ _type: this._llmType(),
154
+ _model: this._modelType(),
155
+ };
156
+ const filteredEntries = Object.entries(params).filter(([_, value]) => value !== undefined);
157
+ const serializedEntries = filteredEntries
158
+ .map(([key, value]) => `${key}:${JSON.stringify(value)}`)
159
+ .sort()
160
+ .join(",");
161
+ return serializedEntries;
162
+ }
127
163
  /**
128
164
  * @deprecated
129
165
  * Return a json-like object representing this LLM.
@@ -1,5 +1,5 @@
1
1
  import type { OpenAI as OpenAIClient } from "openai";
2
- import { BaseMessage, BaseMessageLike, BasePromptValue, LLMResult } from "../schema/index.js";
2
+ import { BaseCache, BaseMessage, BaseMessageLike, BasePromptValue, LLMResult } from "../schema/index.js";
3
3
  import { BaseCallbackConfig, CallbackManager, Callbacks } from "../callbacks/manager.js";
4
4
  import { AsyncCaller, AsyncCallerParams } from "../util/async_caller.js";
5
5
  import { Runnable } from "../schema/runnable/index.js";
@@ -40,6 +40,7 @@ export interface BaseLanguageModelParams extends AsyncCallerParams, BaseLangChai
40
40
  * @deprecated Use `callbacks` instead
41
41
  */
42
42
  callbackManager?: CallbackManager;
43
+ cache?: BaseCache | boolean;
43
44
  }
44
45
  export interface BaseLanguageModelCallOptions extends BaseCallbackConfig {
45
46
  /**
@@ -77,6 +78,7 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
77
78
  * which will thus benefit from the concurrency and retry logic.
78
79
  */
79
80
  caller: AsyncCaller;
81
+ cache?: BaseCache;
80
82
  constructor({ callbacks, callbackManager, ...params }: BaseLanguageModelParams);
81
83
  abstract generatePrompt(promptValues: BasePromptValue[], options?: string[] | CallOptions, callbacks?: Callbacks): Promise<LLMResult>;
82
84
  abstract predict(text: string, options?: string[] | CallOptions, callbacks?: Callbacks): Promise<string>;
@@ -90,6 +92,12 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
90
92
  * Get the identifying parameters of the LLM.
91
93
  */
92
94
  _identifyingParams(): Record<string, any>;
95
+ /**
96
+ * Create a unique cache key for a specific call to a specific language model.
97
+ * @param callOptions Call options for the model
98
+ * @returns A unique cache key.
99
+ */
100
+ protected _getSerializedCacheKeyParametersForCall(callOptions: CallOptions): string;
93
101
  /**
94
102
  * @deprecated
95
103
  * Return a json-like object representing this LLM.
@@ -5,6 +5,7 @@ import { encodingForModel } from "../util/tiktoken.js";
5
5
  import { Runnable } from "../schema/runnable/index.js";
6
6
  import { StringPromptValue } from "../prompts/base.js";
7
7
  import { ChatPromptValue } from "../prompts/chat.js";
8
+ import { InMemoryCache } from "../cache/index.js";
8
9
  const getVerbosity = () => false;
9
10
  /**
10
11
  * Base class for language models, chains, tools.
@@ -76,12 +77,27 @@ export class BaseLanguageModel extends BaseLangChain {
76
77
  writable: true,
77
78
  value: void 0
78
79
  });
80
+ Object.defineProperty(this, "cache", {
81
+ enumerable: true,
82
+ configurable: true,
83
+ writable: true,
84
+ value: void 0
85
+ });
79
86
  Object.defineProperty(this, "_encoding", {
80
87
  enumerable: true,
81
88
  configurable: true,
82
89
  writable: true,
83
90
  value: void 0
84
91
  });
92
+ if (typeof params.cache === "object") {
93
+ this.cache = params.cache;
94
+ }
95
+ else if (params.cache) {
96
+ this.cache = InMemoryCache.global();
97
+ }
98
+ else {
99
+ this.cache = undefined;
100
+ }
85
101
  this.caller = new AsyncCaller(params ?? {});
86
102
  }
87
103
  async getNumTokens(text) {
@@ -120,6 +136,26 @@ export class BaseLanguageModel extends BaseLangChain {
120
136
  _identifyingParams() {
121
137
  return {};
122
138
  }
139
+ /**
140
+ * Create a unique cache key for a specific call to a specific language model.
141
+ * @param callOptions Call options for the model
142
+ * @returns A unique cache key.
143
+ */
144
+ _getSerializedCacheKeyParametersForCall(callOptions) {
145
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
146
+ const params = {
147
+ ...this._identifyingParams(),
148
+ ...callOptions,
149
+ _type: this._llmType(),
150
+ _model: this._modelType(),
151
+ };
152
+ const filteredEntries = Object.entries(params).filter(([_, value]) => value !== undefined);
153
+ const serializedEntries = filteredEntries
154
+ .map(([key, value]) => `${key}:${JSON.stringify(value)}`)
155
+ .sort()
156
+ .join(",");
157
+ return serializedEntries;
158
+ }
123
159
  /**
124
160
  * @deprecated
125
161
  * Return a json-like object representing this LLM.
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getCacheKey = void 0;
6
+ exports.serializeGeneration = exports.deserializeStoredGeneration = exports.getCacheKey = void 0;
7
7
  const object_hash_1 = __importDefault(require("object-hash"));
8
+ const index_js_1 = require("../schema/index.cjs");
8
9
  /**
9
10
  * This cache key should be consistent across all versions of langchain.
10
11
  * It is currently NOT consistent across versions of langchain.
@@ -17,3 +18,25 @@ const object_hash_1 = __importDefault(require("object-hash"));
17
18
  */
18
19
  const getCacheKey = (...strings) => (0, object_hash_1.default)(strings.join("_"));
19
20
  exports.getCacheKey = getCacheKey;
21
+ function deserializeStoredGeneration(storedGeneration) {
22
+ if (storedGeneration.message !== undefined) {
23
+ return {
24
+ text: storedGeneration.text,
25
+ message: (0, index_js_1.mapStoredMessageToChatMessage)(storedGeneration.message),
26
+ };
27
+ }
28
+ else {
29
+ return { text: storedGeneration.text };
30
+ }
31
+ }
32
+ exports.deserializeStoredGeneration = deserializeStoredGeneration;
33
+ function serializeGeneration(generation) {
34
+ const serializedValue = {
35
+ text: generation.text,
36
+ };
37
+ if (generation.message !== undefined) {
38
+ serializedValue.message = generation.message.toDict();
39
+ }
40
+ return serializedValue;
41
+ }
42
+ exports.serializeGeneration = serializeGeneration;
@@ -1,3 +1,4 @@
1
+ import { Generation, StoredGeneration } from "../schema/index.js";
1
2
  /**
2
3
  * This cache key should be consistent across all versions of langchain.
3
4
  * It is currently NOT consistent across versions of langchain.
@@ -9,3 +10,11 @@
9
10
  * TODO: Make cache key consistent across versions of langchain.
10
11
  */
11
12
  export declare const getCacheKey: (...strings: string[]) => string;
13
+ export declare function deserializeStoredGeneration(storedGeneration: StoredGeneration): {
14
+ text: string;
15
+ message: import("../schema/index.js").HumanMessage | import("../schema/index.js").AIMessage | import("../schema/index.js").SystemMessage | import("../schema/index.js").FunctionMessage | import("../schema/index.js").ChatMessage;
16
+ } | {
17
+ text: string;
18
+ message?: undefined;
19
+ };
20
+ export declare function serializeGeneration(generation: Generation): StoredGeneration;
@@ -1,4 +1,5 @@
1
1
  import hash from "object-hash";
2
+ import { mapStoredMessageToChatMessage, } from "../schema/index.js";
2
3
  /**
3
4
  * This cache key should be consistent across all versions of langchain.
4
5
  * It is currently NOT consistent across versions of langchain.
@@ -10,3 +11,23 @@ import hash from "object-hash";
10
11
  * TODO: Make cache key consistent across versions of langchain.
11
12
  */
12
13
  export const getCacheKey = (...strings) => hash(strings.join("_"));
14
+ export function deserializeStoredGeneration(storedGeneration) {
15
+ if (storedGeneration.message !== undefined) {
16
+ return {
17
+ text: storedGeneration.text,
18
+ message: mapStoredMessageToChatMessage(storedGeneration.message),
19
+ };
20
+ }
21
+ else {
22
+ return { text: storedGeneration.text };
23
+ }
24
+ }
25
+ export function serializeGeneration(generation) {
26
+ const serializedValue = {
27
+ text: generation.text,
28
+ };
29
+ if (generation.message !== undefined) {
30
+ serializedValue.message = generation.message.toDict();
31
+ }
32
+ return serializedValue;
33
+ }
@@ -33,10 +33,7 @@ class CloudflareKVCache extends index_js_1.BaseCache {
33
33
  let value = await this.binding.get(key);
34
34
  const generations = [];
35
35
  while (value) {
36
- if (!value) {
37
- break;
38
- }
39
- generations.push({ text: value });
36
+ generations.push((0, base_js_1.deserializeStoredGeneration)(JSON.parse(value)));
40
37
  idx += 1;
41
38
  key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(idx));
42
39
  value = await this.binding.get(key);
@@ -54,7 +51,7 @@ class CloudflareKVCache extends index_js_1.BaseCache {
54
51
  async update(prompt, llmKey, value) {
55
52
  for (let i = 0; i < value.length; i += 1) {
56
53
  const key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(i));
57
- await this.binding.put(key, value[i].text);
54
+ await this.binding.put(key, JSON.stringify((0, base_js_1.serializeGeneration)(value[i])));
58
55
  }
59
56
  }
60
57
  }
@@ -1,5 +1,5 @@
1
1
  import { BaseCache } from "../schema/index.js";
2
- import { getCacheKey } from "./base.js";
2
+ import { getCacheKey, serializeGeneration, deserializeStoredGeneration, } from "./base.js";
3
3
  /**
4
4
  * Represents a specific implementation of a caching mechanism using Cloudflare KV
5
5
  * as the underlying storage system. It extends the `BaseCache` class and
@@ -30,10 +30,7 @@ export class CloudflareKVCache extends BaseCache {
30
30
  let value = await this.binding.get(key);
31
31
  const generations = [];
32
32
  while (value) {
33
- if (!value) {
34
- break;
35
- }
36
- generations.push({ text: value });
33
+ generations.push(deserializeStoredGeneration(JSON.parse(value)));
37
34
  idx += 1;
38
35
  key = getCacheKey(prompt, llmKey, String(idx));
39
36
  value = await this.binding.get(key);
@@ -51,7 +48,7 @@ export class CloudflareKVCache extends BaseCache {
51
48
  async update(prompt, llmKey, value) {
52
49
  for (let i = 0; i < value.length; i += 1) {
53
50
  const key = getCacheKey(prompt, llmKey, String(i));
54
- await this.binding.put(key, value[i].text);
51
+ await this.binding.put(key, JSON.stringify(serializeGeneration(value[i])));
55
52
  }
56
53
  }
57
54
  }
@@ -7,7 +7,7 @@ const base_js_1 = require("./base.cjs");
7
7
  * Cache LLM results using Redis.
8
8
  */
9
9
  class RedisCache extends index_js_1.BaseCache {
10
- constructor(redisClient) {
10
+ constructor(redisClient, config) {
11
11
  super();
12
12
  Object.defineProperty(this, "redisClient", {
13
13
  enumerable: true,
@@ -15,7 +15,14 @@ class RedisCache extends index_js_1.BaseCache {
15
15
  writable: true,
16
16
  value: void 0
17
17
  });
18
+ Object.defineProperty(this, "ttl", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
18
24
  this.redisClient = redisClient;
25
+ this.ttl = config?.ttl;
19
26
  }
20
27
  /**
21
28
  * Retrieves data from the Redis server using a prompt and an LLM key. If
@@ -30,10 +37,8 @@ class RedisCache extends index_js_1.BaseCache {
30
37
  let value = await this.redisClient.get(key);
31
38
  const generations = [];
32
39
  while (value) {
33
- if (!value) {
34
- break;
35
- }
36
- generations.push({ text: value });
40
+ const storedGeneration = JSON.parse(value);
41
+ generations.push((0, base_js_1.deserializeStoredGeneration)(storedGeneration));
37
42
  idx += 1;
38
43
  key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(idx));
39
44
  value = await this.redisClient.get(key);
@@ -49,7 +54,12 @@ class RedisCache extends index_js_1.BaseCache {
49
54
  async update(prompt, llmKey, value) {
50
55
  for (let i = 0; i < value.length; i += 1) {
51
56
  const key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(i));
52
- await this.redisClient.set(key, value[i].text);
57
+ if (this.ttl !== undefined) {
58
+ await this.redisClient.set(key, JSON.stringify((0, base_js_1.serializeGeneration)(value[i])), "EX", this.ttl);
59
+ }
60
+ else {
61
+ await this.redisClient.set(key, JSON.stringify((0, base_js_1.serializeGeneration)(value[i])));
62
+ }
53
63
  }
54
64
  }
55
65
  }
@@ -4,8 +4,11 @@ import { BaseCache, Generation } from "../schema/index.js";
4
4
  * Cache LLM results using Redis.
5
5
  */
6
6
  export declare class RedisCache extends BaseCache {
7
- private redisClient;
8
- constructor(redisClient: Redis);
7
+ protected redisClient: Redis;
8
+ protected ttl?: number;
9
+ constructor(redisClient: Redis, config?: {
10
+ ttl?: number;
11
+ });
9
12
  /**
10
13
  * Retrieves data from the Redis server using a prompt and an LLM key. If
11
14
  * the data is not found, it returns null.
@@ -1,10 +1,10 @@
1
1
  import { BaseCache } from "../schema/index.js";
2
- import { getCacheKey } from "./base.js";
2
+ import { serializeGeneration, deserializeStoredGeneration, getCacheKey, } from "./base.js";
3
3
  /**
4
4
  * Cache LLM results using Redis.
5
5
  */
6
6
  export class RedisCache extends BaseCache {
7
- constructor(redisClient) {
7
+ constructor(redisClient, config) {
8
8
  super();
9
9
  Object.defineProperty(this, "redisClient", {
10
10
  enumerable: true,
@@ -12,7 +12,14 @@ export class RedisCache extends BaseCache {
12
12
  writable: true,
13
13
  value: void 0
14
14
  });
15
+ Object.defineProperty(this, "ttl", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
15
21
  this.redisClient = redisClient;
22
+ this.ttl = config?.ttl;
16
23
  }
17
24
  /**
18
25
  * Retrieves data from the Redis server using a prompt and an LLM key. If
@@ -27,10 +34,8 @@ export class RedisCache extends BaseCache {
27
34
  let value = await this.redisClient.get(key);
28
35
  const generations = [];
29
36
  while (value) {
30
- if (!value) {
31
- break;
32
- }
33
- generations.push({ text: value });
37
+ const storedGeneration = JSON.parse(value);
38
+ generations.push(deserializeStoredGeneration(storedGeneration));
34
39
  idx += 1;
35
40
  key = getCacheKey(prompt, llmKey, String(idx));
36
41
  value = await this.redisClient.get(key);
@@ -46,7 +51,12 @@ export class RedisCache extends BaseCache {
46
51
  async update(prompt, llmKey, value) {
47
52
  for (let i = 0; i < value.length; i += 1) {
48
53
  const key = getCacheKey(prompt, llmKey, String(i));
49
- await this.redisClient.set(key, value[i].text);
54
+ if (this.ttl !== undefined) {
55
+ await this.redisClient.set(key, JSON.stringify(serializeGeneration(value[i])), "EX", this.ttl);
56
+ }
57
+ else {
58
+ await this.redisClient.set(key, JSON.stringify(serializeGeneration(value[i])));
59
+ }
50
60
  }
51
61
  }
52
62
  }
@@ -76,7 +76,11 @@ class MomentoCache extends index_js_1.BaseCache {
76
76
  const getResponse = await this.client.get(this.cacheName, key);
77
77
  if (getResponse instanceof sdk_1.CacheGet.Hit) {
78
78
  const value = getResponse.valueString();
79
- return JSON.parse(value);
79
+ const parsedValue = JSON.parse(value);
80
+ if (!Array.isArray(parsedValue)) {
81
+ return null;
82
+ }
83
+ return JSON.parse(value).map(base_js_1.deserializeStoredGeneration);
80
84
  }
81
85
  else if (getResponse instanceof sdk_1.CacheGet.Miss) {
82
86
  return null;
@@ -99,7 +103,7 @@ class MomentoCache extends index_js_1.BaseCache {
99
103
  */
100
104
  async update(prompt, llmKey, value) {
101
105
  const key = (0, base_js_1.getCacheKey)(prompt, llmKey);
102
- const setResponse = await this.client.set(this.cacheName, key, JSON.stringify(value), { ttl: this.ttlSeconds });
106
+ const setResponse = await this.client.set(this.cacheName, key, JSON.stringify(value.map(base_js_1.serializeGeneration)), { ttl: this.ttlSeconds });
103
107
  if (setResponse instanceof sdk_1.CacheSet.Success) {
104
108
  // pass
105
109
  }
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable no-instanceof/no-instanceof */
2
2
  import { CacheGet, CacheSet, InvalidArgumentError, } from "@gomomento/sdk";
3
3
  import { BaseCache } from "../schema/index.js";
4
- import { getCacheKey } from "./base.js";
4
+ import { deserializeStoredGeneration, getCacheKey, serializeGeneration, } from "./base.js";
5
5
  import { ensureCacheExists } from "../util/momento.js";
6
6
  /**
7
7
  * A cache that uses Momento as the backing store.
@@ -73,7 +73,11 @@ export class MomentoCache extends BaseCache {
73
73
  const getResponse = await this.client.get(this.cacheName, key);
74
74
  if (getResponse instanceof CacheGet.Hit) {
75
75
  const value = getResponse.valueString();
76
- return JSON.parse(value);
76
+ const parsedValue = JSON.parse(value);
77
+ if (!Array.isArray(parsedValue)) {
78
+ return null;
79
+ }
80
+ return JSON.parse(value).map(deserializeStoredGeneration);
77
81
  }
78
82
  else if (getResponse instanceof CacheGet.Miss) {
79
83
  return null;
@@ -96,7 +100,7 @@ export class MomentoCache extends BaseCache {
96
100
  */
97
101
  async update(prompt, llmKey, value) {
98
102
  const key = getCacheKey(prompt, llmKey);
99
- const setResponse = await this.client.set(this.cacheName, key, JSON.stringify(value), { ttl: this.ttlSeconds });
103
+ const setResponse = await this.client.set(this.cacheName, key, JSON.stringify(value.map(serializeGeneration)), { ttl: this.ttlSeconds });
100
104
  if (setResponse instanceof CacheSet.Success) {
101
105
  // pass
102
106
  }
@@ -33,10 +33,8 @@ class RedisCache extends index_js_1.BaseCache {
33
33
  let value = await this.redisClient.get(key);
34
34
  const generations = [];
35
35
  while (value) {
36
- if (!value) {
37
- break;
38
- }
39
- generations.push({ text: value });
36
+ const storedGeneration = JSON.parse(value);
37
+ generations.push((0, base_js_1.deserializeStoredGeneration)(storedGeneration));
40
38
  idx += 1;
41
39
  key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(idx));
42
40
  value = await this.redisClient.get(key);
@@ -54,7 +52,7 @@ class RedisCache extends index_js_1.BaseCache {
54
52
  async update(prompt, llmKey, value) {
55
53
  for (let i = 0; i < value.length; i += 1) {
56
54
  const key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(i));
57
- await this.redisClient.set(key, value[i].text);
55
+ await this.redisClient.set(key, JSON.stringify((0, base_js_1.serializeGeneration)(value[i])));
58
56
  }
59
57
  }
60
58
  }
@@ -1,5 +1,5 @@
1
1
  import { BaseCache } from "../schema/index.js";
2
- import { getCacheKey } from "./base.js";
2
+ import { deserializeStoredGeneration, getCacheKey, serializeGeneration, } from "./base.js";
3
3
  /**
4
4
  * Represents a specific implementation of a caching mechanism using Redis
5
5
  * as the underlying storage system. It extends the `BaseCache` class and
@@ -30,10 +30,8 @@ export class RedisCache extends BaseCache {
30
30
  let value = await this.redisClient.get(key);
31
31
  const generations = [];
32
32
  while (value) {
33
- if (!value) {
34
- break;
35
- }
36
- generations.push({ text: value });
33
+ const storedGeneration = JSON.parse(value);
34
+ generations.push(deserializeStoredGeneration(storedGeneration));
37
35
  idx += 1;
38
36
  key = getCacheKey(prompt, llmKey, String(idx));
39
37
  value = await this.redisClient.get(key);
@@ -51,7 +49,7 @@ export class RedisCache extends BaseCache {
51
49
  async update(prompt, llmKey, value) {
52
50
  for (let i = 0; i < value.length; i += 1) {
53
51
  const key = getCacheKey(prompt, llmKey, String(i));
54
- await this.redisClient.set(key, value[i].text);
52
+ await this.redisClient.set(key, JSON.stringify(serializeGeneration(value[i])));
55
53
  }
56
54
  }
57
55
  }
@@ -37,10 +37,7 @@ class UpstashRedisCache extends index_js_1.BaseCache {
37
37
  let value = await this.redisClient.get(key);
38
38
  const generations = [];
39
39
  while (value) {
40
- if (!value) {
41
- break;
42
- }
43
- generations.push({ text: value });
40
+ generations.push((0, base_js_1.deserializeStoredGeneration)(JSON.parse(value)));
44
41
  idx += 1;
45
42
  key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(idx));
46
43
  value = await this.redisClient.get(key);
@@ -55,7 +52,7 @@ class UpstashRedisCache extends index_js_1.BaseCache {
55
52
  async update(prompt, llmKey, value) {
56
53
  for (let i = 0; i < value.length; i += 1) {
57
54
  const key = (0, base_js_1.getCacheKey)(prompt, llmKey, String(i));
58
- await this.redisClient.set(key, value[i].text);
55
+ await this.redisClient.set(key, JSON.stringify((0, base_js_1.serializeGeneration)(value[i])));
59
56
  }
60
57
  }
61
58
  }
@@ -1,6 +1,6 @@
1
1
  import { Redis } from "@upstash/redis";
2
2
  import { BaseCache } from "../schema/index.js";
3
- import { getCacheKey } from "./base.js";
3
+ import { deserializeStoredGeneration, getCacheKey, serializeGeneration, } from "./base.js";
4
4
  /**
5
5
  * A cache that uses Upstash as the backing store.
6
6
  * See https://docs.upstash.com/redis.
@@ -34,10 +34,7 @@ export class UpstashRedisCache extends BaseCache {
34
34
  let value = await this.redisClient.get(key);
35
35
  const generations = [];
36
36
  while (value) {
37
- if (!value) {
38
- break;
39
- }
40
- generations.push({ text: value });
37
+ generations.push(deserializeStoredGeneration(JSON.parse(value)));
41
38
  idx += 1;
42
39
  key = getCacheKey(prompt, llmKey, String(idx));
43
40
  value = await this.redisClient.get(key);
@@ -52,7 +49,7 @@ export class UpstashRedisCache extends BaseCache {
52
49
  async update(prompt, llmKey, value) {
53
50
  for (let i = 0; i < value.length; i += 1) {
54
51
  const key = getCacheKey(prompt, llmKey, String(i));
55
- await this.redisClient.set(key, value[i].text);
52
+ await this.redisClient.set(key, JSON.stringify(serializeGeneration(value[i])));
56
53
  }
57
54
  }
58
55
  }