@aigne/example-workflow-router 1.1.0-beta.8

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,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
+ 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";
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,63 @@
1
+ #!/usr/bin/env npx -y bun
2
+
3
+ import { AIAgent, ChatModelOpenAI, ExecutionEngine, runChatLoopInTerminal } from "@aigne/core";
4
+
5
+ const model = new ChatModelOpenAI({
6
+ apiKey: process.env.OPENAI_API_KEY,
7
+ });
8
+
9
+ const productSupport = AIAgent.from({
10
+ enableHistory: true,
11
+ name: "product_support",
12
+ description: "Agent to assist with any product-related questions.",
13
+ instructions: `You are an agent capable of handling any product-related questions.
14
+ Your goal is to provide accurate and helpful information about the product.
15
+ Be polite, professional, and ensure the user feels supported.`,
16
+ outputKey: "product_support",
17
+ });
18
+
19
+ const feedback = AIAgent.from({
20
+ enableHistory: true,
21
+ name: "feedback",
22
+ description: "Agent to assist with any feedback-related questions.",
23
+ instructions: `You are an agent capable of handling any feedback-related questions.
24
+ Your goal is to listen to the user's feedback, acknowledge their input, and provide appropriate responses.
25
+ Be empathetic, understanding, and ensure the user feels heard.`,
26
+ outputKey: "feedback",
27
+ });
28
+
29
+ const other = AIAgent.from({
30
+ enableHistory: true,
31
+ name: "other",
32
+ description: "Agent to assist with any general questions.",
33
+ instructions: `You are an agent capable of handling any general questions.
34
+ Your goal is to provide accurate and helpful information on a wide range of topics.
35
+ Be friendly, knowledgeable, and ensure the user feels satisfied with the information provided.`,
36
+ outputKey: "other",
37
+ });
38
+
39
+ const triage = AIAgent.from({
40
+ name: "triage",
41
+ instructions: `You are an agent capable of routing questions to the appropriate agent.
42
+ Your goal is to understand the user's query and direct them to the agent best suited to assist them.
43
+ Be efficient, clear, and ensure the user is connected to the right resource quickly.`,
44
+ tools: [productSupport, feedback, other],
45
+ toolChoice: "router",
46
+ });
47
+
48
+ const engine = new ExecutionEngine({ model });
49
+
50
+ const userAgent = await engine.run(triage);
51
+
52
+ await runChatLoopInTerminal(userAgent, {
53
+ welcome: `Welcome to the support chat!
54
+
55
+ I can help you with any questions you have, such as
56
+ - product-related queries: "How do I use this product?"
57
+ - feedback: "I have feedback about the app."
58
+ - general questions: "What is the weather today?"
59
+
60
+ How can I assist you today?
61
+ `,
62
+ defaultQuestion: "How do I use this product?",
63
+ });
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@aigne/example-workflow-router",
3
+ "version": "1.1.0-beta.8",
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.8"
17
+ },
18
+ "scripts": {
19
+ "start": "npx -y bun run index.ts"
20
+ }
21
+ }
package/usages.ts ADDED
@@ -0,0 +1,64 @@
1
+ import { AIAgent, ChatModelOpenAI, ExecutionEngine } from "@aigne/core";
2
+
3
+ const model = new ChatModelOpenAI({
4
+ apiKey: process.env.OPENAI_API_KEY,
5
+ });
6
+
7
+ const productSupport = AIAgent.from({
8
+ enableHistory: true,
9
+ name: "product_support",
10
+ description: "Agent to assist with any product-related questions.",
11
+ instructions: `You are an agent capable of handling any product-related questions.
12
+ Your goal is to provide accurate and helpful information about the product.
13
+ Be polite, professional, and ensure the user feels supported.`,
14
+ outputKey: "product_support",
15
+ });
16
+
17
+ const feedback = AIAgent.from({
18
+ enableHistory: true,
19
+ name: "feedback",
20
+ description: "Agent to assist with any feedback-related questions.",
21
+ instructions: `You are an agent capable of handling any feedback-related questions.
22
+ Your goal is to listen to the user's feedback, acknowledge their input, and provide appropriate responses.
23
+ Be empathetic, understanding, and ensure the user feels heard.`,
24
+ outputKey: "feedback",
25
+ });
26
+
27
+ const other = AIAgent.from({
28
+ enableHistory: true,
29
+ name: "other",
30
+ description: "Agent to assist with any general questions.",
31
+ instructions: `You are an agent capable of handling any general questions.
32
+ Your goal is to provide accurate and helpful information on a wide range of topics.
33
+ Be friendly, knowledgeable, and ensure the user feels satisfied with the information provided.`,
34
+ outputKey: "other",
35
+ });
36
+
37
+ const triage = AIAgent.from({
38
+ name: "triage",
39
+ instructions: `You are an agent capable of routing questions to the appropriate agent.
40
+ Your goal is to understand the user's query and direct them to the agent best suited to assist them.
41
+ Be efficient, clear, and ensure the user is connected to the right resource quickly.`,
42
+ tools: [productSupport, feedback, other],
43
+ toolChoice: "router", // Set toolChoice to "router" to enable router mode
44
+ });
45
+
46
+ const engine = new ExecutionEngine({ model });
47
+
48
+ const result1 = await engine.run("How to use this product?", triage);
49
+ console.log(result1);
50
+ // {
51
+ // 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?",
52
+ // }
53
+
54
+ const result2 = await engine.run("I have feedback about the app.", triage);
55
+ console.log(result2);
56
+ // {
57
+ // 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.",
58
+ // }
59
+
60
+ const result3 = await engine.run("What is the weather today?", triage);
61
+ console.log(result3);
62
+ // {
63
+ // 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!",
64
+ // }