Skip to content

Latest commit

 

History

History
231 lines (167 loc) · 8.49 KB

File metadata and controls

231 lines (167 loc) · 8.49 KB

Workflow Router Demo

AIGNE Logo

This is a demonstration of using AIGNE Framework to build a router workflow. The example now supports both one-shot and interactive chat modes, along with customizable model settings and pipeline input/output.

flowchart LR
in(In)
out(Out)
triage(Triage)
productSupport(Product Support)
feedback(Feedback)
other(Other)

in ==> triage
triage ==> productSupport ==> out
triage -.-> feedback -.-> out
triage -.-> other -.-> out

classDef inputOutput fill:#f9f0ed,stroke:#debbae,stroke-width:2px,color:#b35b39,font-weight:bolder;
classDef processing fill:#F0F4EB,stroke:#C2D7A7,stroke-width:2px,color:#6B8F3C,font-weight:bolder;

class in inputOutput
class out inputOutput
class triage processing
class productSupport processing
class feedback processing
class other processing
Loading

Prerequisites

  • Node.js (>=20.0) and npm installed on your machine
  • An OpenAI API key for interacting with OpenAI's services
  • Optional dependencies (if running the example from source code):
    • Bun for running unit tests & examples
    • Pnpm for package management

Quick Start (No Installation Required)

Run the Example

# Run in one-shot mode (default)
npx -y @aigne/example-workflow-router

# Run in interactive chat mode
npx -y @aigne/example-workflow-router --interactive

# Use pipeline input
echo "How do I return a product?" | npx -y @aigne/example-workflow-router

Connect to an AI Model

As an example, running npx -y @aigne/example-workflow-router --interactive requires an AI model. If this is your first run, you need to connect one.

run example

  • Connect via the official AIGNE Hub

Choose the first option and your browser will open the official AIGNE Hub page. Follow the prompts to complete the connection. If you're a new user, the system automatically grants 400,000 tokens for you to use.

connect to official aigne hub

  • Connect via a self-hosted AIGNE Hub

Choose the second option, enter the URL of your self-hosted AIGNE Hub, and follow the prompts to complete the connection. If you need to set up a self-hosted AIGNE Hub, visit the Blocklet Store to install and deploy it: Blocklet Store.

connect to self hosted aigne hub

  • Connect via a third-party model provider

Using OpenAI as an example, you can configure the provider's API key via environment variables. After configuration, run the example again:

export OPENAI_API_KEY="" # Set your OpenAI API key here

For more details on third-party model configuration (e.g., OpenAI, DeepSeek, Google Gemini), see .env.local.example.

After configuration, run the example again.

Debugging

The aigne observe command starts a local web server to monitor and analyze agent execution data. It provides a user-friendly interface to inspect traces, view detailed call information, and understand your agent’s behavior during runtime. This tool is essential for debugging, performance tuning, and gaining insight into how your agent processes information and interacts with tools and models.

Start the observation server.

aigne-observe-execute

View a list of recent executions.

aigne-observe-list

Installation

Clone the Repository

git clone https://github.com/AIGNE-io/aigne-framework

Install Dependencies

cd aigne-framework/examples/workflow-router

pnpm install

Run the Example

pnpm start # Run in one-shot mode (default)

# Run in interactive chat mode
pnpm start -- --interactive

# Use pipeline input
echo "How do I return a product?" | pnpm start

Run Options

The example supports the following command-line parameters:

Parameter Description Default
--interactive Run in interactive chat mode Disabled (one-shot mode)
--model <provider[:model]> AI model to use in format 'provider[:model]' where model is optional. Examples: 'openai' or 'openai:gpt-4o-mini' openai
--temperature <value> Temperature for model generation Provider default
--top-p <value> Top-p sampling value Provider default
--presence-penalty <value> Presence penalty value Provider default
--frequency-penalty <value> Frequency penalty value Provider default
--log-level <level> Set logging level (ERROR, WARN, INFO, DEBUG, TRACE) INFO
--input, -i <input> Specify input directly None

Examples

# Run in chat mode (interactive)
pnpm start -- --interactive

# Set logging level
pnpm start -- --log-level DEBUG

# Use pipeline input
echo "How do I return a product?" | pnpm start

Example

The following example demonstrates how to build a router workflow:

import { AIAgent, AIGNE } from "@aigne/core";
import { OpenAIChatModel } from "@aigne/core/models/openai-chat-model.js";

const { OPENAI_API_KEY } = process.env;

const model = new OpenAIChatModel({
  apiKey: OPENAI_API_KEY,
});

const productSupport = AIAgent.from({
  name: "product_support",
  description: "Agent to assist with any product-related questions.",
  instructions: `You are an agent capable of handling any product-related questions.
  Your goal is to provide accurate and helpful information about the product.
  Be polite, professional, and ensure the user feels supported.`,
  outputKey: "product_support",
});

const feedback = AIAgent.from({
  name: "feedback",
  description: "Agent to assist with any feedback-related questions.",
  instructions: `You are an agent capable of handling any feedback-related questions.
  Your goal is to listen to the user's feedback, acknowledge their input, and provide appropriate responses.
  Be empathetic, understanding, and ensure the user feels heard.`,
  outputKey: "feedback",
});

const other = AIAgent.from({
  name: "other",
  description: "Agent to assist with any general questions.",
  instructions: `You are an agent capable of handling any general questions.
  Your goal is to provide accurate and helpful information on a wide range of topics.
  Be friendly, knowledgeable, and ensure the user feels satisfied with the information provided.`,
  outputKey: "other",
});

const triage = AIAgent.from({
  name: "triage",
  instructions: `You are an agent capable of routing questions to the appropriate agent.
  Your goal is to understand the user's query and direct them to the agent best suited to assist them.
  Be efficient, clear, and ensure the user is connected to the right resource quickly.`,
  skills: [productSupport, feedback, other],
  toolChoice: "router", // Set toolChoice to "router" to enable router mode
});

const aigne = new AIGNE({ model });

const result1 = await aigne.invoke(triage, "How to use this product?");
console.log(result1);
// {
//   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?",
// }

const result2 = await aigne.invoke(triage, "I have feedback about the app.");
console.log(result2);
// {
//   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.",
// }

const result3 = await aigne.invoke(triage, "What is the weather today?");
console.log(result3);
// {
//   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!",
// }

License

This project is licensed under the MIT License.