{"id":13484728,"url":"https://github.com/Abraxas-365/langchain-rust","last_synced_at":"2025-03-27T16:31:17.090Z","repository":{"id":220865443,"uuid":"750896555","full_name":"Abraxas-365/langchain-rust","owner":"Abraxas-365","description":"🦜️🔗LangChain for Rust, the easiest way to write LLM-based programs in Rust","archived":false,"fork":false,"pushed_at":"2024-05-01T18:22:13.000Z","size":672,"stargazers_count":198,"open_issues_count":17,"forks_count":24,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-05-02T06:17:02.511Z","etag":null,"topics":["langchain","llm","llms","openai","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Abraxas-365.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-31T14:40:00.000Z","updated_at":"2024-05-27T22:00:26.223Z","dependencies_parsed_at":"2024-05-04T19:23:45.677Z","dependency_job_id":"70e35bd7-6ad3-4d0d-ba47-016372017061","html_url":"https://github.com/Abraxas-365/langchain-rust","commit_stats":null,"previous_names":["abraxas-365/langchain-rust"],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Abraxas-365%2Flangchain-rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Abraxas-365%2Flangchain-rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Abraxas-365%2Flangchain-rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Abraxas-365%2Flangchain-rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Abraxas-365","download_url":"https://codeload.github.com/Abraxas-365/langchain-rust/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245882354,"owners_count":20687870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["langchain","llm","llms","openai","rust"],"created_at":"2024-07-31T17:01:31.758Z","updated_at":"2025-03-27T16:31:17.079Z","avatar_url":"https://github.com/Abraxas-365.png","language":"Rust","funding_links":[],"categories":["Rust","Summary","Machine Learning","Ports to other languages","RAG Tools"],"sub_categories":[],"readme":"# 🦜️🔗LangChain Rust\n\n[![Latest Version]][crates.io]\n\n[Latest Version]: https://img.shields.io/crates/v/langchain-rust.svg\n[crates.io]: https://crates.io/crates/langchain-rust\n\n⚡ Building applications with LLMs through composability, with Rust! ⚡\n\n[![Discord](https://dcbadge.vercel.app/api/server/JJFcTFbanu?style=for-the-badge)](https://discord.gg/JJFcTFbanu)\n[![Docs: Tutorial](https://img.shields.io/badge/docs-tutorial-success?style=for-the-badge\u0026logo=appveyor)](https://langchain-rust.sellie.tech/get-started/quickstart)\n\n## 🤔 What is this?\n\nThis is the Rust language implementation of [LangChain](https://github.com/langchain-ai/langchain).\n\n## Current Features\n\n- LLMs\n\n  - [x] [OpenAi](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/llm_openai.rs)\n  - [x] [Azure OpenAi](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/llm_azure_open_ai.rs)\n  - [x] [Ollama](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/llm_ollama.rs)\n  - [x] [Anthropic Claude](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/llm_anthropic_claude.rs)\n\n- Embeddings\n\n  - [x] [OpenAi](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/embedding_openai.rs)\n  - [x] [Azure OpenAi](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/embedding_azure_open_ai.rs)\n  - [x] [Ollama](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/embedding_ollama.rs)\n  - [x] [Local FastEmbed](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/embedding_fastembed.rs)\n  - [x] [MistralAI](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/embedding_mistralai.rs)\n\n- VectorStores\n\n  - [x] [OpenSearch](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/vector_store_opensearch.rs)\n  - [x] [Postgres](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/vector_store_postgres.rs)\n  - [x] [Qdrant](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/vector_store_qdrant.rs)\n  - [x] [Sqlite](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/vector_store_sqlite_vss.rs)\n  - [x] [SurrealDB](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/vector_store_surrealdb/src/main.rs)\n\n- Chain\n\n  - [x] [LLM Chain](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/llm_chain.rs)\n  - [x] [Conversational Chain](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/conversational_chain.rs)\n  - [x] [Conversational Retriever Simple](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/conversational_retriever_simple_chain.rs)\n  - [x] [Conversational Retriever With Vector Store](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/conversational_retriever_chain_with_vector_store.rs)\n  - [x] [Sequential Chain](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/sequential_chain.rs)\n  - [x] [Q\u0026A Chain](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/qa_chain.rs)\n  - [x] [SQL Chain](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/sql_chain.rs)\n\n- Agents\n\n  - [x] [Chat Agent with Tools](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/agent.rs)\n  - [x] [Open AI Compatible Tools Agent](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/open_ai_tools_agent.rs)\n\n- Tools\n\n  - [x] Serpapi/Google\n  - [x] DuckDuckGo Search\n  - [x] [Wolfram/Math](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/wolfram_tool.rs)\n  - [x] Command line\n  - [x] [Text2Speech](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/speech2text_openai.rs)\n\n- Semantic Routing\n\n  - [x] [Static Routing](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/semantic_routes.rs)\n  - [x] [Dynamic Routing](https://github.com/Abraxas-365/langchain-rust/blob/main/examples/dynamic_semantic_routes.rs)\n\n- Document Loaders\n\n  - [x] PDF\n\n    ```rust\n    use futures_util::StreamExt;\n\n    async fn main() {\n        let path = \"./src/document_loaders/test_data/sample.pdf\";\n\n        let loader = PdfExtractLoader::from_path(path).expect(\"Failed to create PdfExtractLoader\");\n        // let loader = LoPdfLoader::from_path(path).expect(\"Failed to create LoPdfLoader\");\n\n        let docs = loader\n            .load()\n            .await\n            .unwrap()\n            .map(|d| d.unwrap())\n            .collect::\u003cVec\u003c_\u003e\u003e()\n            .await;\n\n    }\n    ```\n\n  - [x] Pandoc\n\n    ```rust\n    use futures_util::StreamExt;\n\n    async fn main() {\n\n        let path = \"./src/document_loaders/test_data/sample.docx\";\n\n        let loader = PandocLoader::from_path(InputFormat::Docx.to_string(), path)\n            .await\n            .expect(\"Failed to create PandocLoader\");\n\n        let docs = loader\n            .load()\n            .await\n            .unwrap()\n            .map(|d| d.unwrap())\n            .collect::\u003cVec\u003c_\u003e\u003e()\n            .await;\n    }\n    ```\n\n  - [x] HTML\n\n    ```rust\n    use futures_util::StreamExt;\n    use url::Url;\n\n    async fn main() {\n        let path = \"./src/document_loaders/test_data/example.html\";\n        let html_loader = HtmlLoader::from_path(path, Url::parse(\"https://example.com/\").unwrap())\n            .expect(\"Failed to create html loader\");\n\n        let documents = html_loader\n            .load()\n            .await\n            .unwrap()\n            .map(|x| x.unwrap())\n            .collect::\u003cVec\u003c_\u003e\u003e()\n            .await;\n    }\n    ```\n\n  - [x] HTML To Markdown\n\n    ```rust\n    use futures_util::StreamExt;\n    use url::Url;\n\n    async fn main() {\n        let path = \"./src/document_loaders/test_data/example.html\";\n        let html_to_markdown_loader = HtmlToMarkdownLoader::from_path(path, Url::parse(\"https://example.com/\").unwrap(), HtmlToMarkdownOptions::default().with_skip_tags(vec![\"figure\".to_string()]))\n            .expect(\"Failed to create html to markdown loader\");\n\n        let documents = html_to_markdown_loader\n            .load()\n            .await\n            .unwrap()\n            .map(|x| x.unwrap())\n            .collect::\u003cVec\u003c_\u003e\u003e()\n            .await;\n    }\n    ```\n\n  - [x] CSV\n\n    ```rust\n    use futures_util::StreamExt;\n\n    async fn main() {\n        let path = \"./src/document_loaders/test_data/test.csv\";\n        let columns = vec![\n            \"name\".to_string(),\n            \"age\".to_string(),\n            \"city\".to_string(),\n            \"country\".to_string(),\n        ];\n        let csv_loader = CsvLoader::from_path(path, columns).expect(\"Failed to create csv loader\");\n\n        let documents = csv_loader\n            .load()\n            .await\n            .unwrap()\n            .map(|x| x.unwrap())\n            .collect::\u003cVec\u003c_\u003e\u003e()\n            .await;\n    }\n    ```\n\n  - [x] Git commits\n\n    ```rust\n    use futures_util::StreamExt;\n\n    async fn main() {\n        let path = \"/path/to/git/repo\";\n        let git_commit_loader = GitCommitLoader::from_path(path).expect(\"Failed to create git commit loader\");\n\n        let documents = csv_loader\n            .load()\n            .await\n            .unwrap()\n            .map(|x| x.unwrap())\n            .collect::\u003cVec\u003c_\u003e\u003e()\n            .await;\n    }\n    ```\n\n  - [x] Source code\n\n    ```rust\n\n    let loader_with_dir =\n    SourceCodeLoader::from_path(\"./src/document_loaders/test_data\".to_string())\n    .with_dir_loader_options(DirLoaderOptions {\n    glob: None,\n    suffixes: Some(vec![\"rs\".to_string()]),\n    exclude: None,\n    });\n\n    let stream = loader_with_dir.load().await.unwrap();\n    let documents = stream.map(|x| x.unwrap()).collect::\u003cVec\u003c_\u003e\u003e().await;\n    ```\n\n## Installation\n\nThis library heavily relies on `serde_json` for its operation.\n\n### Step 1: Add `serde_json`\n\nFirst, ensure `serde_json` is added to your Rust project.\n\n```bash\ncargo add serde_json\n```\n\n### Step 2: Add `langchain-rust`\n\nThen, you can add `langchain-rust` to your Rust project.\n\n#### Simple install\n\n```bash\ncargo add langchain-rust\n```\n\n#### With Sqlite\n\n##### sqlite-vss\n\nDownload additional sqlite_vss libraries from \u003chttps://github.com/asg017/sqlite-vss\u003e\n\n```bash\ncargo add langchain-rust --features sqlite-vss\n```\n\n##### sqlite-vec\n\nDownload additional sqlite_vec libraries from \u003chttps://github.com/asg017/sqlite-vec\u003e\n\n```bash\ncargo add langchain-rust --features sqlite-vec\n```\n\n\n#### With Postgres\n\n```bash\ncargo add langchain-rust --features postgres\n```\n\n#### With SurrialDB\n\n```bash\ncargo add langchain-rust --features surrealdb\n```\n\n#### With Qdrant\n\n```bash\ncargo add langchain-rust --features qdrant\n```\n\nPlease remember to replace the feature flags `sqlite`, `postgres` or `surrealdb` based on your\nspecific use case.\n\nThis will add both `serde_json` and `langchain-rust` as dependencies in your `Cargo.toml`\nfile. Now, when you build your project, both dependencies will be fetched and compiled, and will be available for use in your project.\n\nRemember, `serde_json` is a necessary dependencies, and `sqlite`, `postgres` and `surrealdb`\nare optional features that may be added according to project needs.\n\n### Quick Start Conversational Chain\n\n```rust\nuse langchain_rust::{\n    chain::{Chain, LLMChainBuilder},\n    fmt_message, fmt_placeholder, fmt_template,\n    language_models::llm::LLM,\n    llm::openai::{OpenAI, OpenAIModel},\n    message_formatter,\n    prompt::HumanMessagePromptTemplate,\n    prompt_args,\n    schemas::messages::Message,\n    template_fstring,\n};\n\n#[tokio::main]\nasync fn main() {\n    //We can then initialize the model:\n    // If you'd prefer not to set an environment variable you can pass the key in directly via the `openai_api_key` named parameter when initiating the OpenAI LLM class:\n    // let open_ai = OpenAI::default()\n    //     .with_config(\n    //         OpenAIConfig::default()\n    //             .with_api_key(\"\u003cyour_key\u003e\"),\n    //     ).with_model(OpenAIModel::Gpt4oMini.to_string());\n    let open_ai = OpenAI::default().with_model(OpenAIModel::Gpt4oMini.to_string());\n\n\n    //Once you've installed and initialized the LLM of your choice, we can try using it! Let's ask it what LangSmith is - this is something that wasn't present in the training data so it shouldn't have a very good response.\n    let resp = open_ai.invoke(\"What is rust\").await.unwrap();\n    println!(\"{}\", resp);\n\n    // We can also guide it's response with a prompt template. Prompt templates are used to convert raw user input to a better input to the LLM.\n    let prompt = message_formatter![\n        fmt_message!(Message::new_system_message(\n            \"You are world class technical documentation writer.\"\n        )),\n        fmt_template!(HumanMessagePromptTemplate::new(template_fstring!(\n            \"{input}\", \"input\"\n        )))\n    ];\n\n    //We can now combine these into a simple LLM chain:\n\n    let chain = LLMChainBuilder::new()\n        .prompt(prompt)\n        .llm(open_ai.clone())\n        .build()\n        .unwrap();\n\n    //We can now invoke it and ask the same question. It still won't know the answer, but it should respond in a more proper tone for a technical writer!\n\n    match chain\n        .invoke(prompt_args! {\n        \"input\" =\u003e \"Quien es el escritor de 20000 millas de viaje submarino\",\n           })\n        .await\n    {\n        Ok(result) =\u003e {\n            println!(\"Result: {:?}\", result);\n        }\n        Err(e) =\u003e panic!(\"Error invoking LLMChain: {:?}\", e),\n    }\n\n    //If you want to prompt to have a list of messages you could use the `fmt_placeholder` macro\n\n    let prompt = message_formatter![\n        fmt_message!(Message::new_system_message(\n            \"You are world class technical documentation writer.\"\n        )),\n        fmt_placeholder!(\"history\"),\n        fmt_template!(HumanMessagePromptTemplate::new(template_fstring!(\n            \"{input}\", \"input\"\n        ))),\n    ];\n\n    let chain = LLMChainBuilder::new()\n        .prompt(prompt)\n        .llm(open_ai)\n        .build()\n        .unwrap();\n    match chain\n        .invoke(prompt_args! {\n        \"input\" =\u003e \"Who is the writer of 20,000 Leagues Under the Sea, and what is my name?\",\n        \"history\" =\u003e vec![\n                Message::new_human_message(\"My name is: luis\"),\n                Message::new_ai_message(\"Hi luis\"),\n                ],\n\n        })\n        .await\n    {\n        Ok(result) =\u003e {\n            println!(\"Result: {:?}\", result);\n        }\n        Err(e) =\u003e panic!(\"Error invoking LLMChain: {:?}\", e),\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAbraxas-365%2Flangchain-rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAbraxas-365%2Flangchain-rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAbraxas-365%2Flangchain-rust/lists"}