Table of Contents

Interface IPromptChain

Namespace
AiDotNet.Interfaces
Assembly
AiDotNet.dll

Defines the contract for executing multi-step prompt workflows.

public interface IPromptChain

Remarks

A prompt chain enables complex multi-step workflows where the output of one step becomes the input to the next. Chains support sequential execution, parallel execution, conditional branching, and map-reduce patterns for processing multiple items.

For Beginners: A prompt chain is like an assembly line for AI tasks.

Instead of doing everything in one big prompt, you break it into steps:

// Example: Translate and Summarize
Step 1: Translate document from Spanish to English
Step 2: Summarize the translated document
Step 3: Extract key points as bullet points

Each step takes the previous step's output as input.

Benefits of chains:

  • Simpler prompts (each does one thing well)
  • Better quality (specialized prompts perform better)
  • Easier debugging (you can inspect intermediate results)
  • Flexible workflows (add/remove/modify steps)

Chain types:

  • Sequential: Step 1 → Step 2 → Step 3
  • Parallel: Steps 1, 2, 3 run simultaneously, results merged
  • Conditional: If X then Step A, else Step B
  • Map-Reduce: Process many items in parallel, then combine

Properties

Name

Gets the name of this chain.

string Name { get; }

Property Value

string

Remarks

A human-readable identifier for this chain. Useful for logging and debugging.

Steps

Gets the steps in this chain.

IReadOnlyList<IChainStep> Steps { get; }

Property Value

IReadOnlyList<IChainStep>

Remarks

Returns all steps in the chain in execution order. Useful for inspection and debugging.

For Beginners: Shows what steps are in the chain.

Example:

foreach (var step in chain.Steps)
{
    Console.WriteLine($"Step: {step.Name}");
}

Methods

Execute(IDictionary<string, string>)

Executes the chain with named inputs (for chains with multiple entry points).

ChainResult Execute(IDictionary<string, string> inputs)

Parameters

inputs IDictionary<string, string>

A dictionary of named inputs.

Returns

ChainResult

The result of executing the chain.

Remarks

Some chains accept multiple inputs by name. This method allows passing all inputs at once as a dictionary.

For Beginners: For chains that need multiple pieces of information.

Example - Comparison chain:

var chain = new ComparisonChain(); // Compares two documents

var inputs = new Dictionary<string, string>
{
    ["document1"] = "First document text...",
    ["document2"] = "Second document text..."
};

var result = chain.Execute(inputs);
// result = "Similarities: ... Differences: ..."

Execute(string)

Executes the chain synchronously with the given input.

ChainResult Execute(string input)

Parameters

input string

The initial input to the chain.

Returns

ChainResult

The result of executing all steps in the chain.

Remarks

Executes each step in the chain in sequence (or in parallel if configured), passing the output of each step as input to the next.

For Beginners: Runs all the steps and returns the final result.

Example:

var chain = new SequentialChain()
    .AddStep("Translate to English", translatePrompt)
    .AddStep("Summarize", summarizePrompt);

string result = chain.Execute("Documento en español...");
// result = "Summary: This document discusses..."

ExecuteAsync(IDictionary<string, string>, CancellationToken)

Executes the chain with named inputs asynchronously.

Task<ChainResult> ExecuteAsync(IDictionary<string, string> inputs, CancellationToken cancellationToken = default)

Parameters

inputs IDictionary<string, string>

A dictionary of named inputs.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<ChainResult>

A task that resolves to the chain execution result.

ExecuteAsync(string, CancellationToken)

Executes the chain asynchronously with the given input.

Task<ChainResult> ExecuteAsync(string input, CancellationToken cancellationToken = default)

Parameters

input string

The initial input to the chain.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<ChainResult>

A task that resolves to the chain execution result.

Remarks

Async version of Execute for non-blocking execution. Essential for chains that make API calls to language models.

For Beginners: Same as Execute but doesn't block your program.

Use this when:

  • Running in a web application
  • Processing many items in parallel
  • Making actual API calls to language models

Example:

var result = await chain.ExecuteAsync("Input text...");