@aigne/example-workflow-reflection 1.1.0-beta.12

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.
@@ -0,0 +1,5 @@
1
+ # Change the name of this file to .env.local and fill in the following values
2
+
3
+ DEBUG=aigne:mcp
4
+
5
+ OPENAI_API_KEY="" # Your OpenAI API key
package/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # Workflow Reflection Demo
2
+
3
+ This is a demonstration of using [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework) to build a reflection workflow.
4
+
5
+ ## Prerequisites
6
+
7
+ - [Node.js](https://nodejs.org) and npm installed on your machine
8
+ - [OpenAI API key](https://platform.openai.com/api-keys) used to interact with OpenAI API
9
+ - [Pnpm](https://pnpm.io) [Optional] if you want to run the example from source code
10
+
11
+ ## Try without Installation
12
+
13
+ ```bash
14
+ export OPENAI_API_KEY=YOUR_OPENAI_API_KEY # setup your OpenAI API key
15
+
16
+ npx -y @aigne/example-workflow-reflection # run the example
17
+ ```
18
+
19
+ ## Installation
20
+
21
+ ### Clone the Repository
22
+
23
+ ```bash
24
+ git clone https://github.com/AIGNE-io/aigne-framework
25
+ ```
26
+
27
+ ### Install Dependencies
28
+
29
+ ```bash
30
+ cd aigne-framework/examples/workflow-reflection
31
+
32
+ pnpm install
33
+ ```
34
+
35
+ ### Setup Environment Variables
36
+
37
+ Setup your OpenAI API key in the `.env.local` file:
38
+
39
+ ```bash
40
+ OPENAI_API_KEY="" # setup your OpenAI API key here
41
+ ```
42
+
43
+ ### Run the Example
44
+
45
+ ```bash
46
+ pnpm start
47
+ ```
48
+
49
+ ## Example
50
+
51
+ The following example demonstrates how to build a reflection workflow:
52
+
53
+ ```typescript
54
+ import assert from "node:assert";
55
+ import {
56
+ AIAgent,
57
+ ChatModelOpenAI,
58
+ ExecutionEngine,
59
+ UserInputTopic,
60
+ UserOutputTopic,
61
+ } from "@aigne/core-next";
62
+ import { z } from "zod";
63
+
64
+ const { OPENAI_API_KEY } = process.env;
65
+ assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
66
+
67
+ const model = new ChatModelOpenAI({
68
+ apiKey: OPENAI_API_KEY,
69
+ });
70
+
71
+ const coder = AIAgent.from({
72
+ subscribeTopic: [UserInputTopic, "rewrite_request"],
73
+ publishTopic: "review_request",
74
+ instructions: `\
75
+ You are a proficient coder. You write code to solve problems.
76
+ Work with the reviewer to improve your code.
77
+ Always put all finished code in a single Markdown code block.
78
+ For example:
79
+ \`\`\`python
80
+ def hello_world():
81
+ print("Hello, World!")
82
+ \`\`\`
83
+
84
+ Respond using the following format:
85
+
86
+ Thoughts: <Your comments>
87
+ Code: <Your code>
88
+
89
+ Previous review result:
90
+ {{feedback}}
91
+
92
+ User's question:
93
+ {{question}}
94
+ `,
95
+ outputSchema: z.object({
96
+ code: z.string().describe("Your code"),
97
+ }),
98
+ });
99
+
100
+ const reviewer = AIAgent.from({
101
+ subscribeTopic: "review_request",
102
+ publishTopic: (output) => (output.approval ? UserOutputTopic : "rewrite_request"),
103
+ instructions: `\
104
+ You are a code reviewer. You focus on correctness, efficiency and safety of the code.
105
+
106
+ The problem statement is: {{question}}
107
+ The code is:
108
+ \`\`\`
109
+ {{code}}
110
+ \`\`\`
111
+
112
+ Previous feedback:
113
+ {{feedback}}
114
+
115
+ Please review the code. If previous feedback was provided, see if it was addressed.
116
+ `,
117
+ outputSchema: z.object({
118
+ approval: z.boolean().describe("APPROVE or REVISE"),
119
+ feedback: z.object({
120
+ correctness: z.string().describe("Your comments on correctness"),
121
+ efficiency: z.string().describe("Your comments on efficiency"),
122
+ safety: z.string().describe("Your comments on safety"),
123
+ suggested_changes: z.string().describe("Your comments on suggested changes"),
124
+ }),
125
+ }),
126
+ includeInputInOutput: true,
127
+ });
128
+
129
+ const engine = new ExecutionEngine({ model, agents: [coder, reviewer] });
130
+
131
+ const result = await engine.run("Write a function to find the sum of all even numbers in a list.");
132
+ console.log(result);
133
+ // Output:
134
+ // {
135
+ // code: "def sum_of_even_numbers(numbers):\n \"\"\"Function to calculate the sum of all even numbers in a list.\"\"\"\n return sum(number for number in numbers if number % 2 == 0)",
136
+ // approval: true,
137
+ // feedback: {
138
+ // correctness: "The function correctly calculates the sum of all even numbers in the given list. It properly checks for evenness using the modulus operator and sums the valid numbers.",
139
+ // efficiency: "The implementation is efficient as it uses a generator expression which computes the sum in a single pass over the list. This minimizes memory usage as compared to creating an intermediate list of even numbers.",
140
+ // safety: "The function does not contain any safety issues. However, it assumes that all elements in the input list are integers. It would be prudent to handle cases where the input contains non-integer values (e.g., None, strings, etc.).",
141
+ // suggested_changes: "Consider adding type annotations to the function for better clarity and potential type checking, e.g. `def sum_of_even_numbers(numbers: list[int]) -> int:`. Also, include input validation to ensure 'numbers' is a list of integers.",
142
+ // },
143
+ // }
144
+ ```
145
+
146
+ ## License
147
+
148
+ This project is licensed under the MIT License.
package/index.ts ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env npx -y bun
2
+
3
+ import assert from "node:assert";
4
+ import {
5
+ AIAgent,
6
+ ChatModelOpenAI,
7
+ ExecutionEngine,
8
+ UserInputTopic,
9
+ UserOutputTopic,
10
+ runChatLoopInTerminal,
11
+ } from "@aigne/core-next";
12
+ import { z } from "zod";
13
+
14
+ const { OPENAI_API_KEY } = process.env;
15
+ assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
16
+
17
+ const model = new ChatModelOpenAI({
18
+ apiKey: OPENAI_API_KEY,
19
+ });
20
+
21
+ const coder = AIAgent.from({
22
+ subscribeTopic: [UserInputTopic, "rewrite_request"],
23
+ publishTopic: "review_request",
24
+ instructions: `\
25
+ You are a proficient coder. You write code to solve problems.
26
+ Work with the reviewer to improve your code.
27
+ Always put all finished code in a single Markdown code block.
28
+ For example:
29
+ \`\`\`python
30
+ def hello_world():
31
+ print("Hello, World!")
32
+ \`\`\`
33
+
34
+ Respond using the following format:
35
+
36
+ Thoughts: <Your comments>
37
+ Code: <Your code>
38
+
39
+ Previous review result:
40
+ {{feedback}}
41
+
42
+ User's question:
43
+ {{question}}
44
+ `,
45
+ outputSchema: z.object({
46
+ code: z.string().describe("Your code"),
47
+ }),
48
+ });
49
+
50
+ const reviewer = AIAgent.from({
51
+ subscribeTopic: "review_request",
52
+ publishTopic: (output) => (output.approval ? UserOutputTopic : "rewrite_request"),
53
+ instructions: `\
54
+ You are a code reviewer. You focus on correctness, efficiency and safety of the code.
55
+
56
+ The problem statement is: {{question}}
57
+ The code is:
58
+ \`\`\`
59
+ {{code}}
60
+ \`\`\`
61
+
62
+ Previous feedback:
63
+ {{feedback}}
64
+
65
+ Please review the code. If previous feedback was provided, see if it was addressed.
66
+ `,
67
+ outputSchema: z.object({
68
+ approval: z.boolean().describe("APPROVE or REVISE"),
69
+ feedback: z.object({
70
+ correctness: z.string().describe("Your comments on correctness"),
71
+ efficiency: z.string().describe("Your comments on efficiency"),
72
+ safety: z.string().describe("Your comments on safety"),
73
+ suggested_changes: z.string().describe("Your comments on suggested changes"),
74
+ }),
75
+ }),
76
+ includeInputInOutput: true,
77
+ });
78
+
79
+ const engine = new ExecutionEngine({ model, agents: [coder, reviewer] });
80
+
81
+ const userAgent = await engine.run();
82
+
83
+ await runChatLoopInTerminal(userAgent, {
84
+ welcome: `Hello, I'm a coder with a reviewer. I can help you write code and get it reviewed.`,
85
+ defaultQuestion: "Write a function to find the sum of all even numbers in a list.",
86
+ });
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@aigne/example-workflow-reflection",
3
+ "version": "1.1.0-beta.12",
4
+ "description": "A demonstration of using AIGNE Framework to build a reflection workflow",
5
+ "author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
6
+ "homepage": "https://github.com/AIGNE-io/aigne-framework/tree/main/examples/workflow-reflection",
7
+ "license": "ISC",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/AIGNE-io/aigne-framework"
11
+ },
12
+ "bin": "index.ts",
13
+ "files": [
14
+ ".env.local.example",
15
+ "*.ts",
16
+ "README.md"
17
+ ],
18
+ "dependencies": {
19
+ "zod": "^3.24.2",
20
+ "@aigne/core-next": "^1.1.0-beta.12"
21
+ },
22
+ "scripts": {
23
+ "start": "npx -y bun run index.ts"
24
+ }
25
+ }
package/usages.ts ADDED
@@ -0,0 +1,90 @@
1
+ import assert from "node:assert";
2
+ import {
3
+ AIAgent,
4
+ ChatModelOpenAI,
5
+ ExecutionEngine,
6
+ UserInputTopic,
7
+ UserOutputTopic,
8
+ } from "@aigne/core-next";
9
+ import { z } from "zod";
10
+
11
+ const { OPENAI_API_KEY } = process.env;
12
+ assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
13
+
14
+ const model = new ChatModelOpenAI({
15
+ apiKey: OPENAI_API_KEY,
16
+ });
17
+
18
+ const coder = AIAgent.from({
19
+ subscribeTopic: [UserInputTopic, "rewrite_request"],
20
+ publishTopic: "review_request",
21
+ instructions: `\
22
+ You are a proficient coder. You write code to solve problems.
23
+ Work with the reviewer to improve your code.
24
+ Always put all finished code in a single Markdown code block.
25
+ For example:
26
+ \`\`\`python
27
+ def hello_world():
28
+ print("Hello, World!")
29
+ \`\`\`
30
+
31
+ Respond using the following format:
32
+
33
+ Thoughts: <Your comments>
34
+ Code: <Your code>
35
+
36
+ Previous review result:
37
+ {{feedback}}
38
+
39
+ User's question:
40
+ {{question}}
41
+ `,
42
+ outputSchema: z.object({
43
+ code: z.string().describe("Your code"),
44
+ }),
45
+ });
46
+
47
+ const reviewer = AIAgent.from({
48
+ subscribeTopic: "review_request",
49
+ publishTopic: (output) => (output.approval ? UserOutputTopic : "rewrite_request"),
50
+ instructions: `\
51
+ You are a code reviewer. You focus on correctness, efficiency and safety of the code.
52
+
53
+ The problem statement is: {{question}}
54
+ The code is:
55
+ \`\`\`
56
+ {{code}}
57
+ \`\`\`
58
+
59
+ Previous feedback:
60
+ {{feedback}}
61
+
62
+ Please review the code. If previous feedback was provided, see if it was addressed.
63
+ `,
64
+ outputSchema: z.object({
65
+ approval: z.boolean().describe("APPROVE or REVISE"),
66
+ feedback: z.object({
67
+ correctness: z.string().describe("Your comments on correctness"),
68
+ efficiency: z.string().describe("Your comments on efficiency"),
69
+ safety: z.string().describe("Your comments on safety"),
70
+ suggested_changes: z.string().describe("Your comments on suggested changes"),
71
+ }),
72
+ }),
73
+ includeInputInOutput: true,
74
+ });
75
+
76
+ const engine = new ExecutionEngine({ model, agents: [coder, reviewer] });
77
+
78
+ const result = await engine.run("Write a function to find the sum of all even numbers in a list.");
79
+ console.log(result);
80
+ // Output:
81
+ // {
82
+ // code: "def sum_of_even_numbers(numbers):\n \"\"\"Function to calculate the sum of all even numbers in a list.\"\"\"\n return sum(number for number in numbers if number % 2 == 0)",
83
+ // approval: true,
84
+ // feedback: {
85
+ // correctness: "The function correctly calculates the sum of all even numbers in the given list. It properly checks for evenness using the modulus operator and sums the valid numbers.",
86
+ // efficiency: "The implementation is efficient as it uses a generator expression which computes the sum in a single pass over the list. This minimizes memory usage as compared to creating an intermediate list of even numbers.",
87
+ // safety: "The function does not contain any safety issues. However, it assumes that all elements in the input list are integers. It would be prudent to handle cases where the input contains non-integer values (e.g., None, strings, etc.).",
88
+ // suggested_changes: "Consider adding type annotations to the function for better clarity and potential type checking, e.g. `def sum_of_even_numbers(numbers: list[int]) -> int:`. Also, include input validation to ensure 'numbers' is a list of integers.",
89
+ // },
90
+ // }