langchain 0.0.75 → 0.0.76

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 (114) hide show
  1. package/dist/base_language/count_tokens.cjs +5 -21
  2. package/dist/base_language/count_tokens.d.ts +1 -6
  3. package/dist/base_language/count_tokens.js +4 -19
  4. package/dist/base_language/index.cjs +10 -24
  5. package/dist/base_language/index.d.ts +3 -2
  6. package/dist/base_language/index.js +11 -25
  7. package/dist/cache/redis.d.ts +3 -1
  8. package/dist/callbacks/base.d.ts +4 -4
  9. package/dist/callbacks/handlers/initialize.cjs +2 -9
  10. package/dist/callbacks/handlers/initialize.d.ts +1 -1
  11. package/dist/callbacks/handlers/initialize.js +2 -9
  12. package/dist/callbacks/handlers/tracer.cjs +11 -22
  13. package/dist/callbacks/handlers/tracer.d.ts +2 -2
  14. package/dist/callbacks/handlers/tracer.js +11 -22
  15. package/dist/callbacks/handlers/tracer_langchain.cjs +52 -91
  16. package/dist/callbacks/handlers/tracer_langchain.d.ts +12 -9
  17. package/dist/callbacks/handlers/tracer_langchain.js +52 -68
  18. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +5 -1
  19. package/dist/callbacks/handlers/tracer_langchain_v1.js +5 -1
  20. package/dist/callbacks/manager.cjs +10 -10
  21. package/dist/callbacks/manager.d.ts +2 -2
  22. package/dist/callbacks/manager.js +10 -10
  23. package/dist/chains/question_answering/map_reduce_prompts.cjs +5 -5
  24. package/dist/chains/question_answering/map_reduce_prompts.d.ts +1 -1
  25. package/dist/chains/question_answering/map_reduce_prompts.js +1 -1
  26. package/dist/chains/question_answering/refine_prompts.cjs +5 -5
  27. package/dist/chains/question_answering/refine_prompts.d.ts +1 -1
  28. package/dist/chains/question_answering/refine_prompts.js +1 -1
  29. package/dist/chains/question_answering/stuff_prompts.cjs +2 -2
  30. package/dist/chains/question_answering/stuff_prompts.d.ts +1 -1
  31. package/dist/chains/question_answering/stuff_prompts.js +1 -1
  32. package/dist/chains/sequential_chain.cjs +2 -2
  33. package/dist/chains/sequential_chain.d.ts +2 -2
  34. package/dist/chains/sequential_chain.js +2 -2
  35. package/dist/chains/sql_db/sql_db_prompt.cjs +20 -1
  36. package/dist/chains/sql_db/sql_db_prompt.d.ts +1 -0
  37. package/dist/chains/sql_db/sql_db_prompt.js +19 -0
  38. package/dist/chat_models/base.cjs +17 -1
  39. package/dist/chat_models/base.d.ts +6 -0
  40. package/dist/chat_models/base.js +18 -2
  41. package/dist/chat_models/openai.cjs +2 -2
  42. package/dist/chat_models/openai.js +2 -2
  43. package/dist/client/langchainplus.cjs +90 -29
  44. package/dist/client/langchainplus.d.ts +7 -5
  45. package/dist/client/langchainplus.js +90 -29
  46. package/dist/document_loaders/fs/unstructured.cjs +8 -0
  47. package/dist/document_loaders/fs/unstructured.d.ts +3 -0
  48. package/dist/document_loaders/fs/unstructured.js +8 -0
  49. package/dist/document_loaders/web/apify_dataset.cjs +64 -0
  50. package/dist/document_loaders/web/apify_dataset.d.ts +28 -0
  51. package/dist/document_loaders/web/apify_dataset.js +60 -0
  52. package/dist/embeddings/openai.cjs +2 -2
  53. package/dist/embeddings/openai.js +2 -2
  54. package/dist/llms/base.cjs +18 -1
  55. package/dist/llms/base.d.ts +7 -1
  56. package/dist/llms/base.js +19 -2
  57. package/dist/llms/openai-chat.cjs +2 -2
  58. package/dist/llms/openai-chat.js +2 -2
  59. package/dist/llms/openai.cjs +2 -2
  60. package/dist/llms/openai.js +2 -2
  61. package/dist/memory/base.cjs +9 -1
  62. package/dist/memory/base.d.ts +1 -0
  63. package/dist/memory/base.js +7 -0
  64. package/dist/memory/entity_memory.cjs +151 -0
  65. package/dist/memory/entity_memory.d.ts +35 -0
  66. package/dist/memory/entity_memory.js +147 -0
  67. package/dist/memory/index.cjs +5 -1
  68. package/dist/memory/index.d.ts +2 -0
  69. package/dist/memory/index.js +2 -0
  70. package/dist/memory/prompt.cjs +84 -1
  71. package/dist/memory/prompt.d.ts +6 -0
  72. package/dist/memory/prompt.js +83 -0
  73. package/dist/memory/stores/entity/in_memory.cjs +32 -0
  74. package/dist/memory/stores/entity/in_memory.d.ts +10 -0
  75. package/dist/memory/stores/entity/in_memory.js +28 -0
  76. package/dist/prompts/index.cjs +6 -1
  77. package/dist/prompts/index.d.ts +1 -0
  78. package/dist/prompts/index.js +1 -0
  79. package/dist/{chains/prompt_selector.d.ts → prompts/selectors/conditional.d.ts} +4 -4
  80. package/dist/schema/index.cjs +13 -1
  81. package/dist/schema/index.d.ts +17 -0
  82. package/dist/schema/index.js +11 -0
  83. package/dist/stores/message/dynamodb.cjs +8 -6
  84. package/dist/stores/message/dynamodb.js +8 -6
  85. package/dist/stores/message/redis.cjs +5 -3
  86. package/dist/stores/message/redis.js +5 -3
  87. package/dist/stores/message/utils.cjs +30 -15
  88. package/dist/stores/message/utils.d.ts +4 -2
  89. package/dist/stores/message/utils.js +28 -14
  90. package/dist/text_splitter.cjs +3 -23
  91. package/dist/text_splitter.d.ts +1 -3
  92. package/dist/text_splitter.js +3 -23
  93. package/dist/tools/webbrowser.cjs +5 -7
  94. package/dist/tools/webbrowser.js +3 -5
  95. package/dist/util/env.cjs +39 -7
  96. package/dist/util/env.d.ts +19 -0
  97. package/dist/util/env.js +32 -6
  98. package/dist/util/sql_utils.cjs +18 -0
  99. package/dist/util/sql_utils.js +19 -1
  100. package/dist/util/tiktoken.cjs +26 -0
  101. package/dist/util/tiktoken.d.ts +9 -0
  102. package/dist/util/tiktoken.js +21 -0
  103. package/dist/vectorstores/redis.cjs +236 -0
  104. package/dist/vectorstores/redis.d.ts +80 -0
  105. package/dist/vectorstores/redis.js +232 -0
  106. package/document_loaders/web/apify_dataset.cjs +1 -0
  107. package/document_loaders/web/apify_dataset.d.ts +1 -0
  108. package/document_loaders/web/apify_dataset.js +1 -0
  109. package/package.json +25 -5
  110. package/vectorstores/redis.cjs +1 -0
  111. package/vectorstores/redis.d.ts +1 -0
  112. package/vectorstores/redis.js +1 -0
  113. /package/dist/{chains/prompt_selector.cjs → prompts/selectors/conditional.cjs} +0 -0
  114. /package/dist/{chains/prompt_selector.js → prompts/selectors/conditional.js} +0 -0
