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,64 @@
|
|
|
1
|
+
export class BM25 {
|
|
2
|
+
private documents: string[][];
|
|
3
|
+
private k1: number;
|
|
4
|
+
private b: number;
|
|
5
|
+
private avgDocLength: number;
|
|
6
|
+
private docFreq: Map<string, number>;
|
|
7
|
+
private docLengths: number[];
|
|
8
|
+
private idf: Map<string, number>;
|
|
9
|
+
|
|
10
|
+
constructor(documents: string[][], k1 = 1.5, b = 0.75) {
|
|
11
|
+
this.documents = documents;
|
|
12
|
+
this.k1 = k1;
|
|
13
|
+
this.b = b;
|
|
14
|
+
this.docLengths = documents.map((doc) => doc.length);
|
|
15
|
+
this.avgDocLength =
|
|
16
|
+
this.docLengths.reduce((a, b) => a + b, 0) / documents.length;
|
|
17
|
+
this.docFreq = new Map();
|
|
18
|
+
this.idf = new Map();
|
|
19
|
+
this.computeIdf();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private computeIdf() {
|
|
23
|
+
const N = this.documents.length;
|
|
24
|
+
|
|
25
|
+
// Count document frequency for each term
|
|
26
|
+
for (const doc of this.documents) {
|
|
27
|
+
const terms = new Set(doc);
|
|
28
|
+
for (const term of terms) {
|
|
29
|
+
this.docFreq.set(term, (this.docFreq.get(term) || 0) + 1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Compute IDF for each term
|
|
34
|
+
for (const [term, freq] of this.docFreq) {
|
|
35
|
+
this.idf.set(term, Math.log((N - freq + 0.5) / (freq + 0.5) + 1));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private score(query: string[], doc: string[], index: number): number {
|
|
40
|
+
let score = 0;
|
|
41
|
+
const docLength = this.docLengths[index];
|
|
42
|
+
|
|
43
|
+
for (const term of query) {
|
|
44
|
+
const tf = doc.filter((t) => t === term).length;
|
|
45
|
+
const idf = this.idf.get(term) || 0;
|
|
46
|
+
|
|
47
|
+
score +=
|
|
48
|
+
(idf * tf * (this.k1 + 1)) /
|
|
49
|
+
(tf +
|
|
50
|
+
this.k1 * (1 - this.b + (this.b * docLength) / this.avgDocLength));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return score;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
search(query: string[]): string[][] {
|
|
57
|
+
const scores = this.documents.map((doc, idx) => ({
|
|
58
|
+
doc,
|
|
59
|
+
score: this.score(query, doc, idx),
|
|
60
|
+
}));
|
|
61
|
+
|
|
62
|
+
return scores.sort((a, b) => b.score - a.score).map((item) => item.doc);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { OpenAIEmbedder } from "../embeddings/openai";
|
|
2
|
+
import { OpenAILLM } from "../llms/openai";
|
|
3
|
+
import { OpenAIStructuredLLM } from "../llms/openai_structured";
|
|
4
|
+
import {
|
|
5
|
+
EmbeddingConfig,
|
|
6
|
+
HistoryStoreConfig,
|
|
7
|
+
LLMConfig,
|
|
8
|
+
VectorStoreConfig,
|
|
9
|
+
} from "../types";
|
|
10
|
+
import { Embedder } from "../embeddings/base";
|
|
11
|
+
import { LLM } from "../llms/base";
|
|
12
|
+
import { VectorStore } from "../vectorstores/base";
|
|
13
|
+
import { MemoryHistoryManager } from "../storage/MemoryHistoryManager";
|
|
14
|
+
import { HistoryManager } from "../storage/base";
|
|
15
|
+
import { GoogleEmbedder } from "../embeddings/google";
|
|
16
|
+
import { GoogleLLM } from "../llms/google";
|
|
17
|
+
import { LLMVectorStore } from '../vectorstores/llm';
|
|
18
|
+
import { LiteVectorStore } from "../vectorstores/lite";
|
|
19
|
+
|
|
20
|
+
export class EmbedderFactory {
|
|
21
|
+
static create(provider: string, config: EmbeddingConfig): Embedder {
|
|
22
|
+
switch (provider.toLowerCase()) {
|
|
23
|
+
case "openai":
|
|
24
|
+
return new OpenAIEmbedder(config);
|
|
25
|
+
case "google":
|
|
26
|
+
return new GoogleEmbedder(config);
|
|
27
|
+
default:
|
|
28
|
+
throw new Error(`Unsupported embedder provider: ${provider}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class LLMFactory {
|
|
34
|
+
static create(provider: string, config: LLMConfig): LLM {
|
|
35
|
+
switch (provider.toLowerCase()) {
|
|
36
|
+
case "openai":
|
|
37
|
+
return new OpenAILLM(config);
|
|
38
|
+
case "openai_structured":
|
|
39
|
+
return new OpenAIStructuredLLM(config);
|
|
40
|
+
case "google":
|
|
41
|
+
return new GoogleLLM(config);
|
|
42
|
+
default:
|
|
43
|
+
throw new Error(`Unsupported LLM provider: ${provider}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
export class HistoryManagerFactory {
|
|
50
|
+
static create(provider: string, config: HistoryStoreConfig): HistoryManager {
|
|
51
|
+
switch (provider.toLowerCase()) {
|
|
52
|
+
case "sqlite":
|
|
53
|
+
case "memory":
|
|
54
|
+
return new MemoryHistoryManager();
|
|
55
|
+
default:
|
|
56
|
+
throw new Error(`Unsupported history store provider: ${provider}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface Logger {
|
|
2
|
+
info: (message: string) => void;
|
|
3
|
+
error: (message: string) => void;
|
|
4
|
+
debug: (message: string) => void;
|
|
5
|
+
warn: (message: string) => void;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const logger: Logger = {
|
|
9
|
+
info: (message: string) => console.log(`[INFO] ${message}`),
|
|
10
|
+
error: (message: string) => console.error(`[ERROR] ${message}`),
|
|
11
|
+
debug: (message: string) => console.debug(`[DEBUG] ${message}`),
|
|
12
|
+
warn: (message: string) => console.warn(`[WARN] ${message}`),
|
|
13
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { OpenAILLM } from "../llms/openai";
|
|
2
|
+
import { Message } from "../types";
|
|
3
|
+
|
|
4
|
+
const get_image_description = async (image_url: string) => {
|
|
5
|
+
const llm = new OpenAILLM({
|
|
6
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
7
|
+
});
|
|
8
|
+
const response = await llm.generateResponse([
|
|
9
|
+
{
|
|
10
|
+
role: "user",
|
|
11
|
+
content:
|
|
12
|
+
"Provide a description of the image and do not include any additional text.",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
role: "user",
|
|
16
|
+
content: { type: "image_url", image_url: { url: image_url } },
|
|
17
|
+
},
|
|
18
|
+
]);
|
|
19
|
+
return response;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const parse_vision_messages = async (messages: Message[]) => {
|
|
23
|
+
const parsed_messages = [];
|
|
24
|
+
for (const message of messages) {
|
|
25
|
+
let new_message = {
|
|
26
|
+
role: message.role,
|
|
27
|
+
content: "",
|
|
28
|
+
};
|
|
29
|
+
if (message.role !== "system") {
|
|
30
|
+
if (
|
|
31
|
+
typeof message.content === "object" &&
|
|
32
|
+
message.content.type === "image_url"
|
|
33
|
+
) {
|
|
34
|
+
const description = await get_image_description(
|
|
35
|
+
message.content.image_url.url,
|
|
36
|
+
);
|
|
37
|
+
new_message.content =
|
|
38
|
+
typeof description === "string"
|
|
39
|
+
? description
|
|
40
|
+
: JSON.stringify(description);
|
|
41
|
+
parsed_messages.push(new_message);
|
|
42
|
+
} else parsed_messages.push(message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return parsed_messages;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export { parse_vision_messages };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TelemetryClient,
|
|
3
|
+
TelemetryInstance,
|
|
4
|
+
TelemetryEventData,
|
|
5
|
+
} from "./telemetry.types";
|
|
6
|
+
|
|
7
|
+
let version = "2.1.16";
|
|
8
|
+
|
|
9
|
+
// Safely check for process.env in different environments
|
|
10
|
+
let MEM0_TELEMETRY = true;
|
|
11
|
+
try {
|
|
12
|
+
MEM0_TELEMETRY = process?.env?.MEM0_TELEMETRY === "false" ? false : true;
|
|
13
|
+
} catch (error) {}
|
|
14
|
+
const POSTHOG_API_KEY = "phc_hgJkUVJFYtmaJqrvf6CYN67TIQ8yhXAkWzUn9AMU4yX";
|
|
15
|
+
const POSTHOG_HOST = "https://us.i.posthog.com/i/v0/e/";
|
|
16
|
+
|
|
17
|
+
class UnifiedTelemetry implements TelemetryClient {
|
|
18
|
+
private apiKey: string;
|
|
19
|
+
private host: string;
|
|
20
|
+
|
|
21
|
+
constructor(projectApiKey: string, host: string) {
|
|
22
|
+
this.apiKey = projectApiKey;
|
|
23
|
+
this.host = host;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async captureEvent(distinctId: string, eventName: string, properties = {}) {
|
|
27
|
+
if (!MEM0_TELEMETRY) return;
|
|
28
|
+
|
|
29
|
+
const eventProperties = {
|
|
30
|
+
client_version: version,
|
|
31
|
+
timestamp: new Date().toISOString(),
|
|
32
|
+
...properties,
|
|
33
|
+
$process_person_profile:
|
|
34
|
+
distinctId === "anonymous" || distinctId === "anonymous-supabase"
|
|
35
|
+
? false
|
|
36
|
+
: true,
|
|
37
|
+
$lib: "posthog-node",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const payload = {
|
|
41
|
+
api_key: this.apiKey,
|
|
42
|
+
distinct_id: distinctId,
|
|
43
|
+
event: eventName,
|
|
44
|
+
properties: eventProperties,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
const response = await fetch(this.host, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: {
|
|
51
|
+
"Content-Type": "application/json",
|
|
52
|
+
},
|
|
53
|
+
body: JSON.stringify(payload),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
console.error("Telemetry event capture failed:", await response.text());
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error("Telemetry event capture failed:", error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async shutdown() {
|
|
65
|
+
// No shutdown needed for direct API calls
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const telemetry = new UnifiedTelemetry(POSTHOG_API_KEY, POSTHOG_HOST);
|
|
70
|
+
|
|
71
|
+
async function captureClientEvent(
|
|
72
|
+
eventName: string,
|
|
73
|
+
instance: TelemetryInstance,
|
|
74
|
+
additionalData: Record<string, any> = {},
|
|
75
|
+
) {
|
|
76
|
+
if (!instance.telemetryId) {
|
|
77
|
+
console.warn("No telemetry ID found for instance");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const eventData: TelemetryEventData = {
|
|
82
|
+
function: `${instance.constructor.name}`,
|
|
83
|
+
method: eventName,
|
|
84
|
+
api_host: instance.host,
|
|
85
|
+
timestamp: new Date().toISOString(),
|
|
86
|
+
client_version: version,
|
|
87
|
+
client_source: "nodejs",
|
|
88
|
+
...additionalData,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
await telemetry.captureEvent(
|
|
92
|
+
instance.telemetryId,
|
|
93
|
+
`mem0.${eventName}`,
|
|
94
|
+
eventData,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export { telemetry, captureClientEvent };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface TelemetryClient {
|
|
2
|
+
captureEvent(
|
|
3
|
+
distinctId: string,
|
|
4
|
+
eventName: string,
|
|
5
|
+
properties?: Record<string, any>,
|
|
6
|
+
): Promise<void>;
|
|
7
|
+
shutdown(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface TelemetryInstance {
|
|
11
|
+
telemetryId: string;
|
|
12
|
+
constructor: {
|
|
13
|
+
name: string;
|
|
14
|
+
};
|
|
15
|
+
host?: string;
|
|
16
|
+
apiKey?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface TelemetryEventData {
|
|
20
|
+
function: string;
|
|
21
|
+
method: string;
|
|
22
|
+
api_host?: string;
|
|
23
|
+
timestamp?: string;
|
|
24
|
+
client_source: "browser" | "nodejs";
|
|
25
|
+
client_version: string;
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface TelemetryOptions {
|
|
30
|
+
enabled?: boolean;
|
|
31
|
+
apiKey?: string;
|
|
32
|
+
host?: string;
|
|
33
|
+
version?: string;
|
|
34
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SearchFilters, VectorStoreResult } from "../types";
|
|
2
|
+
|
|
3
|
+
export interface VectorStore {
|
|
4
|
+
insert(
|
|
5
|
+
vectors: number[][],
|
|
6
|
+
ids: string[],
|
|
7
|
+
payloads: Record<string, any>[],
|
|
8
|
+
): Promise<void>;
|
|
9
|
+
search(
|
|
10
|
+
query: number[],
|
|
11
|
+
limit?: number,
|
|
12
|
+
filters?: SearchFilters,
|
|
13
|
+
): Promise<VectorStoreResult[]>;
|
|
14
|
+
get(vectorId: string): Promise<VectorStoreResult | null>;
|
|
15
|
+
update(
|
|
16
|
+
vectorId: string,
|
|
17
|
+
vector: number[],
|
|
18
|
+
payload: Record<string, any>,
|
|
19
|
+
): Promise<void>;
|
|
20
|
+
delete(vectorId: string): Promise<void>;
|
|
21
|
+
deleteCol(): Promise<void>;
|
|
22
|
+
list(
|
|
23
|
+
filters?: SearchFilters,
|
|
24
|
+
limit?: number,
|
|
25
|
+
): Promise<[VectorStoreResult[], number]>;
|
|
26
|
+
getInstanceId(): string;
|
|
27
|
+
}
|