Logo
Guide/Workflow

Inputs / Outputs

Learn how to use different inputs and outputs in your workflows.

Inputs and outputs are the way to communicate between steps in a workflow. In the previous example, we used StartEvent and StopEvent to communicate between steps. However, you can use any type of event to communicate between steps.

Multiple inputs

You can define multiple inputs for a step.

In the following example, we define a complex workflow with multiple inputs and outputs.

import { , , ,  } from '@llamaindex/workflow';
 
class  extends <string> {
	constructor(: string) {
		super();
	}
}
 
class  extends <number> {
	constructor(: number) {
		super();
	}
}
 
class  extends <string> {
	constructor(: string) {
		super();
	}
}

First, let's define the events that we will use in the workflow.

.({
	: [, ],
	: []
}, async (
	,
	,
	
) => {
	const  = .;
	const  = .;
	return new (`A: ${}, B: ${}`);
});

This step means that it requires two events: AEvent and BEvent. It will return a ResultEvent with the data A: ${a}, B: ${b}.

A or B input

If we want to have a step that can accept either AEvent or BEvent, we can define the step like this:

.({
	: [.(, )],
	: []
}, async (
	,
	
) => {
	if (
aOrBEvent: AEvent | BEvent
aOrBEvent
instanceof ) {
const =
aOrBEvent: AEvent
aOrBEvent
.;
return new (`A: ${}`); } else { const =
aOrBEvent: BEvent
aOrBEvent
.;
return new (`B: ${}`); } });

This step means that it requires either AEvent or BEvent. It will return a ResultEvent with the data A: ${a} or B: ${b}.

You can still combine the logic with context.requireEvent to get the data from the event.

Multiple outputs

You can define multiple outputs for a step.

.({
	: [<string>],
	: [, ]
}, async (
	,
	
) => {
	const  = .;
	if (.() > 0.5) {
		return new (`Hello, ${}!`);
	} else {
		return new (42);
	}
});

This step will return either an AEvent or a BEvent based on a random number.

Edit on GitHub

Last updated on

On this page