@@ -42,3 +42,10 @@ export function getBufferString(messages, humanPrefix = "Human", aiPrefix = "AI"
42
42
  }
43
43
  return string_messages.join("\n");
44
44
  }
45
+ export function getPromptInputKey(inputs, memoryVariables) {
46
+ const promptInputKeys = Object.keys(inputs).filter((key) => !memoryVariables.includes(key) && key !== "stop");
47
+ if (promptInputKeys.length !== 1) {
48
+ throw new Error(`One input key expected, but got ${promptInputKeys.length}`);
49
+ }
50
+ return promptInputKeys[0];
51
+ }
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EntityMemory = void 0;
4
+ const chat_memory_js_1 = require("./chat_memory.cjs");
5
+ const prompt_js_1 = require("./prompt.cjs");
6
+ const base_js_1 = require("./base.cjs");
7
+ const llm_chain_js_1 = require("../chains/llm_chain.cjs");
8
+ const in_memory_js_1 = require("./stores/entity/in_memory.cjs");
9
+ // Entity extractor & summarizer to memory.
10
+ class EntityMemory extends chat_memory_js_1.BaseChatMemory {
11
+ constructor(fields) {
12
+ super({
13
+ chatHistory: fields.chatHistory,
14
+ returnMessages: fields.returnMessages ?? false,
15
+ inputKey: fields.inputKey,
16
+ outputKey: fields.outputKey,
17
+ });
18
+ Object.defineProperty(this, "entityExtractionChain", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "entitySummarizationChain", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "entityStore", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "entityCache", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: []
41
+ });
42
+ Object.defineProperty(this, "k", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: 3
47
+ });
48
+ Object.defineProperty(this, "chatHistoryKey", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: "history"
53
+ });
54
+ Object.defineProperty(this, "llm", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
60
+ Object.defineProperty(this, "entitiesKey", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: "entities"
65
+ });
66
+ Object.defineProperty(this, "humanPrefix", {
67
+ enumerable: true,
68
+ configurable: true,
69
+ writable: true,
70
+ value: void 0
71
+ });
72
+ Object.defineProperty(this, "aiPrefix", {
73
+ enumerable: true,
74
+ configurable: true,
75
+ writable: true,
76
+ value: void 0
77
+ });
78
+ this.llm = fields.llm;
79
+ this.humanPrefix = fields.humanPrefix;
80
+ this.aiPrefix = fields.aiPrefix;
81
+ this.chatHistoryKey = fields.chatHistoryKey ?? this.chatHistoryKey;
82
+ this.entitiesKey = fields.entitiesKey ?? this.entitiesKey;
83
+ this.entityExtractionChain = new llm_chain_js_1.LLMChain({
84
+ llm: this.llm,
85
+ prompt: fields.entityExtractionPrompt ?? prompt_js_1.ENTITY_EXTRACTION_PROMPT,
86
+ });
87
+ this.entitySummarizationChain = new llm_chain_js_1.LLMChain({
88
+ llm: this.llm,
89
+ prompt: fields.entitySummarizationPrompt ?? prompt_js_1.ENTITY_SUMMARIZATION_PROMPT,
90
+ });
91
+ this.entityStore = fields.entityStore ?? new in_memory_js_1.InMemoryEntityStore();
92
+ this.entityCache = fields.entityCache ?? this.entityCache;
93
+ this.k = fields.k ?? this.k;
94
+ }
95
+ get memoryKeys() {
96
+ return [this.chatHistoryKey];
97
+ }
98
+ // Will always return list of memory variables.
99
+ get memoryVariables() {
100
+ return [this.entitiesKey, this.chatHistoryKey];
101
+ }
102
+ // Return history buffer.
103
+ async loadMemoryVariables(inputs) {
104
+ const promptInputKey = this.inputKey ?? (0, base_js_1.getPromptInputKey)(inputs, this.memoryVariables);
105
+ const messages = await this.chatHistory.getMessages();
106
+ const serializedMessages = (0, base_js_1.getBufferString)(messages.slice(-this.k * 2), this.humanPrefix, this.aiPrefix);
107
+ const output = await this.entityExtractionChain.predict({
108
+ history: serializedMessages,
109
+ input: inputs[promptInputKey],
110
+ });
111
+ const entities = output.trim() === "NONE" ? [] : output.split(",").map((w) => w.trim());
112
+ const entitySummaries = {};
113
+ for (const entity of entities) {
114
+ entitySummaries[entity] = await this.entityStore.get(entity, "No current information known.");
115
+ }
116
+ this.entityCache = [...entities];
117
+ const buffer = this.returnMessages
118
+ ? messages.slice(-this.k * 2)
119
+ : serializedMessages;
120
+ return {
121
+ [this.chatHistoryKey]: buffer,
122
+ [this.entitiesKey]: entitySummaries,
123
+ };
124
+ }
125
+ // Save context from this conversation to buffer.
126
+ async saveContext(inputs, outputs) {
127
+ await super.saveContext(inputs, outputs);
128
+ const promptInputKey = this.inputKey ?? (0, base_js_1.getPromptInputKey)(inputs, this.memoryVariables);
129
+ const messages = await this.chatHistory.getMessages();
130
+ const serializedMessages = (0, base_js_1.getBufferString)(messages.slice(-this.k * 2), this.humanPrefix, this.aiPrefix);
131
+ const inputData = inputs[promptInputKey];
132
+ for (const entity of this.entityCache) {
133
+ const existingSummary = await this.entityStore.get(entity, "No current information known.");
134
+ const output = await this.entitySummarizationChain.predict({
135
+ summary: existingSummary,
136
+ entity,
137
+ history: serializedMessages,
138
+ input: inputData,
139
+ });
140
+ if (output.trim() !== "UNCHANGED") {
141
+ await this.entityStore.set(entity, output.trim());
142
+ }
143
+ }
144
+ }
145
+ // Clear memory contents.
146
+ async clear() {
147
+ await super.clear();
148
+ await this.entityStore.clear();
149
+ }
150
+ }
151
+ exports.EntityMemory = EntityMemory;
@@ -0,0 +1,35 @@
1
+ import { BaseLanguageModel } from "../base_language/index.js";
2
+ import { BaseEntityStore } from "../schema/index.js";
3
+ import { BaseChatMemory, BaseChatMemoryInput } from "./chat_memory.js";
4
+ import { InputValues, MemoryVariables, OutputValues } from "./base.js";
5
+ import { PromptTemplate } from "../prompts/prompt.js";
6
+ export interface EntityMemoryInput extends BaseChatMemoryInput {
7
+ llm: BaseLanguageModel;
8
+ humanPrefix?: string;
9
+ aiPrefix?: string;
10
+ entityExtractionPrompt?: PromptTemplate;
11
+ entitySummarizationPrompt?: PromptTemplate;
12
+ entityCache?: string[];
13
+ k?: number;
14
+ chatHistoryKey?: string;
15
+ entitiesKey?: string;
16
+ entityStore?: BaseEntityStore;
17
+ }
18
+ export declare class EntityMemory extends BaseChatMemory implements EntityMemoryInput {
19
+ private entityExtractionChain;
20
+ private entitySummarizationChain;
21
+ entityStore: BaseEntityStore;
22
+ entityCache: string[];
23
+ k: number;
24
+ chatHistoryKey: string;
25
+ llm: BaseLanguageModel;
26
+ entitiesKey: string;
27
+ humanPrefix?: string;
28
+ aiPrefix?: string;
29
+ constructor(fields: EntityMemoryInput);
30
+ get memoryKeys(): string[];
31
+ get memoryVariables(): string[];
32
+ loadMemoryVariables(inputs: InputValues): Promise<MemoryVariables>;
33
+ saveContext(inputs: InputValues, outputs: OutputValues): Promise<void>;
34
+ clear(): Promise<void>;
35
+ }
@@ -0,0 +1,147 @@
1
+ import { BaseChatMemory } from "./chat_memory.js";
2
+ import { ENTITY_EXTRACTION_PROMPT, ENTITY_SUMMARIZATION_PROMPT, } from "./prompt.js";
3
+ import { getBufferString, getPromptInputKey, } from "./base.js";
4
+ import { LLMChain } from "../chains/llm_chain.js";
5
+ import { InMemoryEntityStore } from "./stores/entity/in_memory.js";
6
+ // Entity extractor & summarizer to memory.
7
+ export class EntityMemory extends BaseChatMemory {
8
+ constructor(fields) {
9
+ super({
10
+ chatHistory: fields.chatHistory,
11
+ returnMessages: fields.returnMessages ?? false,
12
+ inputKey: fields.inputKey,
13
+ outputKey: fields.outputKey,
14
+ });
15
+ Object.defineProperty(this, "entityExtractionChain", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "entitySummarizationChain", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "entityStore", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "entityCache", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: []
38
+ });
39
+ Object.defineProperty(this, "k", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: 3
44
+ });
45
+ Object.defineProperty(this, "chatHistoryKey", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: "history"
50
+ });
51
+ Object.defineProperty(this, "llm", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, "entitiesKey", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: "entities"
62
+ });
63
+ Object.defineProperty(this, "humanPrefix", {
64
+ enumerable: true,
65
+ configurable: true,
66
+ writable: true,
67
+ value: void 0
68
+ });
69
+ Object.defineProperty(this, "aiPrefix", {
70
+ enumerable: true,
71
+ configurable: true,
72
+ writable: true,
73
+ value: void 0
74
+ });
75
+ this.llm = fields.llm;
76
+ this.humanPrefix = fields.humanPrefix;
77
+ this.aiPrefix = fields.aiPrefix;
78
+ this.chatHistoryKey = fields.chatHistoryKey ?? this.chatHistoryKey;
79
+ this.entitiesKey = fields.entitiesKey ?? this.entitiesKey;
80
+ this.entityExtractionChain = new LLMChain({
81
+ llm: this.llm,
82
+ prompt: fields.entityExtractionPrompt ?? ENTITY_EXTRACTION_PROMPT,
83
+ });
84
+ this.entitySummarizationChain = new LLMChain({
85
+ llm: this.llm,
86
+ prompt: fields.entitySummarizationPrompt ?? ENTITY_SUMMARIZATION_PROMPT,
87
+ });
88
+ this.entityStore = fields.entityStore ?? new InMemoryEntityStore();
89
+ this.entityCache = fields.entityCache ?? this.entityCache;
90
+ this.k = fields.k ?? this.k;
91
+ }
92
+ get memoryKeys() {
93
+ return [this.chatHistoryKey];
94
+ }
95
+ // Will always return list of memory variables.
96
+ get memoryVariables() {
97
+ return [this.entitiesKey, this.chatHistoryKey];
98
+ }
99
+ // Return history buffer.
100
+ async loadMemoryVariables(inputs) {
101
+ const promptInputKey = this.inputKey ?? getPromptInputKey(inputs, this.memoryVariables);
102
+ const messages = await this.chatHistory.getMessages();
103
+ const serializedMessages = getBufferString(messages.slice(-this.k * 2), this.humanPrefix, this.aiPrefix);
104
+ const output = await this.entityExtractionChain.predict({
105
+ history: serializedMessages,
106
+ input: inputs[promptInputKey],
107
+ });
108
+ const entities = output.trim() === "NONE" ? [] : output.split(",").map((w) => w.trim());
109
+ const entitySummaries = {};
110
+ for (const entity of entities) {
111
+ entitySummaries[entity] = await this.entityStore.get(entity, "No current information known.");
112
+ }
113
+ this.entityCache = [...entities];
114
+ const buffer = this.returnMessages
115
+ ? messages.slice(-this.k * 2)
116
+ : serializedMessages;
117
+ return {
118
+ [this.chatHistoryKey]: buffer,
119
+ [this.entitiesKey]: entitySummaries,
120
+ };
121
+ }
122
+ // Save context from this conversation to buffer.
123
+ async saveContext(inputs, outputs) {
124
+ await super.saveContext(inputs, outputs);
125
+ const promptInputKey = this.inputKey ?? getPromptInputKey(inputs, this.memoryVariables);
126
+ const messages = await this.chatHistory.getMessages();
127
+ const serializedMessages = getBufferString(messages.slice(-this.k * 2), this.humanPrefix, this.aiPrefix);
128
+ const inputData = inputs[promptInputKey];
129
+ for (const entity of this.entityCache) {
130
+ const existingSummary = await this.entityStore.get(entity, "No current information known.");
131
+ const output = await this.entitySummarizationChain.predict({
132
+ summary: existingSummary,
133
+ entity,
134
+ history: serializedMessages,
135
+ input: inputData,
136
+ });
137
+ if (output.trim() !== "UNCHANGED") {
138
+ await this.entityStore.set(entity, output.trim());
139
+ }
140
+ }
141
+ }
142
+ // Clear memory contents.
143
+ async clear() {
144
+ await super.clear();
145
+ await this.entityStore.clear();
146
+ }
147
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VectorStoreRetrieverMemory = exports.MotorheadMemory = exports.ChatMessageHistory = exports.BaseChatMemory = exports.BufferWindowMemory = exports.ConversationSummaryMemory = exports.getBufferString = exports.getInputValue = exports.BaseMemory = exports.BufferMemory = void 0;
3
+ exports.ENTITY_MEMORY_CONVERSATION_TEMPLATE = exports.EntityMemory = exports.VectorStoreRetrieverMemory = exports.MotorheadMemory = exports.ChatMessageHistory = exports.BaseChatMemory = exports.BufferWindowMemory = exports.ConversationSummaryMemory = exports.getBufferString = exports.getInputValue = exports.BaseMemory = exports.BufferMemory = void 0;
4
4
  var buffer_memory_js_1 = require("./buffer_memory.cjs");
