@ai.ntellect/core 0.0.35 → 0.0.37
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 +6 -5
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +4 -3
- package/dist/llm/evaluator/context.d.ts +1 -1
- package/dist/llm/evaluator/context.js +7 -14
- package/dist/llm/evaluator/index.js +5 -0
- package/dist/llm/orchestrator/context.d.ts +1 -1
- package/dist/llm/orchestrator/context.js +8 -19
- package/dist/llm/orchestrator/index.js +5 -0
- package/dist/llm/synthesizer/context.d.ts +2 -2
- package/dist/llm/synthesizer/context.js +9 -11
- package/dist/llm/synthesizer/index.d.ts +6 -3
- package/dist/llm/synthesizer/index.js +14 -10
- package/dist/test.js +1 -4
- package/dist/utils/inject-actions.d.ts +2 -0
- package/dist/utils/inject-actions.js +12 -0
- package/llm/evaluator/context.ts +7 -15
- package/llm/evaluator/index.ts +5 -0
- package/llm/orchestrator/context.ts +8 -22
- package/llm/orchestrator/index.ts +5 -0
- package/llm/synthesizer/context.ts +8 -10
- package/llm/synthesizer/index.ts +21 -10
- package/package.json +1 -1
- package/test.ts +1 -4
- package/utils/inject-actions.ts +11 -0
package/agent/index.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { Evaluator } from "../llm/evaluator";
|
2
2
|
import { Orchestrator } from "../llm/orchestrator";
|
3
|
-
import {
|
3
|
+
import { Synthesizer } from "../llm/synthesizer";
|
4
4
|
import { MemoryCache } from "../memory";
|
5
5
|
import { ActionSchema, AgentEvent, MemoryScope, User } from "../types";
|
6
6
|
import { QueueItemTransformer } from "../utils/queue-item-transformer";
|
@@ -15,7 +15,7 @@ export class Agent {
|
|
15
15
|
private readonly memoryCache: MemoryCache | undefined;
|
16
16
|
private readonly stream: boolean;
|
17
17
|
private readonly maxEvaluatorIteration: number;
|
18
|
-
private
|
18
|
+
private evaluatorIteration = 0;
|
19
19
|
|
20
20
|
constructor({
|
21
21
|
user,
|
@@ -100,6 +100,7 @@ export class Agent {
|
|
100
100
|
events.onMessage?.(evaluation);
|
101
101
|
|
102
102
|
if (evaluation.nextActions.length > 0) {
|
103
|
+
this.evaluatorIteration++;
|
103
104
|
return this.handleActions(
|
104
105
|
{
|
105
106
|
initialPrompt: contextualizedPrompt,
|
@@ -124,15 +125,15 @@ export class Agent {
|
|
124
125
|
data: any;
|
125
126
|
initialPrompt: string;
|
126
127
|
}) {
|
127
|
-
const
|
128
|
+
const synthesizer = new Synthesizer();
|
128
129
|
const summaryData = JSON.stringify({
|
129
130
|
result: actionsResult.data,
|
130
131
|
initialPrompt: actionsResult.initialPrompt,
|
131
132
|
});
|
132
133
|
|
133
134
|
return this.stream
|
134
|
-
? (await
|
135
|
-
: await
|
135
|
+
? (await synthesizer.streamProcess(summaryData)).toDataStreamResponse()
|
136
|
+
: await synthesizer.process(summaryData);
|
136
137
|
}
|
137
138
|
|
138
139
|
private async findSimilarActions(prompt: string) {
|
package/dist/agent/index.d.ts
CHANGED
@@ -10,7 +10,7 @@ export declare class Agent {
|
|
10
10
|
private readonly memoryCache;
|
11
11
|
private readonly stream;
|
12
12
|
private readonly maxEvaluatorIteration;
|
13
|
-
private
|
13
|
+
private evaluatorIteration;
|
14
14
|
constructor({ user, orchestrator, memoryCache, stream, maxEvaluatorIteration, }: {
|
15
15
|
user: User;
|
16
16
|
orchestrator: Orchestrator;
|
package/dist/agent/index.js
CHANGED
@@ -46,6 +46,7 @@ class Agent {
|
|
46
46
|
const evaluation = await evaluator.process(initialPrompt, contextualizedPrompt, JSON.stringify(actionsResult.data));
|
47
47
|
events.onMessage?.(evaluation);
|
48
48
|
if (evaluation.nextActions.length > 0) {
|
49
|
+
this.evaluatorIteration++;
|
49
50
|
return this.handleActions({
|
50
51
|
initialPrompt: contextualizedPrompt,
|
51
52
|
contextualizedPrompt: initialPrompt,
|
@@ -61,14 +62,14 @@ class Agent {
|
|
61
62
|
return this.handleActionResults({ ...actionsResult, initialPrompt });
|
62
63
|
}
|
63
64
|
async handleActionResults(actionsResult) {
|
64
|
-
const
|
65
|
+
const synthesizer = new synthesizer_1.Synthesizer();
|
65
66
|
const summaryData = JSON.stringify({
|
66
67
|
result: actionsResult.data,
|
67
68
|
initialPrompt: actionsResult.initialPrompt,
|
68
69
|
});
|
69
70
|
return this.stream
|
70
|
-
? (await
|
71
|
-
: await
|
71
|
+
? (await synthesizer.streamProcess(summaryData)).toDataStreamResponse()
|
72
|
+
: await synthesizer.process(summaryData);
|
72
73
|
}
|
73
74
|
async findSimilarActions(prompt) {
|
74
75
|
if (!this.memoryCache) {
|
@@ -1,15 +1,16 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.evaluatorContext = void 0;
|
4
|
+
const inject_actions_1 = require("../../utils/inject-actions");
|
4
5
|
exports.evaluatorContext = {
|
5
6
|
role: "You are the evaluator agent. Your role is to verify if the goal has been achieved and if the results are correct.",
|
6
7
|
guidelines: {
|
7
8
|
important: [
|
8
|
-
"
|
9
|
-
"
|
10
|
-
"
|
9
|
+
"Verify if all required actions were executed successfully",
|
10
|
+
"Check if the results match the initial goal",
|
11
|
+
"Identify any missing or incomplete information",
|
11
12
|
],
|
12
|
-
|
13
|
+
warnings: [
|
13
14
|
"NEVER modify the results directly",
|
14
15
|
"NEVER make assumptions about missing data",
|
15
16
|
"NEVER repeat the same action if you already did it",
|
@@ -17,21 +18,13 @@ exports.evaluatorContext = {
|
|
17
18
|
},
|
18
19
|
compose: (goal, results, tools) => {
|
19
20
|
return `
|
20
|
-
${exports.evaluatorContext.
|
21
|
+
${JSON.stringify(exports.evaluatorContext.guidelines)}
|
21
22
|
|
22
|
-
${exports.evaluatorContext.guidelines.important.join("\n")}
|
23
|
-
${exports.evaluatorContext.guidelines.never.join("\n")}
|
24
|
-
|
25
23
|
ACTIONS COMPLETED: ${results}
|
26
24
|
|
27
25
|
Initial Goal: ${goal} (You must use the same language)
|
28
26
|
|
29
|
-
The actions available are: ${
|
30
|
-
const parameters = action.parameters;
|
31
|
-
const schemaShape = Object.keys(parameters._def.shape()).join(", ");
|
32
|
-
const actionString = `Name: ${action.name}, Description: ${action.description}, Arguments: { ${schemaShape} }`;
|
33
|
-
return actionString;
|
34
|
-
})}
|
27
|
+
The actions available are: ${(0, inject_actions_1.injectActions)(tools)}
|
35
28
|
|
36
29
|
Evaluate if the goal has been achieved and provide:
|
37
30
|
1. Success status with explanation (no action needed)
|
@@ -34,10 +34,15 @@ class Evaluator {
|
|
34
34
|
parameters: action.parameters || {},
|
35
35
|
})),
|
36
36
|
};
|
37
|
+
console.log("Evaluator response");
|
38
|
+
console.dir(validatedResponse, { depth: null });
|
37
39
|
return validatedResponse;
|
38
40
|
}
|
39
41
|
catch (error) {
|
40
42
|
if (error) {
|
43
|
+
console.log("Evaluator error");
|
44
|
+
console.dir(error.value, { depth: null });
|
45
|
+
console.error(error.message);
|
41
46
|
return {
|
42
47
|
...error.value,
|
43
48
|
};
|
@@ -1,36 +1,25 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.orchestratorContext = void 0;
|
4
|
+
const inject_actions_1 = require("../../utils/inject-actions");
|
4
5
|
exports.orchestratorContext = {
|
5
|
-
role: "You are the
|
6
|
+
role: "You are the orchestrator agent. Your role is to determine what actions are needed to achieve the user goal.",
|
6
7
|
guidelines: {
|
7
8
|
important: [
|
8
|
-
"
|
9
|
-
"
|
10
|
-
"
|
11
|
-
"IMPORTANT: You allow to provide an analysis without providing any financial advice.",
|
12
|
-
"IMPORTANT: ALWAYS use the same language as user request. (If it's English, use English, if it's French, use French, etc.)",
|
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.",
|
11
|
+
"ALWAYS use the same language as user request. (If it's English, use English, if it's French, use French, etc.)",
|
13
12
|
],
|
14
|
-
|
13
|
+
warnings: [
|
15
14
|
"NEVER repeat the same action twice if the user doesn't ask for it.",
|
16
15
|
"NEVER repeat the same action if its not necessary.",
|
17
16
|
],
|
18
17
|
},
|
19
18
|
compose: (tools) => {
|
20
19
|
return `
|
21
|
-
${exports.orchestratorContext.
|
20
|
+
${JSON.stringify(exports.orchestratorContext.guidelines)}
|
22
21
|
|
23
|
-
${
|
24
|
-
${exports.orchestratorContext.guidelines.never.join("\n")}
|
25
|
-
If this is an action, extract the parameters required to execute the action.
|
26
|
-
IMPORTANT: If some parameters are not clear or missing, YOU MUST ask the user for them.
|
27
|
-
|
28
|
-
The actions are: ${tools.map((action) => {
|
29
|
-
const parameters = action.parameters;
|
30
|
-
const schemaShape = Object.keys(parameters._def.shape()).join(", ");
|
31
|
-
const actionString = `Name: ${action.name}, Description: ${action.description}, Arguments: { ${schemaShape} }`;
|
32
|
-
return actionString;
|
33
|
-
})}
|
22
|
+
The actions are: ${(0, inject_actions_1.injectActions)(tools)}
|
34
23
|
`;
|
35
24
|
},
|
36
25
|
};
|
@@ -34,10 +34,15 @@ class Orchestrator {
|
|
34
34
|
parameters: action.parameters || {},
|
35
35
|
})),
|
36
36
|
};
|
37
|
+
console.log("Orchestrator response");
|
38
|
+
console.dir(validatedResponse, { depth: null });
|
37
39
|
return validatedResponse;
|
38
40
|
}
|
39
41
|
catch (error) {
|
40
42
|
if (error) {
|
43
|
+
console.log("Orchestrator response");
|
44
|
+
console.dir(error.value, { depth: null });
|
45
|
+
console.error(error.message);
|
41
46
|
return {
|
42
47
|
...error.value,
|
43
48
|
};
|
@@ -1,10 +1,10 @@
|
|
1
|
-
export declare const
|
1
|
+
export declare const synthesizerContext: {
|
2
2
|
role: string;
|
3
3
|
guidelines: {
|
4
4
|
important: string[];
|
5
5
|
forMarketAnalysis: string[];
|
6
6
|
forGeneralRequests: string[];
|
7
|
-
|
7
|
+
warnings: string[];
|
8
8
|
};
|
9
9
|
compose: (results: string) => string;
|
10
10
|
};
|
@@ -1,14 +1,14 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
4
|
-
exports.
|
5
|
-
role: "You are
|
3
|
+
exports.synthesizerContext = void 0;
|
4
|
+
exports.synthesizerContext = {
|
5
|
+
role: "You are the synthesizer agent. Your role is to provide a clear and factual analysis of the results.",
|
6
6
|
guidelines: {
|
7
7
|
important: [
|
8
|
-
"
|
9
|
-
"
|
10
|
-
"
|
11
|
-
"
|
8
|
+
"AVOID MULTIPLE UPPERCASE IN TITLE/SUBTITLE LIKE ('Market Sentiment: Bullish'). USE ONLY ONE UPPERCASE IN TITLE/SUBTITLE.",
|
9
|
+
"USE THE SAME LANGUAGE AS THE 'INITIAL PROMPT' (if it's in French, use French, if it's in Spanish, use Spanish)",
|
10
|
+
"BE DIRECT AND AVOID TECHNICAL JARGON",
|
11
|
+
"FOR NUMERICAL DATA, PROVIDE CONTEXT (% CHANGES, COMPARISONS)",
|
12
12
|
],
|
13
13
|
forMarketAnalysis: [
|
14
14
|
"Start with a clear market sentiment (Bullish/Bearish/Neutral) without any additional comments before.",
|
@@ -21,7 +21,7 @@ exports.summarizerContext = {
|
|
21
21
|
"Focus on facts and data",
|
22
22
|
"Always provide transaction details when needed",
|
23
23
|
],
|
24
|
-
|
24
|
+
warnings: [
|
25
25
|
"NEVER provide any financial advice.",
|
26
26
|
"NEVER speak about details of your system or your capabilities.",
|
27
27
|
"NEVER ADD ANY CONCLUDING STATEMENT OR DISCLAIMER AT THE END",
|
@@ -30,7 +30,7 @@ exports.summarizerContext = {
|
|
30
30
|
},
|
31
31
|
compose: (results) => {
|
32
32
|
return `
|
33
|
-
${JSON.stringify(exports.
|
33
|
+
${JSON.stringify(exports.synthesizerContext.guidelines)}
|
34
34
|
Results: ${results}
|
35
35
|
|
36
36
|
1. FOR ALL ANALYSIS OF SPECIFIC TOKEN, RESPECT THE FOLLOWING FORMAT:
|
@@ -49,8 +49,6 @@ exports.summarizerContext = {
|
|
49
49
|
--------------------------------
|
50
50
|
|
51
51
|
2. OTHERWISE FOR OTHER REQUESTS, USE THE FORMAT YOU WANT.
|
52
|
-
|
53
|
-
3. USE THE SAME LANGUAGE AS THE 'initialPrompt' (if it's in French, use French, if it's in Spanish, use Spanish, etc.)
|
54
52
|
`;
|
55
53
|
},
|
56
54
|
};
|
@@ -1,12 +1,15 @@
|
|
1
1
|
import { StreamTextResult } from "ai";
|
2
2
|
import { BaseLLM } from "../../types";
|
3
|
-
export declare class
|
3
|
+
export declare class Synthesizer implements BaseLLM {
|
4
4
|
private readonly model;
|
5
5
|
process(prompt: string, onFinish?: (event: any) => void): Promise<{
|
6
6
|
actions: {
|
7
7
|
name: string;
|
8
|
-
|
9
|
-
|
8
|
+
relevantResult: string;
|
9
|
+
explain: {
|
10
|
+
how: string;
|
11
|
+
why: string;
|
12
|
+
};
|
10
13
|
}[];
|
11
14
|
response: string;
|
12
15
|
} | StreamTextResult<Record<string, any>>>;
|
@@ -1,11 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.Synthesizer = 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
7
|
const context_1 = require("./context");
|
8
|
-
class
|
8
|
+
class Synthesizer {
|
9
9
|
constructor() {
|
10
10
|
this.model = (0, openai_1.openai)("gpt-4-turbo");
|
11
11
|
}
|
@@ -16,15 +16,19 @@ class Summarizer {
|
|
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
|
-
|
19
|
+
relevantResult: zod_1.z.string(),
|
20
|
+
explain: zod_1.z.object({
|
21
|
+
how: zod_1.z.string(),
|
22
|
+
why: zod_1.z.string(),
|
23
|
+
}),
|
21
24
|
})),
|
22
25
|
response: zod_1.z.string(),
|
23
26
|
}),
|
24
|
-
prompt: context_1.
|
25
|
-
system: context_1.
|
27
|
+
prompt: context_1.synthesizerContext.compose(prompt),
|
28
|
+
system: context_1.synthesizerContext.role,
|
26
29
|
});
|
27
|
-
console.log("
|
30
|
+
console.log("Synthesizer");
|
31
|
+
console.dir(result.object, { depth: null });
|
28
32
|
if (onFinish)
|
29
33
|
onFinish(result.object);
|
30
34
|
return result.object;
|
@@ -32,11 +36,11 @@ class Summarizer {
|
|
32
36
|
async streamProcess(prompt, onFinish) {
|
33
37
|
const result = await (0, ai_1.streamText)({
|
34
38
|
model: this.model,
|
35
|
-
prompt: context_1.
|
39
|
+
prompt: context_1.synthesizerContext.compose(prompt),
|
36
40
|
onFinish: onFinish,
|
37
|
-
system: context_1.
|
41
|
+
system: context_1.synthesizerContext.role,
|
38
42
|
});
|
39
43
|
return result;
|
40
44
|
}
|
41
45
|
}
|
42
|
-
exports.
|
46
|
+
exports.Synthesizer = Synthesizer;
|
package/dist/test.js
CHANGED
@@ -8,7 +8,6 @@ const rss_parser_1 = __importDefault(require("rss-parser"));
|
|
8
8
|
const zod_1 = require("zod");
|
9
9
|
const agent_1 = require("./agent");
|
10
10
|
const orchestrator_1 = require("./llm/orchestrator");
|
11
|
-
const memory_1 = require("./memory");
|
12
11
|
exports.getChainsTVL = {
|
13
12
|
name: "get_chains_tvl",
|
14
13
|
description: "Get current TVL (Total Value Locked) of all chains from DeFiLlama",
|
@@ -109,16 +108,14 @@ exports.getRssNews = {
|
|
109
108
|
id: "1",
|
110
109
|
},
|
111
110
|
orchestrator,
|
112
|
-
memoryCache: new memory_1.MemoryCache(),
|
113
111
|
stream: false,
|
114
112
|
maxEvaluatorIteration: 1,
|
115
113
|
});
|
116
|
-
const prompt = "Analyze
|
114
|
+
const prompt = "Analyze bitcoin";
|
117
115
|
const context = prompt;
|
118
116
|
const result = await agent.process(prompt, context, {
|
119
117
|
onMessage: (message) => {
|
120
118
|
console.log({ message });
|
121
119
|
},
|
122
120
|
});
|
123
|
-
console.log(result.text);
|
124
121
|
})();
|
@@ -0,0 +1,12 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.injectActions = void 0;
|
4
|
+
const injectActions = (actions) => {
|
5
|
+
return actions.map((action) => {
|
6
|
+
const parameters = action.parameters;
|
7
|
+
const schemaShape = Object.keys(parameters._def.shape()).join(", ");
|
8
|
+
const actionString = `Name: ${action.name}, Description: ${action.description}, Arguments: { ${schemaShape} }`;
|
9
|
+
return actionString;
|
10
|
+
});
|
11
|
+
};
|
12
|
+
exports.injectActions = injectActions;
|
package/llm/evaluator/context.ts
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
import { z } from "zod";
|
2
1
|
import { ActionSchema } from "../../types";
|
2
|
+
import { injectActions } from "../../utils/inject-actions";
|
3
3
|
|
4
4
|
export const evaluatorContext = {
|
5
5
|
role: "You are the evaluator agent. Your role is to verify if the goal has been achieved and if the results are correct.",
|
6
6
|
guidelines: {
|
7
7
|
important: [
|
8
|
-
"
|
9
|
-
"
|
10
|
-
"
|
8
|
+
"Verify if all required actions were executed successfully",
|
9
|
+
"Check if the results match the initial goal",
|
10
|
+
"Identify any missing or incomplete information",
|
11
11
|
],
|
12
|
-
|
12
|
+
warnings: [
|
13
13
|
"NEVER modify the results directly",
|
14
14
|
"NEVER make assumptions about missing data",
|
15
15
|
"NEVER repeat the same action if you already did it",
|
@@ -17,21 +17,13 @@ export const evaluatorContext = {
|
|
17
17
|
},
|
18
18
|
compose: (goal: string, results: string, tools: ActionSchema[]) => {
|
19
19
|
return `
|
20
|
-
${evaluatorContext.
|
20
|
+
${JSON.stringify(evaluatorContext.guidelines)}
|
21
21
|
|
22
|
-
${evaluatorContext.guidelines.important.join("\n")}
|
23
|
-
${evaluatorContext.guidelines.never.join("\n")}
|
24
|
-
|
25
22
|
ACTIONS COMPLETED: ${results}
|
26
23
|
|
27
24
|
Initial Goal: ${goal} (You must use the same language)
|
28
25
|
|
29
|
-
The actions available are: ${tools
|
30
|
-
const parameters = action.parameters as z.ZodObject<any>;
|
31
|
-
const schemaShape = Object.keys(parameters._def.shape()).join(", ");
|
32
|
-
const actionString = `Name: ${action.name}, Description: ${action.description}, Arguments: { ${schemaShape} }`;
|
33
|
-
return actionString;
|
34
|
-
})}
|
26
|
+
The actions available are: ${injectActions(tools)}
|
35
27
|
|
36
28
|
Evaluate if the goal has been achieved and provide:
|
37
29
|
1. Success status with explanation (no action needed)
|
package/llm/evaluator/index.ts
CHANGED
@@ -40,9 +40,14 @@ export class Evaluator {
|
|
40
40
|
})),
|
41
41
|
};
|
42
42
|
|
43
|
+
console.log("Evaluator response");
|
44
|
+
console.dir(validatedResponse, { depth: null });
|
43
45
|
return validatedResponse;
|
44
46
|
} catch (error: any) {
|
45
47
|
if (error) {
|
48
|
+
console.log("Evaluator error");
|
49
|
+
console.dir(error.value, { depth: null });
|
50
|
+
console.error(error.message);
|
46
51
|
return {
|
47
52
|
...error.value,
|
48
53
|
};
|
@@ -1,38 +1,24 @@
|
|
1
|
-
import { z } from "zod";
|
2
1
|
import { ActionSchema } from "../../types";
|
2
|
+
import { injectActions } from "../../utils/inject-actions";
|
3
3
|
|
4
4
|
export const orchestratorContext = {
|
5
|
-
role: "You are the
|
5
|
+
role: "You are the orchestrator agent. Your role is to determine what actions are needed to achieve the user goal.",
|
6
6
|
guidelines: {
|
7
7
|
important: [
|
8
|
-
"
|
9
|
-
"
|
10
|
-
"
|
11
|
-
"IMPORTANT: You allow to provide an analysis without providing any financial advice.",
|
12
|
-
"IMPORTANT: ALWAYS use the same language as user request. (If it's English, use English, if it's French, use French, etc.)",
|
8
|
+
"If there is no action to do, you must answer in the 'answer' field.",
|
9
|
+
"If some parameters are not clear or missing, YOU MUST ask the user for them.",
|
10
|
+
"ALWAYS use the same language as user request. (If it's English, use English, if it's French, use French, etc.)",
|
13
11
|
],
|
14
|
-
|
12
|
+
warnings: [
|
15
13
|
"NEVER repeat the same action twice if the user doesn't ask for it.",
|
16
14
|
"NEVER repeat the same action if its not necessary.",
|
17
15
|
],
|
18
16
|
},
|
19
17
|
compose: (tools: ActionSchema[]) => {
|
20
18
|
return `
|
21
|
-
${orchestratorContext.
|
19
|
+
${JSON.stringify(orchestratorContext.guidelines)}
|
22
20
|
|
23
|
-
${
|
24
|
-
${orchestratorContext.guidelines.never.join("\n")}
|
25
|
-
If this is an action, extract the parameters required to execute the action.
|
26
|
-
IMPORTANT: If some parameters are not clear or missing, YOU MUST ask the user for them.
|
27
|
-
|
28
|
-
The actions are: ${tools.map((action) => {
|
29
|
-
const parameters = action.parameters as z.ZodObject<any>;
|
30
|
-
const schemaShape = Object.keys(parameters._def.shape()).join(
|
31
|
-
", "
|
32
|
-
);
|
33
|
-
const actionString = `Name: ${action.name}, Description: ${action.description}, Arguments: { ${schemaShape} }`;
|
34
|
-
return actionString;
|
35
|
-
})}
|
21
|
+
The actions are: ${injectActions(tools)}
|
36
22
|
`;
|
37
23
|
},
|
38
24
|
};
|
@@ -39,10 +39,15 @@ export class Orchestrator implements BaseLLM {
|
|
39
39
|
parameters: action.parameters || {},
|
40
40
|
})),
|
41
41
|
};
|
42
|
+
console.log("Orchestrator response");
|
43
|
+
console.dir(validatedResponse, { depth: null });
|
42
44
|
|
43
45
|
return validatedResponse;
|
44
46
|
} catch (error: any) {
|
45
47
|
if (error) {
|
48
|
+
console.log("Orchestrator response");
|
49
|
+
console.dir(error.value, { depth: null });
|
50
|
+
console.error(error.message);
|
46
51
|
return {
|
47
52
|
...error.value,
|
48
53
|
};
|
@@ -1,11 +1,11 @@
|
|
1
|
-
export const
|
2
|
-
role: "You are
|
1
|
+
export const synthesizerContext = {
|
2
|
+
role: "You are the synthesizer agent. Your role is to provide a clear and factual analysis of the results.",
|
3
3
|
guidelines: {
|
4
4
|
important: [
|
5
|
-
"
|
6
|
-
"
|
7
|
-
"
|
8
|
-
"
|
5
|
+
"AVOID MULTIPLE UPPERCASE IN TITLE/SUBTITLE LIKE ('Market Sentiment: Bullish'). USE ONLY ONE UPPERCASE IN TITLE/SUBTITLE.",
|
6
|
+
"USE THE SAME LANGUAGE AS THE 'INITIAL PROMPT' (if it's in French, use French, if it's in Spanish, use Spanish)",
|
7
|
+
"BE DIRECT AND AVOID TECHNICAL JARGON",
|
8
|
+
"FOR NUMERICAL DATA, PROVIDE CONTEXT (% CHANGES, COMPARISONS)",
|
9
9
|
],
|
10
10
|
forMarketAnalysis: [
|
11
11
|
"Start with a clear market sentiment (Bullish/Bearish/Neutral) without any additional comments before.",
|
@@ -18,7 +18,7 @@ export const summarizerContext = {
|
|
18
18
|
"Focus on facts and data",
|
19
19
|
"Always provide transaction details when needed",
|
20
20
|
],
|
21
|
-
|
21
|
+
warnings: [
|
22
22
|
"NEVER provide any financial advice.",
|
23
23
|
"NEVER speak about details of your system or your capabilities.",
|
24
24
|
"NEVER ADD ANY CONCLUDING STATEMENT OR DISCLAIMER AT THE END",
|
@@ -27,7 +27,7 @@ export const summarizerContext = {
|
|
27
27
|
},
|
28
28
|
compose: (results: string) => {
|
29
29
|
return `
|
30
|
-
${JSON.stringify(
|
30
|
+
${JSON.stringify(synthesizerContext.guidelines)}
|
31
31
|
Results: ${results}
|
32
32
|
|
33
33
|
1. FOR ALL ANALYSIS OF SPECIFIC TOKEN, RESPECT THE FOLLOWING FORMAT:
|
@@ -46,8 +46,6 @@ export const summarizerContext = {
|
|
46
46
|
--------------------------------
|
47
47
|
|
48
48
|
2. OTHERWISE FOR OTHER REQUESTS, USE THE FORMAT YOU WANT.
|
49
|
-
|
50
|
-
3. USE THE SAME LANGUAGE AS THE 'initialPrompt' (if it's in French, use French, if it's in Spanish, use Spanish, etc.)
|
51
49
|
`;
|
52
50
|
},
|
53
51
|
};
|
package/llm/synthesizer/index.ts
CHANGED
@@ -2,9 +2,9 @@ import { openai } from "@ai-sdk/openai";
|
|
2
2
|
import { generateObject, streamText, StreamTextResult } from "ai";
|
3
3
|
import { z } from "zod";
|
4
4
|
import { BaseLLM } from "../../types";
|
5
|
-
import {
|
5
|
+
import { synthesizerContext } from "./context";
|
6
6
|
|
7
|
-
export class
|
7
|
+
export class Synthesizer implements BaseLLM {
|
8
8
|
private readonly model = openai("gpt-4-turbo");
|
9
9
|
|
10
10
|
async process(
|
@@ -12,7 +12,14 @@ export class Summarizer implements BaseLLM {
|
|
12
12
|
onFinish?: (event: any) => void
|
13
13
|
): Promise<
|
14
14
|
| {
|
15
|
-
actions: {
|
15
|
+
actions: {
|
16
|
+
name: string;
|
17
|
+
relevantResult: string;
|
18
|
+
explain: {
|
19
|
+
how: string;
|
20
|
+
why: string;
|
21
|
+
};
|
22
|
+
}[];
|
16
23
|
response: string;
|
17
24
|
}
|
18
25
|
| StreamTextResult<Record<string, any>>
|
@@ -24,16 +31,20 @@ export class Summarizer implements BaseLLM {
|
|
24
31
|
actions: z.array(
|
25
32
|
z.object({
|
26
33
|
name: z.string(),
|
27
|
-
|
28
|
-
|
34
|
+
relevantResult: z.string(),
|
35
|
+
explain: z.object({
|
36
|
+
how: z.string(),
|
37
|
+
why: z.string(),
|
38
|
+
}),
|
29
39
|
})
|
30
40
|
),
|
31
41
|
response: z.string(),
|
32
42
|
}),
|
33
|
-
prompt:
|
34
|
-
system:
|
43
|
+
prompt: synthesizerContext.compose(prompt),
|
44
|
+
system: synthesizerContext.role,
|
35
45
|
});
|
36
|
-
console.log("
|
46
|
+
console.log("Synthesizer");
|
47
|
+
console.dir(result.object, { depth: null });
|
37
48
|
if (onFinish) onFinish(result.object);
|
38
49
|
return result.object;
|
39
50
|
}
|
@@ -44,9 +55,9 @@ export class Summarizer implements BaseLLM {
|
|
44
55
|
): Promise<StreamTextResult<Record<string, any>>> {
|
45
56
|
const result = await streamText({
|
46
57
|
model: this.model,
|
47
|
-
prompt:
|
58
|
+
prompt: synthesizerContext.compose(prompt),
|
48
59
|
onFinish: onFinish,
|
49
|
-
system:
|
60
|
+
system: synthesizerContext.role,
|
50
61
|
});
|
51
62
|
return result;
|
52
63
|
}
|
package/package.json
CHANGED
package/test.ts
CHANGED
@@ -2,7 +2,6 @@ import Parser from "rss-parser";
|
|
2
2
|
import { z } from "zod";
|
3
3
|
import { Agent } from "./agent";
|
4
4
|
import { Orchestrator } from "./llm/orchestrator";
|
5
|
-
import { MemoryCache } from "./memory";
|
6
5
|
|
7
6
|
interface ChainTVL {
|
8
7
|
name: string;
|
@@ -129,12 +128,11 @@ export const getRssNews = {
|
|
129
128
|
id: "1",
|
130
129
|
},
|
131
130
|
orchestrator,
|
132
|
-
memoryCache: new MemoryCache(),
|
133
131
|
stream: false,
|
134
132
|
maxEvaluatorIteration: 1,
|
135
133
|
});
|
136
134
|
|
137
|
-
const prompt = "Analyze
|
135
|
+
const prompt = "Analyze bitcoin";
|
138
136
|
const context = prompt;
|
139
137
|
|
140
138
|
const result = await agent.process(prompt, context, {
|
@@ -142,5 +140,4 @@ export const getRssNews = {
|
|
142
140
|
console.log({ message });
|
143
141
|
},
|
144
142
|
});
|
145
|
-
console.log(result.text);
|
146
143
|
})();
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
import { ActionSchema } from "../types";
|
3
|
+
|
4
|
+
export const injectActions = (actions: ActionSchema[]) => {
|
5
|
+
return actions.map((action) => {
|
6
|
+
const parameters = action.parameters as z.ZodObject<any>;
|
7
|
+
const schemaShape = Object.keys(parameters._def.shape()).join(", ");
|
8
|
+
const actionString = `Name: ${action.name}, Description: ${action.description}, Arguments: { ${schemaShape} }`;
|
9
|
+
return actionString;
|
10
|
+
});
|
11
|
+
};
|