langchain 0.0.83 → 0.0.85

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.
Files changed (123) hide show
  1. package/cache/momento.cjs +1 -0
  2. package/cache/momento.d.ts +1 -0
  3. package/cache/momento.js +1 -0
  4. package/dist/agents/initialize.cjs +2 -1
  5. package/dist/agents/initialize.d.ts +0 -1
  6. package/dist/agents/initialize.js +2 -1
  7. package/dist/agents/structured_chat/index.cjs +10 -3
  8. package/dist/agents/structured_chat/index.d.ts +5 -1
  9. package/dist/agents/structured_chat/index.js +10 -3
  10. package/dist/cache/momento.cjs +114 -0
  11. package/dist/cache/momento.d.ts +72 -0
  12. package/dist/cache/momento.js +110 -0
  13. package/dist/callbacks/handlers/tracer_langchain.cjs +6 -20
  14. package/dist/callbacks/handlers/tracer_langchain.js +7 -21
  15. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +5 -8
  16. package/dist/callbacks/handlers/tracer_langchain_v1.js +5 -8
  17. package/dist/callbacks/manager.cjs +5 -16
  18. package/dist/callbacks/manager.js +5 -16
  19. package/dist/chains/conversational_retrieval_chain.cjs +12 -12
  20. package/dist/chains/conversational_retrieval_chain.d.ts +8 -0
  21. package/dist/chains/conversational_retrieval_chain.js +13 -13
  22. package/dist/chains/openai_moderation.cjs +2 -3
  23. package/dist/chains/openai_moderation.js +2 -3
  24. package/dist/chains/retrieval_qa.cjs +1 -1
  25. package/dist/chains/retrieval_qa.d.ts +1 -1
  26. package/dist/chains/retrieval_qa.js +1 -1
  27. package/dist/chains/router/multi_prompt.cjs +15 -1
  28. package/dist/chains/router/multi_prompt.d.ts +13 -0
  29. package/dist/chains/router/multi_prompt.js +15 -1
  30. package/dist/chains/router/multi_retrieval_qa.cjs +17 -3
  31. package/dist/chains/router/multi_retrieval_qa.d.ts +15 -0
  32. package/dist/chains/router/multi_retrieval_qa.js +18 -4
  33. package/dist/chat_models/anthropic.cjs +2 -5
  34. package/dist/chat_models/anthropic.js +2 -5
  35. package/dist/chat_models/openai.cjs +5 -21
  36. package/dist/chat_models/openai.js +6 -22
  37. package/dist/client/langchainplus.cjs +7 -27
  38. package/dist/client/langchainplus.js +7 -27
  39. package/dist/document_loaders/web/apify_dataset.cjs +2 -3
  40. package/dist/document_loaders/web/apify_dataset.js +2 -3
  41. package/dist/document_loaders/web/figma.cjs +65 -0
  42. package/dist/document_loaders/web/figma.d.ts +35 -0
  43. package/dist/document_loaders/web/figma.js +61 -0
  44. package/dist/document_loaders/web/github.cjs +2 -4
  45. package/dist/document_loaders/web/github.js +2 -4
  46. package/dist/document_loaders/web/notiondb.cjs +168 -0
  47. package/dist/document_loaders/web/notiondb.d.ts +20 -0
  48. package/dist/document_loaders/web/notiondb.js +164 -0
  49. package/dist/embeddings/cohere.cjs +2 -5
  50. package/dist/embeddings/cohere.js +2 -5
  51. package/dist/embeddings/hf.cjs +2 -5
  52. package/dist/embeddings/hf.js +2 -5
  53. package/dist/embeddings/openai.cjs +6 -23
  54. package/dist/embeddings/openai.js +7 -24
  55. package/dist/llms/cohere.cjs +2 -5
  56. package/dist/llms/cohere.js +2 -5
  57. package/dist/llms/hf.cjs +2 -5
  58. package/dist/llms/hf.js +2 -5
  59. package/dist/llms/openai-chat.cjs +6 -25
  60. package/dist/llms/openai-chat.js +7 -26
  61. package/dist/llms/openai.cjs +7 -27
  62. package/dist/llms/openai.js +8 -28
  63. package/dist/llms/replicate.cjs +2 -3
  64. package/dist/llms/replicate.js +2 -3
  65. package/dist/memory/zep.cjs +130 -0
  66. package/dist/memory/zep.d.ts +23 -0
  67. package/dist/memory/zep.js +126 -0
  68. package/dist/retrievers/zep.cjs +56 -0
  69. package/dist/retrievers/zep.d.ts +25 -0
  70. package/dist/retrievers/zep.js +52 -0
  71. package/dist/stores/message/momento.cjs +120 -0
  72. package/dist/stores/message/momento.d.ts +60 -0
  73. package/dist/stores/message/momento.js +116 -0
  74. package/dist/text_splitter.cjs +35 -1
  75. package/dist/text_splitter.d.ts +5 -0
  76. package/dist/text_splitter.js +33 -0
  77. package/dist/tools/bingserpapi.cjs +2 -4
  78. package/dist/tools/bingserpapi.js +2 -4
  79. package/dist/tools/google_custom_search.cjs +3 -8
  80. package/dist/tools/google_custom_search.js +3 -8
  81. package/dist/tools/requests.cjs +1 -1
  82. package/dist/tools/requests.js +1 -1
  83. package/dist/tools/serpapi.cjs +2 -4
  84. package/dist/tools/serpapi.js +2 -4
  85. package/dist/tools/serper.cjs +2 -4
  86. package/dist/tools/serper.js +2 -4
  87. package/dist/tools/zapier.cjs +2 -5
  88. package/dist/tools/zapier.js +2 -5
  89. package/dist/util/env.cjs +15 -1
  90. package/dist/util/env.d.ts +1 -0
  91. package/dist/util/env.js +13 -0
  92. package/dist/util/hub.cjs +3 -8
  93. package/dist/util/hub.js +3 -8
  94. package/dist/util/momento.cjs +26 -0
  95. package/dist/util/momento.d.ts +9 -0
  96. package/dist/util/momento.js +22 -0
  97. package/dist/vectorstores/milvus.cjs +3 -3
  98. package/dist/vectorstores/milvus.d.ts +1 -0
  99. package/dist/vectorstores/milvus.js +3 -3
  100. package/dist/vectorstores/qdrant.cjs +2 -3
  101. package/dist/vectorstores/qdrant.js +2 -3
  102. package/dist/vectorstores/tigris.cjs +102 -0
  103. package/dist/vectorstores/tigris.d.ts +17 -0
  104. package/dist/vectorstores/tigris.js +75 -0
  105. package/document_loaders/web/figma.cjs +1 -0
  106. package/document_loaders/web/figma.d.ts +1 -0
  107. package/document_loaders/web/figma.js +1 -0
  108. package/document_loaders/web/notiondb.cjs +1 -0
  109. package/document_loaders/web/notiondb.d.ts +1 -0
  110. package/document_loaders/web/notiondb.js +1 -0
  111. package/memory/zep.cjs +1 -0
  112. package/memory/zep.d.ts +1 -0
  113. package/memory/zep.js +1 -0
  114. package/package.json +74 -3
  115. package/retrievers/zep.cjs +1 -0
  116. package/retrievers/zep.d.ts +1 -0
  117. package/retrievers/zep.js +1 -0
  118. package/stores/message/momento.cjs +1 -0
  119. package/stores/message/momento.d.ts +1 -0
  120. package/stores/message/momento.js +1 -0
  121. package/vectorstores/tigris.cjs +1 -0
  122. package/vectorstores/tigris.d.ts +1 -0
  123. package/vectorstores/tigris.js +1 -0
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/cache/momento.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/cache/momento.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/cache/momento.js'
@@ -84,10 +84,11 @@ async function initializeAgentExecutorWithOptions(tools, llm, options = {
84
84
  return executor;
85
85
  }
86
86
  case "structured-chat-zero-shot-react-description": {
87
- const { agentArgs, ...rest } = options;
87
+ const { agentArgs, memory, ...rest } = options;
88
88
  const executor = executor_js_1.AgentExecutor.fromAgentAndTools({
89
89
  agent: index_js_3.StructuredChatAgent.fromLLMAndTools(llm, tools, agentArgs),
90
90
  tools,
91
+ memory,
91
92
  ...rest,
92
93
  });
93
94
  return executor;
@@ -32,7 +32,6 @@ export type InitializeAgentExecutorOptions = ({
32
32
  export type InitializeAgentExecutorOptionsStructured = {
33
33
  agentType: "structured-chat-zero-shot-react-description";
34
34
  agentArgs?: Parameters<typeof StructuredChatAgent.fromLLMAndTools>[2];
35
- memory?: never;
36
35
  } & Omit<AgentExecutorInput, "agent" | "tools">;
37
36
  /**
38
37
  * Initialize an agent executor with options
@@ -80,10 +80,11 @@ export async function initializeAgentExecutorWithOptions(tools, llm, options = {
80
80
  return executor;
81
81
  }
82
82
  case "structured-chat-zero-shot-react-description": {
83
- const { agentArgs, ...rest } = options;
83
+ const { agentArgs, memory, ...rest } = options;
84
84
  const executor = AgentExecutor.fromAgentAndTools({
85
85
  agent: StructuredChatAgent.fromLLMAndTools(llm, tools, agentArgs),
86
86
  tools,
87
+ memory,
87
88
  ...rest,
88
89
  });
89
90
  return executor;
@@ -66,20 +66,27 @@ class StructuredChatAgent extends agent_js_1.Agent {
66
66
  * @param args - Arguments to create the prompt with.
67
67
  * @param args.suffix - String to put after the list of tools.
68
68
  * @param args.prefix - String to put before the list of tools.
69
+ * @param args.inputVariables List of input variables the final prompt will expect.
70
+ * @param args.memoryPrompts List of historical prompts from memory.
69
71
  */
70
72
  static createPrompt(tools, args) {
71
- const { prefix = prompt_js_2.PREFIX, suffix = prompt_js_2.SUFFIX } = args ?? {};
73
+ const { prefix = prompt_js_2.PREFIX, suffix = prompt_js_2.SUFFIX, inputVariables = ["input", "agent_scratchpad"], memoryPrompts = [], } = args ?? {};
72
74
  const template = [prefix, prompt_js_2.FORMAT_INSTRUCTIONS, suffix].join("\n\n");
75
+ const humanMessageTemplate = "{input}\n\n{agent_scratchpad}";
73
76
  const messages = [
74
77
  new chat_js_1.SystemMessagePromptTemplate(new prompt_js_1.PromptTemplate({
75
78
  template,
76
- inputVariables: [],
79
+ inputVariables,
77
80
  partialVariables: {
78
81
  tool_schemas: StructuredChatAgent.createToolSchemasString(tools),
79
82
  tool_names: tools.map((tool) => tool.name).join(", "),
80
83
  },
81
84
  })),
82
- chat_js_1.HumanMessagePromptTemplate.fromTemplate("{input}\n\n{agent_scratchpad}"),
85
+ ...memoryPrompts,
86
+ new chat_js_1.HumanMessagePromptTemplate(new prompt_js_1.PromptTemplate({
87
+ template: humanMessageTemplate,
88
+ inputVariables,
89
+ })),
83
90
  ];
84
91
  return chat_js_1.ChatPromptTemplate.fromPromptMessages(messages);
85
92
  }
@@ -1,5 +1,5 @@
1
1
  import { BaseLanguageModel } from "../../base_language/index.js";
2
- import { ChatPromptTemplate } from "../../prompts/chat.js";
2
+ import { BaseMessagePromptTemplate, ChatPromptTemplate } from "../../prompts/chat.js";
3
3
  import { AgentStep } from "../../schema/index.js";
4
4
  import { StructuredTool } from "../../tools/base.js";
5
5
  import { Optional } from "../../types/type-utils.js";
@@ -13,6 +13,8 @@ export interface StructuredChatCreatePromptArgs {
13
13
  prefix?: string;
14
14
  /** List of input variables the final prompt will expect. */
15
15
  inputVariables?: string[];
16
+ /** List of historical prompts from memory. */
17
+ memoryPrompts?: BaseMessagePromptTemplate[];
16
18
  }
17
19
  export type StructuredChatAgentInput = Optional<AgentInput, "outputParser">;
18
20
  /**
@@ -38,6 +40,8 @@ export declare class StructuredChatAgent extends Agent {
38
40
  * @param args - Arguments to create the prompt with.
39
41
  * @param args.suffix - String to put after the list of tools.
40
42
  * @param args.prefix - String to put before the list of tools.
43
+ * @param args.inputVariables List of input variables the final prompt will expect.
44
+ * @param args.memoryPrompts List of historical prompts from memory.
41
45
  */
42
46
  static createPrompt(tools: StructuredTool[], args?: StructuredChatCreatePromptArgs): ChatPromptTemplate;
43
47
  static fromLLMAndTools(llm: BaseLanguageModel, tools: StructuredTool[], args?: StructuredChatCreatePromptArgs & AgentArgs): StructuredChatAgent;
@@ -63,20 +63,27 @@ export class StructuredChatAgent extends Agent {
63
63
  * @param args - Arguments to create the prompt with.
64
64
  * @param args.suffix - String to put after the list of tools.
65
65
  * @param args.prefix - String to put before the list of tools.
66
+ * @param args.inputVariables List of input variables the final prompt will expect.
67
+ * @param args.memoryPrompts List of historical prompts from memory.
66
68
  */
67
69
  static createPrompt(tools, args) {
68
- const { prefix = PREFIX, suffix = SUFFIX } = args ?? {};
70
+ const { prefix = PREFIX, suffix = SUFFIX, inputVariables = ["input", "agent_scratchpad"], memoryPrompts = [], } = args ?? {};
69
71
  const template = [prefix, FORMAT_INSTRUCTIONS, suffix].join("\n\n");
72
+ const humanMessageTemplate = "{input}\n\n{agent_scratchpad}";
70
73
  const messages = [
71
74
  new SystemMessagePromptTemplate(new PromptTemplate({
72
75
  template,
73
- inputVariables: [],
76
+ inputVariables,
74
77
  partialVariables: {
75
78
  tool_schemas: StructuredChatAgent.createToolSchemasString(tools),
76
79
  tool_names: tools.map((tool) => tool.name).join(", "),
77
80
  },
78
81
  })),
79
- HumanMessagePromptTemplate.fromTemplate("{input}\n\n{agent_scratchpad}"),
82
+ ...memoryPrompts,
83
+ new HumanMessagePromptTemplate(new PromptTemplate({
84
+ template: humanMessageTemplate,
85
+ inputVariables,
86
+ })),
80
87
  ];
81
88
  return ChatPromptTemplate.fromPromptMessages(messages);
82
89
  }
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MomentoCache = void 0;
4
+ /* eslint-disable no-instanceof/no-instanceof */
5
+ const sdk_1 = require("@gomomento/sdk");
6
+ const index_js_1 = require("../schema/index.cjs");
7
+ const base_js_1 = require("./base.cjs");
8
+ const momento_js_1 = require("../util/momento.cjs");
9
+ /**
10
+ * A cache that uses Momento as the backing store.
11
+ * See https://gomomento.com.
12
+ */
13
+ class MomentoCache extends index_js_1.BaseCache {
14
+ constructor(props) {
15
+ super();
16
+ Object.defineProperty(this, "client", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ Object.defineProperty(this, "cacheName", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ Object.defineProperty(this, "ttlSeconds", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ this.client = props.client;
35
+ this.cacheName = props.cacheName;
36
+ this.validateTtlSeconds(props.ttlSeconds);
37
+ this.ttlSeconds = props.ttlSeconds;
38
+ }
39
+ /**
40
+ * Create a new standard cache backed by Momento.
41
+ *
42
+ * @param {MomentoCacheProps} props The settings to instantiate the cache.
43
+ * @param {ICacheClient} props.client The Momento cache client.
44
+ * @param {string} props.cacheName The name of the cache to use to store the data.
45
+ * @param {number} props.ttlSeconds The time to live for the cache items. If not specified,
46
+ * the cache client default is used.
47
+ * @param {boolean} props.ensureCacheExists If true, ensure that the cache exists before returning.
48
+ * If false, the cache is not checked for existence. Defaults to true.
49
+ * @throws {@link InvalidArgumentError} if {@link props.ttlSeconds} is not strictly positive.
50
+ * @returns The Momento-backed cache.
51
+ */
52
+ static async fromProps(props) {
53
+ const instance = new MomentoCache(props);
54
+ if (props.ensureCacheExists || props.ensureCacheExists === undefined) {
55
+ await (0, momento_js_1.ensureCacheExists)(props.client, props.cacheName);
56
+ }
57
+ return instance;
58
+ }
59
+ /**
60
+ * Validate the user-specified TTL, if provided, is strictly positive.
61
+ * @param ttlSeconds The TTL to validate.
62
+ */
63
+ validateTtlSeconds(ttlSeconds) {
64
+ if (ttlSeconds !== undefined && ttlSeconds <= 0) {
65
+ throw new sdk_1.InvalidArgumentError("ttlSeconds must be positive.");
66
+ }
67
+ }
68
+ /**
69
+ * Lookup LLM generations in cache by prompt and associated LLM key.
70
+ * @param prompt The prompt to lookup.
71
+ * @param llmKey The LLM key to lookup.
72
+ * @returns The generations associated with the prompt and LLM key, or null if not found.
73
+ */
74
+ async lookup(prompt, llmKey) {
75
+ const key = (0, base_js_1.getCacheKey)(prompt, llmKey);
76
+ const getResponse = await this.client.get(this.cacheName, key);
77
+ if (getResponse instanceof sdk_1.CacheGet.Hit) {
78
+ const value = getResponse.valueString();
79
+ return JSON.parse(value);
80
+ }
81
+ else if (getResponse instanceof sdk_1.CacheGet.Miss) {
82
+ return null;
83
+ }
84
+ else if (getResponse instanceof sdk_1.CacheGet.Error) {
85
+ throw getResponse.innerException();
86
+ }
87
+ else {
88
+ throw new Error(`Unknown response type: ${getResponse.toString()}`);
89
+ }
90
+ }
91
+ /**
92
+ * Update the cache with the given generations.
93
+ *
94
+ * Note this overwrites any existing generations for the given prompt and LLM key.
95
+ *
96
+ * @param prompt The prompt to update.
97
+ * @param llmKey The LLM key to update.
98
+ * @param value The generations to store.
99
+ */
100
+ async update(prompt, llmKey, value) {
101
+ 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 });
103
+ if (setResponse instanceof sdk_1.CacheSet.Success) {
104
+ // pass
105
+ }
106
+ else if (setResponse instanceof sdk_1.CacheSet.Error) {
107
+ throw setResponse.innerException();
108
+ }
109
+ else {
110
+ throw new Error(`Unknown response type: ${setResponse.toString()}`);
111
+ }
112
+ }
113
+ }
114
+ exports.MomentoCache = MomentoCache;
@@ -0,0 +1,72 @@
1
+ import { ICacheClient } from "@gomomento/sdk";
2
+ import { BaseCache, Generation } from "../schema/index.js";
3
+ /**
4
+ * The settings to instantiate the Momento standard cache.
5
+ */
6
+ export interface MomentoCacheProps {
7
+ /**
8
+ * The Momento cache client.
9
+ */
10
+ client: ICacheClient;
11
+ /**
12
+ * The name of the cache to use to store the data.
13
+ */
14
+ cacheName: string;
15
+ /**
16
+ * The time to live for the cache items. If not specified,
17
+ * the cache client default is used.
18
+ */
19
+ ttlSeconds?: number;
20
+ /**
21
+ * If true, ensure that the cache exists before returning.
22
+ * If false, the cache is not checked for existence.
23
+ * Defaults to true.
24
+ */
25
+ ensureCacheExists?: true;
26
+ }
27
+ /**
28
+ * A cache that uses Momento as the backing store.
29
+ * See https://gomomento.com.
30
+ */
31
+ export declare class MomentoCache extends BaseCache {
32
+ private client;
33
+ private readonly cacheName;
34
+ private readonly ttlSeconds?;
35
+ private constructor();
36
+ /**
37
+ * Create a new standard cache backed by Momento.
38
+ *
39
+ * @param {MomentoCacheProps} props The settings to instantiate the cache.
40
+ * @param {ICacheClient} props.client The Momento cache client.
41
+ * @param {string} props.cacheName The name of the cache to use to store the data.
42
+ * @param {number} props.ttlSeconds The time to live for the cache items. If not specified,
43
+ * the cache client default is used.
44
+ * @param {boolean} props.ensureCacheExists If true, ensure that the cache exists before returning.
45
+ * If false, the cache is not checked for existence. Defaults to true.
46
+ * @throws {@link InvalidArgumentError} if {@link props.ttlSeconds} is not strictly positive.
47
+ * @returns The Momento-backed cache.
48
+ */
49
+ static fromProps(props: MomentoCacheProps): Promise<MomentoCache>;
50
+ /**
51
+ * Validate the user-specified TTL, if provided, is strictly positive.
52
+ * @param ttlSeconds The TTL to validate.
53
+ */
54
+ private validateTtlSeconds;
55
+ /**
56
+ * Lookup LLM generations in cache by prompt and associated LLM key.
57
+ * @param prompt The prompt to lookup.
58
+ * @param llmKey The LLM key to lookup.
59
+ * @returns The generations associated with the prompt and LLM key, or null if not found.
60
+ */
61
+ lookup(prompt: string, llmKey: string): Promise<Generation[] | null>;
62
+ /**
63
+ * Update the cache with the given generations.
64
+ *
65
+ * Note this overwrites any existing generations for the given prompt and LLM key.
66
+ *
67
+ * @param prompt The prompt to update.
68
+ * @param llmKey The LLM key to update.
69
+ * @param value The generations to store.
70
+ */
71
+ update(prompt: string, llmKey: string, value: Generation[]): Promise<void>;
72
+ }
@@ -0,0 +1,110 @@
1
+ /* eslint-disable no-instanceof/no-instanceof */
2
+ import { CacheGet, CacheSet, InvalidArgumentError, } from "@gomomento/sdk";
3
+ import { BaseCache } from "../schema/index.js";
4
+ import { getCacheKey } from "./base.js";
5
+ import { ensureCacheExists } from "../util/momento.js";
6
+ /**
7
+ * A cache that uses Momento as the backing store.
8
+ * See https://gomomento.com.
9
+ */
10
+ export class MomentoCache extends BaseCache {
11
+ constructor(props) {
12
+ super();
13
+ Object.defineProperty(this, "client", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "cacheName", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ Object.defineProperty(this, "ttlSeconds", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: void 0
30
+ });
31
+ this.client = props.client;
32
+ this.cacheName = props.cacheName;
33
+ this.validateTtlSeconds(props.ttlSeconds);
34
+ this.ttlSeconds = props.ttlSeconds;
35
+ }
36
+ /**
37
+ * Create a new standard cache backed by Momento.
38
+ *
39
+ * @param {MomentoCacheProps} props The settings to instantiate the cache.
40
+ * @param {ICacheClient} props.client The Momento cache client.
41
+ * @param {string} props.cacheName The name of the cache to use to store the data.
42
+ * @param {number} props.ttlSeconds The time to live for the cache items. If not specified,
43
+ * the cache client default is used.
44
+ * @param {boolean} props.ensureCacheExists If true, ensure that the cache exists before returning.
45
+ * If false, the cache is not checked for existence. Defaults to true.
46
+ * @throws {@link InvalidArgumentError} if {@link props.ttlSeconds} is not strictly positive.
47
+ * @returns The Momento-backed cache.
48
+ */
49
+ static async fromProps(props) {
50
+ const instance = new MomentoCache(props);
51
+ if (props.ensureCacheExists || props.ensureCacheExists === undefined) {
52
+ await ensureCacheExists(props.client, props.cacheName);
53
+ }
54
+ return instance;
55
+ }
56
+ /**
57
+ * Validate the user-specified TTL, if provided, is strictly positive.
58
+ * @param ttlSeconds The TTL to validate.
59
+ */
60
+ validateTtlSeconds(ttlSeconds) {
61
+ if (ttlSeconds !== undefined && ttlSeconds <= 0) {
62
+ throw new InvalidArgumentError("ttlSeconds must be positive.");
63
+ }
64
+ }
65
+ /**
66
+ * Lookup LLM generations in cache by prompt and associated LLM key.
67
+ * @param prompt The prompt to lookup.
68
+ * @param llmKey The LLM key to lookup.
69
+ * @returns The generations associated with the prompt and LLM key, or null if not found.
70
+ */
71
+ async lookup(prompt, llmKey) {
72
+ const key = getCacheKey(prompt, llmKey);
73
+ const getResponse = await this.client.get(this.cacheName, key);
74
+ if (getResponse instanceof CacheGet.Hit) {
75
+ const value = getResponse.valueString();
76
+ return JSON.parse(value);
77
+ }
78
+ else if (getResponse instanceof CacheGet.Miss) {
79
+ return null;
80
+ }
81
+ else if (getResponse instanceof CacheGet.Error) {
82
+ throw getResponse.innerException();
83
+ }
84
+ else {
85
+ throw new Error(`Unknown response type: ${getResponse.toString()}`);
86
+ }
87
+ }
88
+ /**
89
+ * Update the cache with the given generations.
90
+ *
91
+ * Note this overwrites any existing generations for the given prompt and LLM key.
92
+ *
93
+ * @param prompt The prompt to update.
94
+ * @param llmKey The LLM key to update.
95
+ * @param value The generations to store.
96
+ */
97
+ async update(prompt, llmKey, value) {
98
+ const key = getCacheKey(prompt, llmKey);
99
+ const setResponse = await this.client.set(this.cacheName, key, JSON.stringify(value), { ttl: this.ttlSeconds });
100
+ if (setResponse instanceof CacheSet.Success) {
101
+ // pass
102
+ }
103
+ else if (setResponse instanceof CacheSet.Error) {
104
+ throw setResponse.innerException();
105
+ }
106
+ else {
107
+ throw new Error(`Unknown response type: ${setResponse.toString()}`);
108
+ }
109
+ }
110
+ }
@@ -17,10 +17,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
17
17
  enumerable: true,
18
18
  configurable: true,
19
19
  writable: true,
20
- value: (typeof process !== "undefined"
21
- ? // eslint-disable-next-line no-process-env
22
- process.env?.LANGCHAIN_ENDPOINT
23
- : undefined) || "http://localhost:1984"
20
+ value: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_ENDPOINT") || "http://localhost:1984"
24
21
  });
25
22
  Object.defineProperty(this, "headers", {
26
23
  enumerable: true,
@@ -66,24 +63,13 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
66
63
  writable: true,
67
64
  value: void 0
68
65
  });
69
- // eslint-disable-next-line no-process-env
70
- if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
71
- // eslint-disable-next-line no-process-env
72
- this.headers["x-api-key"] = process.env?.LANGCHAIN_API_KEY;
66
+ const apiKey = (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_API_KEY");
67
+ if (apiKey) {
68
+ this.headers["x-api-key"] = apiKey;
73
69
  }
74
- this.tenantId =
75
- tenantId ??
76
- (typeof process !== "undefined"
77
- ? // eslint-disable-next-line no-process-env
78
- process.env?.LANGCHAIN_TENANT_ID
79
- : undefined);
70
+ this.tenantId = tenantId ?? (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_TENANT_ID");
80
71
  this.sessionName =
81
- sessionName ??
82
- (typeof process !== "undefined"
83
- ? // eslint-disable-next-line no-process-env
84
- process.env?.LANGCHAIN_SESSION
85
- : undefined) ??
86
- "default";
72
+ sessionName ?? (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION") ?? "default";
87
73
  this.sessionExtra = sessionExtra;
88
74
  this.exampleId = exampleId;
89
75
  this.caller = new async_caller_js_1.AsyncCaller(callerParams ?? {});
@@ -1,5 +1,5 @@
1
1
  import { AsyncCaller } from "../../util/async_caller.js";
2
- import { getRuntimeEnvironment } from "../../util/env.js";
2
+ import { getEnvironmentVariable, getRuntimeEnvironment, } from "../../util/env.js";
3
3
  import { BaseTracer } from "./tracer.js";
4
4
  export class LangChainTracer extends BaseTracer {
5
5
  constructor({ exampleId, tenantId, sessionName, sessionExtra, callerParams, } = {}) {
@@ -14,10 +14,7 @@ export class LangChainTracer extends BaseTracer {
14
14
  enumerable: true,
15
15
  configurable: true,
16
16
  writable: true,
17
- value: (typeof process !== "undefined"
18
- ? // eslint-disable-next-line no-process-env
19
- process.env?.LANGCHAIN_ENDPOINT
20
- : undefined) || "http://localhost:1984"
17
+ value: getEnvironmentVariable("LANGCHAIN_ENDPOINT") || "http://localhost:1984"
21
18
  });
22
19
  Object.defineProperty(this, "headers", {
23
20
  enumerable: true,
@@ -63,24 +60,13 @@ export class LangChainTracer extends BaseTracer {
63
60
  writable: true,
64
61
  value: void 0
65
62
  });
66
- // eslint-disable-next-line no-process-env
67
- if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
68
- // eslint-disable-next-line no-process-env
69
- this.headers["x-api-key"] = process.env?.LANGCHAIN_API_KEY;
63
+ const apiKey = getEnvironmentVariable("LANGCHAIN_API_KEY");
64
+ if (apiKey) {
65
+ this.headers["x-api-key"] = apiKey;
70
66
  }
71
- this.tenantId =
72
- tenantId ??
73
- (typeof process !== "undefined"
74
- ? // eslint-disable-next-line no-process-env
75
- process.env?.LANGCHAIN_TENANT_ID
76
- : undefined);
67
+ this.tenantId = tenantId ?? getEnvironmentVariable("LANGCHAIN_TENANT_ID");
77
68
  this.sessionName =
78
- sessionName ??
79
- (typeof process !== "undefined"
80
- ? // eslint-disable-next-line no-process-env
81
- process.env?.LANGCHAIN_SESSION
82
- : undefined) ??
83
- "default";
69
+ sessionName ?? getEnvironmentVariable("LANGCHAIN_SESSION") ?? "default";
84
70
  this.sessionExtra = sessionExtra;
85
71
  this.exampleId = exampleId;
86
72
  this.caller = new AsyncCaller(callerParams ?? {});
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LangChainTracerV1 = void 0;
4
4
  const base_js_1 = require("../../memory/base.cjs");
5
+ const env_js_1 = require("../../util/env.cjs");
5
6
  const tracer_js_1 = require("./tracer.cjs");
6
7
  class LangChainTracerV1 extends tracer_js_1.BaseTracer {
7
8
  constructor() {
@@ -16,10 +17,7 @@ class LangChainTracerV1 extends tracer_js_1.BaseTracer {
16
17
  enumerable: true,
17
18
  configurable: true,
18
19
  writable: true,
19
- value: (typeof process !== "undefined"
20
- ? // eslint-disable-next-line no-process-env
21
- process.env?.LANGCHAIN_ENDPOINT
22
- : undefined) || "http://localhost:1984"
20
+ value: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_ENDPOINT") || "http://localhost:1984"
23
21
  });
24
22
  Object.defineProperty(this, "headers", {
25
23
  enumerable: true,
@@ -35,10 +33,9 @@ class LangChainTracerV1 extends tracer_js_1.BaseTracer {
35
33
  writable: true,
36
34
  value: void 0
37
35
  });
38
- // eslint-disable-next-line no-process-env
39
- if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
40
- // eslint-disable-next-line no-process-env
41
- this.headers["x-api-key"] = process.env?.LANGCHAIN_API_KEY;
36
+ const apiKey = (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_API_KEY");
37
+ if (apiKey) {
38
+ this.headers["x-api-key"] = apiKey;
42
39
  }
43
40
  }
44
41
  async newSession(sessionName) {
@@ -1,4 +1,5 @@
1
1
  import { getBufferString } from "../../memory/base.js";
2
+ import { getEnvironmentVariable } from "../../util/env.js";
2
3
  import { BaseTracer } from "./tracer.js";
3
4
  export class LangChainTracerV1 extends BaseTracer {
4
5
  constructor() {
@@ -13,10 +14,7 @@ export class LangChainTracerV1 extends BaseTracer {
13
14
  enumerable: true,
14
15
  configurable: true,
15
16
  writable: true,
16
- value: (typeof process !== "undefined"
17
- ? // eslint-disable-next-line no-process-env
18
- process.env?.LANGCHAIN_ENDPOINT
19
- : undefined) || "http://localhost:1984"
17
+ value: getEnvironmentVariable("LANGCHAIN_ENDPOINT") || "http://localhost:1984"
20
18
  });
21
19
  Object.defineProperty(this, "headers", {
22
20
  enumerable: true,
@@ -32,10 +30,9 @@ export class LangChainTracerV1 extends BaseTracer {
32
30
  writable: true,
33
31
  value: void 0
34
32
  });
35
- // eslint-disable-next-line no-process-env
36
- if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
37
- // eslint-disable-next-line no-process-env
38
- this.headers["x-api-key"] = process.env?.LANGCHAIN_API_KEY;
33
+ const apiKey = getEnvironmentVariable("LANGCHAIN_API_KEY");
34
+ if (apiKey) {
35
+ this.headers["x-api-key"] = apiKey;
39
36
  }
40
37
  }
41
38
  async newSession(sessionName) {
@@ -6,6 +6,7 @@ const base_js_1 = require("./base.cjs");
6
6
  const console_js_1 = require("./handlers/console.cjs");
7
7
  const initialize_js_1 = require("./handlers/initialize.cjs");
8
8
  const base_js_2 = require("../memory/base.cjs");
9
+ const env_js_1 = require("../util/env.cjs");
9
10
  class BaseCallbackManager {
10
11
  setHandler(handler) {
11
12
  return this.setHandlers([handler]);
@@ -334,19 +335,10 @@ class CallbackManager extends BaseCallbackManager {
334
335
  ? localHandlers.map(ensureHandler)
335
336
  : localHandlers?.handlers, false);
336
337
  }
337
- const verboseEnabled = (typeof process !== "undefined"
338
- ? // eslint-disable-next-line no-process-env
339
- process.env?.LANGCHAIN_VERBOSE !== undefined
340
- : false) || options?.verbose;
341
- const tracingV2Enabled = typeof process !== "undefined"
342
- ? // eslint-disable-next-line no-process-env
343
- process.env?.LANGCHAIN_TRACING_V2 !== undefined
344
- : false;
338
+ const verboseEnabled = (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_VERBOSE") || options?.verbose;
339
+ const tracingV2Enabled = (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_TRACING_V2") ?? false;
345
340
  const tracingEnabled = tracingV2Enabled ||
346
- (typeof process !== "undefined"
347
- ? // eslint-disable-next-line no-process-env
348
- process.env?.LANGCHAIN_TRACING !== undefined
349
- : false);
341
+ ((0, env_js_1.getEnvironmentVariable)("LANGCHAIN_TRACING") ?? false);
350
342
  if (verboseEnabled || tracingEnabled) {
351
343
  if (!callbackManager) {
352
344
  callbackManager = new CallbackManager();
@@ -362,10 +354,7 @@ class CallbackManager extends BaseCallbackManager {
362
354
  callbackManager.addHandler(await (0, initialize_js_1.getTracingV2CallbackHandler)(), true);
363
355
  }
364
356
  else {
365
- const session = typeof process !== "undefined"
366
- ? // eslint-disable-next-line no-process-env
367
- process.env?.LANGCHAIN_SESSION
368
- : undefined;
357
+ const session = (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION");
369
358
  callbackManager.addHandler(await (0, initialize_js_1.getTracingCallbackHandler)(session), true);
370
359
  }
371
360
  }