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,132 @@
|
|
|
1
|
+
import { MemoryConfig, MemoryConfigSchema } from "../types";
|
|
2
|
+
import { DEFAULT_MEMORY_CONFIG } from "./defaults";
|
|
3
|
+
|
|
4
|
+
export class ConfigManager {
|
|
5
|
+
static mergeConfig(userConfig: Partial<MemoryConfig> = {}): MemoryConfig {
|
|
6
|
+
const mergedConfig = {
|
|
7
|
+
version: userConfig.version || DEFAULT_MEMORY_CONFIG.version,
|
|
8
|
+
embedder: {
|
|
9
|
+
provider:
|
|
10
|
+
userConfig.embedder?.provider ||
|
|
11
|
+
DEFAULT_MEMORY_CONFIG.embedder.provider,
|
|
12
|
+
config: (() => {
|
|
13
|
+
const defaultConf = DEFAULT_MEMORY_CONFIG.embedder.config;
|
|
14
|
+
const userConf = userConfig.embedder?.config;
|
|
15
|
+
let finalModel: string | any = defaultConf.model;
|
|
16
|
+
|
|
17
|
+
if (userConf?.model && typeof userConf.model === "object") {
|
|
18
|
+
finalModel = userConf.model;
|
|
19
|
+
} else if (userConf?.model && typeof userConf.model === "string") {
|
|
20
|
+
finalModel = userConf.model;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
apiKey:
|
|
25
|
+
userConf?.apiKey !== undefined
|
|
26
|
+
? userConf.apiKey
|
|
27
|
+
: defaultConf.apiKey,
|
|
28
|
+
model: finalModel,
|
|
29
|
+
url: userConf?.url,
|
|
30
|
+
dimension: userConf?.dimension || defaultConf.dimension,
|
|
31
|
+
modelProperties:
|
|
32
|
+
userConf?.modelProperties !== undefined
|
|
33
|
+
? userConf.modelProperties
|
|
34
|
+
: defaultConf.modelProperties,
|
|
35
|
+
};
|
|
36
|
+
})(),
|
|
37
|
+
},
|
|
38
|
+
vectorStore: {
|
|
39
|
+
provider:
|
|
40
|
+
userConfig.vectorStore?.provider ||
|
|
41
|
+
DEFAULT_MEMORY_CONFIG.vectorStore.provider,
|
|
42
|
+
config: (() => {
|
|
43
|
+
const defaultConf = DEFAULT_MEMORY_CONFIG.vectorStore.config;
|
|
44
|
+
const userConf = userConfig.vectorStore?.config;
|
|
45
|
+
|
|
46
|
+
// Prioritize user-provided client instance
|
|
47
|
+
if (userConf?.client && typeof userConf.client === "object") {
|
|
48
|
+
return {
|
|
49
|
+
client: userConf.client,
|
|
50
|
+
collectionName: userConf.collectionName,
|
|
51
|
+
dimension: userConf.dimension || defaultConf.dimension,
|
|
52
|
+
// Merge scoring deeply if present in userConf, otherwise use default
|
|
53
|
+
scoring: userConf.scoring ? {
|
|
54
|
+
procedural: { ...defaultConf.scoring?.procedural, ...userConf.scoring.procedural },
|
|
55
|
+
episodic: { ...defaultConf.scoring?.episodic, ...userConf.scoring.episodic },
|
|
56
|
+
factual: { ...defaultConf.scoring?.factual, ...userConf.scoring.factual },
|
|
57
|
+
semantic: { ...defaultConf.scoring?.semantic, ...userConf.scoring.semantic },
|
|
58
|
+
assistant_preference: { ...defaultConf.scoring?.assistant_preference, ...userConf.scoring.assistant_preference },
|
|
59
|
+
default: { ...defaultConf.scoring?.default, ...userConf.scoring.default },
|
|
60
|
+
} : defaultConf.scoring,
|
|
61
|
+
...userConf, // Include any other passthrough fields from user
|
|
62
|
+
};
|
|
63
|
+
} else {
|
|
64
|
+
// If no client provided, merge standard fields including scoring and cleanup threshold
|
|
65
|
+
return {
|
|
66
|
+
collectionName:
|
|
67
|
+
userConf?.collectionName || defaultConf.collectionName,
|
|
68
|
+
dimension: userConf?.dimension || defaultConf.dimension,
|
|
69
|
+
client: undefined,
|
|
70
|
+
// Merge scoring deeply if present in userConf, otherwise use default
|
|
71
|
+
scoring: userConf?.scoring ? {
|
|
72
|
+
procedural: { ...defaultConf.scoring?.procedural, ...userConf.scoring.procedural },
|
|
73
|
+
episodic: { ...defaultConf.scoring?.episodic, ...userConf.scoring.episodic },
|
|
74
|
+
factual: { ...defaultConf.scoring?.factual, ...userConf.scoring.factual },
|
|
75
|
+
semantic: { ...defaultConf.scoring?.semantic, ...userConf.scoring.semantic },
|
|
76
|
+
assistant_preference: { ...defaultConf.scoring?.assistant_preference, ...userConf.scoring.assistant_preference },
|
|
77
|
+
default: { ...defaultConf.scoring?.default, ...userConf.scoring.default },
|
|
78
|
+
} : defaultConf.scoring,
|
|
79
|
+
recencyCleanupThreshold:
|
|
80
|
+
userConf?.recencyCleanupThreshold ?? defaultConf.recencyCleanupThreshold, // Merge cleanup threshold
|
|
81
|
+
...userConf,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
})(),
|
|
85
|
+
},
|
|
86
|
+
llm: {
|
|
87
|
+
provider:
|
|
88
|
+
userConfig.llm?.provider || DEFAULT_MEMORY_CONFIG.llm.provider,
|
|
89
|
+
config: (() => {
|
|
90
|
+
const defaultConf = DEFAULT_MEMORY_CONFIG.llm.config;
|
|
91
|
+
const userConf = userConfig.llm?.config;
|
|
92
|
+
let finalModel: string | any = defaultConf.model;
|
|
93
|
+
|
|
94
|
+
if (userConf?.model && typeof userConf.model === "object") {
|
|
95
|
+
finalModel = userConf.model;
|
|
96
|
+
} else if (userConf?.model && typeof userConf.model === "string") {
|
|
97
|
+
finalModel = userConf.model;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
apiKey:
|
|
102
|
+
userConf?.apiKey !== undefined
|
|
103
|
+
? userConf.apiKey
|
|
104
|
+
: defaultConf.apiKey,
|
|
105
|
+
model: finalModel,
|
|
106
|
+
modelProperties:
|
|
107
|
+
userConf?.modelProperties !== undefined
|
|
108
|
+
? userConf.modelProperties
|
|
109
|
+
: defaultConf.modelProperties,
|
|
110
|
+
};
|
|
111
|
+
})(),
|
|
112
|
+
},
|
|
113
|
+
historyDbPath:
|
|
114
|
+
userConfig.historyDbPath || DEFAULT_MEMORY_CONFIG.historyDbPath,
|
|
115
|
+
customPrompt: userConfig.customPrompt,
|
|
116
|
+
graphStore: {
|
|
117
|
+
...DEFAULT_MEMORY_CONFIG.graphStore,
|
|
118
|
+
...userConfig.graphStore,
|
|
119
|
+
},
|
|
120
|
+
historyStore: {
|
|
121
|
+
...DEFAULT_MEMORY_CONFIG.historyStore,
|
|
122
|
+
...userConfig.historyStore,
|
|
123
|
+
},
|
|
124
|
+
disableHistory:
|
|
125
|
+
userConfig.disableHistory || DEFAULT_MEMORY_CONFIG.disableHistory,
|
|
126
|
+
enableGraph: userConfig.enableGraph || DEFAULT_MEMORY_CONFIG.enableGraph,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// Validate the merged config
|
|
130
|
+
return MemoryConfigSchema.parse(mergedConfig);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { GoogleGenAI } from "@google/genai";
|
|
2
|
+
import { Embedder } from "./base";
|
|
3
|
+
import { EmbeddingConfig } from "../types";
|
|
4
|
+
|
|
5
|
+
export class GoogleEmbedder implements Embedder {
|
|
6
|
+
private google: GoogleGenAI;
|
|
7
|
+
private model: string;
|
|
8
|
+
private dimension: number;
|
|
9
|
+
constructor(config: EmbeddingConfig) {
|
|
10
|
+
this.google = new GoogleGenAI({ apiKey: config.apiKey });
|
|
11
|
+
this.model = config.model || "text-embedding-004";
|
|
12
|
+
this.dimension = config.dimension || 1536;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async embed(text: string): Promise<number[]> {
|
|
16
|
+
const response = await this.google.models.embedContent({
|
|
17
|
+
model: this.model,
|
|
18
|
+
contents: text,
|
|
19
|
+
config: { outputDimensionality: this.dimension },
|
|
20
|
+
});
|
|
21
|
+
return response.embeddings![0].values!;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async embedBatch(texts: string[]): Promise<number[][]> {
|
|
25
|
+
const response = await this.google.models.embedContent({
|
|
26
|
+
model: this.model,
|
|
27
|
+
contents: texts,
|
|
28
|
+
config: { outputDimensionality: this.dimension },
|
|
29
|
+
});
|
|
30
|
+
return response.embeddings!.map((item) => item.values!);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import { Embedder } from "./base";
|
|
3
|
+
import { EmbeddingConfig } from "../types";
|
|
4
|
+
|
|
5
|
+
export class OpenAIEmbedder implements Embedder {
|
|
6
|
+
private openai: OpenAI;
|
|
7
|
+
private model: string;
|
|
8
|
+
private dimension: number;
|
|
9
|
+
|
|
10
|
+
constructor(config: EmbeddingConfig) {
|
|
11
|
+
this.openai = new OpenAI({ apiKey: config.apiKey });
|
|
12
|
+
this.model = config.model || "text-embedding-3-small";
|
|
13
|
+
this.dimension = config.dimension || 1536;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async embed(text: string): Promise<number[]> {
|
|
17
|
+
const response = await this.openai.embeddings.create({
|
|
18
|
+
model: this.model,
|
|
19
|
+
input: text,
|
|
20
|
+
dimensions: this.dimension,
|
|
21
|
+
});
|
|
22
|
+
return response.data[0].embedding;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async embedBatch(texts: string[]): Promise<number[][]> {
|
|
26
|
+
const response = await this.openai.embeddings.create({
|
|
27
|
+
model: this.model,
|
|
28
|
+
input: texts,
|
|
29
|
+
dimensions: this.dimension,
|
|
30
|
+
});
|
|
31
|
+
return response.data.map((item) => item.embedding);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { LLMConfig } from "../types";
|
|
2
|
+
|
|
3
|
+
export interface Neo4jConfig {
|
|
4
|
+
url: string | null;
|
|
5
|
+
username: string | null;
|
|
6
|
+
password: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface GraphStoreConfig {
|
|
10
|
+
provider: string;
|
|
11
|
+
config: Neo4jConfig;
|
|
12
|
+
llm?: LLMConfig;
|
|
13
|
+
customPrompt?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function validateNeo4jConfig(config: Neo4jConfig): void {
|
|
17
|
+
const { url, username, password } = config;
|
|
18
|
+
if (!url || !username || !password) {
|
|
19
|
+
throw new Error("Please provide 'url', 'username' and 'password'.");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function validateGraphStoreConfig(config: GraphStoreConfig): void {
|
|
24
|
+
const { provider } = config;
|
|
25
|
+
if (provider === "neo4j") {
|
|
26
|
+
validateNeo4jConfig(config.config);
|
|
27
|
+
} else {
|
|
28
|
+
throw new Error(`Unsupported graph store provider: ${provider}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export interface GraphToolParameters {
|
|
4
|
+
source: string;
|
|
5
|
+
destination: string;
|
|
6
|
+
relationship: string;
|
|
7
|
+
source_type?: string;
|
|
8
|
+
destination_type?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface GraphEntitiesParameters {
|
|
12
|
+
entities: Array<{
|
|
13
|
+
entity: string;
|
|
14
|
+
entity_type: string;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface GraphRelationsParameters {
|
|
19
|
+
entities: Array<{
|
|
20
|
+
source: string;
|
|
21
|
+
relationship: string;
|
|
22
|
+
destination: string;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// --- Zod Schemas for Tool Arguments ---
|
|
27
|
+
|
|
28
|
+
// Schema for simple relationship arguments (Update, Delete)
|
|
29
|
+
export const GraphSimpleRelationshipArgsSchema = z.object({
|
|
30
|
+
source: z
|
|
31
|
+
.string()
|
|
32
|
+
.describe("The identifier of the source node in the relationship."),
|
|
33
|
+
relationship: z
|
|
34
|
+
.string()
|
|
35
|
+
.describe("The relationship between the source and destination nodes."),
|
|
36
|
+
destination: z
|
|
37
|
+
.string()
|
|
38
|
+
.describe("The identifier of the destination node in the relationship."),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Schema for adding a relationship (includes types)
|
|
42
|
+
export const GraphAddRelationshipArgsSchema =
|
|
43
|
+
GraphSimpleRelationshipArgsSchema.extend({
|
|
44
|
+
source_type: z
|
|
45
|
+
.string()
|
|
46
|
+
.describe("The type or category of the source node."),
|
|
47
|
+
destination_type: z
|
|
48
|
+
.string()
|
|
49
|
+
.describe("The type or category of the destination node."),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Schema for extracting entities
|
|
53
|
+
export const GraphExtractEntitiesArgsSchema = z.object({
|
|
54
|
+
entities: z
|
|
55
|
+
.array(
|
|
56
|
+
z.object({
|
|
57
|
+
entity: z.string().describe("The name or identifier of the entity."),
|
|
58
|
+
entity_type: z.string().describe("The type or category of the entity."),
|
|
59
|
+
}),
|
|
60
|
+
)
|
|
61
|
+
.describe("An array of entities with their types."),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Schema for establishing relationships
|
|
65
|
+
export const GraphRelationsArgsSchema = z.object({
|
|
66
|
+
entities: z
|
|
67
|
+
.array(GraphSimpleRelationshipArgsSchema)
|
|
68
|
+
.describe("An array of relationships (source, relationship, destination)."),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// --- Tool Definitions (using JSON schema, keep as is) ---
|
|
72
|
+
|
|
73
|
+
// Note: The tool definitions themselves still use JSON schema format
|
|
74
|
+
// as expected by the LLM APIs. The Zod schemas above are for internal
|
|
75
|
+
// validation and potentially for use with Langchain's .withStructuredOutput
|
|
76
|
+
// if we adapt it to handle tool calls via schema.
|
|
77
|
+
|
|
78
|
+
export const UPDATE_MEMORY_TOOL_GRAPH = {
|
|
79
|
+
type: "function",
|
|
80
|
+
function: {
|
|
81
|
+
name: "update_graph_memory",
|
|
82
|
+
description:
|
|
83
|
+
"Update the relationship key of an existing graph memory based on new information.",
|
|
84
|
+
parameters: {
|
|
85
|
+
type: "object",
|
|
86
|
+
properties: {
|
|
87
|
+
source: {
|
|
88
|
+
type: "string",
|
|
89
|
+
description:
|
|
90
|
+
"The identifier of the source node in the relationship to be updated.",
|
|
91
|
+
},
|
|
92
|
+
destination: {
|
|
93
|
+
type: "string",
|
|
94
|
+
description:
|
|
95
|
+
"The identifier of the destination node in the relationship to be updated.",
|
|
96
|
+
},
|
|
97
|
+
relationship: {
|
|
98
|
+
type: "string",
|
|
99
|
+
description:
|
|
100
|
+
"The new or updated relationship between the source and destination nodes.",
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
required: ["source", "destination", "relationship"],
|
|
104
|
+
additionalProperties: false,
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export const ADD_MEMORY_TOOL_GRAPH = {
|
|
110
|
+
type: "function",
|
|
111
|
+
function: {
|
|
112
|
+
name: "add_graph_memory",
|
|
113
|
+
description: "Add a new graph memory to the knowledge graph.",
|
|
114
|
+
parameters: {
|
|
115
|
+
type: "object",
|
|
116
|
+
properties: {
|
|
117
|
+
source: {
|
|
118
|
+
type: "string",
|
|
119
|
+
description:
|
|
120
|
+
"The identifier of the source node in the new relationship.",
|
|
121
|
+
},
|
|
122
|
+
destination: {
|
|
123
|
+
type: "string",
|
|
124
|
+
description:
|
|
125
|
+
"The identifier of the destination node in the new relationship.",
|
|
126
|
+
},
|
|
127
|
+
relationship: {
|
|
128
|
+
type: "string",
|
|
129
|
+
description:
|
|
130
|
+
"The type of relationship between the source and destination nodes.",
|
|
131
|
+
},
|
|
132
|
+
source_type: {
|
|
133
|
+
type: "string",
|
|
134
|
+
description: "The type or category of the source node.",
|
|
135
|
+
},
|
|
136
|
+
destination_type: {
|
|
137
|
+
type: "string",
|
|
138
|
+
description: "The type or category of the destination node.",
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
required: [
|
|
142
|
+
"source",
|
|
143
|
+
"destination",
|
|
144
|
+
"relationship",
|
|
145
|
+
"source_type",
|
|
146
|
+
"destination_type",
|
|
147
|
+
],
|
|
148
|
+
additionalProperties: false,
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export const NOOP_TOOL = {
|
|
154
|
+
type: "function",
|
|
155
|
+
function: {
|
|
156
|
+
name: "noop",
|
|
157
|
+
description: "No operation should be performed to the graph entities.",
|
|
158
|
+
parameters: {
|
|
159
|
+
type: "object",
|
|
160
|
+
properties: {},
|
|
161
|
+
required: [],
|
|
162
|
+
additionalProperties: false,
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
export const RELATIONS_TOOL = {
|
|
168
|
+
type: "function",
|
|
169
|
+
function: {
|
|
170
|
+
name: "establish_relationships",
|
|
171
|
+
description:
|
|
172
|
+
"Establish relationships among the entities based on the provided text.",
|
|
173
|
+
parameters: {
|
|
174
|
+
type: "object",
|
|
175
|
+
properties: {
|
|
176
|
+
entities: {
|
|
177
|
+
type: "array",
|
|
178
|
+
items: {
|
|
179
|
+
type: "object",
|
|
180
|
+
properties: {
|
|
181
|
+
source: {
|
|
182
|
+
type: "string",
|
|
183
|
+
description: "The source entity of the relationship.",
|
|
184
|
+
},
|
|
185
|
+
relationship: {
|
|
186
|
+
type: "string",
|
|
187
|
+
description:
|
|
188
|
+
"The relationship between the source and destination entities.",
|
|
189
|
+
},
|
|
190
|
+
destination: {
|
|
191
|
+
type: "string",
|
|
192
|
+
description: "The destination entity of the relationship.",
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
required: ["source", "relationship", "destination"],
|
|
196
|
+
additionalProperties: false,
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
required: ["entities"],
|
|
201
|
+
additionalProperties: false,
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export const EXTRACT_ENTITIES_TOOL = {
|
|
207
|
+
type: "function",
|
|
208
|
+
function: {
|
|
209
|
+
name: "extract_entities",
|
|
210
|
+
description: "Extract entities and their types from the text.",
|
|
211
|
+
parameters: {
|
|
212
|
+
type: "object",
|
|
213
|
+
properties: {
|
|
214
|
+
entities: {
|
|
215
|
+
type: "array",
|
|
216
|
+
items: {
|
|
217
|
+
type: "object",
|
|
218
|
+
properties: {
|
|
219
|
+
entity: {
|
|
220
|
+
type: "string",
|
|
221
|
+
description: "The name or identifier of the entity.",
|
|
222
|
+
},
|
|
223
|
+
entity_type: {
|
|
224
|
+
type: "string",
|
|
225
|
+
description: "The type or category of the entity.",
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
required: ["entity", "entity_type"],
|
|
229
|
+
additionalProperties: false,
|
|
230
|
+
},
|
|
231
|
+
description: "An array of entities with their types.",
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
required: ["entities"],
|
|
235
|
+
additionalProperties: false,
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
export const DELETE_MEMORY_TOOL_GRAPH = {
|
|
241
|
+
type: "function",
|
|
242
|
+
function: {
|
|
243
|
+
name: "delete_graph_memory",
|
|
244
|
+
description: "Delete the relationship between two nodes.",
|
|
245
|
+
parameters: {
|
|
246
|
+
type: "object",
|
|
247
|
+
properties: {
|
|
248
|
+
source: {
|
|
249
|
+
type: "string",
|
|
250
|
+
description: "The identifier of the source node in the relationship.",
|
|
251
|
+
},
|
|
252
|
+
relationship: {
|
|
253
|
+
type: "string",
|
|
254
|
+
description:
|
|
255
|
+
"The existing relationship between the source and destination nodes that needs to be deleted.",
|
|
256
|
+
},
|
|
257
|
+
destination: {
|
|
258
|
+
type: "string",
|
|
259
|
+
description:
|
|
260
|
+
"The identifier of the destination node in the relationship.",
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
required: ["source", "relationship", "destination"],
|
|
264
|
+
additionalProperties: false,
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
export const UPDATE_GRAPH_PROMPT = `
|
|
2
|
+
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.
|
|
3
|
+
|
|
4
|
+
Input:
|
|
5
|
+
1. Existing Graph Memories: A list of current graph memories, each containing source, target, and relationship information.
|
|
6
|
+
2. New Graph Memory: Fresh information to be integrated into the existing graph structure.
|
|
7
|
+
|
|
8
|
+
Guidelines:
|
|
9
|
+
1. Identification: Use the source and target as primary identifiers when matching existing memories with new information.
|
|
10
|
+
2. Conflict Resolution:
|
|
11
|
+
- If new information contradicts an existing memory:
|
|
12
|
+
a) For matching source and target but differing content, update the relationship of the existing memory.
|
|
13
|
+
b) If the new memory provides more recent or accurate information, update the existing memory accordingly.
|
|
14
|
+
3. Comprehensive Review: Thoroughly examine each existing graph memory against the new information, updating relationships as necessary. Multiple updates may be required.
|
|
15
|
+
4. Consistency: Maintain a uniform and clear style across all memories. Each entry should be concise yet comprehensive.
|
|
16
|
+
5. Semantic Coherence: Ensure that updates maintain or improve the overall semantic structure of the graph.
|
|
17
|
+
6. Temporal Awareness: If timestamps are available, consider the recency of information when making updates.
|
|
18
|
+
7. Relationship Refinement: Look for opportunities to refine relationship descriptions for greater precision or clarity.
|
|
19
|
+
8. Redundancy Elimination: Identify and merge any redundant or highly similar relationships that may result from the update.
|
|
20
|
+
|
|
21
|
+
Memory Format:
|
|
22
|
+
source -- RELATIONSHIP -- destination
|
|
23
|
+
|
|
24
|
+
Task Details:
|
|
25
|
+
======= Existing Graph Memories:=======
|
|
26
|
+
{existing_memories}
|
|
27
|
+
|
|
28
|
+
======= New Graph Memory:=======
|
|
29
|
+
{new_memories}
|
|
30
|
+
|
|
31
|
+
Output:
|
|
32
|
+
Provide a list of update instructions, each specifying the source, target, and the new relationship to be set. Only include memories that require updates.
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
export const EXTRACT_RELATIONS_PROMPT = `
|
|
36
|
+
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:
|
|
37
|
+
|
|
38
|
+
1. Extract only explicitly stated information from the text.
|
|
39
|
+
2. Establish relationships among the entities provided.
|
|
40
|
+
3. Use "USER_ID" as the source entity for any self-references (e.g., "I," "me," "my," etc.) in user messages.
|
|
41
|
+
CUSTOM_PROMPT
|
|
42
|
+
|
|
43
|
+
Relationships:
|
|
44
|
+
- Use consistent, general, and timeless relationship types.
|
|
45
|
+
- Example: Prefer "professor" over "became_professor."
|
|
46
|
+
- Relationships should only be established among the entities explicitly mentioned in the user message.
|
|
47
|
+
|
|
48
|
+
Entity Consistency:
|
|
49
|
+
- Ensure that relationships are coherent and logically align with the context of the message.
|
|
50
|
+
- Maintain consistent naming for entities across the extracted data.
|
|
51
|
+
|
|
52
|
+
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.
|
|
53
|
+
|
|
54
|
+
Adhere strictly to these guidelines to ensure high-quality knowledge graph extraction.
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
export const DELETE_RELATIONS_SYSTEM_PROMPT = `
|
|
58
|
+
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.
|
|
59
|
+
Input:
|
|
60
|
+
1. Existing Graph Memories: A list of current graph memories, each containing source, relationship, and destination information.
|
|
61
|
+
2. New Text: The new information to be integrated into the existing graph structure.
|
|
62
|
+
3. Use "USER_ID" as node for any self-references (e.g., "I," "me," "my," etc.) in user messages.
|
|
63
|
+
|
|
64
|
+
Guidelines:
|
|
65
|
+
1. Identification: Use the new information to evaluate existing relationships in the memory graph.
|
|
66
|
+
2. Deletion Criteria: Delete a relationship only if it meets at least one of these conditions:
|
|
67
|
+
- Outdated or Inaccurate: The new information is more recent or accurate.
|
|
68
|
+
- Contradictory: The new information conflicts with or negates the existing information.
|
|
69
|
+
3. DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes.
|
|
70
|
+
4. Comprehensive Analysis:
|
|
71
|
+
- Thoroughly examine each existing relationship against the new information and delete as necessary.
|
|
72
|
+
- Multiple deletions may be required based on the new information.
|
|
73
|
+
5. Semantic Integrity:
|
|
74
|
+
- Ensure that deletions maintain or improve the overall semantic structure of the graph.
|
|
75
|
+
- Avoid deleting relationships that are NOT contradictory/outdated to the new information.
|
|
76
|
+
6. Temporal Awareness: Prioritize recency when timestamps are available.
|
|
77
|
+
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.
|
|
78
|
+
|
|
79
|
+
Note: DO NOT DELETE if their is a possibility of same type of relationship but different destination nodes.
|
|
80
|
+
|
|
81
|
+
For example:
|
|
82
|
+
Existing Memory: alice -- loves_to_eat -- pizza
|
|
83
|
+
New Information: Alice also loves to eat burger.
|
|
84
|
+
|
|
85
|
+
Do not delete in the above example because there is a possibility that Alice loves to eat both pizza and burger.
|
|
86
|
+
|
|
87
|
+
Memory Format:
|
|
88
|
+
source -- relationship -- destination
|
|
89
|
+
|
|
90
|
+
Provide a list of deletion instructions, each specifying the relationship to be deleted.
|
|
91
|
+
`;
|
|
92
|
+
|
|
93
|
+
export function getDeleteMessages(
|
|
94
|
+
existingMemoriesString: string,
|
|
95
|
+
data: string,
|
|
96
|
+
userId: string,
|
|
97
|
+
): [string, string] {
|
|
98
|
+
return [
|
|
99
|
+
DELETE_RELATIONS_SYSTEM_PROMPT.replace("USER_ID", userId),
|
|
100
|
+
`Here are the existing memories: ${existingMemoriesString} \n\n New Information: ${data}`,
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function formatEntities(
|
|
105
|
+
entities: Array<{
|
|
106
|
+
source: string;
|
|
107
|
+
relationship: string;
|
|
108
|
+
destination: string;
|
|
109
|
+
}>,
|
|
110
|
+
): string {
|
|
111
|
+
return entities
|
|
112
|
+
.map((e) => `${e.source} -- ${e.relationship} -- ${e.destination}`)
|
|
113
|
+
.join("\n");
|
|
114
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from "./memory";
|
|
2
|
+
export * from "./memory/memory.types";
|
|
3
|
+
export * from "./types";
|
|
4
|
+
export * from "./prompts";
|
|
5
|
+
export * from "./embeddings/base";
|
|
6
|
+
export * from "./embeddings/openai";
|
|
7
|
+
export * from "./embeddings/google";
|
|
8
|
+
export * from "./llms/base";
|
|
9
|
+
export * from "./llms/openai";
|
|
10
|
+
export * from "./llms/openai_structured";
|
|
11
|
+
export * from "./vectorstores/base";
|
|
12
|
+
export * from "./vectorstores/llm";
|
|
13
|
+
export * from "./vectorstores/lite";
|
|
14
|
+
export * from "./utils/factory";
|
package/src/llms/base.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Message } from "../types";
|
|
3
|
+
|
|
4
|
+
export interface LLMResponse {
|
|
5
|
+
content: string;
|
|
6
|
+
role: string;
|
|
7
|
+
toolCalls?: Array<{
|
|
8
|
+
name: string;
|
|
9
|
+
arguments: string;
|
|
10
|
+
}>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface LLM {
|
|
14
|
+
generateResponse(
|
|
15
|
+
messages: Array<{ role: string; content: string }>,
|
|
16
|
+
response_format?: any,
|
|
17
|
+
tools?: any[],structuredOutput?: boolean, model?:string
|
|
18
|
+
): Promise<any>;
|
|
19
|
+
generateChat(messages: Message[]): Promise<LLMResponse>;
|
|
20
|
+
}
|