-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathcaches.ts
87 lines (79 loc) · 2.7 KB
/
caches.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import type { createCluster, createClient } from "redis";
import {
BaseCache,
deserializeStoredGeneration,
getCacheKey,
serializeGeneration,
} from "@langchain/core/caches";
import { Generation } from "@langchain/core/outputs";
/**
* Represents the type of the Redis client used to interact with the Redis
* database.
*/
type RedisClientType =
| ReturnType<typeof createClient>
| ReturnType<typeof createCluster>;
/**
* @deprecated Import from "@langchain/community/caches/ioredis" instead.
* Represents a specific implementation of a caching mechanism using Redis
* as the underlying storage system. It extends the `BaseCache` class and
* overrides its methods to provide the Redis-specific logic.
*
* @example
* ```typescript
* const model = new ChatOpenAI({
* cache: new RedisCache(new Redis(), { ttl: 60 }),
* });
*
* // Invoke the model to perform an action
* const response = await model.invoke("Do something random!");
* console.log(response);
* ```
*/
export class RedisCache extends BaseCache {
private redisClient: RedisClientType;
constructor(redisClient: RedisClientType) {
super();
this.redisClient = redisClient;
}
/**
* Retrieves data from the cache. It constructs a cache key from the given
* `prompt` and `llmKey`, and retrieves the corresponding value from the
* Redis database.
*
* @param prompt The prompt used to construct the cache key.
* @param llmKey The LLM key used to construct the cache key.
* @returns An array of Generations if found, null otherwise.
*/
public async lookup(prompt: string, llmKey: string) {
let idx = 0;
let key = getCacheKey(prompt, llmKey, String(idx));
let value = await this.redisClient.get(key);
const generations: Generation[] = [];
while (value) {
const storedGeneration = JSON.parse(value);
generations.push(deserializeStoredGeneration(storedGeneration));
idx += 1;
key = getCacheKey(prompt, llmKey, String(idx));
value = await this.redisClient.get(key);
}
return generations.length > 0 ? generations : null;
}
/**
* Updates the cache with new data. It constructs a cache key from the
* given `prompt` and `llmKey`, and stores the `value` in the Redis
* database.
* @param prompt The prompt used to construct the cache key.
* @param llmKey The LLM key used to construct the cache key.
* @param value The value to be stored in the cache.
*/
public async update(prompt: string, llmKey: string, value: Generation[]) {
for (let i = 0; i < value.length; i += 1) {
const key = getCacheKey(prompt, llmKey, String(i));
await this.redisClient.set(
key,
JSON.stringify(serializeGeneration(value[i]))
);
}
}
}