### Quick Start: OpenAI Chat Integration Source: https://docs.rs/langchainrust This example demonstrates how to initialize and use the OpenAIChat model for a basic chat interaction. Ensure the OPENAI_API_KEY environment variable is set. ```rust use langchainrust::{OpenAIChat, OpenAIConfig, BaseChatModel}; use langchainrust::schema::Message; #[tokio::main] async fn main() -> Result<(), Box> { let config = OpenAIConfig { api_key: std::env::var("OPENAI_API_KEY")?, base_url: "https://api.openai.com/v1".to_string(), model: "gpt-3.5-turbo".to_string(), ..Default::default() }; let llm = OpenAIChat::new(config); let response = llm.chat(vec![ Message::system("You are a helpful assistant."), Message::human("What is Rust?"), ], None).await?; println!("{}", response.content); Ok(()) } ``` -------------------------------- ### BM25 Keyword Search Example Source: https://docs.rs/langchainrust Demonstrates how to use the BM25Retriever for keyword search. Documents are added to the retriever, and then a search is performed to find relevant documents based on a query. ```rust use langchainrust::{BM25Retriever, Document}; let mut retriever = BM25Retriever::new(); retriever.add_documents_sync(vec![ Document::new("Rust is a systems programming language"), Document::new("Python is a scripting language"), ]); let results = retriever.search("systems programming", 3); for result in results { println!("Document: {}", result.document.content); println!("Score: {}", result.score); } ``` -------------------------------- ### Initialize Chat Models from Environment Source: https://docs.rs/langchainrust This snippet shows how to initialize various chat models, including DeepSeek, Moonshot, Anthropic, and Ollama, using environment variables or model names. ```rust use langchainrust::{ DeepSeekChat, MoonshotChat, AnthropicChat, OllamaChat, }; let deepseek = DeepSeekChat::from_env(); let moonshot = MoonshotChat::with_model("moonshot-v1-128k"); let claude = AnthropicChat::from_env(); let ollama = OllamaChat::new("llama3.2"); ``` -------------------------------- ### Run Tests Source: https://docs.rs/langchainrust Execute the project's tests using the cargo test command. ```bash cargo test ``` -------------------------------- ### Langchain Rust Architecture Overview Source: https://docs.rs/langchainrust This diagram illustrates the layered architecture of the langchainrust library, showing its components for LLM, Embeddings, Agent, Retrieval, and Utility layers. ```text ┌─────────────────────────────────────────────────────────────┐ │ langchainrust │ ├─────────────────────────────────────────────────────────────┤ │ LLM Layer │ │ ├── OpenAIChat / OllamaChat │ │ ├── DeepSeek / Moonshot / Zhipu / Qwen (OpenAI compatible) │ │ ├── AnthropicChat (Claude API) │ │ ├── GeminiChat │ │ ├── Function Calling (bind_tools) │ │ └── Streaming (stream_chat) │ ├─────────────────────────────────────────────────────────────┤ │ Embeddings Layer │ │ ├── OpenAIEmbeddings / DeepSeekEmbeddings │ │ └── QwenEmbeddings / MockEmbeddings │ ├─────────────────────────────────────────────────────────────┤ │ Agent Layer │ │ ├── ReActAgent / FunctionCallingAgent │ │ ├── AgentExecutor │ │ └── LangGraph (StateGraph, Subgraph, Parallel) │ ├─────────────────────────────────────────────────────────────┤ │ Retrieval Layer │ │ ├── RAG (TextSplitter, VectorStore) │ │ ├── BM25 (Keyword Search, AutoMerging) │ │ ├── Hybrid (BM25 + Vector, RRF Fusion) │ │ └── HyDE / MultiQuery / Reranking │ │ └── Storage (InMemory, Qdrant, MongoDB) │ ├─────────────────────────────────────────────────────────────┤ │ Utility Layer │ │ ├── Memory (Buffer, Window, Summary) │ │ ├── Chains (LLMChain, SequentialChain) │ │ ├── Prompts (PromptTemplate, ChatPromptTemplate) │ │ ├── Tools (Calculator, DateTime, URLFetch) │ │ ├── Output Parsers │ │ ├── LLM Cache │ │ └── Callbacks (LangSmith, StdOut, multipart batch) │ └─────────────────────────────────────────────────────────────┘ ``` -------------------------------- ### Add Langchain Rust to Cargo.toml Source: https://docs.rs/langchainrust Add the langchainrust crate to your project's dependencies in Cargo.toml. Optional features can be enabled for specific integrations like MongoDB, Qdrant, Redis, or SQLite storage. ```toml [dependencies] langchainrust = "0.2.18" tokio = { version = "1.0", features = ["full"] } # Optional features langchainrust = { version = "0.2.18", features = ["mongodb-persistence"] } # MongoDB storage langchainrust = { version = "0.2.18", features = ["qdrant-integration"] } # Qdrant vector DB langchainrust = { version = "0.2.18", features = ["redis-storage"] } # Redis storage langchainrust = { version = "0.2.18", features = ["sqlite-storage"] } # SQLite storage ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.