@aigne/example-workflow-code-execution 1.1.0-beta.13
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/.env.local.example +5 -0
- package/README.md +138 -0
- package/index.ts +51 -0
- package/package.json +25 -0
- package/usage.ts +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Workflow code-execution Demo
|
|
2
|
+
|
|
3
|
+
This is a demonstration of using [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework) to build a code-execution workflow.
|
|
4
|
+
|
|
5
|
+
```mermaid
|
|
6
|
+
flowchart LR
|
|
7
|
+
|
|
8
|
+
in(In)
|
|
9
|
+
out(Out)
|
|
10
|
+
coder(Coder)
|
|
11
|
+
sandbox(Sandbox)
|
|
12
|
+
|
|
13
|
+
coder -.-> sandbox
|
|
14
|
+
sandbox -.-> coder
|
|
15
|
+
in ==> coder ==> out
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
classDef inputOutput fill:#f9f0ed,stroke:#debbae,stroke-width:2px,color:#b35b39,font-weight:bolder;
|
|
19
|
+
classDef processing fill:#F0F4EB,stroke:#C2D7A7,stroke-width:2px,color:#6B8F3C,font-weight:bolder;
|
|
20
|
+
|
|
21
|
+
class in inputOutput
|
|
22
|
+
class out inputOutput
|
|
23
|
+
class coder processing
|
|
24
|
+
class sandbox processing
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Workflow of a code-execution between user and coder using a sandbox:
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
```mermaid
|
|
31
|
+
sequenceDiagram
|
|
32
|
+
|
|
33
|
+
participant User
|
|
34
|
+
participant Coder as Agent Coder
|
|
35
|
+
participant Sandbox as Agent Sandbox
|
|
36
|
+
|
|
37
|
+
User ->> Coder: 10! = ?
|
|
38
|
+
Coder ->> Sandbox: "function factorial(n) { return n <= 1 ? 1 : n * factorial(n - 1) } factorial(10)"
|
|
39
|
+
Sandbox ->> Coder: { result: 3628800 }
|
|
40
|
+
Coder ->> User: The value of \(10!\) (10 factorial) is 3,628,800.
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Prerequisites
|
|
44
|
+
|
|
45
|
+
- [Node.js](https://nodejs.org) and npm installed on your machine
|
|
46
|
+
- [OpenAI API key](https://platform.openai.com/api-keys) used to interact with OpenAI API
|
|
47
|
+
- [Pnpm](https://pnpm.io) [Optional] if you want to run the example from source code
|
|
48
|
+
|
|
49
|
+
## Try without Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY # setup your OpenAI API key
|
|
53
|
+
|
|
54
|
+
npx -y @aigne/example-workflow-code-execution # run the example
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
### Clone the Repository
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
git clone https://github.com/AIGNE-io/aigne-framework
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Install Dependencies
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
cd aigne-framework/examples/workflow-code-execution
|
|
69
|
+
|
|
70
|
+
pnpm install
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Setup Environment Variables
|
|
74
|
+
|
|
75
|
+
Setup your OpenAI API key in the `.env.local` file:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
OPENAI_API_KEY="" # setup your OpenAI API key here
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Run the Example
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pnpm start
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Example
|
|
88
|
+
|
|
89
|
+
The following example demonstrates how to build a code-execution workflow:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import assert from "node:assert";
|
|
93
|
+
import { AIAgent, ChatModelOpenAI, ExecutionEngine, FunctionAgent } from "@aigne/core-next";
|
|
94
|
+
import { z } from "zod";
|
|
95
|
+
|
|
96
|
+
const { OPENAI_API_KEY } = process.env;
|
|
97
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
98
|
+
|
|
99
|
+
const model = new ChatModelOpenAI({
|
|
100
|
+
apiKey: OPENAI_API_KEY,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const sandbox = FunctionAgent.from({
|
|
104
|
+
name: "js-sandbox",
|
|
105
|
+
description: "A js sandbox for running javascript code",
|
|
106
|
+
inputSchema: z.object({
|
|
107
|
+
code: z.string().describe("The code to run"),
|
|
108
|
+
}),
|
|
109
|
+
fn: async (input: { code: string }) => {
|
|
110
|
+
const { code } = input;
|
|
111
|
+
// biome-ignore lint/security/noGlobalEval: <explanation>
|
|
112
|
+
const result = eval(code);
|
|
113
|
+
return { result };
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const coder = AIAgent.from({
|
|
118
|
+
name: "coder",
|
|
119
|
+
instructions: `\
|
|
120
|
+
You are a proficient coder. You write code to solve problems.
|
|
121
|
+
Work with the sandbox to execute your code.
|
|
122
|
+
`,
|
|
123
|
+
tools: [sandbox],
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const engine = new ExecutionEngine({ model });
|
|
127
|
+
|
|
128
|
+
const result = await engine.run("10! = ?", coder);
|
|
129
|
+
console.log(result);
|
|
130
|
+
// Output:
|
|
131
|
+
// {
|
|
132
|
+
// text: "The value of \\(10!\\) (10 factorial) is 3,628,800.",
|
|
133
|
+
// }
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
This project is licensed under the MIT License.
|
package/index.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env npx -y bun
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert";
|
|
4
|
+
import {
|
|
5
|
+
AIAgent,
|
|
6
|
+
ChatModelOpenAI,
|
|
7
|
+
ExecutionEngine,
|
|
8
|
+
FunctionAgent,
|
|
9
|
+
runChatLoopInTerminal,
|
|
10
|
+
} from "@aigne/core-next";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
const { OPENAI_API_KEY } = process.env;
|
|
14
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
15
|
+
|
|
16
|
+
const model = new ChatModelOpenAI({
|
|
17
|
+
apiKey: OPENAI_API_KEY,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const sandbox = FunctionAgent.from({
|
|
21
|
+
name: "js-sandbox",
|
|
22
|
+
description: "A js sandbox for running javascript code",
|
|
23
|
+
inputSchema: z.object({
|
|
24
|
+
code: z.string().describe("The code to run"),
|
|
25
|
+
}),
|
|
26
|
+
fn: async (input: { code: string }) => {
|
|
27
|
+
const { code } = input;
|
|
28
|
+
// biome-ignore lint/security/noGlobalEval: <explanation>
|
|
29
|
+
const result = eval(code);
|
|
30
|
+
return { result };
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const coder = AIAgent.from({
|
|
35
|
+
name: "coder",
|
|
36
|
+
instructions: `\
|
|
37
|
+
You are a proficient coder. You write code to solve problems.
|
|
38
|
+
Work with the sandbox to execute your code.
|
|
39
|
+
`,
|
|
40
|
+
tools: [sandbox],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const engine = new ExecutionEngine({ model });
|
|
44
|
+
|
|
45
|
+
const userAgent = await engine.run(coder);
|
|
46
|
+
|
|
47
|
+
await runChatLoopInTerminal(userAgent, {
|
|
48
|
+
welcome:
|
|
49
|
+
"Welcome to the code execution workflow! you can ask me anything can be resolved by running code.",
|
|
50
|
+
defaultQuestion: "10! = ?",
|
|
51
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aigne/example-workflow-code-execution",
|
|
3
|
+
"version": "1.1.0-beta.13",
|
|
4
|
+
"description": "A demonstration of using AIGNE Framework to build a code-execution workflow",
|
|
5
|
+
"author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
|
|
6
|
+
"homepage": "https://github.com/AIGNE-io/aigne-framework/tree/main/examples/workflow-code-execution",
|
|
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.13"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"start": "npx -y bun run index.ts"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/usage.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env npx -y bun
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert";
|
|
4
|
+
import { AIAgent, ChatModelOpenAI, ExecutionEngine, FunctionAgent } from "@aigne/core-next";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
|
|
7
|
+
const { OPENAI_API_KEY } = process.env;
|
|
8
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
9
|
+
|
|
10
|
+
const model = new ChatModelOpenAI({
|
|
11
|
+
apiKey: OPENAI_API_KEY,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const sandbox = FunctionAgent.from({
|
|
15
|
+
name: "js-sandbox",
|
|
16
|
+
description: "A js sandbox for running javascript code",
|
|
17
|
+
inputSchema: z.object({
|
|
18
|
+
code: z.string().describe("The code to run"),
|
|
19
|
+
}),
|
|
20
|
+
fn: async (input: { code: string }) => {
|
|
21
|
+
const { code } = input;
|
|
22
|
+
// biome-ignore lint/security/noGlobalEval: <explanation>
|
|
23
|
+
const result = eval(code);
|
|
24
|
+
return { result };
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const coder = AIAgent.from({
|
|
29
|
+
name: "coder",
|
|
30
|
+
instructions: `\
|
|
31
|
+
You are a proficient coder. You write code to solve problems.
|
|
32
|
+
Work with the sandbox to execute your code.
|
|
33
|
+
`,
|
|
34
|
+
tools: [sandbox],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const engine = new ExecutionEngine({ model });
|
|
38
|
+
|
|
39
|
+
const result = await engine.run("10! = ?", coder);
|
|
40
|
+
console.log(result);
|
|
41
|
+
// Output:
|
|
42
|
+
// {
|
|
43
|
+
// text: "The value of \\(10!\\) (10 factorial) is 3,628,800.",
|
|
44
|
+
// }
|