@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 4.16 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","names":["sha256","mapStoredMessageToChatMessage"],"sources":["../../src/caches/index.ts"],"sourcesContent":["import { sha256, type HashKeyEncoder } from \"../utils/hash.js\";\nimport type { Generation, ChatGeneration } from \"../outputs.js\";\nimport { mapStoredMessageToChatMessage } from \"../messages/utils.js\";\nimport { type StoredGeneration } from \"../messages/base.js\";\n\nexport const defaultHashKeyEncoder: HashKeyEncoder = (...strings) =>\n sha256(strings.join(\"_\"));\n\nexport function deserializeStoredGeneration(\n storedGeneration: StoredGeneration\n) {\n if (storedGeneration.message !== undefined) {\n return {\n text: storedGeneration.text,\n message: mapStoredMessageToChatMessage(storedGeneration.message),\n };\n } else {\n return { text: storedGeneration.text };\n }\n}\n\nexport function serializeGeneration(generation: Generation) {\n const serializedValue: StoredGeneration = {\n text: generation.text,\n };\n if ((generation as ChatGeneration).message !== undefined) {\n serializedValue.message = (generation as ChatGeneration).message.toDict();\n }\n return serializedValue;\n}\n\n/**\n * Base class for all caches. All caches should extend this class.\n */\nexport abstract class BaseCache<T = Generation[]> {\n protected keyEncoder: HashKeyEncoder = defaultHashKeyEncoder;\n\n /**\n * Sets a custom key encoder function for the cache.\n * This function should take a prompt and an LLM key and return a string\n * that will be used as the cache key.\n * @param keyEncoderFn The custom key encoder function.\n */\n makeDefaultKeyEncoder(keyEncoderFn: HashKeyEncoder): void {\n this.keyEncoder = keyEncoderFn;\n }\n\n abstract lookup(prompt: string, llmKey: string): Promise<T | null>;\n\n abstract update(prompt: string, llmKey: string, value: T): Promise<void>;\n}\n\nconst GLOBAL_MAP = new Map();\n\n/**\n * A cache for storing LLM generations that stores data in memory.\n */\nexport class InMemoryCache<T = Generation[]> extends BaseCache<T> {\n private cache: Map<string, T>;\n\n constructor(map?: Map<string, T>) {\n super();\n this.cache = map ?? new Map();\n }\n\n /**\n * Retrieves data from the cache using a prompt and an LLM key. If the\n * data is not found, it returns null.\n * @param prompt The prompt used to find the data.\n * @param llmKey The LLM key used to find the data.\n * @returns The data corresponding to the prompt and LLM key, or null if not found.\n */\n lookup(prompt: string, llmKey: string): Promise<T | null> {\n return Promise.resolve(\n this.cache.get(this.keyEncoder(prompt, llmKey)) ?? null\n );\n }\n\n /**\n * Updates the cache with new data using a prompt and an LLM key.\n * @param prompt The prompt used to store the data.\n * @param llmKey The LLM key used to store the data.\n * @param value The data to be stored.\n */\n async update(prompt: string, llmKey: string, value: T): Promise<void> {\n this.cache.set(this.keyEncoder(prompt, llmKey), value);\n }\n\n /**\n * Returns a global instance of InMemoryCache using a predefined global\n * map as the initial cache.\n * @returns A global instance of InMemoryCache.\n */\n static global(): InMemoryCache {\n return new InMemoryCache(GLOBAL_MAP);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAKA,MAAa,yBAAyC,GAAG,YACvDA,oBAAO,QAAQ,KAAK,IAAI,CAAC;AAE3B,SAAgB,4BACd,kBACA;AACA,KAAI,iBAAiB,YAAY,OAC/B,QAAO;EACL,MAAM,iBAAiB;EACvB,SAASC,4CAA8B,iBAAiB,QAAQ;EACjE;KAED,QAAO,EAAE,MAAM,iBAAiB,MAAM;;AAI1C,SAAgB,oBAAoB,YAAwB;CAC1D,MAAM,kBAAoC,EACxC,MAAM,WAAW,MAClB;AACD,KAAK,WAA8B,YAAY,OAC7C,iBAAgB,UAAW,WAA8B,QAAQ,QAAQ;AAE3E,QAAO;;;;;AAMT,IAAsB,YAAtB,MAAkD;CAChD,AAAU,aAA6B;;;;;;;CAQvC,sBAAsB,cAAoC;AACxD,OAAK,aAAa;;;AAQtB,MAAM,6BAAa,IAAI,KAAK;;;;AAK5B,IAAa,gBAAb,MAAa,sBAAwC,UAAa;CAChE,AAAQ;CAER,YAAY,KAAsB;AAChC,SAAO;AACP,OAAK,QAAQ,uBAAO,IAAI,KAAK;;;;;;;;;CAU/B,OAAO,QAAgB,QAAmC;AACxD,SAAO,QAAQ,QACb,KAAK,MAAM,IAAI,KAAK,WAAW,QAAQ,OAAO,CAAC,IAAI,KACpD;;;;;;;;CASH,MAAM,OAAO,QAAgB,QAAgB,OAAyB;AACpE,OAAK,MAAM,IAAI,KAAK,WAAW,QAAQ,OAAO,EAAE,MAAM;;;;;;;CAQxD,OAAO,SAAwB;AAC7B,SAAO,IAAI,cAAc,WAAW"}