5
5
  Object.defineProperty(exports, "BufferMemory", { enumerable: true, get: function () { return buffer_memory_js_1.BufferMemory; } });
6
6
  var base_js_1 = require("./base.cjs");
@@ -19,3 +19,7 @@ var motorhead_memory_js_1 = require("./motorhead_memory.cjs");
19
19
  Object.defineProperty(exports, "MotorheadMemory", { enumerable: true, get: function () { return motorhead_memory_js_1.MotorheadMemory; } });
20
20
  var vector_store_js_1 = require("./vector_store.cjs");
21
21
  Object.defineProperty(exports, "VectorStoreRetrieverMemory", { enumerable: true, get: function () { return vector_store_js_1.VectorStoreRetrieverMemory; } });
22
+ var entity_memory_js_1 = require("./entity_memory.cjs");
23
+ Object.defineProperty(exports, "EntityMemory", { enumerable: true, get: function () { return entity_memory_js_1.EntityMemory; } });
24
+ var prompt_js_1 = require("./prompt.cjs");
25
+ Object.defineProperty(exports, "ENTITY_MEMORY_CONVERSATION_TEMPLATE", { enumerable: true, get: function () { return prompt_js_1.ENTITY_MEMORY_CONVERSATION_TEMPLATE; } });
@@ -6,3 +6,5 @@ export { BaseChatMemory, BaseChatMemoryInput } from "./chat_memory.js";
6
6
  export { ChatMessageHistory } from "../stores/message/in_memory.js";
