lc-chains 0.22.0

Chain compositions for langchainrust — LLMChain, SequentialChain, RetrievalQA, etc.
Documentation
#![warn(missing_docs)]
// lc-chains/src/lib.rs
//! Chain system for composing operations.
//!
//! Chain is LangChain's core abstraction, representing a sequence of operations.
//!
//! # Core Concepts
//!
//! - **BaseChain**: Base trait for chains.
//! - **LLMChain**: Most basic chain (Prompt + LLM).
//! - **SequentialChain**: Execute multiple chains sequentially.
//!
//! # Example
//!
//! ```ignore
//! use lc_chains::{LLMChain, SequentialChain};
//!
//! let chain1 = LLMChain::new(llm.clone(), "Generate a word about {topic}");
//! let chain2 = LLMChain::new(llm, "Make a sentence with word: {word}");
//!
//! let seq_chain = SequentialChain::new()
//!     .add_chain(Arc::new(chain1), vec!["topic"], vec!["word"])
//!     .add_chain(Arc::new(chain2), vec!["word"], vec!["sentence"]);
//!
//! let inputs = HashMap::from([("topic".into(), "programming".into())]);
//! let result = seq_chain.invoke(inputs).await?;
//! ```

pub mod adapter;
pub mod base;
pub mod conversation_chain;
pub mod conversation_retrieval;
pub mod document_chains;
pub mod llm_chain;
pub mod retrieval_qa;
pub mod router_chain;
pub mod sequential_chain;

pub use adapter::ChainRunnable;
pub use base::{BaseChain, ChainError, ChainResult, ChainStream, StreamToken};
pub use conversation_chain::{ConversationChain, ConversationChainBuilder};
pub use conversation_retrieval::ConversationRetrievalChain;
pub use document_chains::{
    MapReduceDocumentsChain, MapRerankDocumentsChain, RefineDocumentsChain, StuffDocumentsChain,
};
pub use llm_chain::{LLMChain, LLMChainBuilder};
pub use retrieval_qa::RetrievalQA;
pub use router_chain::{LLMRouterChain, RouteDestination, RouterChain};
pub use sequential_chain::SequentialChain;

use lc_core::BaseChatModel;
use lc_providers::ProviderError;
use std::sync::Arc;

/// Uniform chat model trait object stored by every chain.
///
/// Chains hold the LLM behind this `Arc<dyn BaseChatModel<Error = ProviderError>>`
/// instead of a generic `M: BaseChatModel`, so chains can be freely composed
/// behind `Arc<dyn BaseChain>` without carrying type parameters.
pub(crate) type BoxedChatModel = Arc<dyn BaseChatModel<Error = ProviderError> + Send + Sync>;