@aigne/example-workflow-router 1.1.0-beta.10
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 +122 -0
- package/index.ts +67 -0
- package/package.json +21 -0
- package/usages.ts +68 -0
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Workflow Router Demo
|
|
2
|
+
|
|
3
|
+
This is a demonstration of using [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework) to build a router 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-router # 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-router
|
|
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 router workflow:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { AIAgent, ChatModelOpenAI, ExecutionEngine } from "@aigne/core-next";
|
|
55
|
+
|
|
56
|
+
const model = new ChatModelOpenAI({
|
|
57
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const productSupport = AIAgent.from({
|
|
61
|
+
enableHistory: true,
|
|
62
|
+
name: "product_support",
|
|
63
|
+
description: "Agent to assist with any product-related questions.",
|
|
64
|
+
instructions: `You are an agent capable of handling any product-related questions.
|
|
65
|
+
Your goal is to provide accurate and helpful information about the product.
|
|
66
|
+
Be polite, professional, and ensure the user feels supported.`,
|
|
67
|
+
outputKey: "product_support",
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const feedback = AIAgent.from({
|
|
71
|
+
enableHistory: true,
|
|
72
|
+
name: "feedback",
|
|
73
|
+
description: "Agent to assist with any feedback-related questions.",
|
|
74
|
+
instructions: `You are an agent capable of handling any feedback-related questions.
|
|
75
|
+
Your goal is to listen to the user's feedback, acknowledge their input, and provide appropriate responses.
|
|
76
|
+
Be empathetic, understanding, and ensure the user feels heard.`,
|
|
77
|
+
outputKey: "feedback",
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const other = AIAgent.from({
|
|
81
|
+
enableHistory: true,
|
|
82
|
+
name: "other",
|
|
83
|
+
description: "Agent to assist with any general questions.",
|
|
84
|
+
instructions: `You are an agent capable of handling any general questions.
|
|
85
|
+
Your goal is to provide accurate and helpful information on a wide range of topics.
|
|
86
|
+
Be friendly, knowledgeable, and ensure the user feels satisfied with the information provided.`,
|
|
87
|
+
outputKey: "other",
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const triage = AIAgent.from({
|
|
91
|
+
name: "triage",
|
|
92
|
+
instructions: `You are an agent capable of routing questions to the appropriate agent.
|
|
93
|
+
Your goal is to understand the user's query and direct them to the agent best suited to assist them.
|
|
94
|
+
Be efficient, clear, and ensure the user is connected to the right resource quickly.`,
|
|
95
|
+
tools: [productSupport, feedback, other],
|
|
96
|
+
toolChoice: "router", // Set toolChoice to "router" to enable router mode
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const engine = new ExecutionEngine({ model });
|
|
100
|
+
|
|
101
|
+
const result1 = await engine.run("How to use this product?", triage);
|
|
102
|
+
console.log(result1);
|
|
103
|
+
// {
|
|
104
|
+
// product_support: "I’d be happy to help you with that! However, I need to know which specific product you’re referring to. Could you please provide me with the name or type of product you have in mind?",
|
|
105
|
+
// }
|
|
106
|
+
|
|
107
|
+
const result2 = await engine.run("I have feedback about the app.", triage);
|
|
108
|
+
console.log(result2);
|
|
109
|
+
// {
|
|
110
|
+
// feedback: "Thank you for sharing your feedback! I'm here to listen. Please go ahead and let me know what you’d like to share about the app.",
|
|
111
|
+
// }
|
|
112
|
+
|
|
113
|
+
const result3 = await engine.run("What is the weather today?", triage);
|
|
114
|
+
console.log(result3);
|
|
115
|
+
// {
|
|
116
|
+
// other: "I can't provide real-time weather updates. However, you can check a reliable weather website or a weather app on your phone for the current conditions in your area. If you tell me your location, I can suggest a few sources where you can find accurate weather information!",
|
|
117
|
+
// }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
This project is licensed under the MIT License.
|
package/index.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env npx -y bun
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert";
|
|
4
|
+
import { AIAgent, ChatModelOpenAI, ExecutionEngine, runChatLoopInTerminal } from "@aigne/core-next";
|
|
5
|
+
|
|
6
|
+
const { OPENAI_API_KEY } = process.env;
|
|
7
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
8
|
+
|
|
9
|
+
const model = new ChatModelOpenAI({
|
|
10
|
+
apiKey: OPENAI_API_KEY,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const productSupport = AIAgent.from({
|
|
14
|
+
enableHistory: true,
|
|
15
|
+
name: "product_support",
|
|
16
|
+
description: "Agent to assist with any product-related questions.",
|
|
17
|
+
instructions: `You are an agent capable of handling any product-related questions.
|
|
18
|
+
Your goal is to provide accurate and helpful information about the product.
|
|
19
|
+
Be polite, professional, and ensure the user feels supported.`,
|
|
20
|
+
outputKey: "product_support",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const feedback = AIAgent.from({
|
|
24
|
+
enableHistory: true,
|
|
25
|
+
name: "feedback",
|
|
26
|
+
description: "Agent to assist with any feedback-related questions.",
|
|
27
|
+
instructions: `You are an agent capable of handling any feedback-related questions.
|
|
28
|
+
Your goal is to listen to the user's feedback, acknowledge their input, and provide appropriate responses.
|
|
29
|
+
Be empathetic, understanding, and ensure the user feels heard.`,
|
|
30
|
+
outputKey: "feedback",
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const other = AIAgent.from({
|
|
34
|
+
enableHistory: true,
|
|
35
|
+
name: "other",
|
|
36
|
+
description: "Agent to assist with any general questions.",
|
|
37
|
+
instructions: `You are an agent capable of handling any general questions.
|
|
38
|
+
Your goal is to provide accurate and helpful information on a wide range of topics.
|
|
39
|
+
Be friendly, knowledgeable, and ensure the user feels satisfied with the information provided.`,
|
|
40
|
+
outputKey: "other",
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const triage = AIAgent.from({
|
|
44
|
+
name: "triage",
|
|
45
|
+
instructions: `You are an agent capable of routing questions to the appropriate agent.
|
|
46
|
+
Your goal is to understand the user's query and direct them to the agent best suited to assist them.
|
|
47
|
+
Be efficient, clear, and ensure the user is connected to the right resource quickly.`,
|
|
48
|
+
tools: [productSupport, feedback, other],
|
|
49
|
+
toolChoice: "router",
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const engine = new ExecutionEngine({ model });
|
|
53
|
+
|
|
54
|
+
const userAgent = await engine.run(triage);
|
|
55
|
+
|
|
56
|
+
await runChatLoopInTerminal(userAgent, {
|
|
57
|
+
welcome: `Welcome to the support chat!
|
|
58
|
+
|
|
59
|
+
I can help you with any questions you have, such as
|
|
60
|
+
- product-related queries: "How do I use this product?"
|
|
61
|
+
- feedback: "I have feedback about the app."
|
|
62
|
+
- general questions: "What is the weather today?"
|
|
63
|
+
|
|
64
|
+
How can I assist you today?
|
|
65
|
+
`,
|
|
66
|
+
defaultQuestion: "How do I use this product?",
|
|
67
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aigne/example-workflow-router",
|
|
3
|
+
"version": "1.1.0-beta.10",
|
|
4
|
+
"description": "A demonstration of using AIGNE Framework to build a router workflow",
|
|
5
|
+
"author": "",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"bin": "index.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
".env.local.example",
|
|
11
|
+
"*.ts",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"zod": "^3.24.2",
|
|
16
|
+
"@aigne/core-next": "^1.1.0-beta.10"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"start": "npx -y bun run index.ts"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/usages.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { AIAgent, ChatModelOpenAI, ExecutionEngine } from "@aigne/core-next";
|
|
3
|
+
|
|
4
|
+
const { OPENAI_API_KEY } = process.env;
|
|
5
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
6
|
+
|
|
7
|
+
const model = new ChatModelOpenAI({
|
|
8
|
+
apiKey: OPENAI_API_KEY,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const productSupport = AIAgent.from({
|
|
12
|
+
enableHistory: true,
|
|
13
|
+
name: "product_support",
|
|
14
|
+
description: "Agent to assist with any product-related questions.",
|
|
15
|
+
instructions: `You are an agent capable of handling any product-related questions.
|
|
16
|
+
Your goal is to provide accurate and helpful information about the product.
|
|
17
|
+
Be polite, professional, and ensure the user feels supported.`,
|
|
18
|
+
outputKey: "product_support",
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const feedback = AIAgent.from({
|
|
22
|
+
enableHistory: true,
|
|
23
|
+
name: "feedback",
|
|
24
|
+
description: "Agent to assist with any feedback-related questions.",
|
|
25
|
+
instructions: `You are an agent capable of handling any feedback-related questions.
|
|
26
|
+
Your goal is to listen to the user's feedback, acknowledge their input, and provide appropriate responses.
|
|
27
|
+
Be empathetic, understanding, and ensure the user feels heard.`,
|
|
28
|
+
outputKey: "feedback",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const other = AIAgent.from({
|
|
32
|
+
enableHistory: true,
|
|
33
|
+
name: "other",
|
|
34
|
+
description: "Agent to assist with any general questions.",
|
|
35
|
+
instructions: `You are an agent capable of handling any general questions.
|
|
36
|
+
Your goal is to provide accurate and helpful information on a wide range of topics.
|
|
37
|
+
Be friendly, knowledgeable, and ensure the user feels satisfied with the information provided.`,
|
|
38
|
+
outputKey: "other",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const triage = AIAgent.from({
|
|
42
|
+
name: "triage",
|
|
43
|
+
instructions: `You are an agent capable of routing questions to the appropriate agent.
|
|
44
|
+
Your goal is to understand the user's query and direct them to the agent best suited to assist them.
|
|
45
|
+
Be efficient, clear, and ensure the user is connected to the right resource quickly.`,
|
|
46
|
+
tools: [productSupport, feedback, other],
|
|
47
|
+
toolChoice: "router", // Set toolChoice to "router" to enable router mode
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const engine = new ExecutionEngine({ model });
|
|
51
|
+
|
|
52
|
+
const result1 = await engine.run("How to use this product?", triage);
|
|
53
|
+
console.log(result1);
|
|
54
|
+
// {
|
|
55
|
+
// product_support: "I’d be happy to help you with that! However, I need to know which specific product you’re referring to. Could you please provide me with the name or type of product you have in mind?",
|
|
56
|
+
// }
|
|
57
|
+
|
|
58
|
+
const result2 = await engine.run("I have feedback about the app.", triage);
|
|
59
|
+
console.log(result2);
|
|
60
|
+
// {
|
|
61
|
+
// feedback: "Thank you for sharing your feedback! I'm here to listen. Please go ahead and let me know what you’d like to share about the app.",
|
|
62
|
+
// }
|
|
63
|
+
|
|
64
|
+
const result3 = await engine.run("What is the weather today?", triage);
|
|
65
|
+
console.log(result3);
|
|
66
|
+
// {
|
|
67
|
+
// other: "I can't provide real-time weather updates. However, you can check a reliable weather website or a weather app on your phone for the current conditions in your area. If you tell me your location, I can suggest a few sources where you can find accurate weather information!",
|
|
68
|
+
// }
|