memories-lite 0.9.0
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.
- package/MEMORIES.md +39 -0
- package/README.md +221 -0
- package/TECHNICAL.md +135 -0
- package/dist/config/defaults.d.ts +2 -0
- package/dist/config/defaults.js +61 -0
- package/dist/config/manager.d.ts +4 -0
- package/dist/config/manager.js +121 -0
- package/dist/embeddings/base.d.ts +4 -0
- package/dist/embeddings/base.js +2 -0
- package/dist/embeddings/google.d.ts +10 -0
- package/dist/embeddings/google.js +28 -0
- package/dist/embeddings/openai.d.ts +10 -0
- package/dist/embeddings/openai.js +31 -0
- package/dist/graphs/configs.d.ts +14 -0
- package/dist/graphs/configs.js +19 -0
- package/dist/graphs/tools.d.ts +271 -0
- package/dist/graphs/tools.js +220 -0
- package/dist/graphs/utils.d.ts +9 -0
- package/dist/graphs/utils.js +105 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +30 -0
- package/dist/llms/base.d.ts +16 -0
- package/dist/llms/base.js +2 -0
- package/dist/llms/google.d.ts +11 -0
- package/dist/llms/google.js +44 -0
- package/dist/llms/openai.d.ts +9 -0
- package/dist/llms/openai.js +73 -0
- package/dist/llms/openai_structured.d.ts +11 -0
- package/dist/llms/openai_structured.js +72 -0
- package/dist/memory/index.d.ts +42 -0
- package/dist/memory/index.js +499 -0
- package/dist/memory/memory.types.d.ts +23 -0
- package/dist/memory/memory.types.js +2 -0
- package/dist/prompts/index.d.ts +102 -0
- package/dist/prompts/index.js +233 -0
- package/dist/storage/DummyHistoryManager.d.ts +7 -0
- package/dist/storage/DummyHistoryManager.js +19 -0
- package/dist/storage/MemoryHistoryManager.d.ts +8 -0
- package/dist/storage/MemoryHistoryManager.js +36 -0
- package/dist/storage/base.d.ts +6 -0
- package/dist/storage/base.js +2 -0
- package/dist/storage/index.d.ts +3 -0
- package/dist/storage/index.js +19 -0
- package/dist/types/index.d.ts +1071 -0
- package/dist/types/index.js +100 -0
- package/dist/utils/bm25.d.ts +13 -0
- package/dist/utils/bm25.js +51 -0
- package/dist/utils/factory.d.ts +13 -0
- package/dist/utils/factory.js +49 -0
- package/dist/utils/logger.d.ts +7 -0
- package/dist/utils/logger.js +9 -0
- package/dist/utils/memory.d.ts +3 -0
- package/dist/utils/memory.js +44 -0
- package/dist/utils/telemetry.d.ts +11 -0
- package/dist/utils/telemetry.js +74 -0
- package/dist/utils/telemetry.types.d.ts +27 -0
- package/dist/utils/telemetry.types.js +2 -0
- package/dist/vectorstores/base.d.ts +11 -0
- package/dist/vectorstores/base.js +2 -0
- package/dist/vectorstores/lite.d.ts +40 -0
- package/dist/vectorstores/lite.js +319 -0
- package/dist/vectorstores/llm.d.ts +31 -0
- package/dist/vectorstores/llm.js +88 -0
- package/jest.config.js +22 -0
- package/memories-lite.db +0 -0
- package/package.json +38 -0
- package/src/config/defaults.ts +61 -0
- package/src/config/manager.ts +132 -0
- package/src/embeddings/base.ts +4 -0
- package/src/embeddings/google.ts +32 -0
- package/src/embeddings/openai.ts +33 -0
- package/src/graphs/configs.ts +30 -0
- package/src/graphs/tools.ts +267 -0
- package/src/graphs/utils.ts +114 -0
- package/src/index.ts +14 -0
- package/src/llms/base.ts +20 -0
- package/src/llms/google.ts +56 -0
- package/src/llms/openai.ts +85 -0
- package/src/llms/openai_structured.ts +82 -0
- package/src/memory/index.ts +723 -0
- package/src/memory/memory.types.ts +27 -0
- package/src/prompts/index.ts +268 -0
- package/src/storage/DummyHistoryManager.ts +27 -0
- package/src/storage/MemoryHistoryManager.ts +58 -0
- package/src/storage/base.ts +14 -0
- package/src/storage/index.ts +3 -0
- package/src/types/index.ts +243 -0
- package/src/utils/bm25.ts +64 -0
- package/src/utils/factory.ts +59 -0
- package/src/utils/logger.ts +13 -0
- package/src/utils/memory.ts +48 -0
- package/src/utils/telemetry.ts +98 -0
- package/src/utils/telemetry.types.ts +34 -0
- package/src/vectorstores/base.ts +27 -0
- package/src/vectorstores/lite.ts +402 -0
- package/src/vectorstores/llm.ts +126 -0
- package/tests/lite.spec.ts +158 -0
- package/tests/memory.facts.test.ts +211 -0
- package/tests/memory.test.ts +406 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.OpenAIEmbedder = void 0;
|
|
7
|
+
const openai_1 = __importDefault(require("openai"));
|
|
8
|
+
class OpenAIEmbedder {
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.openai = new openai_1.default({ apiKey: config.apiKey });
|
|
11
|
+
this.model = config.model || "text-embedding-3-small";
|
|
12
|
+
this.dimension = config.dimension || 1536;
|
|
13
|
+
}
|
|
14
|
+
async embed(text) {
|
|
15
|
+
const response = await this.openai.embeddings.create({
|
|
16
|
+
model: this.model,
|
|
17
|
+
input: text,
|
|
18
|
+
dimensions: this.dimension,
|
|
19
|
+
});
|
|
20
|
+
return response.data[0].embedding;
|
|
21
|
+
}
|
|
22
|
+
async embedBatch(texts) {
|
|
23
|
+
const response = await this.openai.embeddings.create({
|
|
24
|
+
model: this.model,
|
|
25
|
+
input: texts,
|
|
26
|
+
dimensions: this.dimension,
|
|
27
|
+
});
|
|
28
|
+
return response.data.map((item) => item.embedding);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.OpenAIEmbedder = OpenAIEmbedder;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { LLMConfig } from "../types";
|
|
2
|
+
export interface Neo4jConfig {
|
|
3
|
+
url: string | null;
|
|
4
|
+
username: string | null;
|
|
5
|
+
password: string | null;
|
|
6
|
+
}
|
|
7
|
+
export interface GraphStoreConfig {
|
|
8
|
+
provider: string;
|
|
9
|
+
config: Neo4jConfig;
|
|
10
|
+
llm?: LLMConfig;
|
|
11
|
+
customPrompt?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function validateNeo4jConfig(config: Neo4jConfig): void;
|
|
14
|
+
export declare function validateGraphStoreConfig(config: GraphStoreConfig): void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateNeo4jConfig = validateNeo4jConfig;
|
|
4
|
+
exports.validateGraphStoreConfig = validateGraphStoreConfig;
|
|
5
|
+
function validateNeo4jConfig(config) {
|
|
6
|
+
const { url, username, password } = config;
|
|
7
|
+
if (!url || !username || !password) {
|
|
8
|
+
throw new Error("Please provide 'url', 'username' and 'password'.");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function validateGraphStoreConfig(config) {
|
|
12
|
+
const { provider } = config;
|
|
13
|
+
if (provider === "neo4j") {
|
|
14
|
+
validateNeo4jConfig(config.config);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
throw new Error(`Unsupported graph store provider: ${provider}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export interface GraphToolParameters {
|
|
3
|
+
source: string;
|
|
4
|
+
destination: string;
|
|
5
|
+
relationship: string;
|
|
6
|
+
source_type?: string;
|
|
7
|
+
destination_type?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface GraphEntitiesParameters {
|
|
10
|
+
entities: Array<{
|
|
11
|
+
entity: string;
|
|
12
|
+
entity_type: string;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
15
|
+
export interface GraphRelationsParameters {
|
|
16
|
+
entities: Array<{
|
|
17
|
+
source: string;
|
|
18
|
+
relationship: string;
|
|
19
|
+
destination: string;
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
22
|
+
export declare const GraphSimpleRelationshipArgsSchema: z.ZodObject<{
|
|
23
|
+
source: z.ZodString;
|
|
24
|
+
relationship: z.ZodString;
|
|
25
|
+
destination: z.ZodString;
|
|
26
|
+
}, "strip", z.ZodTypeAny, {
|
|
27
|
+
source: string;
|
|
28
|
+
relationship: string;
|
|
29
|
+
destination: string;
|
|
30
|
+
}, {
|
|
31
|
+
source: string;
|
|
32
|
+
relationship: string;
|
|
33
|
+
destination: string;
|
|
34
|
+
}>;
|
|
35
|
+
export declare const GraphAddRelationshipArgsSchema: z.ZodObject<{
|
|
36
|
+
source: z.ZodString;
|
|
37
|
+
relationship: z.ZodString;
|
|
38
|
+
destination: z.ZodString;
|
|
39
|
+
} & {
|
|
40
|
+
source_type: z.ZodString;
|
|
41
|
+
destination_type: z.ZodString;
|
|
42
|
+
}, "strip", z.ZodTypeAny, {
|
|
43
|
+
source: string;
|
|
44
|
+
relationship: string;
|
|
45
|
+
destination: string;
|
|
46
|
+
source_type: string;
|
|
47
|
+
destination_type: string;
|
|
48
|
+
}, {
|
|
49
|
+
source: string;
|
|
50
|
+
relationship: string;
|
|
51
|
+
destination: string;
|
|
52
|
+
source_type: string;
|
|
53
|
+
destination_type: string;
|
|
54
|
+
}>;
|
|
55
|
+
export declare const GraphExtractEntitiesArgsSchema: z.ZodObject<{
|
|
56
|
+
entities: z.ZodArray<z.ZodObject<{
|
|
57
|
+
entity: z.ZodString;
|
|
58
|
+
entity_type: z.ZodString;
|
|
59
|
+
}, "strip", z.ZodTypeAny, {
|
|
60
|
+
entity: string;
|
|
61
|
+
entity_type: string;
|
|
62
|
+
}, {
|
|
63
|
+
entity: string;
|
|
64
|
+
entity_type: string;
|
|
65
|
+
}>, "many">;
|
|
66
|
+
}, "strip", z.ZodTypeAny, {
|
|
67
|
+
entities: {
|
|
68
|
+
entity: string;
|
|
69
|
+
entity_type: string;
|
|
70
|
+
}[];
|
|
71
|
+
}, {
|
|
72
|
+
entities: {
|
|
73
|
+
entity: string;
|
|
74
|
+
entity_type: string;
|
|
75
|
+
}[];
|
|
76
|
+
}>;
|
|
77
|
+
export declare const GraphRelationsArgsSchema: z.ZodObject<{
|
|
78
|
+
entities: z.ZodArray<z.ZodObject<{
|
|
79
|
+
source: z.ZodString;
|
|
80
|
+
relationship: z.ZodString;
|
|
81
|
+
destination: z.ZodString;
|
|
82
|
+
}, "strip", z.ZodTypeAny, {
|
|
83
|
+
source: string;
|
|
84
|
+
relationship: string;
|
|
85
|
+
destination: string;
|
|
86
|
+
}, {
|
|
87
|
+
source: string;
|
|
88
|
+
relationship: string;
|
|
89
|
+
destination: string;
|
|
90
|
+
}>, "many">;
|
|
91
|
+
}, "strip", z.ZodTypeAny, {
|
|
92
|
+
entities: {
|
|
93
|
+
source: string;
|
|
94
|
+
relationship: string;
|
|
95
|
+
destination: string;
|
|
96
|
+
}[];
|
|
97
|
+
}, {
|
|
98
|
+
entities: {
|
|
99
|
+
source: string;
|
|
100
|
+
relationship: string;
|
|
101
|
+
destination: string;
|
|
102
|
+
}[];
|
|
103
|
+
}>;
|
|
104
|
+
export declare const UPDATE_MEMORY_TOOL_GRAPH: {
|
|
105
|
+
type: string;
|
|
106
|
+
function: {
|
|
107
|
+
name: string;
|
|
108
|
+
description: string;
|
|
109
|
+
parameters: {
|
|
110
|
+
type: string;
|
|
111
|
+
properties: {
|
|
112
|
+
source: {
|
|
113
|
+
type: string;
|
|
114
|
+
description: string;
|
|
115
|
+
};
|
|
116
|
+
destination: {
|
|
117
|
+
type: string;
|
|
118
|
+
description: string;
|
|
119
|
+
};
|
|
120
|
+
relationship: {
|
|
121
|
+
type: string;
|
|
122
|
+
description: string;
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
required: string[];
|
|
126
|
+
additionalProperties: boolean;
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
export declare const ADD_MEMORY_TOOL_GRAPH: {
|
|
131
|
+
type: string;
|
|
132
|
+
function: {
|
|
133
|
+
name: string;
|
|
134
|
+
description: string;
|
|
135
|
+
parameters: {
|
|
136
|
+
type: string;
|
|
137
|
+
properties: {
|
|
138
|
+
source: {
|
|
139
|
+
type: string;
|
|
140
|
+
description: string;
|
|
141
|
+
};
|
|
142
|
+
destination: {
|
|
143
|
+
type: string;
|
|
144
|
+
description: string;
|
|
145
|
+
};
|
|
146
|
+
relationship: {
|
|
147
|
+
type: string;
|
|
148
|
+
description: string;
|
|
149
|
+
};
|
|
150
|
+
source_type: {
|
|
151
|
+
type: string;
|
|
152
|
+
description: string;
|
|
153
|
+
};
|
|
154
|
+
destination_type: {
|
|
155
|
+
type: string;
|
|
156
|
+
description: string;
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
required: string[];
|
|
160
|
+
additionalProperties: boolean;
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
export declare const NOOP_TOOL: {
|
|
165
|
+
type: string;
|
|
166
|
+
function: {
|
|
167
|
+
name: string;
|
|
168
|
+
description: string;
|
|
169
|
+
parameters: {
|
|
170
|
+
type: string;
|
|
171
|
+
properties: {};
|
|
172
|
+
required: never[];
|
|
173
|
+
additionalProperties: boolean;
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
export declare const RELATIONS_TOOL: {
|
|
178
|
+
type: string;
|
|
179
|
+
function: {
|
|
180
|
+
name: string;
|
|
181
|
+
description: string;
|
|
182
|
+
parameters: {
|
|
183
|
+
type: string;
|
|
184
|
+
properties: {
|
|
185
|
+
entities: {
|
|
186
|
+
type: string;
|
|
187
|
+
items: {
|
|
188
|
+
type: string;
|
|
189
|
+
properties: {
|
|
190
|
+
source: {
|
|
191
|
+
type: string;
|
|
192
|
+
description: string;
|
|
193
|
+
};
|
|
194
|
+
relationship: {
|
|
195
|
+
type: string;
|
|
196
|
+
description: string;
|
|
197
|
+
};
|
|
198
|
+
destination: {
|
|
199
|
+
type: string;
|
|
200
|
+
description: string;
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
required: string[];
|
|
204
|
+
additionalProperties: boolean;
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
required: string[];
|
|
209
|
+
additionalProperties: boolean;
|
|
210
|
+
};
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
export declare const EXTRACT_ENTITIES_TOOL: {
|
|
214
|
+
type: string;
|
|
215
|
+
function: {
|
|
216
|
+
name: string;
|
|
217
|
+
description: string;
|
|
218
|
+
parameters: {
|
|
219
|
+
type: string;
|
|
220
|
+
properties: {
|
|
221
|
+
entities: {
|
|
222
|
+
type: string;
|
|
223
|
+
items: {
|
|
224
|
+
type: string;
|
|
225
|
+
properties: {
|
|
226
|
+
entity: {
|
|
227
|
+
type: string;
|
|
228
|
+
description: string;
|
|
229
|
+
};
|
|
230
|
+
entity_type: {
|
|
231
|
+
type: string;
|
|
232
|
+
description: string;
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
required: string[];
|
|
236
|
+
additionalProperties: boolean;
|
|
237
|
+
};
|
|
238
|
+
description: string;
|
|
239
|
+
};
|
|
240
|
+
};
|
|
241
|
+
required: string[];
|
|
242
|
+
additionalProperties: boolean;
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
};
|
|
246
|
+
export declare const DELETE_MEMORY_TOOL_GRAPH: {
|
|
247
|
+
type: string;
|
|
248
|
+
function: {
|
|
249
|
+
name: string;
|
|
250
|
+
description: string;
|
|
251
|
+
parameters: {
|
|
252
|
+
type: string;
|
|
253
|
+
properties: {
|
|
254
|
+
source: {
|
|
255
|
+
type: string;
|
|
256
|
+
description: string;
|
|
257
|
+
};
|
|
258
|
+
relationship: {
|
|
259
|
+
type: string;
|
|
260
|
+
description: string;
|
|
261
|
+
};
|
|
262
|
+
destination: {
|
|
263
|
+
type: string;
|
|
264
|
+
description: string;
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
required: string[];
|
|
268
|
+
additionalProperties: boolean;
|
|
269
|
+
};
|
|
270
|
+
};
|
|
271
|
+
};
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DELETE_MEMORY_TOOL_GRAPH = exports.EXTRACT_ENTITIES_TOOL = exports.RELATIONS_TOOL = exports.NOOP_TOOL = exports.ADD_MEMORY_TOOL_GRAPH = exports.UPDATE_MEMORY_TOOL_GRAPH = exports.GraphRelationsArgsSchema = exports.GraphExtractEntitiesArgsSchema = exports.GraphAddRelationshipArgsSchema = exports.GraphSimpleRelationshipArgsSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
// --- Zod Schemas for Tool Arguments ---
|
|
6
|
+
// Schema for simple relationship arguments (Update, Delete)
|
|
7
|
+
exports.GraphSimpleRelationshipArgsSchema = zod_1.z.object({
|
|
8
|
+
source: zod_1.z
|
|
9
|
+
.string()
|
|
10
|
+
.describe("The identifier of the source node in the relationship."),
|
|
11
|
+
relationship: zod_1.z
|
|
12
|
+
.string()
|
|
13
|
+
.describe("The relationship between the source and destination nodes."),
|
|
14
|
+
destination: zod_1.z
|
|
15
|
+
.string()
|
|
16
|
+
.describe("The identifier of the destination node in the relationship."),
|
|
17
|
+
});
|
|
18
|
+
// Schema for adding a relationship (includes types)
|
|
19
|
+
exports.GraphAddRelationshipArgsSchema = exports.GraphSimpleRelationshipArgsSchema.extend({
|
|
20
|
+
source_type: zod_1.z
|
|
21
|
+
.string()
|
|
22
|
+
.describe("The type or category of the source node."),
|
|
23
|
+
destination_type: zod_1.z
|
|
24
|
+
.string()
|
|
25
|
+
.describe("The type or category of the destination node."),
|
|
26
|
+
});
|
|
27
|
+
// Schema for extracting entities
|
|
28
|
+
exports.GraphExtractEntitiesArgsSchema = zod_1.z.object({
|
|
29
|
+
entities: zod_1.z
|
|
30
|
+
.array(zod_1.z.object({
|
|
31
|
+
entity: zod_1.z.string().describe("The name or identifier of the entity."),
|
|
32
|
+
entity_type: zod_1.z.string().describe("The type or category of the entity."),
|
|
33
|
+
}))
|
|
34
|
+
.describe("An array of entities with their types."),
|
|
35
|
+
});
|
|
36
|
+
// Schema for establishing relationships
|
|
37
|
+
exports.GraphRelationsArgsSchema = zod_1.z.object({
|
|
38
|
+
entities: zod_1.z
|
|
39
|
+
.array(exports.GraphSimpleRelationshipArgsSchema)
|
|
40
|
+
.describe("An array of relationships (source, relationship, destination)."),
|
|
41
|
+
});
|
|
42
|
+
// --- Tool Definitions (using JSON schema, keep as is) ---
|
|
43
|
+
// Note: The tool definitions themselves still use JSON schema format
|
|
44
|
+
// as expected by the LLM APIs. The Zod schemas above are for internal
|
|
45
|
+
// validation and potentially for use with Langchain's .withStructuredOutput
|
|
46
|
+
// if we adapt it to handle tool calls via schema.
|
|
47
|
+
exports.UPDATE_MEMORY_TOOL_GRAPH = {
|
|
48
|
+
type: "function",
|
|
49
|
+
function: {
|
|
50
|
+
name: "update_graph_memory",
|
|
51
|
+
description: "Update the relationship key of an existing graph memory based on new information.",
|
|
52
|
+
parameters: {
|
|
53
|
+
type: "object",
|
|
54
|
+
properties: {
|
|
55
|
+
source: {
|
|
56
|
+
type: "string",
|
|
57
|
+
description: "The identifier of the source node in the relationship to be updated.",
|
|
58
|
+
},
|
|
59
|
+
destination: {
|
|
60
|
+
type: "string",
|
|
61
|
+
description: "The identifier of the destination node in the relationship to be updated.",
|
|
62
|
+
},
|
|
63
|
+
relationship: {
|
|
64
|
+
type: "string",
|
|
65
|
+
description: "The new or updated relationship between the source and destination nodes.",
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
required: ["source", "destination", "relationship"],
|
|
69
|
+
additionalProperties: false,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
exports.ADD_MEMORY_TOOL_GRAPH = {
|
|
74
|
+
type: "function",
|
|
75
|
+
function: {
|
|
76
|
+
name: "add_graph_memory",
|
|
77
|
+
description: "Add a new graph memory to the knowledge graph.",
|
|
78
|
+
parameters: {
|
|
79
|
+
type: "object",
|
|
80
|
+
properties: {
|
|
81
|
+
source: {
|
|
82
|
+
type: "string",
|
|
83
|
+
description: "The identifier of the source node in the new relationship.",
|
|
84
|
+
},
|
|
85
|
+
destination: {
|
|
86
|
+
type: "string",
|
|
87
|
+
description: "The identifier of the destination node in the new relationship.",
|
|
88
|
+
},
|
|
89
|
+
relationship: {
|
|
90
|
+
type: "string",
|
|
91
|
+
description: "The type of relationship between the source and destination nodes.",
|
|
92
|
+
},
|
|
93
|
+
source_type: {
|
|
94
|
+
type: "string",
|
|
95
|
+
description: "The type or category of the source node.",
|
|
96
|
+
},
|
|
97
|
+
destination_type: {
|
|
98
|
+
type: "string",
|
|
99
|
+
description: "The type or category of the destination node.",
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
required: [
|
|
103
|
+
"source",
|
|
104
|
+
"destination",
|
|
105
|
+
"relationship",
|
|
106
|
+
"source_type",
|
|
107
|
+
"destination_type",
|
|
108
|
+
],
|
|
109
|
+
additionalProperties: false,
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
exports.NOOP_TOOL = {
|
|
114
|
+
type: "function",
|
|
115
|
+
function: {
|
|
116
|
+
name: "noop",
|
|
117
|
+
description: "No operation should be performed to the graph entities.",
|
|
118
|
+
parameters: {
|
|
119
|
+
type: "object",
|
|
120
|
+
properties: {},
|
|
121
|
+
required: [],
|
|
122
|
+
additionalProperties: false,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
exports.RELATIONS_TOOL = {
|
|
127
|
+
type: "function",
|
|
128
|
+
function: {
|
|
129
|
+
name: "establish_relationships",
|
|
130
|
+
description: "Establish relationships among the entities based on the provided text.",
|
|
131
|
+
parameters: {
|
|
132
|
+
type: "object",
|
|
133
|
+
properties: {
|
|
134
|
+
entities: {
|
|
135
|
+
type: "array",
|
|
136
|
+
items: {
|
|
137
|
+
type: "object",
|
|
138
|
+
properties: {
|
|
139
|
+
source: {
|
|
140
|
+
type: "string",
|
|
141
|
+
description: "The source entity of the relationship.",
|
|
142
|
+
},
|
|
143
|
+
relationship: {
|
|
144
|
+
type: "string",
|
|
145
|
+
description: "The relationship between the source and destination entities.",
|
|
146
|
+
},
|
|
147
|
+
destination: {
|
|
148
|
+
type: "string",
|
|
149
|
+
description: "The destination entity of the relationship.",
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
required: ["source", "relationship", "destination"],
|
|
153
|
+
additionalProperties: false,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
required: ["entities"],
|
|
158
|
+
additionalProperties: false,
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
exports.EXTRACT_ENTITIES_TOOL = {
|
|
163
|
+
type: "function",
|
|
164
|
+
function: {
|
|
165
|
+
name: "extract_entities",
|
|
166
|
+
description: "Extract entities and their types from the text.",
|
|
167
|
+
parameters: {
|
|
168
|
+
type: "object",
|
|
169
|
+
properties: {
|
|
170
|
+
entities: {
|
|
171
|
+
type: "array",
|
|
172
|
+
items: {
|
|
173
|
+
type: "object",
|
|
174
|
+
properties: {
|
|
175
|
+
entity: {
|
|
176
|
+
type: "string",
|
|
177
|
+
description: "The name or identifier of the entity.",
|
|
178
|
+
},
|
|
179
|
+
entity_type: {
|
|
180
|
+
type: "string",
|
|
181
|
+
description: "The type or category of the entity.",
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
required: ["entity", "entity_type"],
|
|
185
|
+
additionalProperties: false,
|
|
186
|
+
},
|
|
187
|
+
description: "An array of entities with their types.",
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
required: ["entities"],
|
|
191
|
+
additionalProperties: false,
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
exports.DELETE_MEMORY_TOOL_GRAPH = {
|
|
196
|
+
type: "function",
|
|
197
|
+
function: {
|
|
198
|
+
name: "delete_graph_memory",
|
|
199
|
+
description: "Delete the relationship between two nodes.",
|
|
200
|
+
parameters: {
|
|
201
|
+
type: "object",
|
|
202
|
+
properties: {
|
|
203
|
+
source: {
|
|
204
|
+
type: "string",
|
|
205
|
+
description: "The identifier of the source node in the relationship.",
|
|
206
|
+
},
|
|
207
|
+
relationship: {
|
|
208
|
+
type: "string",
|
|
209
|
+
description: "The existing relationship between the source and destination nodes that needs to be deleted.",
|
|
210
|
+
},
|
|
211
|
+
destination: {
|
|
212
|
+
type: "string",
|
|
213
|
+
description: "The identifier of the destination node in the relationship.",
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
required: ["source", "relationship", "destination"],
|
|
217
|
+
additionalProperties: false,
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const UPDATE_GRAPH_PROMPT = "\nYou are an AI expert specializing in graph memory management and optimization. Your task is to analyze existing graph memories alongside new information, and update the relationships in the memory list to ensure the most accurate, current, and coherent representation of knowledge.\n\nInput:\n1. Existing Graph Memories: A list of current graph memories, each containing source, target, and relationship information.\n2. New Graph Memory: Fresh information to be integrated into the existing graph structure.\n\nGuidelines:\n1. Identification: Use the source and target as primary identifiers when matching existing memories with new information.\n2. Conflict Resolution:\n - If new information contradicts an existing memory:\n a) For matching source and target but differing content, update the relationship of the existing memory.\n b) If the new memory provides more recent or accurate information, update the existing memory accordingly.\n3. Comprehensive Review: Thoroughly examine each existing graph memory against the new information, updating relationships as necessary. Multiple updates may be required.\n4. Consistency: Maintain a uniform and clear style across all memories. Each entry should be concise yet comprehensive.\n5. Semantic Coherence: Ensure that updates maintain or improve the overall semantic structure of the graph.\n6. Temporal Awareness: If timestamps are available, consider the recency of information when making updates.\n7. Relationship Refinement: Look for opportunities to refine relationship descriptions for greater precision or clarity.\n8. Redundancy Elimination: Identify and merge any redundant or highly similar relationships that may result from the update.\n\nMemory Format:\nsource -- RELATIONSHIP -- destination\n\nTask Details:\n======= Existing Graph Memories:=======\n{existing_memories}\n\n======= New Graph Memory:=======\n{new_memories}\n\nOutput:\nProvide a list of update instructions, each specifying the source, target, and the new relationship to be set. Only include memories that require updates.\n";
|
|
2
|
+
export declare const EXTRACT_RELATIONS_PROMPT = "\nYou are an advanced algorithm designed to extract structured information from text to construct knowledge graphs. Your goal is to capture comprehensive and accurate information. Follow these key principles:\n\n1. Extract only explicitly stated information from the text.\n2. Establish relationships among the entities provided.\n3. Use \"USER_ID\" as the source entity for any self-references (e.g., \"I,\" \"me,\" \"my,\" etc.) in user messages.\nCUSTOM_PROMPT\n\nRelationships:\n - Use consistent, general, and timeless relationship types.\n - Example: Prefer \"professor\" over \"became_professor.\"\n - Relationships should only be established among the entities explicitly mentioned in the user message.\n\nEntity Consistency:\n - Ensure that relationships are coherent and logically align with the context of the message.\n - Maintain consistent naming for entities across the extracted data.\n\nStrive to construct a coherent and easily understandable knowledge graph by eshtablishing all the relationships among the entities and adherence to the user's context.\n\nAdhere strictly to these guidelines to ensure high-quality knowledge graph extraction.\n";
|
|
3
|
+
export declare const DELETE_RELATIONS_SYSTEM_PROMPT = "\nYou are a graph memory manager specializing in identifying, managing, and optimizing relationships within graph-based memories. Your primary task is to analyze a list of existing relationships and determine which ones should be deleted based on the new information provided.\nInput:\n1. Existing Graph Memories: A list of current graph memories, each containing source, relationship, and destination information.\n2. New Text: The new information to be integrated into the existing graph structure.\n3. Use \"USER_ID\" as node for any self-references (e.g., \"I,\" \"me,\" \"my,\" etc.) in user messages.\n\nGuidelines:\n1. Identification: Use the new information to evaluate existing relationships in the memory graph.\n2. Deletion Criteria: Delete a relationship only if it meets at least one of these conditions:\n - Outdated or Inaccurate: The new information is more recent or accurate.\n - Contradictory: The new information conflicts with or negates the existing information.\n3. DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes.\n4. Comprehensive Analysis:\n - Thoroughly examine each existing relationship against the new information and delete as necessary.\n - Multiple deletions may be required based on the new information.\n5. Semantic Integrity:\n - Ensure that deletions maintain or improve the overall semantic structure of the graph.\n - Avoid deleting relationships that are NOT contradictory/outdated to the new information.\n6. Temporal Awareness: Prioritize recency when timestamps are available.\n7. Necessity Principle: Only DELETE relationships that must be deleted and are contradictory/outdated to the new information to maintain an accurate and coherent memory graph.\n\nNote: DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes. \n\nFor example: \nExisting Memory: alice -- loves_to_eat -- pizza\nNew Information: Alice also loves to eat burger.\n\nDo not delete in the above example because there is a possibility that Alice loves to eat both pizza and burger.\n\nMemory Format:\nsource -- relationship -- destination\n\nProvide a list of deletion instructions, each specifying the relationship to be deleted.\n";
|
|
4
|
+
export declare function getDeleteMessages(existingMemoriesString: string, data: string, userId: string): [string, string];
|
|
5
|
+
export declare function formatEntities(entities: Array<{
|
|
6
|
+
source: string;
|
|
7
|
+
relationship: string;
|
|
8
|
+
destination: string;
|
|
9
|
+
}>): string;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DELETE_RELATIONS_SYSTEM_PROMPT = exports.EXTRACT_RELATIONS_PROMPT = exports.UPDATE_GRAPH_PROMPT = void 0;
|
|
4
|
+
exports.getDeleteMessages = getDeleteMessages;
|
|
5
|
+
exports.formatEntities = formatEntities;
|
|
6
|
+
exports.UPDATE_GRAPH_PROMPT = `
|
|
7
|
+
You are an AI expert specializing in graph memory management and optimization. Your task is to analyze existing graph memories alongside new information, and update the relationships in the memory list to ensure the most accurate, current, and coherent representation of knowledge.
|
|
8
|
+
|
|
9
|
+
Input:
|
|
10
|
+
1. Existing Graph Memories: A list of current graph memories, each containing source, target, and relationship information.
|
|
11
|
+
2. New Graph Memory: Fresh information to be integrated into the existing graph structure.
|
|
12
|
+
|
|
13
|
+
Guidelines:
|
|
14
|
+
1. Identification: Use the source and target as primary identifiers when matching existing memories with new information.
|
|
15
|
+
2. Conflict Resolution:
|
|
16
|
+
- If new information contradicts an existing memory:
|
|
17
|
+
a) For matching source and target but differing content, update the relationship of the existing memory.
|
|
18
|
+
b) If the new memory provides more recent or accurate information, update the existing memory accordingly.
|
|
19
|
+
3. Comprehensive Review: Thoroughly examine each existing graph memory against the new information, updating relationships as necessary. Multiple updates may be required.
|
|
20
|
+
4. Consistency: Maintain a uniform and clear style across all memories. Each entry should be concise yet comprehensive.
|
|
21
|
+
5. Semantic Coherence: Ensure that updates maintain or improve the overall semantic structure of the graph.
|
|
22
|
+
6. Temporal Awareness: If timestamps are available, consider the recency of information when making updates.
|
|
23
|
+
7. Relationship Refinement: Look for opportunities to refine relationship descriptions for greater precision or clarity.
|
|
24
|
+
8. Redundancy Elimination: Identify and merge any redundant or highly similar relationships that may result from the update.
|
|
25
|
+
|
|
26
|
+
Memory Format:
|
|
27
|
+
source -- RELATIONSHIP -- destination
|
|
28
|
+
|
|
29
|
+
Task Details:
|
|
30
|
+
======= Existing Graph Memories:=======
|
|
31
|
+
{existing_memories}
|
|
32
|
+
|
|
33
|
+
======= New Graph Memory:=======
|
|
34
|
+
{new_memories}
|
|
35
|
+
|
|
36
|
+
Output:
|
|
37
|
+
Provide a list of update instructions, each specifying the source, target, and the new relationship to be set. Only include memories that require updates.
|
|
38
|
+
`;
|
|
39
|
+
exports.EXTRACT_RELATIONS_PROMPT = `
|
|
40
|
+
You are an advanced algorithm designed to extract structured information from text to construct knowledge graphs. Your goal is to capture comprehensive and accurate information. Follow these key principles:
|
|
41
|
+
|
|
42
|
+
1. Extract only explicitly stated information from the text.
|
|
43
|
+
2. Establish relationships among the entities provided.
|
|
44
|
+
3. Use "USER_ID" as the source entity for any self-references (e.g., "I," "me," "my," etc.) in user messages.
|
|
45
|
+
CUSTOM_PROMPT
|
|
46
|
+
|
|
47
|
+
Relationships:
|
|
48
|
+
- Use consistent, general, and timeless relationship types.
|
|
49
|
+
- Example: Prefer "professor" over "became_professor."
|
|
50
|
+
- Relationships should only be established among the entities explicitly mentioned in the user message.
|
|
51
|
+
|
|
52
|
+
Entity Consistency:
|
|
53
|
+
- Ensure that relationships are coherent and logically align with the context of the message.
|
|
54
|
+
- Maintain consistent naming for entities across the extracted data.
|
|
55
|
+
|
|
56
|
+
Strive to construct a coherent and easily understandable knowledge graph by eshtablishing all the relationships among the entities and adherence to the user's context.
|
|
57
|
+
|
|
58
|
+
Adhere strictly to these guidelines to ensure high-quality knowledge graph extraction.
|
|
59
|
+
`;
|
|
60
|
+
exports.DELETE_RELATIONS_SYSTEM_PROMPT = `
|
|
61
|
+
You are a graph memory manager specializing in identifying, managing, and optimizing relationships within graph-based memories. Your primary task is to analyze a list of existing relationships and determine which ones should be deleted based on the new information provided.
|
|
62
|
+
Input:
|
|
63
|
+
1. Existing Graph Memories: A list of current graph memories, each containing source, relationship, and destination information.
|
|
64
|
+
2. New Text: The new information to be integrated into the existing graph structure.
|
|
65
|
+
3. Use "USER_ID" as node for any self-references (e.g., "I," "me," "my," etc.) in user messages.
|
|
66
|
+
|
|
67
|
+
Guidelines:
|
|
68
|
+
1. Identification: Use the new information to evaluate existing relationships in the memory graph.
|
|
69
|
+
2. Deletion Criteria: Delete a relationship only if it meets at least one of these conditions:
|
|
70
|
+
- Outdated or Inaccurate: The new information is more recent or accurate.
|
|
71
|
+
- Contradictory: The new information conflicts with or negates the existing information.
|
|
72
|
+
3. DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes.
|
|
73
|
+
4. Comprehensive Analysis:
|
|
74
|
+
- Thoroughly examine each existing relationship against the new information and delete as necessary.
|
|
75
|
+
- Multiple deletions may be required based on the new information.
|
|
76
|
+
5. Semantic Integrity:
|
|
77
|
+
- Ensure that deletions maintain or improve the overall semantic structure of the graph.
|
|
78
|
+
- Avoid deleting relationships that are NOT contradictory/outdated to the new information.
|
|
79
|
+
6. Temporal Awareness: Prioritize recency when timestamps are available.
|
|
80
|
+
7. Necessity Principle: Only DELETE relationships that must be deleted and are contradictory/outdated to the new information to maintain an accurate and coherent memory graph.
|
|
81
|
+
|
|
82
|
+
Note: DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes.
|
|
83
|
+
|
|
84
|
+
For example:
|
|
85
|
+
Existing Memory: alice -- loves_to_eat -- pizza
|
|
86
|
+
New Information: Alice also loves to eat burger.
|
|
87
|
+
|
|
88
|
+
Do not delete in the above example because there is a possibility that Alice loves to eat both pizza and burger.
|
|
89
|
+
|
|
90
|
+
Memory Format:
|
|
91
|
+
source -- relationship -- destination
|
|
92
|
+
|
|
93
|
+
Provide a list of deletion instructions, each specifying the relationship to be deleted.
|
|
94
|
+
`;
|
|
95
|
+
function getDeleteMessages(existingMemoriesString, data, userId) {
|
|
96
|
+
return [
|
|
97
|
+
exports.DELETE_RELATIONS_SYSTEM_PROMPT.replace("USER_ID", userId),
|
|
98
|
+
`Here are the existing memories: ${existingMemoriesString} \n\n New Information: ${data}`,
|
|
99
|
+
];
|
|
100
|
+
}
|
|
101
|
+
function formatEntities(entities) {
|
|
102
|
+
return entities
|
|
103
|
+
.map((e) => `${e.source} -- ${e.relationship} -- ${e.destination}`)
|
|
104
|
+
.join("\n");
|
|
105
|
+
}
|