Skip to main content

ParallelChain

Struct ParallelChain 

Source
pub struct ParallelChain { /* private fields */ }
Expand description

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

Each branch receives the same input and runs in parallel via tokio. Their outputs are stored under distinct keys in the result map.

Implementations§

Source§

impl ParallelChain

Source

pub fn new() -> Self

Create an empty parallel chain.

Source

pub fn branch( self, chain: impl Chain + 'static, output_key: impl Into<String>, ) -> Self

Add a branch: run chain in parallel and store its output under output_key.

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

struct Noop;
#[async_trait]
impl Chain for Noop {
    async fn call(&self, _: PromptArgs) -> Result<GenerateResult, ChainError> {
        Ok(GenerateResult::from_text(""))
    }
}

let par = ParallelChain::new().branch(Noop, "a").branch(Noop, "b");
assert_eq!(par.output_keys(), vec!["a", "b", "generate_result"]);

Trait Implementations§

Source§

impl Chain for ParallelChain

Source§

fn call<'life0, 'async_trait>( &'life0 self, input: PromptArgs, ) -> Pin<Box<dyn Future<Output = Result<GenerateResult, ChainError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Run the chain and return the full GenerateResult including token usage.
Source§

fn execute<'life0, 'async_trait>( &'life0 self, input: PromptArgs, ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, Value>, ChainError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Run the chain and return a named-output map ready to pipe into the next step. Read more
Source§

fn output_keys(&self) -> Vec<String>

The keys this chain writes into the map returned by Chain::execute.
Source§

fn input_keys(&self) -> Vec<String>

The variable names this chain reads from its PromptArgs input.
Source§

fn invoke<'life0, 'async_trait>( &'life0 self, input_variables: HashMap<String, Value>, ) -> Pin<Box<dyn Future<Output = Result<String, ChainError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Run the chain and return the generated text string only. Read more
Source§

fn stream<'life0, 'async_trait>( &'life0 self, _input_variables: HashMap<String, Value>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamData, ChainError>> + Send>>, ChainError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Stream tokens as they are produced rather than waiting for the full completion. Read more
Source§

impl Default for ParallelChain

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.