Module anchor_chain::parallel_node

source ·
Expand description

Provides a structure for processing input through multiple nodes in parallel.

The ParallelNode struct represents a node that processes input through multiple nodes in parallel. The output of each node is then combined using a provided function to produce the final output.

Example:

use async_trait::async_trait;
use futures::{future::BoxFuture, Future};
use std::collections::HashMap;

use anchor_chain::{
    chain::ChainBuilder,
    models::openai::OpenAIModel,
    parallel_node::{ParallelNode, to_boxed_future},
    nodes::prompt::Prompt,
};

#[tokio::main]
async fn main() {
    let gpt3 =
        Box::new(OpenAIModel::new_gpt3_5_turbo("You are a helpful assistant").await);
    let gpt4 = Box::new(OpenAIModel::new_gpt4_turbo("You are a helpful assistant").await);

    let concat_fn = to_boxed_future(|outputs: Vec<String>| {
        Ok(outputs
            .iter()
            .enumerate()
            .map(|(i, output)| format!("Output {}:\n```\n{}\n```\n", i + 1, output))
            .collect::<Vec<String>>()
            .concat())
    });


    let chain = ChainBuilder::new()
        .link(Prompt::new("{{ input }}"))
        .link(ParallelNode::new(vec![gpt3, gpt4], concat_fn))
        .build();

    let output = chain
        .process(HashMap::from([("input", "Write a hello world program in Rust")]))
        .await
        .expect("Error processing chain");
    println!("{}", output);
}

Structs§

  • A node that processes input through multiple nodes in parallel.

Functions§

  • Converts a function into a BoxFuture that can be used in a ParallelNode.