7
7
  export { MotorheadMemory, MotorheadMemoryInput } from "./motorhead_memory.js";
8
8
  export { VectorStoreRetrieverMemory, VectorStoreRetrieverMemoryParams, } from "./vector_store.js";
9
+ export { EntityMemory } from "./entity_memory.js";
10
+ export { ENTITY_MEMORY_CONVERSATION_TEMPLATE } from "./prompt.js";
@@ -6,3 +6,5 @@ export { BaseChatMemory } from "./chat_memory.js";
6
6
  export { ChatMessageHistory } from "../stores/message/in_memory.js";
7
7
  export { MotorheadMemory } from "./motorhead_memory.js";
8
8
  export { VectorStoreRetrieverMemory, } from "./vector_store.js";
9
+ export { EntityMemory } from "./entity_memory.js";
10
+ export { ENTITY_MEMORY_CONVERSATION_TEMPLATE } from "./prompt.js";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SUMMARY_PROMPT = void 0;
3
+ exports.ENTITY_SUMMARIZATION_PROMPT = exports._DEFAULT_ENTITY_SUMMARIZATION_TEMPLATE = exports.ENTITY_EXTRACTION_PROMPT = exports._DEFAULT_ENTITY_EXTRACTION_TEMPLATE = exports.ENTITY_MEMORY_CONVERSATION_TEMPLATE = exports._DEFAULT_ENTITY_MEMORY_CONVERSATION_TEMPLATE = exports.SUMMARY_PROMPT = void 0;
4
4
  const prompt_js_1 = require("../prompts/prompt.cjs");
