⚡ Building applications with LLMs through composability, with Rust! ⚡
This is the Rust language implementation of LangChain, providing a powerful and type-safe way to build LLM applications in Rust.
- 🚀 Multiple LLM Providers: Support for OpenAI, Azure OpenAI, Anthropic Claude, MistralAI, Google Gemini, AWS Bedrock, HuggingFace, Alibaba Qwen, DeepSeek, and Ollama
- 🔗 Chains: LLM chains, conversational chains, sequential chains, Q&A chains, SQL chains, and more
- 🤖 Agents: Chat agents with tools, multi-agent systems (router, subagents, skills, handoffs)
- 📚 RAG: Agentic RAG, Hybrid RAG, and two-step RAG implementations
- 🧠 Memory: Simple memory, conversational memory, and long-term memory with metadata
- 🛠️ Tools: Search tools, command line, Wolfram Alpha, text-to-speech, SequentialThinking (chain-of-thought), BrowserUse (browser automation with
browser-usefeature), and more - 📄 Document Loaders: PDF, HTML, CSV, Git commits, source code, and more
- 🗄️ Vector Stores: PostgreSQL (pgvector), Qdrant, SQLite (VSS/Vec), SurrealDB, OpenSearch, In-Memory, Chroma, FAISS (hnsw_rs), MongoDB Atlas, Pinecone, Weaviate
- 🎯 Embeddings: OpenAI, Azure OpenAI, Ollama, FastEmbed, MistralAI
- 🔧 Middleware: Logging, PII detection, content filtering, rate limiting, retry, and custom middleware
- 🎨 Structured Output: JSON schema validation and structured response generation
- ⚙️ Runtime Context: Dynamic prompts, typed context, and runtime-aware middleware
- 📊 LangGraph: State graphs, streaming, persistence (SQLite/memory), interrupts, subgraphs, and time-travel debugging
- 🤖 Deep Agent: Planning (write_todos), filesystem tools (ls, read_file, write_file, edit_file), skills, long-term memory, and human-in-the-loop
This library heavily relies on serde_json for its operation.
First, ensure serde_json is added to your Rust project.
cargo add serde_jsonThen, you can add langchain-ai-rust to your Rust project.
cargo add langchain-ai-rustcargo add langchain-ai-rust --features postgrescargo add langchain-ai-rust --features qdrantDownload additional sqlite_vss libraries from https://github.com/asg017/sqlite-vss
cargo add langchain-ai-rust --features sqlite-vssDownload additional sqlite_vec libraries from https://github.com/asg017/sqlite-vec
cargo add langchain-ai-rust --features sqlite-veccargo add langchain-ai-rust --features surrealdbcargo add langchain-ai-rust --features opensearchcargo add langchain-ai-rust --features in-memorycargo add langchain-ai-rust --features chromacargo add langchain-ai-rust --features faisscargo add langchain-ai-rust --features mongodbcargo add langchain-ai-rust --features pineconecargo add langchain-ai-rust --features weaviatecargo add langchain-ai-rust --features ollamacargo add langchain-ai-rust --features mistralaicargo add langchain-ai-rust --features geminicargo add langchain-ai-rust --features bedrockcargo add langchain-ai-rust --features pdf-extractcargo add langchain-ai-rust --features lopdfcargo add langchain-ai-rust --features html-to-markdowncargo add langchain-ai-rust --features tree-sittercargo add langchain-ai-rust --features fastembedRequires Chrome/Chromium installed (or use headless_chrome's bundled Chromium where available).
cargo add langchain-ai-rust --features browser-useuse langchain_ai_rust::llm::openai::{OpenAI, OpenAIModel};
#[tokio::main]
async fn main() {
let llm = OpenAI::default().with_model(OpenAIModel::Gpt4oMini.to_string());
let response = llm.invoke("What is Rust?").await.unwrap();
println!("{}", response);
}The init_chat_model function provides a unified interface to initialize any supported LLM:
use langchain_ai_rust::language_models::init_chat_model;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize any supported model
let model = init_chat_model("gpt-4o-mini", None, None, None, None, None, None, None).await?;
let response = model.invoke("Hello, world!").await?;
println!("{}", response);
Ok(())
}Supported model formats:
gpt-4o-mini,gpt-4o,gpt-4-turbo(OpenAI)claude-3-5-sonnet-20241022(Anthropic)mistralai/mistral-large-latest(MistralAI)gemini-1.5-pro(Google Gemini)anthropic.claude-3-5-sonnet-20241022-v2:0(AWS Bedrock)meta-llama/Llama-3.1-8B-Instruct(HuggingFace)qwen-plus(Alibaba Qwen)deepseek-chat(DeepSeek)llama3(Ollama)
use langchain_ai_rust::{
chain::{Chain, LLMChainBuilder},
fmt_message, fmt_placeholder, fmt_template,
llm::openai::{OpenAI, OpenAIModel},
message_formatter,
prompt::HumanMessagePromptTemplate,
prompt_args,
schemas::messages::Message,
template_fstring,
};
#[tokio::main]
async fn main() {
let open_ai = OpenAI::default().with_model(OpenAIModel::Gpt4oMini.to_string());
let prompt = message_formatter![
fmt_message!(Message::new_system_message(
"You are a helpful assistant."
)),
fmt_placeholder!("history"),
fmt_template!(HumanMessagePromptTemplate::new(template_fstring!(
"{input}", "input"
))),
];
let chain = LLMChainBuilder::new()
.prompt(prompt)
.llm(open_ai)
.build()
.unwrap();
match chain
.invoke(prompt_args! {
"input" => "What is Rust?",
"history" => vec![
Message::new_human_message("Hello"),
Message::new_ai_message("Hi there!"),
],
})
.await
{
Ok(result) => println!("Result: {:?}", result),
Err(e) => panic!("Error: {:?}", e),
}
}use std::sync::Arc;
use langchain_ai_rust::{
agent::create_agent,
schemas::messages::Message,
tools::CommandExecutor,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let command_executor = Arc::new(CommandExecutor::default());
let agent = create_agent(
"gpt-4o-mini",
&[command_executor],
Some("You are a helpful assistant that can execute commands."),
None,
)?;
let result = agent
.invoke_messages(vec![Message::new_human_message(
"What files are in the current directory?",
)])
.await?;
println!("{}", result);
Ok(())
}Build a state graph with MessagesState, add a node with function_node, connect START to the node and the node to END, then compile and invoke:
use langchain_ai_rust::langgraph::{function_node, MessagesState, StateGraph, END, START};
use langchain_ai_rust::schemas::messages::Message;
let mock_llm = function_node("mock_llm", |_state: &MessagesState| async move {
use std::collections::HashMap;
let mut update = HashMap::new();
update.insert(
"messages".to_string(),
serde_json::to_value(vec![Message::new_ai_message("hello world")])?,
);
Ok(update)
});
let mut graph = StateGraph::<MessagesState>::new();
graph.add_node("mock_llm", mock_llm)?;
graph.add_edge(START, "mock_llm");
graph.add_edge("mock_llm", END);
let compiled = graph.compile()?;
let initial_state = MessagesState::with_messages(vec![Message::new_human_message("hi!")]);
let final_state = compiled.invoke(initial_state).await?;See LangGraph Hello World and LangGraph Streaming for more.
Use create_deep_agent with planning and filesystem enabled; the agent gets a workspace and built-in tools (write_todos, ls, read_file, write_file, edit_file):
use langchain_ai_rust::{
agent::{create_deep_agent, DeepAgentConfig},
chain::Chain,
prompt_args,
schemas::messages::Message,
};
let workspace = std::env::temp_dir().join("my_agent_workspace");
std::fs::create_dir_all(&workspace)?;
let config = DeepAgentConfig::new()
.with_planning(true)
.with_filesystem(true)
.with_workspace_root(workspace);
let agent = create_deep_agent(
"gpt-4o-mini",
&[],
Some("You are a helpful assistant with planning and file tools."),
config,
)?;
let result = agent
.invoke(prompt_args! {
"messages" => vec![Message::new_human_message("List files in the workspace.")]
})
.await?;See Deep Agent Basic and Deep Agent Customization for more.
- OpenAI
- Azure OpenAI
- Anthropic Claude
- MistralAI
- Google Gemini
- AWS Bedrock
- HuggingFace
- Alibaba Qwen
- DeepSeek
- Ollama
- Unified Model Initialization
- PostgreSQL (pgvector)
- Qdrant
- SQLite VSS
- SQLite Vec
- SurrealDB
- OpenSearch
- In-Memory
- Chroma
- FAISS
- MongoDB Atlas
- Pinecone
- Weaviate
- LLM Chain
- Conversational Chain
- Conversational Retriever Simple
- Conversational Retriever With Vector Store
- Sequential Chain
- Q&A Chain
- SQL Chain
- Streaming Chain
- Simple Agent
- Chat Agent with Tools
- OpenAI Compatible Tools Agent
- Multi-Agent Router
- Multi-Agent Subagents
- Multi-Agent Skills
- Multi-Agent Handoffs
- Hello World
- Streaming
- Persistence Basic, Persistence SQLite, Persistence Replay
- Interrupts, Interrupts Approval, Interrupts Review
- Subgraph Shared State, Subgraph Streaming
- Memory Store, Memory Basic
- Agent Workflow, Parallel Execution, Time Travel, Task Example
- Basic (planning + filesystem)
- Customization
- Skills
- Planning
- Filesystem
- Human-in-the-Loop
- Long-term Memory
- With Task Tool
- RecursiveCharacterTextSplitter - Recommended default, splits recursively by separators
- CharacterTextSplitter - Simple character-based splitting with single separator
- PlainTextSplitter - Basic text splitting
- TokenSplitter - Token-based splitting (Tiktoken)
- MarkdownSplitter - Split Markdown by structure
- HTMLSplitter - Split HTML by tags
- JsonSplitter - Split JSON by objects/arrays
- CodeSplitter - Split code by syntax tree (tree-sitter 0.26+, requires
tree-sitterfeature)
- Agentic RAG - Agent decides when to retrieve
- Hybrid RAG - Combines multiple retrieval strategies
- Two-Step RAG - Two-stage retrieval process
- Wikipedia Retriever - Retrieve Wikipedia articles
- Arxiv Retriever - Retrieve academic papers from arXiv
- Tavily Search API Retriever - Real-time web search
- BM25 Retriever - BM25 algorithm for text retrieval
- TF-IDF Retriever - TF-IDF based retrieval
- SVM Retriever - Support Vector Machine based retrieval
- Cohere Reranker - Rerank using Cohere API
- FlashRank Reranker - Local ONNX model reranking
- Contextual AI Reranker - Contextual AI API reranking
- Merger Retriever - Combine multiple retrievers
- Ensemble Retriever - Voting mechanism from multiple retrievers
- RePhrase Query Retriever - LLM-based query rephrasing
- Multi Query Retriever - Generate multiple query variations
- Embeddings Redundant Filter - Filter redundant documents by similarity
- Serpapi/Google Search
- DuckDuckGo Search
- Wolfram Alpha
- Command Line Executor
- Text-to-Speech
- Speech-to-Text
- SequentialThinking – Chain-of-thought reasoning via an LLM (built-in; pass an
Arc<dyn LLM>to the tool) - BrowserUse – Browser automation (navigate, click, type, scroll, get content) with
browser-usefeature - Advanced Tools
- Logging Middleware
- PII Detection
- Content Filtering
- Custom Middleware
- Runtime-Aware Middleware
- Dynamic Prompt Middleware
- Simple Memory
- Conversational Memory
- Long-Term Memory (Basic)
- Long-Term Memory (Search)
- Long-Term Memory (Tool)
- Configurable Models
- Invocation Config
- Semantic Routing
- Dynamic Semantic Routing
- Vision LLM Chain
- Tool Runtime
- PDF (pdf-extract or lopdf)
- HTML
- HTML to Markdown
- CSV
- TSV (Tab-Separated Values)
- JSON (including JSONL)
- Markdown
- TOML (with
tomlfeature) - YAML (with
yamlfeature) - XML (with
xmlfeature)
- Excel (.xlsx, .xls) (with
excelfeature) - Word, PowerPoint, and more via PandocLoader
- WebBaseLoader - Load content from URLs
- RecursiveURLLoader - Recursively crawl websites
- SitemapLoader - Load all URLs from sitemap.xml (with
xmlfeature)
- AWS S3 (with
aws-s3feature)
- GitHub (with
githubfeature) - Git Commits (with
gitfeature)
- Source Code (with tree-sitter feature)
- Pandoc (various formats: docx, epub, html, ipynb, markdown, etc.)
See the examples directory for complete examples of each feature.
For OpenAI:
export OPENAI_API_KEY="your-api-key"For Anthropic:
export ANTHROPIC_API_KEY="your-api-key"For MistralAI:
export MISTRAL_API_KEY="your-api-key"For Google Gemini:
export GOOGLE_API_KEY="your-api-key"For AWS Bedrock:
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="us-east-1"Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- LangChain - The original Python implementation
- All contributors and users of this library