️LangChain for Rust, the easiest way to write LLM-based programs in Rust
️LangChain for Rust, the easiest way to write LLM-based programs in Rust
⚡ Building applications with LLMs through composability, with Rust! ⚡
This is the Rust language implementation of LangChain.
LLMs
Embeddings
VectorStores
Chain
Agents
Tools
Semantic Routing
Document Loaders
use futures_util::StreamExt;
async fn main() {
let path = "./src/document_loaders/test_data/sample.pdf";
let loader = PdfExtractLoader::from_path(path).expect("Failed to create PdfExtractLoader");
// let loader = LoPdfLoader::from_path(path).expect("Failed to create LoPdfLoader");
let docs = loader
.load()
.await
.unwrap()
.map(|d| d.unwrap())
.collect::>()
.await;
}Pandoc
use futures_util::StreamExt;
async fn main() {
let path = "./src/document_loaders/test_data/sample.docx";
let loader = PandocLoader::from_path(InputFormat::Docx.to_string(), path)
.await
.expect("Failed to create PandocLoader");
let docs = loader
.load()
.await
.unwrap()
.map(|d| d.unwrap())
.collect::>()
.await;
}HTML
use futures_util::StreamExt;
use url::Url;
async fn main() {
let path = "./src/document_loaders/test_data/example.html";
let html_loader = HtmlLoader::from_path(path, Url::parse("https://example.com/").unwrap())
.expect("Failed to create html loader");
let documents = html_loader
.load()
.await
.unwrap()
.map(|x| x.unwrap())
.collect::>()
.await;
}HTML To Markdown
…CSV
use futures_util::StreamExt;
async fn main() {
let path = "./src/document_loaders/test_data/test.csv";
let columns = vec![
"name".to_string(),
"age".to_string(),
"city".to_string(),
"country".to_string(),
];
let csv_loader = CsvLoader::from_path(path, columns).expect("Failed to create csv loader");
let documents = csv_loader
.load()
.await
.unwrap()
.map(|x| x.unwrap())
.collect::>()
.await;
}Git commits
use futures_util::StreamExt;
async fn main() {
let path = "/path/to/git/repo";
let git_commit_loader = GitCommitLoader::from_path(path).expect("Failed to create git commit loader");
let documents = csv_loader
.load()
.await
.unwrap()
.map(|x| x.unwrap())
.collect::>()
.await;
}Source code
let loader_with_dir =
SourceCodeLoader::from_path("./src/document_loaders/test_data".to_string())
.with_dir_loader_options(DirLoaderOptions {
glob: None,
suffixes: Some(vec!["rs".to_string()]),
exclude: None,
});
let stream = loader_with_dir.load().await.unwrap();
let documents = stream.map(|x| x.unwrap()).collect::>().await;This library heavily relies on serde_json for its operation.
serde_jsonFirst, ensure serde_json is added to your Rust project.
cargo add serde_jsonlangchain-rustThen, you can add langchain-rust to your Rust project.
cargo add langchain-rustDownload additional sqlite_vss libraries from
cargo add langchain-rust --features sqlite-vssDownload additional sqlite_vec libraries from
cargo add langchain-rust --features sqlite-veccargo add langchain-rust --features postgrescargo add langchain-rust --features surrealdbcargo add langchain-rust --features qdrantPlease remember to replace the feature flags sqlite, postgres or surrealdb based on your
specific use case.
This will add both serde_json and langchain-rust as dependencies in your Cargo.toml
file. Now, when you build your project, both dependencies will be fetched and compiled, and will be available for use in your project.
Remember, serde_json is a necessary dependencies, and sqlite, postgres and surrealdb
are optional features that may be added according to project needs.
…No open issues yet, or sync has not completed.