5
5
  const _DEFAULT_SUMMARIZER_TEMPLATE = `Progressively summarize the lines of conversation provided, adding onto the previous summary returning a new summary.
6
6
 
@@ -28,3 +28,86 @@ exports.SUMMARY_PROMPT = new prompt_js_1.PromptTemplate({
28
28
  inputVariables: ["summary", "new_lines"],
29
29
  template: _DEFAULT_SUMMARIZER_TEMPLATE,
30
30
  });
31
+ exports._DEFAULT_ENTITY_MEMORY_CONVERSATION_TEMPLATE = `You are an assistant to a human, powered by a large language model trained by OpenAI.
32
+
33
+ You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
34
+
35
+ You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
36
+
37
+ Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
38
+
39
+ Context:
40
+ {entities}
41
+
42
+ Current conversation:
43
+ {history}
44
+ Last line:
45
+ Human: {input}
46
+ You:`;
47
+ exports.ENTITY_MEMORY_CONVERSATION_TEMPLATE =
48
+ // eslint-disable-next-line spaced-comment
49
+ /*#__PURE__*/ new prompt_js_1.PromptTemplate({
50
+ inputVariables: ["entities", "history", "input"],
51
+ template: exports._DEFAULT_ENTITY_MEMORY_CONVERSATION_TEMPLATE,
52
+ });
53
+ exports._DEFAULT_ENTITY_EXTRACTION_TEMPLATE = `You are an AI assistant reading the transcript of a conversation between an AI and a human. Extract all of the proper nouns from the last line of conversation. As a guideline, a proper noun is generally capitalized. You should definitely extract all names and places.
54
+
55
+ The conversation history is provided just in case of a coreference (e.g. "What do you know about him" where "him" is defined in a previous line) -- ignore items mentioned there that are not in the last line.
56
+
57
+ Return the output as a single comma-separated list, or NONE if there is nothing of note to return (e.g. the user is just issuing a greeting or having a simple conversation).
58
+
59
+ EXAMPLE
60
+ Conversation history:
61
+ Person #1: my name is Jacob. how's it going today?
62
+ AI: "It's going great! How about you?"
63
+ Person #1: good! busy working on Langchain. lots to do.
64
+ AI: "That sounds like a lot of work! What kind of things are you doing to make Langchain better?"
65
+ Last line:
66
+ Person #1: i'm trying to improve Langchain's interfaces, the UX, its integrations with various products the user might want ... a lot of stuff.
67
+ Output: Jacob,Langchain
68
+ END OF EXAMPLE
69
+
70
+ EXAMPLE
71
+ Conversation history:
72
+ Person #1: how's it going today?
73
+ AI: "It's going great! How about you?"
74
+ Person #1: good! busy working on Langchain. lots to do.
75
+ AI: "That sounds like a lot of work! What kind of things are you doing to make Langchain better?"
76
+ Last line:
77
+ Person #1: i'm trying to improve Langchain's interfaces, the UX, its integrations with various products the user might want ... a lot of stuff. I'm working with Person #2.
78
+ Output: Langchain, Person #2
79
+ END OF EXAMPLE
80
+
81
+ Conversation history (for reference only):
82
+ {history}
83
+ Last line of conversation (for extraction):
84
+ Human: {input}
85
+
86
+ Output:`;
87
+ // eslint-disable-next-line spaced-comment
88
+ exports.ENTITY_EXTRACTION_PROMPT = new prompt_js_1.PromptTemplate({
89
+ inputVariables: ["history", "input"],
90
+ template: exports._DEFAULT_ENTITY_EXTRACTION_TEMPLATE,
91
+ });
92
+ exports._DEFAULT_ENTITY_SUMMARIZATION_TEMPLATE = `You are an AI assistant helping a human keep track of facts about relevant people, places, and concepts in their life. Update and add to the summary of the provided entity in the "Entity" section based on the last line of your conversation with the human. If you are writing the summary for the first time, return a single sentence.
93
+ The update should only include facts that are relayed in the last line of conversation about the provided entity, and should only contain facts about the provided entity.
94
+
95
+ If there is no new information about the provided entity or the information is not worth noting (not an important or relevant fact to remember long-term), output the exact string "UNCHANGED" below.
96
+
97
+ Full conversation history (for context):
98
+ {history}
99
+
100
+ Entity to summarize:
101
+ {entity}
102
+
103
+ Existing summary of {entity}:
104
+ {summary}
105
+
106
+ Last line of conversation:
107
+ Human: {input}
108
+ Updated summary (or the exact string "UNCHANGED" if there is no new information about {entity} above):`;
109
+ // eslint-disable-next-line spaced-comment
110
+ exports.ENTITY_SUMMARIZATION_PROMPT = new prompt_js_1.PromptTemplate({
111
+ inputVariables: ["entity", "summary", "history", "input"],
112
+ template: exports._DEFAULT_ENTITY_SUMMARIZATION_TEMPLATE,
113
+ });
@@ -1,2 +1,8 @@
1
1
  import { PromptTemplate } from "../prompts/prompt.js";
2
2
  export declare const SUMMARY_PROMPT: PromptTemplate;
3
+ export declare const _DEFAULT_ENTITY_MEMORY_CONVERSATION_TEMPLATE = "You are an assistant to a human, powered by a large language model trained by OpenAI.\n\nYou are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nYou are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.\n\nContext:\n{entities}\n\nCurrent conversation:\n{history}\nLast line:\nHuman: {input}\nYou:";
4
+ export declare const ENTITY_MEMORY_CONVERSATION_TEMPLATE: PromptTemplate;
5
+ export declare const _DEFAULT_ENTITY_EXTRACTION_TEMPLATE = "You are an AI assistant reading the transcript of a conversation between an AI and a human. Extract all of the proper nouns from the last line of conversation. As a guideline, a proper noun is generally capitalized. You should definitely extract all names and places.\n\nThe conversation history is provided just in case of a coreference (e.g. \"What do you know about him\" where \"him\" is defined in a previous line) -- ignore items mentioned there that are not in the last line.\n\nReturn the output as a single comma-separated list, or NONE if there is nothing of note to return (e.g. the user is just issuing a greeting or having a simple conversation).\n\nEXAMPLE\nConversation history:\nPerson #1: my name is Jacob. how's it going today?\nAI: \"It's going great! How about you?\"\nPerson #1: good! busy working on Langchain. lots to do.\nAI: \"That sounds like a lot of work! What kind of things are you doing to make Langchain better?\"\nLast line:\nPerson #1: i'm trying to improve Langchain's interfaces, the UX, its integrations with various products the user might want ... a lot of stuff.\nOutput: Jacob,Langchain\nEND OF EXAMPLE\n\nEXAMPLE\nConversation history:\nPerson #1: how's it going today?\nAI: \"It's going great! How about you?\"\nPerson #1: good! busy working on Langchain. lots to do.\nAI: \"That sounds like a lot of work! What kind of things are you doing to make Langchain better?\"\nLast line:\nPerson #1: i'm trying to improve Langchain's interfaces, the UX, its integrations with various products the user might want ... a lot of stuff. I'm working with Person #2.\nOutput: Langchain, Person #2\nEND OF EXAMPLE\n\nConversation history (for reference only):\n{history}\nLast line of conversation (for extraction):\nHuman: {input}\n\nOutput:";
6
+ export declare const ENTITY_EXTRACTION_PROMPT: PromptTemplate;
7
+ export declare const _DEFAULT_ENTITY_SUMMARIZATION_TEMPLATE = "You are an AI assistant helping a human keep track of facts about relevant people, places, and concepts in their life. Update and add to the summary of the provided entity in the \"Entity\" section based on the last line of your conversation with the human. If you are writing the summary for the first time, return a single sentence.\nThe update should only include facts that are relayed in the last line of conversation about the provided entity, and should only contain facts about the provided entity.\n\nIf there is no new information about the provided entity or the information is not worth noting (not an important or relevant fact to remember long-term), output the exact string \"UNCHANGED\" below.\n\nFull conversation history (for context):\n{history}\n\nEntity to summarize:\n{entity}\n\nExisting summary of {entity}:\n{summary}\n\nLast line of conversation:\nHuman: {input}\nUpdated summary (or the exact string \"UNCHANGED\" if there is no new information about {entity} above):";
8
+ export declare const ENTITY_SUMMARIZATION_PROMPT: PromptTemplate;
@@ -25,3 +25,86 @@ export const SUMMARY_PROMPT = /*#__PURE__*/ new PromptTemplate({
25
25
  inputVariables: ["summary", "new_lines"],
26
26
  template: _DEFAULT_SUMMARIZER_TEMPLATE,
27
27
  });
28
+ export const _DEFAULT_ENTITY_MEMORY_CONVERSATION_TEMPLATE = `You are an assistant to a human, powered by a large language model trained by OpenAI.
29
+
30
+ You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
31
+
32
+ You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
33
+
34
+ Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
35
+
36
+ Context:
37
+ {entities}
38
+
39
+ Current conversation:
40
+ {history}
41
+ Last line:
42
+ Human: {input}
43
+ You:`;
44
+ export const ENTITY_MEMORY_CONVERSATION_TEMPLATE =
45
+ // eslint-disable-next-line spaced-comment
46
+ /*#__PURE__*/ new PromptTemplate({
47
+ inputVariables: ["entities", "history", "input"],
48
+ template: _DEFAULT_ENTITY_MEMORY_CONVERSATION_TEMPLATE,
49
+ });
50
+ export const _DEFAULT_ENTITY_EXTRACTION_TEMPLATE = `You are an AI assistant reading the transcript of a conversation between an AI and a human. Extract all of the proper nouns from the last line of conversation. As a guideline, a proper noun is generally capitalized. You should definitely extract all names and places.
51
+
52
+ The conversation history is provided just in case of a coreference (e.g. "What do you know about him" where "him" is defined in a previous line) -- ignore items mentioned there that are not in the last line.
53
+
54
+ Return the output as a single comma-separated list, or NONE if there is nothing of note to return (e.g. the user is just issuing a greeting or having a simple conversation).
55
+
56
+ EXAMPLE
57
+ Conversation history:
58
+ Person #1: my name is Jacob. how's it going today?
59
+ AI: "It's going great! How about you?"
60
+ Person #1: good! busy working on Langchain. lots to do.
61
+ AI: "That sounds like a lot of work! What kind of things are you doing to make Langchain better?"
62
+ Last line:
63
+ Person #1: i'm trying to improve Langchain's interfaces, the UX, its integrations with various products the user might want ... a lot of stuff.
64
+ Output: Jacob,Langchain
65
+ END OF EXAMPLE
66
+
67
+ EXAMPLE
68
+ Conversation history:
69
+ Person #1: how's it going today?
70
+ AI: "It's going great! How about you?"
71
+ Person #1: good! busy working on Langchain. lots to do.
72
+ AI: "That sounds like a lot of work! What kind of things are you doing to make Langchain better?"
73
+ Last line:
74
+ Person #1: i'm trying to improve Langchain's interfaces, the UX, its integrations with various products the user might want ... a lot of stuff. I'm working with Person #2.
75
+ Output: Langchain, Person #2
76
+ END OF EXAMPLE
77
+
78
+ Conversation history (for reference only):
79
+ {history}
80
+ Last line of conversation (for extraction):
81
+ Human: {input}
82
+
83
+ Output:`;
84
+ // eslint-disable-next-line spaced-comment
85
+ export const ENTITY_EXTRACTION_PROMPT = /*#__PURE__*/ new PromptTemplate({
86
+ inputVariables: ["history", "input"],
87
+ template: _DEFAULT_ENTITY_EXTRACTION_TEMPLATE,
88
+ });
89
+ export const _DEFAULT_ENTITY_SUMMARIZATION_TEMPLATE = `You are an AI assistant helping a human keep track of facts about relevant people, places, and concepts in their life. Update and add to the summary of the provided entity in the "Entity" section based on the last line of your conversation with the human. If you are writing the summary for the first time, return a single sentence.
90
+ The update should only include facts that are relayed in the last line of conversation about the provided entity, and should only contain facts about the provided entity.
91
+
92
+ If there is no new information about the provided entity or the information is not worth noting (not an important or relevant fact to remember long-term), output the exact string "UNCHANGED" below.
93
+
94
+ Full conversation history (for context):
95
+ {history}
96
+
97
+ Entity to summarize:
98
+ {entity}
99
+
100
+ Existing summary of {entity}:
101
+ {summary}
102
+
103
+ Last line of conversation:
104
+ Human: {input}
105
+ Updated summary (or the exact string "UNCHANGED" if there is no new information about {entity} above):`;
106
+ // eslint-disable-next-line spaced-comment
107
+ export const ENTITY_SUMMARIZATION_PROMPT = /*#__PURE__*/ new PromptTemplate({
108
+ inputVariables: ["entity", "summary", "history", "input"],
109
+ template: _DEFAULT_ENTITY_SUMMARIZATION_TEMPLATE,
110
+ });
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryEntityStore = void 0;
4
+ const index_js_1 = require("../../../schema/index.cjs");
5
+ class InMemoryEntityStore extends index_js_1.BaseEntityStore {
6
+ constructor() {
7
+ super();
8
+ Object.defineProperty(this, "store", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: void 0
13
+ });
14
+ this.store = Object.create(null);
15
+ }
16
+ async get(key, defaultValue) {
17
+ return key in this.store ? this.store[key] : defaultValue;
18
+ }
19
+ async set(key, value) {
20
+ this.store[key] = value;
21
+ }
22
+ async delete(key) {
23
+ delete this.store[key];
24
+ }
25
+ async exists(key) {
26
+ return key in this.store;
27
+ }
28
+ async clear() {
29
+ this.store = Object.create(null);
30
+ }
31
+ }
32
+ exports.InMemoryEntityStore = InMemoryEntityStore;
@@ -0,0 +1,10 @@
1
+ import { BaseEntityStore } from "../../../schema/index.js";
2
+ export declare class InMemoryEntityStore extends BaseEntityStore {
3
+ private store;
4
+ constructor();
5
+ get(key: string, defaultValue: string | undefined): Promise<string | undefined>;
6
+ set(key: string, value: string | undefined): Promise<void>;
7
+ delete(key: string): Promise<void>;
8
+ exists(key: string): Promise<boolean>;
9
+ clear(): Promise<void>;
10
+ }
@@ -0,0 +1,28 @@
1
+ import { BaseEntityStore } from "../../../schema/index.js";
2
+ export class InMemoryEntityStore extends BaseEntityStore {
3
+ constructor() {
4
+ super();
5
+ Object.defineProperty(this, "store", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: void 0
10
+ });
11
+ this.store = Object.create(null);
12
+ }
13
+ async get(key, defaultValue) {
14
+ return key in this.store ? this.store[key] : defaultValue;
15
+ }
16
+ async set(key, value) {
17
+ this.store[key] = value;
18
+ }
19
+ async delete(key) {
20
+ delete this.store[key];
21
+ }
22
+ async exists(key) {
23
+ return key in this.store;
24
+ }
25
+ async clear() {
26
+ this.store = Object.create(null);
27
+ }
28
+ }