@ai.ntellect/core 0.0.28 → 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 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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai.ntellect/core",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {