@ai.ntellect/core 0.1.83 → 0.1.85
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/agent/index.ts +52 -25
- package/dist/agent/index.d.ts +1 -0
- package/dist/agent/index.js +41 -22
- package/dist/llm/evaluator/context.js +3 -5
- package/dist/llm/evaluator/index.js +25 -29
- package/dist/llm/orchestrator/index.js +10 -13
- package/dist/llm/synthesizer/index.d.ts +1 -5
- package/dist/llm/synthesizer/index.js +1 -5
- package/dist/memory/cache.d.ts +1 -1
- package/dist/memory/cache.js +2 -35
- package/dist/memory/persistent.d.ts +1 -1
- package/dist/memory/persistent.js +3 -3
- package/dist/test.d.ts +28 -0
- package/dist/test.js +36 -4
- package/dist/types.d.ts +26 -1
- package/dist/types.js +9 -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/llm/evaluator/context.ts +29 -25
- package/llm/evaluator/index.ts +25 -31
- package/llm/orchestrator/index.ts +9 -17
- package/llm/synthesizer/index.ts +2 -10
- package/memory/cache.ts +4 -42
- package/memory/persistent.ts +3 -3
- package/package.json +1 -1
- package/types.ts +13 -1
- package/utils/queue-item-transformer.ts +25 -11
- package/utils/sanitize-results.ts +66 -0
package/agent/index.ts
CHANGED
@@ -8,9 +8,12 @@ import {
|
|
8
8
|
AgentEvent,
|
9
9
|
MemoryScope,
|
10
10
|
MemoryType,
|
11
|
+
QueueResult,
|
12
|
+
TransformedQueueItem,
|
11
13
|
User,
|
12
14
|
} from "../types";
|
13
15
|
import { QueueItemTransformer } from "../utils/queue-item-transformer";
|
16
|
+
import { ResultSanitizer } from "../utils/sanitize-results";
|
14
17
|
import { ActionHandler } from "./handlers/ActionHandler";
|
15
18
|
|
16
19
|
export class Agent {
|
@@ -24,6 +27,7 @@ export class Agent {
|
|
24
27
|
private readonly stream: boolean;
|
25
28
|
private readonly maxEvaluatorIteration: number;
|
26
29
|
private evaluatorIteration = 0;
|
30
|
+
private accumulatedResults: QueueResult[] = [];
|
27
31
|
|
28
32
|
constructor({
|
29
33
|
user,
|
@@ -47,6 +51,7 @@ export class Agent {
|
|
47
51
|
this.stream = stream;
|
48
52
|
this.maxEvaluatorIteration = maxEvaluatorIteration;
|
49
53
|
this.actionHandler = new ActionHandler();
|
54
|
+
this.accumulatedResults = [];
|
50
55
|
}
|
51
56
|
|
52
57
|
async process(
|
@@ -54,7 +59,9 @@ export class Agent {
|
|
54
59
|
contextualizedPrompt: string,
|
55
60
|
events: AgentEvent
|
56
61
|
): Promise<any> {
|
57
|
-
let actions: ActionSchema[]
|
62
|
+
let actions: ActionSchema[] | TransformedQueueItem[] | undefined =
|
63
|
+
undefined;
|
64
|
+
let isSimilar: boolean = false;
|
58
65
|
|
59
66
|
if (this.cacheMemory) {
|
60
67
|
const similarActions = await this.cacheMemory.findSimilarQueries(prompt, {
|
@@ -63,27 +70,29 @@ export class Agent {
|
|
63
70
|
userId: this.user.id,
|
64
71
|
scope: MemoryScope.GLOBAL,
|
65
72
|
});
|
73
|
+
|
66
74
|
if (similarActions.length > 0) {
|
67
|
-
actions =
|
68
|
-
|
69
|
-
|
75
|
+
actions = QueueItemTransformer.transformActionsToQueueItems(
|
76
|
+
similarActions[0].data
|
77
|
+
);
|
78
|
+
isSimilar = true;
|
70
79
|
}
|
71
80
|
}
|
72
81
|
|
73
|
-
if (!actions
|
74
|
-
console.log("No similar actions found for query: ", prompt);
|
75
|
-
console.log("Requesting orchestrator for actions");
|
82
|
+
if (!actions?.length && !isSimilar) {
|
83
|
+
console.log("No similar actions found in cache for query: ", prompt);
|
84
|
+
console.log("Requesting orchestrator for actions..");
|
76
85
|
const request = await this.orchestrator.process(contextualizedPrompt);
|
77
86
|
events.onMessage?.(request);
|
78
87
|
actions = request.actions;
|
79
88
|
}
|
80
89
|
|
81
|
-
return actions.length > 0
|
90
|
+
return actions && actions.length > 0
|
82
91
|
? this.handleActions(
|
83
92
|
{
|
84
93
|
initialPrompt: prompt,
|
85
94
|
contextualizedPrompt: contextualizedPrompt,
|
86
|
-
actions: actions,
|
95
|
+
actions: actions as ActionSchema[],
|
87
96
|
},
|
88
97
|
events
|
89
98
|
)
|
@@ -116,62 +125,80 @@ export class Agent {
|
|
116
125
|
}
|
117
126
|
);
|
118
127
|
|
128
|
+
this.accumulatedResults = [
|
129
|
+
...this.accumulatedResults,
|
130
|
+
...actionsResult.data,
|
131
|
+
];
|
132
|
+
|
119
133
|
if (this.evaluatorIteration >= this.maxEvaluatorIteration) {
|
120
|
-
return this.handleActionResults({
|
134
|
+
return this.handleActionResults({
|
135
|
+
data: this.accumulatedResults,
|
136
|
+
initialPrompt,
|
137
|
+
});
|
121
138
|
}
|
122
139
|
|
123
140
|
const evaluator = new Evaluator(
|
124
141
|
this.orchestrator.tools,
|
125
142
|
this.persistentMemory
|
126
143
|
);
|
144
|
+
console.log("Accumulated results:");
|
145
|
+
console.dir(this.accumulatedResults, { depth: null });
|
127
146
|
|
147
|
+
const sanitizedResults = ResultSanitizer.sanitize(this.accumulatedResults);
|
128
148
|
const evaluation = await evaluator.process(
|
129
149
|
initialPrompt,
|
130
150
|
contextualizedPrompt,
|
131
|
-
|
151
|
+
sanitizedResults
|
132
152
|
);
|
133
153
|
|
134
154
|
events.onMessage?.(evaluation);
|
135
155
|
|
136
|
-
|
137
|
-
content: initialPrompt,
|
138
|
-
data: actions,
|
139
|
-
scope: MemoryScope.GLOBAL,
|
140
|
-
type: MemoryType.ACTION,
|
141
|
-
});
|
142
|
-
|
143
|
-
if (evaluation.nextActions.length > 0) {
|
156
|
+
if (evaluation.isNextActionNeeded) {
|
144
157
|
this.evaluatorIteration++;
|
145
158
|
return this.handleActions(
|
146
159
|
{
|
147
160
|
initialPrompt: contextualizedPrompt,
|
148
161
|
contextualizedPrompt: initialPrompt,
|
149
|
-
actions: evaluation.
|
162
|
+
actions: evaluation.nextActionsNeeded,
|
150
163
|
},
|
151
164
|
events
|
152
165
|
);
|
153
166
|
}
|
154
167
|
|
155
|
-
if (!this.actionHandler.hasNonPrepareActions(
|
168
|
+
if (!this.actionHandler.hasNonPrepareActions(this.accumulatedResults)) {
|
156
169
|
return {
|
157
|
-
data:
|
170
|
+
data: this.accumulatedResults,
|
158
171
|
initialPrompt,
|
159
172
|
};
|
160
173
|
}
|
161
174
|
|
162
|
-
return this.handleActionResults({
|
175
|
+
return this.handleActionResults({
|
176
|
+
data: this.accumulatedResults,
|
177
|
+
initialPrompt,
|
178
|
+
});
|
163
179
|
}
|
164
180
|
|
165
181
|
private async handleActionResults(actionsResult: {
|
166
|
-
data:
|
182
|
+
data: QueueResult[];
|
167
183
|
initialPrompt: string;
|
168
184
|
}) {
|
169
185
|
const synthesizer = new Synthesizer();
|
186
|
+
const sanitizedResults = ResultSanitizer.sanitize(this.accumulatedResults);
|
170
187
|
const summaryData = JSON.stringify({
|
171
|
-
result:
|
188
|
+
result: sanitizedResults,
|
172
189
|
initialPrompt: actionsResult.initialPrompt,
|
173
190
|
});
|
174
191
|
|
192
|
+
this.accumulatedResults = [];
|
193
|
+
this.evaluatorIteration = 0;
|
194
|
+
|
195
|
+
await this.cacheMemory?.createMemory({
|
196
|
+
content: actionsResult.initialPrompt,
|
197
|
+
data: actionsResult.data,
|
198
|
+
scope: MemoryScope.GLOBAL,
|
199
|
+
type: MemoryType.ACTION,
|
200
|
+
});
|
201
|
+
|
175
202
|
return this.stream
|
176
203
|
? (await synthesizer.streamProcess(summaryData)).toDataStreamResponse()
|
177
204
|
: await synthesizer.process(summaryData);
|
package/dist/agent/index.d.ts
CHANGED
@@ -13,6 +13,7 @@ export declare class Agent {
|
|
13
13
|
private readonly stream;
|
14
14
|
private readonly maxEvaluatorIteration;
|
15
15
|
private evaluatorIteration;
|
16
|
+
private accumulatedResults;
|
16
17
|
constructor({ user, orchestrator, persistentMemory, cacheMemory, stream, maxEvaluatorIteration, }: {
|
17
18
|
user: User;
|
18
19
|
orchestrator: Orchestrator;
|
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,9 +20,11 @@ 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
|
-
let actions =
|
26
|
+
let actions = undefined;
|
27
|
+
let isSimilar = false;
|
24
28
|
if (this.cacheMemory) {
|
25
29
|
const similarActions = await this.cacheMemory.findSimilarQueries(prompt, {
|
26
30
|
similarityThreshold: this.SIMILARITY_THRESHOLD,
|
@@ -29,19 +33,18 @@ class Agent {
|
|
29
33
|
scope: types_1.MemoryScope.GLOBAL,
|
30
34
|
});
|
31
35
|
if (similarActions.length > 0) {
|
32
|
-
actions = similarActions[0].data;
|
33
|
-
|
34
|
-
console.dir(actions, { depth: null });
|
36
|
+
actions = queue_item_transformer_1.QueueItemTransformer.transformActionsToQueueItems(similarActions[0].data);
|
37
|
+
isSimilar = true;
|
35
38
|
}
|
36
39
|
}
|
37
|
-
if (!actions
|
38
|
-
console.log("No similar actions found for query: ", prompt);
|
39
|
-
console.log("Requesting orchestrator for actions");
|
40
|
+
if (!actions?.length && !isSimilar) {
|
41
|
+
console.log("No similar actions found in cache for query: ", prompt);
|
42
|
+
console.log("Requesting orchestrator for actions..");
|
40
43
|
const request = await this.orchestrator.process(contextualizedPrompt);
|
41
44
|
events.onMessage?.(request);
|
42
45
|
actions = request.actions;
|
43
46
|
}
|
44
|
-
return actions.length > 0
|
47
|
+
return actions && actions.length > 0
|
45
48
|
? this.handleActions({
|
46
49
|
initialPrompt: prompt,
|
47
50
|
contextualizedPrompt: contextualizedPrompt,
|
@@ -58,40 +61,56 @@ class Agent {
|
|
58
61
|
onQueueComplete: events.onQueueComplete,
|
59
62
|
onConfirmationRequired: events.onConfirmationRequired,
|
60
63
|
});
|
64
|
+
this.accumulatedResults = [
|
65
|
+
...this.accumulatedResults,
|
66
|
+
...actionsResult.data,
|
67
|
+
];
|
61
68
|
if (this.evaluatorIteration >= this.maxEvaluatorIteration) {
|
62
|
-
return this.handleActionResults({
|
69
|
+
return this.handleActionResults({
|
70
|
+
data: this.accumulatedResults,
|
71
|
+
initialPrompt,
|
72
|
+
});
|
63
73
|
}
|
64
74
|
const evaluator = new evaluator_1.Evaluator(this.orchestrator.tools, this.persistentMemory);
|
65
|
-
|
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);
|
66
79
|
events.onMessage?.(evaluation);
|
67
|
-
|
68
|
-
content: initialPrompt,
|
69
|
-
data: actions,
|
70
|
-
scope: types_1.MemoryScope.GLOBAL,
|
71
|
-
type: types_1.MemoryType.ACTION,
|
72
|
-
});
|
73
|
-
if (evaluation.nextActions.length > 0) {
|
80
|
+
if (evaluation.isNextActionNeeded) {
|
74
81
|
this.evaluatorIteration++;
|
75
82
|
return this.handleActions({
|
76
83
|
initialPrompt: contextualizedPrompt,
|
77
84
|
contextualizedPrompt: initialPrompt,
|
78
|
-
actions: evaluation.
|
85
|
+
actions: evaluation.nextActionsNeeded,
|
79
86
|
}, events);
|
80
87
|
}
|
81
|
-
if (!this.actionHandler.hasNonPrepareActions(
|
88
|
+
if (!this.actionHandler.hasNonPrepareActions(this.accumulatedResults)) {
|
82
89
|
return {
|
83
|
-
data:
|
90
|
+
data: this.accumulatedResults,
|
84
91
|
initialPrompt,
|
85
92
|
};
|
86
93
|
}
|
87
|
-
return this.handleActionResults({
|
94
|
+
return this.handleActionResults({
|
95
|
+
data: this.accumulatedResults,
|
96
|
+
initialPrompt,
|
97
|
+
});
|
88
98
|
}
|
89
99
|
async handleActionResults(actionsResult) {
|
90
100
|
const synthesizer = new synthesizer_1.Synthesizer();
|
101
|
+
const sanitizedResults = sanitize_results_1.ResultSanitizer.sanitize(this.accumulatedResults);
|
91
102
|
const summaryData = JSON.stringify({
|
92
|
-
result:
|
103
|
+
result: sanitizedResults,
|
93
104
|
initialPrompt: actionsResult.initialPrompt,
|
94
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
|
+
});
|
95
114
|
return this.stream
|
96
115
|
? (await synthesizer.streamProcess(summaryData)).toDataStreamResponse()
|
97
116
|
: await synthesizer.process(summaryData);
|
@@ -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
|
});
|
@@ -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) {
|
@@ -18,23 +19,25 @@ class Orchestrator {
|
|
18
19
|
query: zod_1.z.string(),
|
19
20
|
}),
|
20
21
|
execute: async ({ query }) => {
|
21
|
-
const memories = await this.memory.searchSimilarQueries(query
|
22
|
+
const memories = await this.memory.searchSimilarQueries(query, {
|
23
|
+
similarityThreshold: 95,
|
24
|
+
});
|
22
25
|
return memories;
|
23
26
|
},
|
24
27
|
},
|
25
28
|
{
|
26
29
|
name: "save_memory",
|
27
|
-
description: "Save
|
30
|
+
description: "Save relevant information in the internal knowledge base",
|
28
31
|
parameters: zod_1.z.object({
|
29
32
|
query: zod_1.z.string(),
|
30
|
-
|
33
|
+
memoryType: zod_1.z.string(),
|
31
34
|
data: zod_1.z.any(),
|
32
|
-
scope: zod_1.z.
|
33
|
-
userId: zod_1.z.string()
|
35
|
+
scope: zod_1.z.string().default("GLOBAL").describe("GLOBAL or USER"),
|
36
|
+
userId: zod_1.z.string(),
|
34
37
|
whyStored: zod_1.z.string(),
|
35
38
|
}),
|
36
39
|
execute: async ({ query, purpose, data, scope, userId, }) => {
|
37
|
-
const memories = await this.memory.
|
40
|
+
const memories = await this.memory.createMemory({
|
38
41
|
query,
|
39
42
|
purpose,
|
40
43
|
data,
|
@@ -53,13 +56,7 @@ class Orchestrator {
|
|
53
56
|
const response = await (0, ai_1.generateObject)({
|
54
57
|
model: this.model,
|
55
58
|
schema: zod_1.z.object({
|
56
|
-
actions:
|
57
|
-
name: zod_1.z.string(),
|
58
|
-
parameters: zod_1.z.array(zod_1.z.object({
|
59
|
-
name: zod_1.z.string(),
|
60
|
-
value: zod_1.z.string(),
|
61
|
-
})),
|
62
|
-
})),
|
59
|
+
actions: types_1.ActionSchema,
|
63
60
|
answer: zod_1.z.string(),
|
64
61
|
}),
|
65
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
@@ -17,6 +17,6 @@ export declare class CacheMemory {
|
|
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 = {}) {
|
@@ -60,13 +59,10 @@ class CacheMemory {
|
|
60
59
|
.map((memory) => {
|
61
60
|
const similarity = (0, ai_1.cosineSimilarity)(embedding, memory.embedding);
|
62
61
|
const similarityPercentage = (similarity + 1) * 50; // Conversion en pourcentage
|
63
|
-
console.log(`\n📊 Query "${memory.query}":
|
64
|
-
- Similarity: ${similarityPercentage.toFixed(2)}%`);
|
65
62
|
return {
|
66
63
|
data: memory.data,
|
67
64
|
query: memory.query,
|
68
65
|
similarityPercentage,
|
69
|
-
// Optionnel : ajouter des métadonnées utiles
|
70
66
|
memoryId: memory.id,
|
71
67
|
};
|
72
68
|
})
|
@@ -84,7 +80,6 @@ class CacheMemory {
|
|
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) {
|
@@ -129,22 +124,7 @@ class CacheMemory {
|
|
129
124
|
return;
|
130
125
|
}
|
131
126
|
console.log("No similar memory found");
|
132
|
-
|
133
|
-
console.log("Generating variations...");
|
134
|
-
const variations = await (0, ai_1.generateObject)({
|
135
|
-
model: (0, openai_1.openai)("gpt-4"),
|
136
|
-
schema: zod_1.z.object({
|
137
|
-
request: zod_1.z.string().describe("The request to be performed"),
|
138
|
-
queries: zod_1.z.array(zod_1.z.object({ text: zod_1.z.string() })),
|
139
|
-
}),
|
140
|
-
prompt: `For this input: "${input.content}"
|
141
|
-
Generate similar way to ask the same question.
|
142
|
-
Action results: ${JSON.stringify(input.data)}
|
143
|
-
- Keep variations natural and human-like
|
144
|
-
- Add 3-5 variations`,
|
145
|
-
});
|
146
|
-
console.log("Variations generated:", variations.object.queries);
|
147
|
-
await this.createSingleMemory({
|
127
|
+
const memory = await this.createSingleMemory({
|
148
128
|
id: crypto.randomUUID(),
|
149
129
|
content: input.content,
|
150
130
|
type: input.type,
|
@@ -152,20 +132,7 @@ class CacheMemory {
|
|
152
132
|
userId: input.userId,
|
153
133
|
scope: input.scope,
|
154
134
|
});
|
155
|
-
|
156
|
-
if (variation.text !== input.content) {
|
157
|
-
await this.createSingleMemory({
|
158
|
-
id: crypto.randomUUID(),
|
159
|
-
content: variation.text,
|
160
|
-
type: input.type,
|
161
|
-
data: input.data,
|
162
|
-
userId: input.userId,
|
163
|
-
scope: input.scope,
|
164
|
-
});
|
165
|
-
}
|
166
|
-
});
|
167
|
-
await Promise.all(variationPromises);
|
168
|
-
return variations.object.request;
|
135
|
+
return memory;
|
169
136
|
}
|
170
137
|
async createSingleMemory(params) {
|
171
138
|
console.log("Creating new cache memory...", params.content);
|
@@ -101,7 +101,7 @@ class PersistentMemory {
|
|
101
101
|
/**
|
102
102
|
* Store a memory in the database
|
103
103
|
*/
|
104
|
-
async
|
104
|
+
async createMemory(memory) {
|
105
105
|
const indexName = this._getIndexName(memory.scope, memory.userId);
|
106
106
|
await this._getOrCreateIndex(indexName);
|
107
107
|
const chunks = await this.processContent(memory.query);
|
@@ -114,7 +114,7 @@ class PersistentMemory {
|
|
114
114
|
method: "POST",
|
115
115
|
body: JSON.stringify([document]),
|
116
116
|
});
|
117
|
-
console.log("Stored memory response:", response);
|
117
|
+
console.log("Stored persistent memory response:", response);
|
118
118
|
return response;
|
119
119
|
}
|
120
120
|
/**
|
@@ -182,7 +182,7 @@ class PersistentMemory {
|
|
182
182
|
.sort((a, b) => b.similarityPercentage - a.similarityPercentage);
|
183
183
|
// Log results
|
184
184
|
if (results.length > 0) {
|
185
|
-
console.log("\n✨ Similar queries found:");
|
185
|
+
console.log("\n✨ Similar queries found in persistent memory:");
|
186
186
|
results.forEach((match) => {
|
187
187
|
console.log(`- ${match.query} : ${match.similarityPercentage.toFixed(2)}% (${match.purpose})`);
|
188
188
|
console.log(` Matching content: "${match.chunk}"`);
|
package/dist/test.d.ts
CHANGED
@@ -1,4 +1,32 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
+
export declare const fetchMarkPrice: {
|
3
|
+
name: string;
|
4
|
+
description: string;
|
5
|
+
parameters: z.ZodObject<{
|
6
|
+
symbol: z.ZodString;
|
7
|
+
params: z.ZodObject<{
|
8
|
+
subType: z.ZodString;
|
9
|
+
}, "strip", z.ZodTypeAny, {
|
10
|
+
subType: string;
|
11
|
+
}, {
|
12
|
+
subType: string;
|
13
|
+
}>;
|
14
|
+
}, "strip", z.ZodTypeAny, {
|
15
|
+
symbol: string;
|
16
|
+
params: {
|
17
|
+
subType: string;
|
18
|
+
};
|
19
|
+
}, {
|
20
|
+
symbol: string;
|
21
|
+
params: {
|
22
|
+
subType: string;
|
23
|
+
};
|
24
|
+
}>;
|
25
|
+
execute: ({ symbol, params }: {
|
26
|
+
symbol: string;
|
27
|
+
params: any;
|
28
|
+
}) => Promise<import("ccxt").Ticker>;
|
29
|
+
};
|
2
30
|
export declare const getChainsTVL: {
|
3
31
|
name: string;
|
4
32
|
description: string;
|