Skip to main content

Crate rune_chain_parallel

Crate rune_chain_parallel 

Source
Expand description

ParallelChain — run multiple chains concurrently and merge their outputs.

All registered steps execute simultaneously using tokio::spawn. Their text outputs are merged into the shared variable map keyed by the step’s output_key, making them available to downstream chains.

Token usage across all branches is summed.

§Quick Start

use rune_chain_parallel::ParallelChain;
use rune_chain_core::{Chain, ChainError, GenerateResult, PromptArgs, prompt_args};
use async_trait::async_trait;

struct UpperCase;
#[async_trait]
impl Chain for UpperCase {
    async fn call(&self, input: PromptArgs) -> Result<GenerateResult, ChainError> {
        let text = input.get("input").and_then(|v| v.as_str()).unwrap_or("").to_uppercase();
        Ok(GenerateResult::from_text(text))
    }
}

struct LowerCase;
#[async_trait]
impl Chain for LowerCase {
    async fn call(&self, input: PromptArgs) -> Result<GenerateResult, ChainError> {
        let text = input.get("input").and_then(|v| v.as_str()).unwrap_or("").to_lowercase();
        Ok(GenerateResult::from_text(text))
    }
}

async fn run() {
    let par = ParallelChain::new()
        .branch(UpperCase, "upper")
        .branch(LowerCase, "lower");

    let out = par.execute(prompt_args! { "input" => "Hello" }).await.unwrap();
    assert_eq!(out["upper"].as_str().unwrap(), "HELLO");
    assert_eq!(out["lower"].as_str().unwrap(), "hello");
}

Structs§

ParallelChain
A fan-out chain that runs multiple sub-chains concurrently and merges outputs.