UNPKG

@langchain/community

Version:
1 lines • 4.27 kB
{"version":3,"file":"upstash_redis.cjs","names":["BaseCache","props: UpstashRedisCacheProps","Redis","prompt: string","llmKey: string","generations: Generation[]","value: Generation[]"],"sources":["../../src/caches/upstash_redis.ts"],"sourcesContent":["import { Redis, type RedisConfigNodejs } from \"@upstash/redis\";\n\nimport { Generation } from \"@langchain/core/outputs\";\nimport {\n BaseCache,\n deserializeStoredGeneration,\n serializeGeneration,\n} from \"@langchain/core/caches\";\nimport { StoredGeneration } from \"@langchain/core/messages\";\n\nexport type UpstashRedisCacheProps = {\n /**\n * The config to use to instantiate an Upstash Redis client.\n */\n config?: RedisConfigNodejs;\n /**\n * An existing Upstash Redis client.\n */\n client?: Redis;\n /**\n * Time-to-live (TTL) for cached items in seconds.\n */\n ttl?: number;\n};\n\n/**\n * A cache that uses Upstash as the backing store.\n * See https://docs.upstash.com/redis.\n * @example\n * ```typescript\n * const cache = new UpstashRedisCache({\n * config: {\n * url: \"UPSTASH_REDIS_REST_URL\",\n * token: \"UPSTASH_REDIS_REST_TOKEN\",\n * },\n * ttl: 3600, // Optional: Cache entries will expire after 1 hour\n * });\n * // Initialize the OpenAI model with Upstash Redis cache for caching responses\n * const model = new ChatOpenAI({\n * model: \"gpt-4o-mini\",\n * cache,\n * });\n * await model.invoke(\"How are you today?\");\n * const cachedValues = await cache.lookup(\"How are you today?\", \"llmKey\");\n * ```\n */\nexport class UpstashRedisCache extends BaseCache {\n private redisClient: Redis;\n\n private ttl?: number;\n\n constructor(props: UpstashRedisCacheProps) {\n super();\n const { config, client, ttl } = props;\n this.ttl = ttl;\n\n if (client) {\n this.redisClient = client;\n } else if (config) {\n this.redisClient = new Redis(config);\n } else {\n throw new Error(\n `Upstash Redis caches require either a config object or a pre-configured client.`\n );\n }\n }\n\n /**\n * Lookup LLM generations in cache by prompt and associated LLM key.\n */\n public async lookup(prompt: string, llmKey: string) {\n let idx = 0;\n let key = this.keyEncoder(prompt, llmKey, String(idx));\n let value = await this.redisClient.get<StoredGeneration | null>(key);\n const generations: Generation[] = [];\n\n while (value) {\n generations.push(deserializeStoredGeneration(value));\n idx += 1;\n key = this.keyEncoder(prompt, llmKey, String(idx));\n value = await this.redisClient.get<StoredGeneration | null>(key);\n }\n\n return generations.length > 0 ? generations : null;\n }\n\n /**\n * Update the cache with the given generations.\n *\n * Note this overwrites any existing generations for the given prompt and LLM key.\n */\n public async update(prompt: string, llmKey: string, value: Generation[]) {\n for (let i = 0; i < value.length; i += 1) {\n const key = this.keyEncoder(prompt, llmKey, String(i));\n const serializedValue = JSON.stringify(serializeGeneration(value[i]));\n\n if (this.ttl) {\n await this.redisClient.set(key, serializedValue, { ex: this.ttl });\n } else {\n await this.redisClient.set(key, serializedValue);\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,IAAa,oBAAb,cAAuCA,kCAAU;CAC/C,AAAQ;CAER,AAAQ;CAER,YAAYC,OAA+B;EACzC,OAAO;EACP,MAAM,EAAE,QAAQ,QAAQ,KAAK,GAAG;EAChC,KAAK,MAAM;AAEX,MAAI,QACF,KAAK,cAAc;WACV,QACT,KAAK,cAAc,IAAIC,sBAAM;MAE7B,OAAM,IAAI,MACR,CAAC,+EAA+E,CAAC;CAGtF;;;;CAKD,MAAa,OAAOC,QAAgBC,QAAgB;EAClD,IAAI,MAAM;EACV,IAAI,MAAM,KAAK,WAAW,QAAQ,QAAQ,OAAO,IAAI,CAAC;EACtD,IAAI,QAAQ,MAAM,KAAK,YAAY,IAA6B,IAAI;EACpE,MAAMC,cAA4B,CAAE;AAEpC,SAAO,OAAO;GACZ,YAAY,8DAAiC,MAAM,CAAC;GACpD,OAAO;GACP,MAAM,KAAK,WAAW,QAAQ,QAAQ,OAAO,IAAI,CAAC;GAClD,QAAQ,MAAM,KAAK,YAAY,IAA6B,IAAI;EACjE;AAED,SAAO,YAAY,SAAS,IAAI,cAAc;CAC/C;;;;;;CAOD,MAAa,OAAOF,QAAgBC,QAAgBE,OAAqB;AACvE,OAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,GAAG;GACxC,MAAM,MAAM,KAAK,WAAW,QAAQ,QAAQ,OAAO,EAAE,CAAC;GACtD,MAAM,kBAAkB,KAAK,2DAA8B,MAAM,GAAG,CAAC;AAErE,OAAI,KAAK,KACP,MAAM,KAAK,YAAY,IAAI,KAAK,iBAAiB,EAAE,IAAI,KAAK,IAAK,EAAC;QAElE,MAAM,KAAK,YAAY,IAAI,KAAK,gBAAgB;EAEnD;CACF;AACF"}