@ai.ntellect/core 0.6.1 → 0.6.2
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/dist/create-llm-to-select-multiple-graph copy.js +201 -0
- package/dist/create-llm-to-select-multiple-graph.js +142 -0
- package/dist/graph/controller.js +6 -6
- package/dist/graph/engine.js +198 -135
- package/dist/index copy.js +76 -0
- package/dist/utils/setup-graphs.js +28 -0
- package/dist/utils/stringifiy-zod-schema.js +41 -0
- package/graph/controller.ts +7 -9
- package/graph/engine.ts +0 -3
- package/index.ts +1 -1
- package/package.json +1 -1
- package/dist/test/graph/controller.test.js +0 -170
- package/dist/test/graph/engine.test.js +0 -465
- package/dist/test/memory/adapters/meilisearch.test.js +0 -250
- package/dist/test/memory/adapters/redis.test.js +0 -143
- package/dist/test/memory/base.test.js +0 -209
- package/dist/test/services/agenda.test.js +0 -230
- package/dist/test/services/queue.test.js +0 -258
- package/dist/utils/schema-generator.js +0 -46
- package/dist/utils/state-manager.js +0 -20
@@ -0,0 +1,201 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
12
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
13
|
+
var m = o[Symbol.asyncIterator], i;
|
14
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
15
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
16
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
17
|
+
};
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
19
|
+
const openai_1 = require("@ai-sdk/openai");
|
20
|
+
const ai_1 = require("ai");
|
21
|
+
const ethers_1 = require("ethers");
|
22
|
+
const zod_1 = require("zod");
|
23
|
+
const engine_1 = require("./graph/engine");
|
24
|
+
const index_copy_1 = require("./index copy");
|
25
|
+
const setup_graphs_1 = require("./utils/setup-graphs");
|
26
|
+
const stringifiy_zod_schema_1 = require("./utils/stringifiy-zod-schema");
|
27
|
+
// // 2. Define the graph
|
28
|
+
// ---- II. Create an LLM to select the
|
29
|
+
// 2. Define the graph with a name
|
30
|
+
const graphDefinitionA = {
|
31
|
+
name: "prepare-evm-transaction", // Assign a name to match the workflow
|
32
|
+
entryNode: "prepare-evm-transaction",
|
33
|
+
nodes: {
|
34
|
+
"prepare-evm-transaction": {
|
35
|
+
name: "prepare-evm-transaction",
|
36
|
+
description: "Prepare a transaction for the user to sign.",
|
37
|
+
schema: zod_1.z.object({
|
38
|
+
to: zod_1.z.string(),
|
39
|
+
value: zod_1.z
|
40
|
+
.string()
|
41
|
+
.describe("Ask the user for the amount to send, if not specified"),
|
42
|
+
chain: zod_1.z
|
43
|
+
.string()
|
44
|
+
.describe("Examples networks: ethereum, arbitrum, base. IMPORTANT: You must respect the network name."),
|
45
|
+
}),
|
46
|
+
execute: (params, state) => __awaiter(void 0, void 0, void 0, function* () {
|
47
|
+
console.log("💰 Prepare transaction", { params, state });
|
48
|
+
const networkConfig = index_copy_1.networkConfigs[params.chain.toLowerCase()];
|
49
|
+
if (!networkConfig) {
|
50
|
+
throw new Error(`Network ${params.chain} not found`);
|
51
|
+
}
|
52
|
+
return {
|
53
|
+
to: params.to,
|
54
|
+
value: (0, ethers_1.parseEther)(params.value).toString(),
|
55
|
+
chain: {
|
56
|
+
id: networkConfig.id || 0,
|
57
|
+
rpc: networkConfig.rpc,
|
58
|
+
},
|
59
|
+
type: "transfer",
|
60
|
+
};
|
61
|
+
}),
|
62
|
+
},
|
63
|
+
},
|
64
|
+
};
|
65
|
+
const graphDefinitionB = {
|
66
|
+
name: "security-analysis",
|
67
|
+
entryNode: "security-analysis",
|
68
|
+
nodes: {
|
69
|
+
"security-analysis": {
|
70
|
+
name: "security-analysis",
|
71
|
+
description: "Get news",
|
72
|
+
execute: () => __awaiter(void 0, void 0, void 0, function* () {
|
73
|
+
return { messages: "Hello, world!" };
|
74
|
+
}),
|
75
|
+
relationships: [{ name: "end" }],
|
76
|
+
},
|
77
|
+
end: {
|
78
|
+
name: "end",
|
79
|
+
description: "End the graph",
|
80
|
+
execute: () => __awaiter(void 0, void 0, void 0, function* () {
|
81
|
+
return {
|
82
|
+
messages: "Here the security analysis of Bitcoin: Bitcoin is a good investment",
|
83
|
+
};
|
84
|
+
}),
|
85
|
+
},
|
86
|
+
},
|
87
|
+
};
|
88
|
+
// 3. Define the initial state
|
89
|
+
const initialState = {
|
90
|
+
messages: "",
|
91
|
+
reasoning: true,
|
92
|
+
};
|
93
|
+
const graphMaps = [
|
94
|
+
graphDefinitionA,
|
95
|
+
graphDefinitionB,
|
96
|
+
// Add other graphs
|
97
|
+
];
|
98
|
+
// ---- II. Create a node with LLM
|
99
|
+
(() => __awaiter(void 0, void 0, void 0, function* () {
|
100
|
+
const prompt = "je veux envoyer 0.0001 eth à 0x123 sur ethereum";
|
101
|
+
// Dynamically extract start nodes from each graph definition in graphMaps
|
102
|
+
const reasoningGraphDefinition = {
|
103
|
+
name: "reasoning",
|
104
|
+
entryNode: "reasoning",
|
105
|
+
nodes: {
|
106
|
+
reasoning: {
|
107
|
+
name: "reasoning",
|
108
|
+
description: "Reasoning",
|
109
|
+
execute: (params, state) => __awaiter(void 0, void 0, void 0, function* () {
|
110
|
+
var _a, e_1, _b, _c;
|
111
|
+
console.log("Reasoning", { params, state });
|
112
|
+
const startNodesSchema = graphMaps.map((graphDefinition) => graphDefinition.nodes[graphDefinition.entryNode]);
|
113
|
+
// Pass the start nodes into stringifyZodSchema
|
114
|
+
const system = `You are a wallet assistant. Don't reuse the same workflow multiple times.
|
115
|
+
Here the available workflows and parameters:
|
116
|
+
workflows:${(0, stringifiy_zod_schema_1.stringifyZodSchema)(startNodesSchema)}`;
|
117
|
+
console.log(system);
|
118
|
+
const llm = yield (0, ai_1.streamObject)({
|
119
|
+
model: (0, openai_1.openai)("gpt-4o"),
|
120
|
+
prompt,
|
121
|
+
schema: zod_1.z.object({
|
122
|
+
actions: zod_1.z.array(zod_1.z.object({
|
123
|
+
name: zod_1.z.string(),
|
124
|
+
parameters: zod_1.z.array(zod_1.z.object({
|
125
|
+
name: zod_1.z.string(),
|
126
|
+
value: zod_1.z.any(),
|
127
|
+
})),
|
128
|
+
})),
|
129
|
+
response: zod_1.z.string(),
|
130
|
+
}),
|
131
|
+
system,
|
132
|
+
});
|
133
|
+
console.dir(llm.object, { depth: null });
|
134
|
+
try {
|
135
|
+
for (var _d = true, _e = __asyncValues(llm.partialObjectStream), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
136
|
+
_c = _f.value;
|
137
|
+
_d = false;
|
138
|
+
const chunk = _c;
|
139
|
+
if (chunk.response) {
|
140
|
+
// console.log(chunk.response);
|
141
|
+
}
|
142
|
+
}
|
143
|
+
}
|
144
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
145
|
+
finally {
|
146
|
+
try {
|
147
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
148
|
+
}
|
149
|
+
finally { if (e_1) throw e_1.error; }
|
150
|
+
}
|
151
|
+
const actions = (yield llm.object).actions;
|
152
|
+
console.log({ actions });
|
153
|
+
const selectedWorkflows = (yield llm.object).actions;
|
154
|
+
return {
|
155
|
+
messages: "Hello, world!",
|
156
|
+
actions: selectedWorkflows,
|
157
|
+
};
|
158
|
+
}),
|
159
|
+
condition: (state) => {
|
160
|
+
return state.reasoning === true;
|
161
|
+
},
|
162
|
+
relationships: [{ name: "actions" }],
|
163
|
+
},
|
164
|
+
actions: {
|
165
|
+
name: "actions",
|
166
|
+
description: "Actions",
|
167
|
+
execute: (parameters, state) => __awaiter(void 0, void 0, void 0, function* () {
|
168
|
+
if (!state.actions) {
|
169
|
+
throw new Error("No actions found");
|
170
|
+
}
|
171
|
+
const baseStateMapping = {
|
172
|
+
"prepare-evm-transaction": initialState,
|
173
|
+
// Add other workflows and their base states as needed
|
174
|
+
};
|
175
|
+
const { initialStates, graphs: selectedGraphs, startNodes, } = (0, setup_graphs_1.setupGraphsWithActions)(state.actions, baseStateMapping, graphMaps);
|
176
|
+
// Execute graphs with dynamically determined initial states
|
177
|
+
const results = yield engine_1.GraphEngine.executeGraphsInSequence(selectedGraphs, startNodes, initialStates, (graph) => {
|
178
|
+
console.log(`Graph ${graph.name} updated`, graph.getState());
|
179
|
+
}, (error, nodeName, state) => {
|
180
|
+
console.error(`Erreur dans ${nodeName}`, error, state);
|
181
|
+
});
|
182
|
+
console.log({ results });
|
183
|
+
return {
|
184
|
+
messages: "Hello, world!",
|
185
|
+
results,
|
186
|
+
};
|
187
|
+
}),
|
188
|
+
condition: (state) => {
|
189
|
+
if (state.reasoning === true &&
|
190
|
+
state.actions &&
|
191
|
+
state.actions.length > 0) {
|
192
|
+
return true;
|
193
|
+
}
|
194
|
+
return false;
|
195
|
+
},
|
196
|
+
},
|
197
|
+
},
|
198
|
+
};
|
199
|
+
const reasoningGraph = new engine_1.GraphEngine(reasoningGraphDefinition);
|
200
|
+
yield reasoningGraph.execute(initialState, "reasoning");
|
201
|
+
}))();
|
@@ -0,0 +1,142 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
12
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
13
|
+
var m = o[Symbol.asyncIterator], i;
|
14
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
15
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
16
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
17
|
+
};
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
19
|
+
const openai_1 = require("@ai-sdk/openai");
|
20
|
+
const ai_1 = require("ai");
|
21
|
+
const zod_1 = require("zod");
|
22
|
+
const engine_1 = require("./graph/engine");
|
23
|
+
const setup_graphs_1 = require("./utils/setup-graphs");
|
24
|
+
const stringifiy_zod_schema_1 = require("./utils/stringifiy-zod-schema");
|
25
|
+
// // 2. Define the graph
|
26
|
+
// ---- II. Create an LLM to select the
|
27
|
+
// 2. Define the graph with a name
|
28
|
+
const graphDefinitionA = {
|
29
|
+
name: "get-news", // Assign a name to match the workflow
|
30
|
+
entryNode: "get-news",
|
31
|
+
nodes: {
|
32
|
+
"get-news": {
|
33
|
+
name: "get-news",
|
34
|
+
description: "Get news",
|
35
|
+
schema: zod_1.z.object({
|
36
|
+
query: zod_1.z.string(),
|
37
|
+
}),
|
38
|
+
execute: () => __awaiter(void 0, void 0, void 0, function* () {
|
39
|
+
return { messages: "Hello, world!" };
|
40
|
+
}),
|
41
|
+
relationships: [{ name: "end" }],
|
42
|
+
},
|
43
|
+
end: {
|
44
|
+
name: "end",
|
45
|
+
description: "End the graph",
|
46
|
+
execute: () => __awaiter(void 0, void 0, void 0, function* () {
|
47
|
+
return { messages: "Goodbye, world!" };
|
48
|
+
}),
|
49
|
+
},
|
50
|
+
},
|
51
|
+
};
|
52
|
+
const graphDefinitionB = {
|
53
|
+
name: "security-analysis",
|
54
|
+
entryNode: "security-analysis",
|
55
|
+
nodes: {
|
56
|
+
"security-analysis": {
|
57
|
+
name: "security-analysis",
|
58
|
+
description: "Get news",
|
59
|
+
execute: () => __awaiter(void 0, void 0, void 0, function* () {
|
60
|
+
return { messages: "Hello, world!" };
|
61
|
+
}),
|
62
|
+
relationships: [{ name: "end" }],
|
63
|
+
},
|
64
|
+
end: {
|
65
|
+
name: "end",
|
66
|
+
description: "End the graph",
|
67
|
+
execute: () => __awaiter(void 0, void 0, void 0, function* () {
|
68
|
+
return { messages: "Goodbye, world!" };
|
69
|
+
}),
|
70
|
+
},
|
71
|
+
},
|
72
|
+
};
|
73
|
+
// 3. Define the initial state
|
74
|
+
const initialState = {
|
75
|
+
messages: "",
|
76
|
+
};
|
77
|
+
// 3. Define the initial state
|
78
|
+
const initialStateB = {
|
79
|
+
messages: "",
|
80
|
+
};
|
81
|
+
const graphMaps = [graphDefinitionA, graphDefinitionB];
|
82
|
+
// ---- II. Create a node with LLM
|
83
|
+
(() => __awaiter(void 0, void 0, void 0, function* () {
|
84
|
+
var _a, e_1, _b, _c;
|
85
|
+
const prompt = "Salut quelles sont les news sur bitcoin ? et fais une analyse de sécurité";
|
86
|
+
// Dynamically extract start nodes from each graph definition in graphMaps
|
87
|
+
const startNodesSchema = graphMaps.map((graphDefinition) => graphDefinition.nodes[graphDefinition.entryNode]);
|
88
|
+
// Pass the start nodes into stringifyZodSchema
|
89
|
+
const system = `You can select multiple workflows.
|
90
|
+
Here the available workflows and parameters:
|
91
|
+
workflows:${(0, stringifiy_zod_schema_1.stringifyZodSchema)(startNodesSchema)}`;
|
92
|
+
console.log(system);
|
93
|
+
const llm = yield (0, ai_1.streamObject)({
|
94
|
+
model: (0, openai_1.openai)("gpt-4o"),
|
95
|
+
prompt,
|
96
|
+
schema: zod_1.z.object({
|
97
|
+
actions: zod_1.z.array(zod_1.z.object({
|
98
|
+
name: zod_1.z.string(),
|
99
|
+
parameters: zod_1.z.array(zod_1.z.object({
|
100
|
+
name: zod_1.z.string(),
|
101
|
+
value: zod_1.z.any(),
|
102
|
+
})),
|
103
|
+
})),
|
104
|
+
response: zod_1.z.string(),
|
105
|
+
}),
|
106
|
+
system,
|
107
|
+
});
|
108
|
+
try {
|
109
|
+
for (var _d = true, _e = __asyncValues(llm.partialObjectStream), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
110
|
+
_c = _f.value;
|
111
|
+
_d = false;
|
112
|
+
const chunk = _c;
|
113
|
+
if (chunk.response) {
|
114
|
+
console.log(chunk.response);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}
|
118
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
119
|
+
finally {
|
120
|
+
try {
|
121
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
122
|
+
}
|
123
|
+
finally { if (e_1) throw e_1.error; }
|
124
|
+
}
|
125
|
+
const actions = (yield llm.object).actions;
|
126
|
+
console.log(actions);
|
127
|
+
const selectedWorkflows = (yield llm.object).actions;
|
128
|
+
console.dir(llm.object, { depth: null });
|
129
|
+
// Define the base states
|
130
|
+
const baseStateMapping = {
|
131
|
+
"get-news": initialState,
|
132
|
+
"security-analysis": initialStateB,
|
133
|
+
// Add other workflows and their base states as needed
|
134
|
+
};
|
135
|
+
const { initialStates, graphs: selectedGraphs, startNodes, } = (0, setup_graphs_1.setupGraphsWithActions)(selectedWorkflows, baseStateMapping, graphMaps);
|
136
|
+
// Execute graphs with dynamically determined initial states
|
137
|
+
engine_1.GraphEngine.executeGraphsInParallel(selectedGraphs, startNodes, initialStates, (graph) => {
|
138
|
+
console.log(`Graph ${graph.name} updated`, graph.getState());
|
139
|
+
}, (error, nodeName, state) => {
|
140
|
+
console.error(`Erreur dans ${nodeName}`, error, state);
|
141
|
+
});
|
142
|
+
}))();
|
package/dist/graph/controller.js
CHANGED
@@ -41,13 +41,13 @@ class GraphController {
|
|
41
41
|
schema: graphDefinition.schema,
|
42
42
|
autoDetectCycles: true,
|
43
43
|
});
|
44
|
+
console.log("graph", graph);
|
44
45
|
// Construct the initial state from action parameters.
|
45
|
-
const initialState = {
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
};
|
46
|
+
const initialState = action.parameters.reduce((acc, param) => {
|
47
|
+
acc[param.name] = param.value;
|
48
|
+
return acc;
|
49
|
+
}, {});
|
50
|
+
console.log("initialState", initialState);
|
51
51
|
// Execute the graph starting from the defined entry node.
|
52
52
|
yield graph.execute(initialState, graphDefinition.entryNode);
|
53
53
|
// Retrieve the final state after execution.
|