@ai.ntellect/core 0.1.81 → 0.1.84
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/README.FR.md +31 -60
- package/README.md +74 -96
- package/agent/handlers/ActionHandler.ts +1 -1
- package/agent/index.ts +85 -43
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +61 -33
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/llm/evaluator/context.js +3 -5
- package/dist/llm/evaluator/index.js +25 -29
- package/dist/llm/orchestrator/context.js +1 -1
- package/dist/llm/orchestrator/index.js +30 -9
- package/dist/llm/synthesizer/index.d.ts +1 -5
- package/dist/llm/synthesizer/index.js +1 -5
- package/dist/memory/cache.d.ts +5 -5
- package/dist/memory/cache.js +19 -49
- package/dist/memory/persistent.d.ts +1 -1
- package/dist/memory/persistent.js +5 -6
- package/dist/services/queue.js +21 -8
- package/dist/t.d.ts +46 -0
- package/dist/t.js +102 -0
- package/dist/test.d.ts +68 -0
- package/dist/test.js +167 -0
- package/dist/types.d.ts +27 -3
- package/dist/types.js +9 -1
- package/dist/utils/inject-actions.js +1 -1
- package/dist/utils/queue-item-transformer.d.ts +2 -2
- package/dist/utils/queue-item-transformer.js +5 -6
- package/dist/utils/sanitize-results.d.ts +17 -0
- package/dist/utils/sanitize-results.js +60 -0
- package/index.ts +0 -1
- package/llm/evaluator/context.ts +3 -5
- package/llm/evaluator/index.ts +25 -31
- package/llm/orchestrator/context.ts +1 -1
- package/llm/orchestrator/index.ts +42 -13
- package/llm/synthesizer/index.ts +2 -10
- package/memory/cache.ts +23 -61
- package/memory/persistent.ts +7 -6
- package/package.json +3 -1
- package/services/queue.ts +21 -12
- package/t.ts +133 -0
- package/types.ts +14 -3
- package/utils/inject-actions.ts +1 -1
- package/utils/queue-item-transformer.ts +25 -11
- package/utils/sanitize-results.ts +66 -0
package/dist/agent/index.js
CHANGED
@@ -5,12 +5,14 @@ const evaluator_1 = require("../llm/evaluator");
|
|
5
5
|
const synthesizer_1 = require("../llm/synthesizer");
|
6
6
|
const types_1 = require("../types");
|
7
7
|
const queue_item_transformer_1 = require("../utils/queue-item-transformer");
|
8
|
+
const sanitize_results_1 = require("../utils/sanitize-results");
|
8
9
|
const ActionHandler_1 = require("./handlers/ActionHandler");
|
9
10
|
class Agent {
|
10
11
|
constructor({ user, orchestrator, persistentMemory, cacheMemory, stream, maxEvaluatorIteration = 1, }) {
|
11
12
|
this.SIMILARITY_THRESHOLD = 95;
|
12
13
|
this.MAX_RESULTS = 1;
|
13
14
|
this.evaluatorIteration = 0;
|
15
|
+
this.accumulatedResults = [];
|
14
16
|
this.user = user;
|
15
17
|
this.orchestrator = orchestrator;
|
16
18
|
this.cacheMemory = cacheMemory;
|
@@ -18,21 +20,40 @@ class Agent {
|
|
18
20
|
this.stream = stream;
|
19
21
|
this.maxEvaluatorIteration = maxEvaluatorIteration;
|
20
22
|
this.actionHandler = new ActionHandler_1.ActionHandler();
|
23
|
+
this.accumulatedResults = [];
|
21
24
|
}
|
22
25
|
async process(prompt, contextualizedPrompt, events) {
|
23
|
-
|
24
|
-
|
25
|
-
if (
|
26
|
-
|
26
|
+
let actions = undefined;
|
27
|
+
let isSimilar = false;
|
28
|
+
if (this.cacheMemory) {
|
29
|
+
const similarActions = await this.cacheMemory.findSimilarQueries(prompt, {
|
30
|
+
similarityThreshold: this.SIMILARITY_THRESHOLD,
|
31
|
+
maxResults: this.MAX_RESULTS,
|
32
|
+
userId: this.user.id,
|
33
|
+
scope: types_1.MemoryScope.GLOBAL,
|
34
|
+
});
|
35
|
+
if (similarActions.length > 0) {
|
36
|
+
actions = queue_item_transformer_1.QueueItemTransformer.transformActionsToQueueItems(similarActions[0].data);
|
37
|
+
isSimilar = true;
|
38
|
+
}
|
39
|
+
}
|
40
|
+
if (!actions?.length && !isSimilar) {
|
41
|
+
console.log("No similar actions found in cache for query: ", prompt);
|
42
|
+
console.log("Requesting orchestrator for actions..");
|
43
|
+
const request = await this.orchestrator.process(contextualizedPrompt);
|
44
|
+
events.onMessage?.(request);
|
45
|
+
actions = request.actions;
|
46
|
+
}
|
47
|
+
return actions && actions.length > 0
|
48
|
+
? this.handleActions({
|
27
49
|
initialPrompt: prompt,
|
28
50
|
contextualizedPrompt: contextualizedPrompt,
|
29
|
-
actions:
|
30
|
-
}, events)
|
31
|
-
|
51
|
+
actions: actions,
|
52
|
+
}, events)
|
53
|
+
: undefined;
|
32
54
|
}
|
33
55
|
async handleActions({ initialPrompt, contextualizedPrompt, actions, }, events) {
|
34
|
-
const
|
35
|
-
const queueItems = this.transformActions(actions, similarActions);
|
56
|
+
const queueItems = this.transformActions(actions);
|
36
57
|
const actionsResult = await this.actionHandler.executeActions(queueItems, this.orchestrator.tools, {
|
37
58
|
onQueueStart: events.onQueueStart,
|
38
59
|
onActionStart: events.onActionStart,
|
@@ -40,55 +61,62 @@ class Agent {
|
|
40
61
|
onQueueComplete: events.onQueueComplete,
|
41
62
|
onConfirmationRequired: events.onConfirmationRequired,
|
42
63
|
});
|
64
|
+
this.accumulatedResults = [
|
65
|
+
...this.accumulatedResults,
|
66
|
+
...actionsResult.data,
|
67
|
+
];
|
43
68
|
if (this.evaluatorIteration >= this.maxEvaluatorIteration) {
|
44
|
-
return this.handleActionResults({
|
69
|
+
return this.handleActionResults({
|
70
|
+
data: this.accumulatedResults,
|
71
|
+
initialPrompt,
|
72
|
+
});
|
45
73
|
}
|
46
74
|
const evaluator = new evaluator_1.Evaluator(this.orchestrator.tools, this.persistentMemory);
|
47
|
-
|
75
|
+
console.log("Accumulated results:");
|
76
|
+
console.dir(this.accumulatedResults, { depth: null });
|
77
|
+
const sanitizedResults = sanitize_results_1.ResultSanitizer.sanitize(this.accumulatedResults);
|
78
|
+
const evaluation = await evaluator.process(initialPrompt, contextualizedPrompt, sanitizedResults);
|
48
79
|
events.onMessage?.(evaluation);
|
49
|
-
if (evaluation.
|
80
|
+
if (evaluation.isNextActionNeeded) {
|
50
81
|
this.evaluatorIteration++;
|
51
82
|
return this.handleActions({
|
52
83
|
initialPrompt: contextualizedPrompt,
|
53
84
|
contextualizedPrompt: initialPrompt,
|
54
|
-
actions: evaluation.
|
85
|
+
actions: evaluation.nextActionsNeeded,
|
55
86
|
}, events);
|
56
87
|
}
|
57
|
-
if (!this.actionHandler.hasNonPrepareActions(
|
88
|
+
if (!this.actionHandler.hasNonPrepareActions(this.accumulatedResults)) {
|
58
89
|
return {
|
59
|
-
data:
|
90
|
+
data: this.accumulatedResults,
|
60
91
|
initialPrompt,
|
61
92
|
};
|
62
93
|
}
|
63
|
-
return this.handleActionResults({
|
94
|
+
return this.handleActionResults({
|
95
|
+
data: this.accumulatedResults,
|
96
|
+
initialPrompt,
|
97
|
+
});
|
64
98
|
}
|
65
99
|
async handleActionResults(actionsResult) {
|
66
100
|
const synthesizer = new synthesizer_1.Synthesizer();
|
101
|
+
const sanitizedResults = sanitize_results_1.ResultSanitizer.sanitize(this.accumulatedResults);
|
67
102
|
const summaryData = JSON.stringify({
|
68
|
-
result:
|
103
|
+
result: sanitizedResults,
|
69
104
|
initialPrompt: actionsResult.initialPrompt,
|
70
105
|
});
|
106
|
+
this.accumulatedResults = [];
|
107
|
+
this.evaluatorIteration = 0;
|
108
|
+
await this.cacheMemory?.createMemory({
|
109
|
+
content: actionsResult.initialPrompt,
|
110
|
+
data: actionsResult.data,
|
111
|
+
scope: types_1.MemoryScope.GLOBAL,
|
112
|
+
type: types_1.MemoryType.ACTION,
|
113
|
+
});
|
71
114
|
return this.stream
|
72
115
|
? (await synthesizer.streamProcess(summaryData)).toDataStreamResponse()
|
73
116
|
: await synthesizer.process(summaryData);
|
74
117
|
}
|
75
|
-
|
76
|
-
if (!this.cacheMemory) {
|
77
|
-
return [];
|
78
|
-
}
|
79
|
-
return this.cacheMemory.findBestMatches(prompt, {
|
80
|
-
similarityThreshold: this.SIMILARITY_THRESHOLD,
|
81
|
-
maxResults: this.MAX_RESULTS,
|
82
|
-
userId: this.user.id,
|
83
|
-
scope: types_1.MemoryScope.USER,
|
84
|
-
});
|
85
|
-
}
|
86
|
-
transformActions(actions, similarActions) {
|
118
|
+
transformActions(actions) {
|
87
119
|
let predefinedActions = queue_item_transformer_1.QueueItemTransformer.transformActionsToQueueItems(actions) || [];
|
88
|
-
if (similarActions?.length > 0) {
|
89
|
-
predefinedActions =
|
90
|
-
queue_item_transformer_1.QueueItemTransformer.transformFromSimilarActions(similarActions) || [];
|
91
|
-
}
|
92
120
|
return predefinedActions;
|
93
121
|
}
|
94
122
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -17,7 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./agent"), exports);
|
18
18
|
__exportStar(require("./llm/orchestrator"), exports);
|
19
19
|
__exportStar(require("./llm/synthesizer"), exports);
|
20
|
-
__exportStar(require("./services/queue"), exports);
|
21
20
|
__exportStar(require("./types"), exports);
|
22
21
|
__exportStar(require("./memory/cache"), exports);
|
23
22
|
__exportStar(require("./memory/persistent"), exports);
|
@@ -9,7 +9,7 @@ exports.evaluatorContext = {
|
|
9
9
|
"Verify if all required actions were executed successfully",
|
10
10
|
"Check if the results match the initial goal",
|
11
11
|
"Identify any missing or incomplete information",
|
12
|
-
"
|
12
|
+
"Extra and relevant information they don't have be stored in the memory: link symbol to token address, name to wallet, etc.",
|
13
13
|
],
|
14
14
|
warnings: [
|
15
15
|
"NEVER modify the results directly",
|
@@ -31,10 +31,8 @@ exports.evaluatorContext = {
|
|
31
31
|
1. Success status with explanation (no action needed)
|
32
32
|
2. Next actions needed (if any)
|
33
33
|
3. Why you are doing the next actions or why you are not doing them
|
34
|
-
4. Extract relevant information to remember
|
35
|
-
5.
|
36
|
-
6. For each facts, generate a hypothetical query to search in the persistent memory.
|
37
|
-
7. For each facts, generate a memoryType (You have 3 memory types: episodic, semantic, procedural)
|
34
|
+
4. Extract relevant information to remember
|
35
|
+
5. For each facts, generate a memoryType (3 memory types: episodic, semantic, procedural)
|
38
36
|
`;
|
39
37
|
},
|
40
38
|
};
|
@@ -17,35 +17,31 @@ class Evaluator {
|
|
17
17
|
const response = await (0, ai_1.generateObject)({
|
18
18
|
model: this.model,
|
19
19
|
schema: zod_1.z.object({
|
20
|
-
|
21
|
-
|
22
|
-
parameters: zod_1.z.object({
|
23
|
-
name: zod_1.z.string(),
|
24
|
-
value: zod_1.z.string(),
|
25
|
-
}),
|
26
|
-
})),
|
27
|
-
why: zod_1.z.string(),
|
28
|
-
isImportantToRemember: zod_1.z.boolean(),
|
29
|
-
importantToRemembers: zod_1.z.array(zod_1.z.object({
|
20
|
+
isRemindNeeded: zod_1.z.boolean(),
|
21
|
+
extraInformationsToRemember: zod_1.z.array(zod_1.z.object({
|
30
22
|
memoryType: zod_1.z.string(),
|
31
|
-
|
32
|
-
|
23
|
+
content: zod_1.z.string(),
|
24
|
+
data: zod_1.z.string(),
|
33
25
|
})),
|
26
|
+
response: zod_1.z.string(),
|
27
|
+
isNextActionNeeded: zod_1.z.boolean(),
|
28
|
+
nextActionsNeeded: types_1.ActionSchema,
|
29
|
+
why: zod_1.z.string(),
|
34
30
|
}),
|
35
31
|
prompt: prompt,
|
36
32
|
system: context_1.evaluatorContext.compose(goal, results, this.tools),
|
37
33
|
});
|
38
34
|
const validatedResponse = {
|
39
35
|
...response.object,
|
40
|
-
nextActions: response.object.
|
36
|
+
nextActions: response.object.nextActionsNeeded.map((action) => ({
|
41
37
|
...action,
|
42
38
|
parameters: action.parameters || {},
|
43
39
|
})),
|
44
40
|
};
|
45
|
-
if (validatedResponse.
|
46
|
-
for (const item of validatedResponse.
|
41
|
+
if (validatedResponse.isRemindNeeded) {
|
42
|
+
for (const item of validatedResponse.extraInformationsToRemember) {
|
47
43
|
// Check if the item is already in the memory
|
48
|
-
const memories = await this.memory.searchSimilarQueries(item.
|
44
|
+
const memories = await this.memory.searchSimilarQueries(item.content, {
|
49
45
|
similarityThreshold: 95,
|
50
46
|
});
|
51
47
|
if (memories.length > 0) {
|
@@ -56,14 +52,14 @@ class Evaluator {
|
|
56
52
|
}
|
57
53
|
if (memories.length === 0) {
|
58
54
|
console.log("Adding to memory", {
|
59
|
-
query: item.
|
60
|
-
data: item.
|
55
|
+
query: item.content,
|
56
|
+
data: item.data,
|
61
57
|
});
|
62
|
-
await this.memory.
|
58
|
+
await this.memory.createMemory({
|
63
59
|
id: crypto.randomUUID(),
|
64
60
|
purpose: item.memoryType,
|
65
|
-
query: item.
|
66
|
-
data: item.
|
61
|
+
query: item.content,
|
62
|
+
data: item.data,
|
67
63
|
scope: types_1.MemoryScope.GLOBAL,
|
68
64
|
createdAt: new Date(),
|
69
65
|
});
|
@@ -79,20 +75,20 @@ class Evaluator {
|
|
79
75
|
console.log("Evaluator error");
|
80
76
|
console.dir(error.value, { depth: null });
|
81
77
|
console.error(error.message);
|
82
|
-
if (error.value.
|
83
|
-
for (const item of error.value.
|
78
|
+
if (error.value.extraInformationsToRemember.length > 0) {
|
79
|
+
for (const item of error.value.extraInformationsToRemember) {
|
84
80
|
// Check if the item is already in the memory
|
85
|
-
const memories = await this.memory.searchSimilarQueries(item.
|
81
|
+
const memories = await this.memory.searchSimilarQueries(item.content);
|
86
82
|
if (memories.length === 0) {
|
87
83
|
console.log("Adding to memory", {
|
88
|
-
query: item.
|
89
|
-
data: item.
|
84
|
+
query: item.content,
|
85
|
+
data: item.data,
|
90
86
|
});
|
91
|
-
await this.memory.
|
87
|
+
await this.memory.createMemory({
|
92
88
|
id: crypto.randomUUID(),
|
93
89
|
purpose: "importantToRemember",
|
94
|
-
query: item.
|
95
|
-
data: item.
|
90
|
+
query: item.content,
|
91
|
+
data: item.data,
|
96
92
|
scope: types_1.MemoryScope.USER,
|
97
93
|
createdAt: new Date(),
|
98
94
|
});
|
@@ -7,7 +7,7 @@ exports.orchestratorContext = {
|
|
7
7
|
guidelines: {
|
8
8
|
important: [
|
9
9
|
"If there is no action to do, you must answer in the 'answer' field.",
|
10
|
-
"If some parameters are not clear or missing, YOU MUST ask the user for them.",
|
10
|
+
"If some parameters are not clear or missing, don't add the action, YOU MUST ask the user for them.",
|
11
11
|
"ALWAYS use the same language as user request. (If it's English, use English, if it's French, use French, etc.)",
|
12
12
|
"For QUESTIONS or ANALYSIS, BEFORE executing ANY actions, you MUST search in memory for similar queries AS THE ONLY ACTION TO EXECUTE.",
|
13
13
|
],
|
@@ -4,6 +4,7 @@ exports.Orchestrator = void 0;
|
|
4
4
|
const openai_1 = require("@ai-sdk/openai");
|
5
5
|
const ai_1 = require("ai");
|
6
6
|
const zod_1 = require("zod");
|
7
|
+
const types_1 = require("../../types");
|
7
8
|
const context_1 = require("./context");
|
8
9
|
class Orchestrator {
|
9
10
|
constructor(tools, memory) {
|
@@ -17,8 +18,34 @@ class Orchestrator {
|
|
17
18
|
parameters: zod_1.z.object({
|
18
19
|
query: zod_1.z.string(),
|
19
20
|
}),
|
20
|
-
execute: async (
|
21
|
-
const memories = await this.memory.searchSimilarQueries(
|
21
|
+
execute: async ({ query }) => {
|
22
|
+
const memories = await this.memory.searchSimilarQueries(query, {
|
23
|
+
similarityThreshold: 95,
|
24
|
+
});
|
25
|
+
return memories;
|
26
|
+
},
|
27
|
+
},
|
28
|
+
{
|
29
|
+
name: "save_memory",
|
30
|
+
description: "Save relevant information in the internal knowledge base",
|
31
|
+
parameters: zod_1.z.object({
|
32
|
+
query: zod_1.z.string(),
|
33
|
+
memoryType: zod_1.z.string(),
|
34
|
+
data: zod_1.z.any(),
|
35
|
+
scope: zod_1.z.string().default("GLOBAL").describe("GLOBAL or USER"),
|
36
|
+
userId: zod_1.z.string(),
|
37
|
+
whyStored: zod_1.z.string(),
|
38
|
+
}),
|
39
|
+
execute: async ({ query, purpose, data, scope, userId, }) => {
|
40
|
+
const memories = await this.memory.createMemory({
|
41
|
+
query,
|
42
|
+
purpose,
|
43
|
+
data,
|
44
|
+
scope,
|
45
|
+
userId,
|
46
|
+
createdAt: new Date(),
|
47
|
+
id: crypto.randomUUID(),
|
48
|
+
});
|
22
49
|
return memories;
|
23
50
|
},
|
24
51
|
},
|
@@ -29,13 +56,7 @@ class Orchestrator {
|
|
29
56
|
const response = await (0, ai_1.generateObject)({
|
30
57
|
model: this.model,
|
31
58
|
schema: zod_1.z.object({
|
32
|
-
actions:
|
33
|
-
name: zod_1.z.string(),
|
34
|
-
parameters: zod_1.z.object({
|
35
|
-
name: zod_1.z.string(),
|
36
|
-
value: zod_1.z.string(),
|
37
|
-
}),
|
38
|
-
})),
|
59
|
+
actions: types_1.ActionSchema,
|
39
60
|
answer: zod_1.z.string(),
|
40
61
|
}),
|
41
62
|
prompt: prompt,
|
@@ -5,11 +5,7 @@ export declare class Synthesizer implements BaseLLM {
|
|
5
5
|
process(prompt: string, onFinish?: (event: any) => void): Promise<{
|
6
6
|
actions: {
|
7
7
|
name: string;
|
8
|
-
|
9
|
-
explain: {
|
10
|
-
how: string;
|
11
|
-
why: string;
|
12
|
-
};
|
8
|
+
reasoning: string;
|
13
9
|
}[];
|
14
10
|
response: string;
|
15
11
|
} | StreamTextResult<Record<string, any>>>;
|
@@ -16,11 +16,7 @@ class Synthesizer {
|
|
16
16
|
schema: zod_1.z.object({
|
17
17
|
actions: zod_1.z.array(zod_1.z.object({
|
18
18
|
name: zod_1.z.string(),
|
19
|
-
|
20
|
-
explain: zod_1.z.object({
|
21
|
-
how: zod_1.z.string(),
|
22
|
-
why: zod_1.z.string(),
|
23
|
-
}),
|
19
|
+
reasoning: zod_1.z.string(),
|
24
20
|
})),
|
25
21
|
response: zod_1.z.string(),
|
26
22
|
}),
|
package/dist/memory/cache.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { CacheMemoryOptions, CreateMemoryInput, MatchOptions, MemoryScope } from "../types";
|
1
|
+
import { CacheMemoryOptions, CacheMemoryType, CreateMemoryInput, MatchOptions, MemoryScope } from "../types";
|
2
2
|
export declare class CacheMemory {
|
3
3
|
private redis;
|
4
4
|
private readonly CACHE_PREFIX;
|
@@ -7,16 +7,16 @@ export declare class CacheMemory {
|
|
7
7
|
private initRedis;
|
8
8
|
private getMemoryKey;
|
9
9
|
private storeMemory;
|
10
|
-
|
10
|
+
findSimilarQueries(query: string, options?: MatchOptions & {
|
11
11
|
userId?: string;
|
12
12
|
scope?: MemoryScope;
|
13
13
|
}): Promise<{
|
14
14
|
data: any;
|
15
15
|
similarityPercentage: number;
|
16
|
-
|
16
|
+
query: string;
|
17
17
|
}[]>;
|
18
|
-
|
18
|
+
getAllMemories(scope?: MemoryScope, userId?: string): Promise<CacheMemoryType[]>;
|
19
19
|
private getMemoriesFromKeys;
|
20
|
-
createMemory(input: CreateMemoryInput): Promise<
|
20
|
+
createMemory(input: CreateMemoryInput): Promise<CacheMemoryType | undefined>;
|
21
21
|
private createSingleMemory;
|
22
22
|
}
|
package/dist/memory/cache.js
CHANGED
@@ -4,7 +4,6 @@ exports.CacheMemory = void 0;
|
|
4
4
|
const openai_1 = require("@ai-sdk/openai");
|
5
5
|
const ai_1 = require("ai");
|
6
6
|
const redis_1 = require("redis");
|
7
|
-
const zod_1 = require("zod");
|
8
7
|
const types_1 = require("../types");
|
9
8
|
class CacheMemory {
|
10
9
|
constructor(options = {}) {
|
@@ -43,30 +42,27 @@ class CacheMemory {
|
|
43
42
|
async storeMemory(memory) {
|
44
43
|
const prefix = this.getMemoryKey(memory.scope, memory.userId);
|
45
44
|
const key = `${prefix}${memory.id}`;
|
46
|
-
await this.redis.set(key, JSON.stringify(memory), {
|
45
|
+
const result = await this.redis.set(key, JSON.stringify(memory), {
|
47
46
|
EX: this.CACHE_TTL,
|
48
47
|
});
|
48
|
+
console.log("Cache memory created: ", result);
|
49
49
|
}
|
50
|
-
async
|
51
|
-
console.log("\
|
50
|
+
async findSimilarQueries(query, options = {}) {
|
51
|
+
console.log("\nSearching in cache for query:", query);
|
52
52
|
const { embedding } = await (0, ai_1.embed)({
|
53
53
|
model: openai_1.openai.embedding("text-embedding-3-small"),
|
54
54
|
value: query,
|
55
55
|
});
|
56
56
|
const memories = await this.getAllMemories(options.scope, options.userId);
|
57
|
-
console.log("\n📚 Found", memories.length, "
|
57
|
+
console.log("\n📚 Found", memories.length, "queries to compare with");
|
58
58
|
const matches = memories
|
59
59
|
.map((memory) => {
|
60
60
|
const similarity = (0, ai_1.cosineSimilarity)(embedding, memory.embedding);
|
61
61
|
const similarityPercentage = (similarity + 1) * 50; // Conversion en pourcentage
|
62
|
-
console.log(`\n📊 Memory "${memory.purpose}":
|
63
|
-
- Similarity: ${similarityPercentage.toFixed(2)}%
|
64
|
-
- Query: ${memory.query}`);
|
65
62
|
return {
|
66
63
|
data: memory.data,
|
64
|
+
query: memory.query,
|
67
65
|
similarityPercentage,
|
68
|
-
purpose: memory.purpose,
|
69
|
-
// Optionnel : ajouter des métadonnées utiles
|
70
66
|
memoryId: memory.id,
|
71
67
|
};
|
72
68
|
})
|
@@ -76,15 +72,14 @@ class CacheMemory {
|
|
76
72
|
? matches.slice(0, options.maxResults)
|
77
73
|
: matches;
|
78
74
|
if (results.length > 0) {
|
79
|
-
console.log("\n✨
|
75
|
+
console.log("\n✨ Similar queries found:");
|
80
76
|
results.forEach((match) => {
|
81
|
-
console.log(`- ${match.
|
77
|
+
console.log(`- ${match.query} (${match.similarityPercentage.toFixed(2)}%)`);
|
82
78
|
});
|
83
79
|
}
|
84
80
|
else {
|
85
81
|
console.log("No matches found");
|
86
82
|
}
|
87
|
-
console.dir({ results });
|
88
83
|
return results;
|
89
84
|
}
|
90
85
|
async getAllMemories(scope, userId) {
|
@@ -114,68 +109,43 @@ class CacheMemory {
|
|
114
109
|
return memories;
|
115
110
|
}
|
116
111
|
async createMemory(input) {
|
117
|
-
|
112
|
+
console.log("Searching for similar memory", input);
|
113
|
+
const existingPattern = await this.findSimilarQueries(input.content, {
|
118
114
|
similarityThreshold: 95,
|
119
115
|
userId: input.userId,
|
120
116
|
scope: input.scope,
|
121
117
|
});
|
122
118
|
if (existingPattern.length > 0) {
|
123
|
-
console.log("\
|
119
|
+
console.log("\nSimilar cache memory found:");
|
124
120
|
existingPattern.forEach((match) => {
|
125
|
-
console.log(`- ${match.
|
121
|
+
console.log(`- ${match.query} (${match.similarityPercentage.toFixed(2)}%)`);
|
126
122
|
});
|
123
|
+
console.log("Cache memory already exists. No need to create new one..");
|
127
124
|
return;
|
128
125
|
}
|
129
|
-
|
130
|
-
const
|
131
|
-
model: (0, openai_1.openai)("gpt-4"),
|
132
|
-
schema: zod_1.z.object({
|
133
|
-
request: zod_1.z.string().describe("The request to be performed"),
|
134
|
-
queries: zod_1.z.array(zod_1.z.object({ text: zod_1.z.string() })),
|
135
|
-
}),
|
136
|
-
prompt: `For this input: "${input.content}"
|
137
|
-
Generate similar variations that should match the same context.
|
138
|
-
Context type: ${input.type}
|
139
|
-
Data: ${JSON.stringify(input.data)}
|
140
|
-
- Keep variations natural and human-like
|
141
|
-
- Include the original input
|
142
|
-
- Add 3-5 variations`,
|
143
|
-
});
|
144
|
-
await this.createSingleMemory({
|
126
|
+
console.log("No similar memory found");
|
127
|
+
const memory = await this.createSingleMemory({
|
145
128
|
id: crypto.randomUUID(),
|
146
129
|
content: input.content,
|
147
130
|
type: input.type,
|
148
131
|
data: input.data,
|
149
|
-
purpose: variations.object.request,
|
150
132
|
userId: input.userId,
|
151
133
|
scope: input.scope,
|
152
134
|
});
|
153
|
-
|
154
|
-
if (variation.text !== input.content) {
|
155
|
-
await this.createSingleMemory({
|
156
|
-
id: crypto.randomUUID(),
|
157
|
-
content: variation.text,
|
158
|
-
type: input.type,
|
159
|
-
data: input.data,
|
160
|
-
purpose: variations.object.request,
|
161
|
-
userId: input.userId,
|
162
|
-
scope: input.scope,
|
163
|
-
});
|
164
|
-
}
|
165
|
-
});
|
166
|
-
await Promise.all(variationPromises);
|
167
|
-
return variations.object.request;
|
135
|
+
return memory;
|
168
136
|
}
|
169
137
|
async createSingleMemory(params) {
|
138
|
+
console.log("Creating new cache memory...", params.content);
|
139
|
+
console.log("Creating embedding...");
|
170
140
|
const { embedding } = await (0, ai_1.embed)({
|
171
141
|
model: openai_1.openai.embedding("text-embedding-3-small"),
|
172
142
|
value: params.content,
|
173
143
|
});
|
144
|
+
console.log("Embedding created");
|
174
145
|
const memory = {
|
175
146
|
id: params.id,
|
176
147
|
type: params.type,
|
177
148
|
data: params.data,
|
178
|
-
purpose: params.purpose,
|
179
149
|
query: params.content,
|
180
150
|
embedding,
|
181
151
|
userId: params.userId,
|
@@ -28,7 +28,6 @@ class PersistentMemory {
|
|
28
28
|
*/
|
29
29
|
async _makeRequest(path, options = {}) {
|
30
30
|
const url = `${this.host}${path}`;
|
31
|
-
console.log("Making request to:", url);
|
32
31
|
const response = await fetch(url, {
|
33
32
|
...options,
|
34
33
|
headers: {
|
@@ -102,7 +101,7 @@ class PersistentMemory {
|
|
102
101
|
/**
|
103
102
|
* Store a memory in the database
|
104
103
|
*/
|
105
|
-
async
|
104
|
+
async createMemory(memory) {
|
106
105
|
const indexName = this._getIndexName(memory.scope, memory.userId);
|
107
106
|
await this._getOrCreateIndex(indexName);
|
108
107
|
const chunks = await this.processContent(memory.query);
|
@@ -115,14 +114,14 @@ class PersistentMemory {
|
|
115
114
|
method: "POST",
|
116
115
|
body: JSON.stringify([document]),
|
117
116
|
});
|
118
|
-
console.log("Stored memory response:", response);
|
117
|
+
console.log("Stored persistent memory response:", response);
|
119
118
|
return response;
|
120
119
|
}
|
121
120
|
/**
|
122
121
|
* Find best matching memories
|
123
122
|
*/
|
124
123
|
async searchSimilarQueries(query, options = {}) {
|
125
|
-
console.log("\
|
124
|
+
console.log("\nSearching in persistent memory:", query);
|
126
125
|
// Generate embedding for the query
|
127
126
|
const { embedding: queryEmbedding } = await (0, ai_1.embed)({
|
128
127
|
model: openai_1.openai.embedding("text-embedding-3-small"),
|
@@ -162,7 +161,7 @@ class PersistentMemory {
|
|
162
161
|
searchResults.push(...userResults.hits);
|
163
162
|
}
|
164
163
|
}
|
165
|
-
console.log(
|
164
|
+
console.log(`📚 Found ${searchResults.length} queries in persistent memory`);
|
166
165
|
// Process and filter results using cosine similarity
|
167
166
|
const results = searchResults
|
168
167
|
.flatMap((hit) => {
|
@@ -183,7 +182,7 @@ class PersistentMemory {
|
|
183
182
|
.sort((a, b) => b.similarityPercentage - a.similarityPercentage);
|
184
183
|
// Log results
|
185
184
|
if (results.length > 0) {
|
186
|
-
console.log("\n✨
|
185
|
+
console.log("\n✨ Similar queries found in persistent memory:");
|
187
186
|
results.forEach((match) => {
|
188
187
|
console.log(`- ${match.query} : ${match.similarityPercentage.toFixed(2)}% (${match.purpose})`);
|
189
188
|
console.log(` Matching content: "${match.chunk}"`);
|