base.cjs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.InMemoryCache = exports.BaseCache = exports.serializeGeneration = exports.deserializeStoredGeneration = exports.getCacheKey = void 0;
  4. const hash_js_1 = require("../utils/hash.cjs");
  5. const utils_js_1 = require("../messages/utils.cjs");
  6. /**
  7. * This cache key should be consistent across all versions of LangChain.
  8. * It is currently NOT consistent across versions of LangChain.
  9. *
  10. * A huge benefit of having a remote cache (like redis) is that you can
  11. * access the cache from different processes/machines. The allows you to
  12. * separate concerns and scale horizontally.
  13. *
  14. * TODO: Make cache key consistent across versions of LangChain.
  15. */
  16. const getCacheKey = (...strings) => (0, hash_js_1.insecureHash)(strings.join("_"));
  17. exports.getCacheKey = getCacheKey;
  18. function deserializeStoredGeneration(storedGeneration) {
  19. if (storedGeneration.message !== undefined) {
  20. return {
  21. text: storedGeneration.text,
  22. message: (0, utils_js_1.mapStoredMessageToChatMessage)(storedGeneration.message),
  23. };
  24. }
  25. else {
  26. return { text: storedGeneration.text };
  27. }
  28. }
  29. exports.deserializeStoredGeneration = deserializeStoredGeneration;
  30. function serializeGeneration(generation) {
  31. const serializedValue = {
  32. text: generation.text,
  33. };
  34. if (generation.message !== undefined) {
  35. serializedValue.message = generation.message.toDict();
  36. }
  37. return serializedValue;
  38. }
  39. exports.serializeGeneration = serializeGeneration;
  40. /**
  41. * Base class for all caches. All caches should extend this class.
  42. */
  43. class BaseCache {
  44. }
  45. exports.BaseCache = BaseCache;
  46. const GLOBAL_MAP = new Map();
  47. /**
  48. * A cache for storing LLM generations that stores data in memory.
  49. */
  50. class InMemoryCache extends BaseCache {
  51. constructor(map) {
  52. super();
  53. Object.defineProperty(this, "cache", {
  54. enumerable: true,
  55. configurable: true,
  56. writable: true,
  57. value: void 0
  58. });
  59. this.cache = map ?? new Map();
  60. }
  61. /**
  62. * Retrieves data from the cache using a prompt and an LLM key. If the
  63. * data is not found, it returns null.
  64. * @param prompt The prompt used to find the data.
  65. * @param llmKey The LLM key used to find the data.
  66. * @returns The data corresponding to the prompt and LLM key, or null if not found.
  67. */
  68. lookup(prompt, llmKey) {
  69. return Promise.resolve(this.cache.get((0, exports.getCacheKey)(prompt, llmKey)) ?? null);
  70. }
  71. /**
  72. * Updates the cache with new data using a prompt and an LLM key.
  73. * @param prompt The prompt used to store the data.
  74. * @param llmKey The LLM key used to store the data.
  75. * @param value The data to be stored.
  76. */
  77. async update(prompt, llmKey, value) {
  78. this.cache.set((0, exports.getCacheKey)(prompt, llmKey), value);
  79. }
  80. /**
  81. * Returns a global instance of InMemoryCache using a predefined global
  82. * map as the initial cache.
  83. * @returns A global instance of InMemoryCache.
  84. */
  85. static global() {
  86. return new InMemoryCache(GLOBAL_MAP);
  87. }
  88. }
  89. exports.InMemoryCache = InMemoryCache;