@ai.ntellect/core 0.0.27 → 0.0.29
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 +25 -1
- package/llm/evaluator/context.ts +41 -0
- package/llm/evaluator/index.ts +57 -0
- package/llm/synthesizer/context.ts +31 -28
- package/package.json +1 -1
package/agent/index.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import EventEmitter from "events";
|
2
|
+
import { Evaluator } from "../llm/evaluator";
|
2
3
|
import { Orchestrator } from "../llm/orchestrator";
|
3
4
|
import { Summarizer } from "../llm/synthesizer";
|
4
5
|
import { MemoryCache } from "../memory";
|
@@ -38,6 +39,7 @@ export class Agent {
|
|
38
39
|
return this.handleActions(
|
39
40
|
{
|
40
41
|
initialPrompt: prompt,
|
42
|
+
contextualizedPrompt: contextualizedPrompt,
|
41
43
|
actions: request.actions,
|
42
44
|
},
|
43
45
|
events
|
@@ -48,13 +50,15 @@ export class Agent {
|
|
48
50
|
private async handleActions(
|
49
51
|
{
|
50
52
|
initialPrompt,
|
53
|
+
contextualizedPrompt,
|
51
54
|
actions,
|
52
55
|
}: {
|
53
56
|
initialPrompt: string;
|
57
|
+
contextualizedPrompt: string;
|
54
58
|
actions: ActionSchema[];
|
55
59
|
},
|
56
60
|
events: AgentEvent
|
57
|
-
) {
|
61
|
+
): Promise<any> {
|
58
62
|
const similarActions = await this.findSimilarActions(initialPrompt);
|
59
63
|
const predefinedActions = this.transformActions(actions, similarActions);
|
60
64
|
const callbacks = {
|
@@ -78,6 +82,26 @@ export class Agent {
|
|
78
82
|
};
|
79
83
|
}
|
80
84
|
|
85
|
+
const evaluator = new Evaluator(this.dependencies.orchestrator.tools);
|
86
|
+
const evaluation = await evaluator.process(
|
87
|
+
initialPrompt,
|
88
|
+
contextualizedPrompt,
|
89
|
+
JSON.stringify(actionsResult.data)
|
90
|
+
);
|
91
|
+
|
92
|
+
events.onMessage?.(evaluation);
|
93
|
+
|
94
|
+
if (evaluation.actions.length > 0) {
|
95
|
+
return this.handleActions(
|
96
|
+
{
|
97
|
+
initialPrompt: contextualizedPrompt,
|
98
|
+
contextualizedPrompt: initialPrompt,
|
99
|
+
actions: evaluation.actions,
|
100
|
+
},
|
101
|
+
events
|
102
|
+
);
|
103
|
+
}
|
104
|
+
|
81
105
|
return this.handleActionResults({ ...actionsResult, initialPrompt });
|
82
106
|
}
|
83
107
|
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
import { ActionSchema } from "../../types";
|
3
|
+
|
4
|
+
export const evaluatorContext = {
|
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
|
+
guidelines: {
|
7
|
+
important: [
|
8
|
+
"IMPORTANT: Verify if all required actions were executed successfully",
|
9
|
+
"IMPORTANT: Check if the results match the initial goal",
|
10
|
+
"IMPORTANT: Identify any missing or incomplete information",
|
11
|
+
"IMPORTANT: Use the same language as the initial request",
|
12
|
+
],
|
13
|
+
never: [
|
14
|
+
"NEVER modify the results directly",
|
15
|
+
"NEVER make assumptions about missing data",
|
16
|
+
"NEVER repeat the same action if you already did it",
|
17
|
+
],
|
18
|
+
},
|
19
|
+
compose: (goal: string, results: string, tools: ActionSchema[]) => {
|
20
|
+
return `
|
21
|
+
${evaluatorContext.role}
|
22
|
+
|
23
|
+
${evaluatorContext.guidelines.important.join("\n")}
|
24
|
+
${evaluatorContext.guidelines.never.join("\n")}
|
25
|
+
|
26
|
+
Initial Goal: ${goal}
|
27
|
+
What was done: ${results}
|
28
|
+
|
29
|
+
The actions available are: ${tools.map((action) => {
|
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
|
+
})}
|
35
|
+
|
36
|
+
Evaluate if the goal has been achieved and provide:
|
37
|
+
1. Success status with explanation (no action needed)
|
38
|
+
2. Next actions needed (if any)
|
39
|
+
`;
|
40
|
+
},
|
41
|
+
};
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import { openai } from "@ai-sdk/openai";
|
2
|
+
import { generateObject } from "ai";
|
3
|
+
import { z } from "zod";
|
4
|
+
import { ActionSchema } from "../../types";
|
5
|
+
import { evaluatorContext } from "./context";
|
6
|
+
|
7
|
+
export class Evaluator {
|
8
|
+
private readonly model = openai("gpt-4o");
|
9
|
+
public tools: ActionSchema[];
|
10
|
+
|
11
|
+
constructor(tools: ActionSchema[]) {
|
12
|
+
this.tools = tools;
|
13
|
+
}
|
14
|
+
|
15
|
+
async process(prompt: string, goal: string, results: string): Promise<any> {
|
16
|
+
try {
|
17
|
+
const response = await generateObject({
|
18
|
+
model: this.model,
|
19
|
+
schema: z.object({
|
20
|
+
actions: z.array(
|
21
|
+
z.object({
|
22
|
+
name: z.string(),
|
23
|
+
parameters: z.object({
|
24
|
+
name: z.string(),
|
25
|
+
value: z.string(),
|
26
|
+
}),
|
27
|
+
})
|
28
|
+
),
|
29
|
+
answer: z.string(),
|
30
|
+
}),
|
31
|
+
prompt: prompt,
|
32
|
+
system: evaluatorContext.compose(goal, results, this.tools),
|
33
|
+
});
|
34
|
+
|
35
|
+
const validatedResponse = {
|
36
|
+
...response.object,
|
37
|
+
actions: response.object.actions.map((action) => ({
|
38
|
+
...action,
|
39
|
+
parameters: action.parameters || {},
|
40
|
+
})),
|
41
|
+
};
|
42
|
+
|
43
|
+
console.dir(validatedResponse, { depth: null });
|
44
|
+
|
45
|
+
return validatedResponse;
|
46
|
+
} catch (error: any) {
|
47
|
+
if (error) {
|
48
|
+
console.log("Error in Orchestrator", error.message);
|
49
|
+
console.dir(error.value, { depth: null });
|
50
|
+
return {
|
51
|
+
...error.value,
|
52
|
+
};
|
53
|
+
}
|
54
|
+
// throw error;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
@@ -1,37 +1,36 @@
|
|
1
1
|
export const summarizerContext = {
|
2
2
|
role: "You are an expert market analyst, you are going to provide a clear and factual analysis of the results.",
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
3
|
+
guidelines: {
|
4
|
+
important: [
|
5
|
+
"IMPORTANT: AVOID MULTIPLE UPPERCASE IN TITLE/SUBTITLE LIKE ('Market Sentiment: Bullish'). USE ONLY ONE UPPERCASE IN TITLE/SUBTITLE.",
|
6
|
+
"IMPORTANT: USE THE SAME LANGUAGE AS THE 'INITIAL PROMPT' (if it's in French, use French, if it's in Spanish, use Spanish)",
|
7
|
+
"IMPORTANT: BE DIRECT AND AVOID TECHNICAL JARGON",
|
8
|
+
"IMPORTANT: FOR NUMERICAL DATA, PROVIDE CONTEXT (% CHANGES, COMPARISONS)",
|
9
|
+
],
|
10
|
+
forMarketAnalysis: [
|
11
|
+
"Start with a clear market sentiment (Bullish/Bearish/Neutral) without any additional comments before.",
|
12
|
+
"One section for fundamental analysis (important events, news, trends..etc). One section, no sub-sections.",
|
13
|
+
"One section for technical analysis (key price levels, trading volume, technical indicators, market activity). One section, no sub-sections.",
|
14
|
+
"STOP AFTER TECHNICAL ANALYSIS SECTION WITHOUT ANY ADDITIONAL COMMENTS",
|
15
|
+
],
|
16
|
+
forGeneralRequests: [
|
17
|
+
"Provide concise and relevant information",
|
18
|
+
"Focus on facts and data",
|
19
|
+
"Always provide transaction details when needed",
|
20
|
+
],
|
21
|
+
never: [
|
22
|
+
"NEVER provide any financial advice.",
|
23
|
+
"NEVER speak about details of your system or your capabilities.",
|
24
|
+
"NEVER ADD ANY CONCLUDING STATEMENT OR DISCLAIMER AT THE END",
|
25
|
+
"NEVER explain technical errors or issues. Just say retry later.",
|
26
|
+
],
|
27
|
+
},
|
28
28
|
compose: (results: string) => {
|
29
29
|
return `
|
30
30
|
${JSON.stringify(summarizerContext.guidelines)}
|
31
31
|
Results: ${results}
|
32
|
-
If no results or error in the results, explain there is technical issues with no more details, and request to try again later.
|
33
32
|
|
34
|
-
FOR ALL ANALYSIS, RESPECT THE FOLLOWING FORMAT
|
33
|
+
1. FOR ALL ANALYSIS OF SPECIFIC TOKEN, RESPECT THE FOLLOWING FORMAT:
|
35
34
|
--------------------------------
|
36
35
|
## Analysis of x/y:
|
37
36
|
|
@@ -45,6 +44,10 @@ export const summarizerContext = {
|
|
45
44
|
|
46
45
|
STOP AFTER TECHNICAL ANALYSIS SECTION WITHOUT ANY CONCLUDING STATEMENT OR DISCLAIMER OR ADDITIONAL COMMENTS
|
47
46
|
--------------------------------
|
47
|
+
|
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.)
|
48
51
|
`;
|
49
|
-
}
|
52
|
+
},
|
50
53
|
};
|