Skip to main content

rune_chain_core/
lib.rs

1//! Core traits and types for the `rune-chain` LLM orchestration framework.
2//!
3//! `rune-chain-core` is the foundation layer of the `rune-chain-*` crate family —
4//! a modern, async-first Rust port of the LangChain concept. It defines the shared
5//! vocabulary (types, traits, error variants) that every other `rune-chain-*` crate
6//! depends on, without pulling in any LLM provider SDK itself.
7//!
8//! # Features
9//!
10//! - [`Chain`] — the central trait; implement it to create any unit of LLM work
11//! - [`Llm`] — provider-agnostic interface to any large language model
12//! - [`Memory`] — pluggable conversation history (in-process, DB, vector store, …)
13//! - [`Message`] / [`Role`] — typed conversation turns (System, Human, Ai, Tool)
14//! - [`GenerateResult`] / [`TokenUsage`] — structured output with token accounting
15//! - [`StreamData`] — incremental token chunks for streaming responses
16//! - [`PromptArgs`] + [`prompt_args!`] — ergonomic key-value input for templates
17//! - [`ChainError`] / [`LlmError`] — structured error types with `From` wiring
18//!
19//! # Quick Start
20//!
21//! Implement [`Chain`] for your own type:
22//!
23//! ```rust
24//! use rune_chain_core::{Chain, ChainError, GenerateResult, PromptArgs, prompt_args};
25//! use async_trait::async_trait;
26//!
27//! struct EchoChain;
28//!
29//! #[async_trait]
30//! impl Chain for EchoChain {
31//!     async fn call(&self, input: PromptArgs) -> Result<GenerateResult, ChainError> {
32//!         let text = input
33//!             .get("input")
34//!             .and_then(|v| v.as_str())
35//!             .unwrap_or("")
36//!             .to_string();
37//!         Ok(GenerateResult::from_text(text))
38//!     }
39//! }
40//!
41//! # tokio_test::block_on(async {
42//! let chain = EchoChain;
43//! let result = chain.invoke(prompt_args! { "input" => "hello" }).await.unwrap();
44//! assert_eq!(result, "hello");
45//! # });
46//! ```
47
48mod chain;
49mod error;
50mod generate;
51mod llm;
52mod memory;
53mod message;
54mod prompt;
55mod stream;
56mod tool;
57
58pub use chain::Chain;
59pub use error::{ChainError, LlmError};
60pub use generate::{GenerateResult, TokenUsage};
61pub use llm::Llm;
62pub use memory::Memory;
63pub use message::{Message, Role};
64pub use prompt::PromptArgs;
65pub use stream::StreamData;
66pub use tool::Tool;