lc_chains/lib.rs
1#![warn(missing_docs)]
2// lc-chains/src/lib.rs
3//! Chain system for composing operations.
4//!
5//! Chain is LangChain's core abstraction, representing a sequence of operations.
6//!
7//! # Core Concepts
8//!
9//! - **BaseChain**: Base trait for chains.
10//! - **LLMChain**: Most basic chain (Prompt + LLM).
11//! - **SequentialChain**: Execute multiple chains sequentially.
12//!
13//! # Example
14//!
15//! ```ignore
16//! use lc_chains::{LLMChain, SequentialChain};
17//!
18//! let chain1 = LLMChain::new(llm.clone(), "Generate a word about {topic}");
19//! let chain2 = LLMChain::new(llm, "Make a sentence with word: {word}");
20//!
21//! let seq_chain = SequentialChain::new()
22//! .add_chain(Arc::new(chain1), vec!["topic"], vec!["word"])
23//! .add_chain(Arc::new(chain2), vec!["word"], vec!["sentence"]);
24//!
25//! let inputs = HashMap::from([("topic".into(), "programming".into())]);
26//! let result = seq_chain.invoke(inputs).await?;
27//! ```
28
29pub mod adapter;
30pub mod base;
31pub mod conversation_chain;
32pub mod conversation_retrieval;
33pub mod document_chains;
34pub mod llm_chain;
35pub mod retrieval_qa;
36pub mod router_chain;
37pub mod sequential_chain;
38
39pub use adapter::ChainRunnable;
40pub use base::{BaseChain, ChainError, ChainResult, ChainStream, StreamToken};
41pub use conversation_chain::{ConversationChain, ConversationChainBuilder};
42pub use conversation_retrieval::ConversationRetrievalChain;
43pub use document_chains::{
44 MapReduceDocumentsChain, MapRerankDocumentsChain, RefineDocumentsChain, StuffDocumentsChain,
45};
46pub use llm_chain::{LLMChain, LLMChainBuilder};
47pub use retrieval_qa::RetrievalQA;
48pub use router_chain::{LLMRouterChain, RouteDestination, RouterChain};
49pub use sequential_chain::SequentialChain;
50
51use lc_core::BaseChatModel;
52use lc_providers::ProviderError;
53use std::sync::Arc;
54
55/// Uniform chat model trait object stored by every chain.
56///
57/// Chains hold the LLM behind this `Arc<dyn BaseChatModel<Error = ProviderError>>`
58/// instead of a generic `M: BaseChatModel`, so chains can be freely composed
59/// behind `Arc<dyn BaseChain>` without carrying type parameters.
60pub(crate) type BoxedChatModel = Arc<dyn BaseChatModel<Error = ProviderError> + Send + Sync>;