### Quick Start Conversational Chain with OpenAI Source: https://crates.io/crates/langchain-rust/index Demonstrates the creation of a conversational chain using the OpenAI LLM. It shows how to initialize the OpenAI model, invoke it directly, and then build a more complex chain with formatted prompts and message history. This example requires the 'openai' feature for Langchain-Rust and assumes the OpenAI API key is set as an environment variable or passed directly. ```rust use langchain_rust::{ chain::{Chain, LLMChainBuilder}, fmt_message, fmt_placeholder, fmt_template, language_models::llm::LLM, llm::openai::{OpenAI, OpenAIModel}, message_formatter, prompt::HumanMessagePromptTemplate, prompt_args, schemas::messages::Message, template_fstring, }; #[tokio::main] async fn main() { //We can then initialize the model: // 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: // let open_ai = OpenAI::default() // .with_config( // OpenAIConfig::default() // .with_api_key(""), // ).with_model(OpenAIModel::Gpt4oMini.to_string()); let open_ai = OpenAI::default().with_model(OpenAIModel::Gpt4oMini.to_string()); //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. let resp = open_ai.invoke("What is rust").await.unwrap(); println!("{}", resp); // 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. let prompt = message_formatter![ fmt_message!(Message::new_system_message( "You are world class technical documentation writer." )), fmt_template!(HumanMessagePromptTemplate::new(template_fstring!( "{input}", "input" ))) ]; //We can now combine these into a simple LLM chain: let chain = LLMChainBuilder::new() .prompt(prompt) .llm(open_ai.clone()) .build() .unwrap(); //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! match chain .invoke(prompt_args! { "input" => "Quien es el escritor de 20000 millas de viaje submarino", }) .await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } //If you want to prompt to have a list of messages you could use the `fmt_placeholder` macro let prompt = message_formatter![ fmt_message!(Message::new_system_message( "You are world class technical documentation writer." )), 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" => "Who is the writer of 20,000 Leagues Under the Sea, and what is my name?", "history" => vec![ Message::new_human_message("My name is: luis"), Message::new_ai_message("Hi luis"), ], }) .await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } } ``` -------------------------------- ### Interact with Vector Stores using Macros in Rust Source: https://crates.io/crates/langchain-rust/1 Illustrates the use of macros for interacting with vector stores in Rust, simplifying operations like adding documents and performing similarity searches. This example uses the same setup as the SQL-based interaction but leverages macro syntax for more concise code. It includes adding documents and searching for similar ones. ```rust async fn db_test() { let embedder = OpenAiEmbedder::default(); let store = StoreBuilder::new() .embedder(embedder) .connection_url("postgresql://postgres:postgres@localhost:5432/postgres") .vector_dimensions(1536) .build() .await .unwrap(); let document = Document { page_content: "this is a test".to_string(), metadata: HashMap::new(), score: 0.0, }; let docs = vec![document]; //If you want to send options: //add_documents!(store, &docs,&VecStoreOptions::default()).await.unwrap(); add_documents!(store, &docs).await.unwrap(); //If you want to send options: //similarity_search!(store, "this is a test", 10,&VecStoreOptions::default()).await.unwrap(); let similar = similarity_search!(store, "this is a test", 10) .await .unwrap(); println!("Similar: {:?}", similar); } ``` -------------------------------- ### Langchain-Rust: Conversational Chain Example Source: https://crates.io/crates/langchain-rust/0 Demonstrates the usage of the ConversationalChain in langchain-rust, which maintains conversation history. It shows how to build the chain with an LLM and invoke it with different inputs to get responses. ```rust let llm = OpenAI::default().with_model(OpenAIModel::Gpt35); let chain = ConversationalChainBuilder::new() .llm(llm) .build() .expect("Error building ConversationalChain"); let input_variables = prompt_args! { "input" => "Soy de peru", }; match chain.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } let input_variables = prompt_args! { "input" => "Cuales son platos tipicos de mi pais", }; match chain.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } ``` -------------------------------- ### SQL Chain Example with PostgreSQL Source: https://crates.io/crates/langchain-rust/0 Demonstrates building and invoking a SQLDatabaseChain using PostgreSQL. It requires a running PostgreSQL instance and the 'langchain-rust' crate. The chain takes a natural language query and returns information from the database. ```rust async fn test_sql_chain() { let options = ChainCallOptions::default(); let mut llm = OpenAI::default().with_model(OpenAIModel::Gpt35); let engine = PostgreSQLEngine::new("postgresql://postgres:postgres@localhost:5432/postgres") .await .unwrap(); let db = SQLDatabaseBuilder::new(engine) .custom_sample_rows_number(0) .build() .await .unwrap(); let chain = SQLDatabaseChainBuilder::new() .llm(llm) .top_k(4) .database(db) .options(options) .build() .expect("Failed to build LLMChain"); let input_variables = prompt_args! { "query" => "what info have the client 1", }; match chain.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } } ``` -------------------------------- ### LLM Default Implementation with OpenAI Source: https://crates.io/crates/langchain-rust/0 Shows a basic interaction with the OpenAI LLM using its default settings. It requires the 'openai' crate and an API key configured. This example generates a response to a simple human message. ```rust let options = CallOptions::default(); let open_ai = OpenAI::default(); let messages = vec![Message::new_human_message("Hello, how are you?")]; let resp=open_ai.generate(&messages).await.unwrap(); println!("Generate Result: {:?}", resp); ``` -------------------------------- ### Quick Start Conversational Chain with OpenAI Source: https://crates.io/crates/langchain-rust/4 Demonstrates building and invoking a conversational chain using LangChain Rust and the OpenAI LLM. It shows how to initialize the OpenAI model, use basic prompts, and construct chains with prompt templates, including handling message history. ```rust use langchain_rust::{ chain::{Chain, LLMChainBuilder}, fmt_message, fmt_placeholder, fmt_template, language_models::llm::LLM, llm::openai::{OpenAI, OpenAIModel}, message_formatter, prompt::HumanMessagePromptTemplate, prompt_args, schemas::messages::Message, template_fstring, }; #[tokio::main] async fn main() { //We can then initialize the model: // 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: // let open_ai = OpenAI::default() // .with_config( // OpenAIConfig::default() // .with_api_key(""), // ).with_model(OpenAIModel::Gpt4oMini.to_string()); let open_ai = OpenAI::default().with_model(OpenAIModel::Gpt4oMini.to_string()); //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. let resp = open_ai.invoke("What is rust").await.unwrap(); println!("{}", resp); // 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. let prompt = message_formatter![ fmt_message!(Message::new_system_message( "You are world class technical documentation writer." )), fmt_template!(HumanMessagePromptTemplate::new(template_fstring!( "{input}", "input" ))) ]; //We can now combine these into a simple LLM chain: let chain = LLMChainBuilder::new() .prompt(prompt) .llm(open_ai.clone()) .build() .unwrap(); //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! match chain .invoke(prompt_args! { "input" => "Quien es el escritor de 20000 millas de viaje submarino", }) .await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } //If you want to prompt to have a list of messages you could use the `fmt_placeholder` macro let prompt = message_formatter![ fmt_message!(Message::new_system_message( "You are world class technical documentation writer." )), 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" => "Who is the writer of 20,000 Leagues Under the Sea, and what is my name?", "history" => vec![ Message::new_human_message("My name is: luis"), Message::new_ai_message("Hi luis"), ], }) .await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } } ``` -------------------------------- ### Rust Quick Start Conversational Chain with OpenAI Source: https://crates.io/crates/langchain-rust/3 Demonstrates initializing the OpenAI LLM, creating prompt templates using message formatting macros, building an LLM chain, and invoking it with user input and message history. Requires the 'langchain-rust' and 'tokio' crates. ```rust use langchain_rust::{ chain::{Chain, LLMChainBuilder}, fmt_message, fmt_placeholder, fmt_template, language_models::llm::LLM, llm::openai::{OpenAI, OpenAIModel}, message_formatter, prompt::HumanMessagePromptTemplate, prompt_args, schemas::messages::Message, template_fstring, }; #[tokio::main] async fn main() { //We can then initialize the model: // 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: //let open_ai = OpenAI::default().with_api_key("..."); let open_ai = OpenAI::default().with_model(OpenAIModel::Gpt35.to_string()); //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. let resp = open_ai.invoke("What is rust").await.unwrap(); println!("{}", resp); // 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. let prompt = message_formatter![ fmt_message!(Message::new_system_message( "You are world class technical documentation writer." )), fmt_template!(HumanMessagePromptTemplate::new(template_fstring!( "{input}", "input" ))) ]; //We can now combine these into a simple LLM chain: let chain = LLMChainBuilder::new() .prompt(prompt) .llm(open_ai.clone()) .build() .unwrap(); //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! match chain .invoke(prompt_args! { "input" => "Quien es el escritor de 20000 millas de viaje submarino", }) .await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } //If you want to prompt to have a list of messages you could use the `fmt_placeholder` macro let prompt = message_formatter![ fmt_message!(Message::new_system_message( "You are world class technical documentation writer." )), 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" => "Who is the writer of 20,000 Leagues Under the Sea, and what is my name?", "history" => vec![ Message::new_human_message("My name is: luis"), Message::new_ai_message("Hi luis"), ], }) .await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } } ``` -------------------------------- ### Rust Conversational Chain with OpenAI Source: https://crates.io/crates/langchain-rust/2 Initializes an OpenAI LLM, defines prompt templates, and builds a conversational chain. It demonstrates invoking the chain with single input and with a message history. The example uses the `langchain-rust` crate and `tokio` for asynchronous operations. ```rust use langchain_rust::{ chain::{Chain, LLMChainBuilder}, fmt_message, fmt_placeholder, fmt_template, language_models::llm::LLM, llm::openai::{OpenAI, OpenAIModel}, message_formatter, prompt::HumanMessagePromptTemplate, prompt_args, schemas::messages::Message, template_fstring, }; #[tokio::main] async fn main() { //We can then initialize the model: // 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: //let open_ai = OpenAI::default().with_api_key("..."); let open_ai = OpenAI::default().with_model(OpenAIModel::Gpt35.to_string()); //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. let resp = open_ai.invoke("What is rust").await.unwrap(); println!("{}", resp); // 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. let prompt = message_formatter![ fmt_message!(Message::new_system_message( "You are world class technical documentation writer." )), fmt_template!(HumanMessagePromptTemplate::new(template_fstring!( "{input}", "input" ))) ]; //We can now combine these into a simple LLM chain: let chain = LLMChainBuilder::new() .prompt(prompt) .llm(open_ai.clone()) .build() .unwrap(); //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! match chain .invoke(prompt_args! { "input" => "Quien es el escritor de 20000 millas de viaje submarino", }) .await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } //If you want to prompt to have a list of messages you could use the `fmt_placeholder` macro let prompt = message_formatter![ fmt_message!(Message::new_system_message( "You are world class technical documentation writer." )), 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" => "Who is the writer of 20,000 Leagues Under the Sea, and what is my name?", "history" => vec![ Message::new_human_message("My name is: luis"), Message::new_ai_message("Hi luis"), ], }) .await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } } ``` -------------------------------- ### Add LangChain Rust with Qdrant Feature Source: https://crates.io/crates/langchain-rust/4 Installs the langchain-rust crate with the 'qdrant' feature enabled. This adds 'serde_json' and 'langchain-rust' as dependencies to your Cargo.toml. Other optional features like 'sqlite', 'postgres', or 'surrealdb' can be added based on project requirements. ```bash cargo add langchain-rust --features qdrant ``` -------------------------------- ### Install Langchain-Rust (Simple) Source: https://crates.io/crates/langchain-rust/2 Adds the `langchain-rust` crate to your Rust project using Cargo for basic functionality. ```rust cargo add langchain-rust ``` -------------------------------- ### Interact with Vector Stores using SQL and Embeddings in Rust Source: https://crates.io/crates/langchain-rust/1 Shows how to use the `StoreBuilder` to create and interact with a vector store in Rust, connecting to a PostgreSQL database. It demonstrates embedding documents using `OpenAiEmbedder` and performing similarity searches. The code includes setup, document addition, and retrieval of similar documents. ```rust async fn db_test() { let embedder = OpenAiEmbedder::default(); let store = StoreBuilder::new() .embedder(embedder) .connection_url("postgresql://postgres:postgres@localhost:5432/postgres") .vector_dimensions(1536) .build() .await .unwrap(); let document = Document { page_content: "this is a test".to_string(), metadata: HashMap::new(), score: 0.0, }; let docs = vec![document]; store .add_documents(&docs, &VecStoreOptions::default()) .await .unwrap(); println!("Result:"); let similar = store .similarity_search("this is a test", 10, &VecStoreOptions::default()) .await .unwrap(); println!("Similar: {:?}", similar); } ``` -------------------------------- ### Install Langchain-Rust with SurrealDB Feature Source: https://crates.io/crates/langchain-rust/2 Adds the `langchain-rust` crate with the `surrealdb` feature enabled, allowing integration with SurrealDB. ```rust cargo add langchain-rust --features surrealdb ``` -------------------------------- ### Add langchain-rust Dependency with Cargo Source: https://crates.io/crates/langchain-rust/3 Shows how to add the `langchain-rust` crate to a Rust project using `cargo add`. This is the basic installation command. ```shell cargo add langchain-rust ``` -------------------------------- ### Add Langchain-Rust with Qdrant Feature Source: https://crates.io/crates/langchain-rust/index Installs the langchain-rust crate with the 'qdrant' feature enabled. This command modifies your Cargo.toml file to include langchain-rust and serde_json as dependencies. The 'qdrant' feature flag, along with others like 'sqlite', 'postgres', or 'surrealdb', can be conditionally added based on project requirements. ```bash cargo add langchain-rust --features qdrant ``` -------------------------------- ### Install Serde JSON Source: https://crates.io/crates/langchain-rust/2 Adds the `serde_json` crate to your Rust project using Cargo. This is a necessary dependency for the `langchain-rust` library. ```rust cargo add serde_json ``` -------------------------------- ### Create Conversational Agent with Memory and Tools in Rust Source: https://crates.io/crates/langchain-rust/1 Demonstrates the creation of a conversational agent using the `ConversationalAgentBuilder` in Rust. It configures the agent with OpenAI's GPT-4 model, a mock calculator tool, a chat output parser, and memory. The example shows invoking the agent with conversational inputs and handling the results. ```rust let llm = OpenAI::default().with_model(OpenAIModel::Gpt4); let memory = SimpleMemory::new(); let tool_calc = Calc {}; let agent = ConversationalAgentBuilder::new() .tools(vec![Arc::new(tool_calc)]) .output_parser(ChatOutputParser::new().into()) .build(llm) .unwrap(); let input_variables = prompt_args! { "input" => "hola,Me llamo luis, y tengo 10 anos, y estudio Computer scinence", }; let executor = AgentExecutor::from_agent(agent).with_memory(memory.into()); match executor.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } let input_variables = prompt_args! { "input" => "cuanta es la edad de luis +10 y que estudia", }; match executor.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } ``` -------------------------------- ### Install Langchain-Rust with Postgres Feature Source: https://crates.io/crates/langchain-rust/2 Adds the `langchain-rust` crate with the `postgres` feature enabled, allowing integration with PostgreSQL. ```rust cargo add langchain-rust --features postgres ``` -------------------------------- ### Create a Mock Calculator Tool for Langchain Agent in Rust Source: https://crates.io/crates/langchain-rust/1 Defines a mock 'Calculator' tool for use with Langchain agents in Rust. This tool implements the `Tool` trait, providing a name, description, and a placeholder `call` method that returns a predefined string result. This serves as a basic example for creating custom tools. ```rust struct Calc {} #[async_trait] impl Tool for Calc { fn name(&self) -> String { "Calculator".to_string() } fn description(&self) -> String { "Usefull to make calculations".to_string() } async fn call(&self, _input: &str) -> Result> { Ok("25".to_string()) } } ``` -------------------------------- ### Load CSV Data with Rust Source: https://crates.io/crates/langchain-rust/3 Provides a Rust code example for loading CSV files using `CsvLoader`. It specifies the file path and the desired column names. Relies on `futures_util::StreamExt`. ```rust 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; } ``` -------------------------------- ### Rust Dynamic Prompt Templates with Jinja2 Source: https://crates.io/crates/langchain-rust/0 Demonstrates creating and formatting dynamic prompt templates using Jinja2 syntax in LangChain Rust. Templates can include variables that are filled in at runtime. ```rust use langchain::prompt::{PromptTemplate, TemplateFormat, prompt_args, template_fstring}; // Creating a Jinja2 template for a greeting message let greeting_template = template_fstring!( "Hello, {name}! Welcome to our service.", "name" ); // Formatting the template with a given name let formatted_greeting = greeting_template.format(prompt_args! { "name" => "Alice", }).unwrap(); println!("{}", formatted_greeting); ``` -------------------------------- ### LLM with Options and Streaming Source: https://crates.io/crates/langchain-rust/0 Illustrates how to use the LLM with custom options, including setting up a streaming callback function. This allows processing the LLM's response in chunks as it's generated. Requires the 'openai' crate and an API key. ```rust let message_complete = Arc::new(Mutex::new(String::new())); let streaming_func = { let message_complete = message_complete.clone(); move |content: String| { let message_complete = message_complete.clone(); async move { let mut message_complete_lock = message_complete.lock().await; println!("Content: {:?}", content); message_complete_lock.push_str(&content); Ok(()) } } }; let options = CallOptions::new().with_streaming_func(streaming_func); let open_ai = OpenAI::new(options).with_model(OpenAIModel::Gpt35); // You can change the model as needed let messages = vec![Message::new_human_message("Hello, how are you?")]; match open_ai.generate(&messages).await { Ok(result) => { println!("Generate Result: {:?}", result); println!("Message Complete: {:?}", message_complete.lock().await); } Err(e) => { eprintln!("Error calling generate: {:?}", e); } } ``` -------------------------------- ### Initialize OpenAI LLM Source: https://crates.io/crates/langchain-rust/1 Initializes the OpenAI LLM with default settings. This requires the OPENAI_API_KEY environment variable to be set. ```rust let open_ai = OpenAI::default(); ``` -------------------------------- ### Initialize OpenAI LLM with API Key Source: https://crates.io/crates/langchain-rust/1 Initializes the OpenAI LLM by directly passing the API key. This is an alternative to setting the API key as an environment variable. ```rust let open_ai = OpenAI::default().with_api_key("..."); ``` -------------------------------- ### Rust OpenAI Embeddings Initialization Source: https://crates.io/crates/langchain-rust/0 Shows how to initialize the OpenAI embedder in Rust. You can either provide your OpenAI API key directly or use the default implementation which might use environment variables. ```rust use langchain::embedding::openai::OpenAiEmbedder; let openai_embedder = OpenAiEmbedder::new("your_openai_api_key".to_string()); ``` ```rust let openai_embedder = OpenAiEmbedder::default(); ``` -------------------------------- ### Conversational Agent with Calculator Tool Source: https://crates.io/crates/langchain-rust/0 Demonstrates creating a conversational agent that can use tools, specifically a 'Calculator' tool. This involves defining a struct that implements the `Tool` trait and then building an `AgentExecutor` with the agent and tools. It showcases multi-turn conversation handling. ```rust struct Calc {} #[async_trait] impl Tool for Calc { fn name(&self) -> String { "Calculator".to_string() } fn description(&self) -> String { "Usefull to make calculations".to_string() } async fn call(&self, _input: &str) -> Result> { Ok("25".to_string()) } } async fn agent_run() { let llm = OpenAI::default().with_model(OpenAIModel::Gpt4); let memory = SimpleMemory::new(); let tool_calc = Calc {}; let agent = ConversationalAgentBuilder::new() .tools(vec![Arc::new(tool_calc)]) .output_parser(ChatOutputParser::new().into()) .build(llm) .unwrap(); let input_variables = prompt_args! { "input" => "hola,Me llamo luis, y tengo 10 anos, y estudio Computer scinence", }; let executor = AgentExecutor::from_agent(agent).with_memory(memory.into()); match executor.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } let input_variables = prompt_args! { "input" => "cuanta es la edad de luis +10 y que estudia", }; match executor.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } } ``` -------------------------------- ### Set OpenAI API Key Source: https://crates.io/crates/langchain-rust/0 Shows two methods for setting the OpenAI API key: exporting it as an environment variable or using the `with_api_key` method directly in the code. This is essential for authenticating with the OpenAI API. ```shell export OPENAI_API_KEY={{api key}} ``` ```rust let open_ai = OpenAI::new(options) .with_api_key("you api key"); ``` -------------------------------- ### Add langchain-rust with SQLite Feature Source: https://crates.io/crates/langchain-rust/3 Instructions to add `langchain-rust` with the `sqlite` feature enabled using `cargo add`. This also requires downloading additional SQLite libraries. ```shell cargo add langchain-rust --features sqlite ``` -------------------------------- ### Rust Dev-Dependencies for LangChain Source: https://crates.io/crates/langchain-rust/1.0.1/dependencies Lists development dependencies for the langchain-rust crate, specifically testcontainers for integration testing with Docker and tokio-test for testing asynchronous code. ```Rust testcontainers = "^0.11" tokio-test = "^0.4.0" ``` -------------------------------- ### Create LLM Chain Source: https://crates.io/crates/langchain-rust/1 Builds a simple LLM chain using the LLMChainBuilder. It combines a prompt formatter and an LLM to create a chain for invoking LLM calls. ```rust let chain = LLMChainBuilder::new() .prompt(formatter) .llm(llm) .build(); ``` -------------------------------- ### Load Source Code Files with Rust Source: https://crates.io/crates/langchain-rust/4 Shows how to load source code files from a directory using SourceCodeLoader in Rust. It allows specifying file suffixes and exclusion patterns. The loader reads files matching the criteria and returns their content. ```rust 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; ``` -------------------------------- ### Format Messages with Templates in Rust Source: https://crates.io/crates/langchain-rust/0 This snippet shows how to use the `MessageFormatter` macro to combine different message types and prompt templates for creating conversational flows. It defines AI message prompts, uses `message_formatter!` to sequence them with message placeholders, and then formats these messages using `format_messages` with provided input variables. It also includes assertions to verify the output. ```rust let human_msg = Message::new_human_message("Hello from user"); // Create an AI message prompt template let ai_message_prompt = AIMessagePromptTemplate::new(template_fstring!( "AI response: {content} {test}", "content", "test" )); // Use the `message_formatter` macro to construct the formatter let formatter = message_formatter![ MessageOrTemplate::Message(human_msg), MessageOrTemplate::Template(ai_message_prompt.into()), MessageOrTemplate::MessagesPlaceholder("history".to_string()) ]; // Define input variables for the AI message template let input_variables = prompt_args! { "content" => "This is a test", "test" => "test2", "history" => vec![ Message::new_human_message("Placeholder message 1"), Message::new_ai_message("Placeholder message 2"), ], }; // Format messages let formatted_messages = formatter.format_messages(input_variables).unwrap(); // Verify the number of messages assert_eq!(formatted_messages.len(), 4); // Verify the content of each message assert_eq!(formatted_messages[0].content, "Hello from user"); assert_eq!( formatted_messages[1].content, "AI response: This is a test test2" ); assert_eq!(formatted_messages[2].content, "Placeholder message 1"); assert_eq!(formatted_messages[3].content, "Placeholder message 2"); ``` -------------------------------- ### Rust Dependencies for LangChain Source: https://crates.io/crates/langchain-rust/1.0.1/dependencies Lists core Rust dependencies for the langchain-rust crate, including async-openai, async-trait, futures, handlebars, html-escape, log, mockito, regex, reqwest, reqwest-eventsource, rusoto_core, rusoto_s3, rusoto_textract, scraper, serde, serde_json, sqlx, tiktoken-rs, and tokio. ```Rust async-openai = "^0.18.3" async-trait = "^0.1.71" futures = "^0.3" handlebars = "^4.4.0" html-escape = "^0.2.13" log = "^0.4.19" mockito = "^1.3.0" regex = "^1.9.3" reqwest = "^0.11" reqwest-eventsource = "^0.5.0" rusoto_core = "^0.46.0" rusoto_s3 = "^0.46.0" rusoto_textract = "^0.46.0" scraper = "^0.12" serde = "^1.0" serde_json = "^1.0" sqlx = "^0.5" tiktoken-rs = "^0.5.8" tokio = "^1" ``` -------------------------------- ### Add langchain-rust with SurrealDB support in Rust Source: https://crates.io/crates/langchain-rust/index Provides the command to add `langchain-rust` with `surrealdb` feature support using Cargo, facilitating integration with SurrealDB. ```bash cargo add langchain-rust --features surrealdb ``` -------------------------------- ### Add langchain-rust with Sqlite support in Rust Source: https://crates.io/crates/langchain-rust/index Explains how to add `langchain-rust` with `sqlite` feature support using Cargo. It also mentions downloading additional `sqlite_vss` libraries from GitHub for enhanced functionality. ```bash cargo add langchain-rust --features sqlite ``` -------------------------------- ### Add langchain-rust dependency to Rust project Source: https://crates.io/crates/langchain-rust/index Shows the basic command to add the `langchain-rust` crate to a Rust project using Cargo. ```bash cargo add langchain-rust ``` -------------------------------- ### Langchain-Rust: Default LLM Chain Source: https://crates.io/crates/langchain-rust/0 Shows how to create and use the default LLMChain in langchain-rust. This involves defining a human message prompt template and building the chain with the prompt and an LLM. ```rust let human_message_prompt = HumanMessagePromptTemplate::new(template_fstring!( "Mi nombre es: {nombre} ", "nombre", )); let formatter = message_formatter![MessageOrTemplate::Template(human_message_prompt.into()),]; let llm = OpenAI::default(); let chain = LLMChainBuilder::new() .prompt(formatter) .llm(llm) .build() .expect("Failed to build LLMChain"); let input_variables = prompt_args! { "nombre" => "luis", }; match chain.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } ``` -------------------------------- ### Create Conversational Chain with Memory in Rust Source: https://crates.io/crates/langchain-rust/1 Demonstrates how to build a conversational chain with memory using OpenAI's GPT-3.5 model. It initializes the LLM, memory, and the chain, then invokes it with different user inputs to showcase conversational capabilities. Error handling is included for the invocation process. ```rust let llm = OpenAI::default().with_model(OpenAIModel::Gpt35); let memory=SimpleMemory::new(); let chain = ConversationalChainBuilder::new() .llm(llm) .memory(memory.into()) .build() .expect("Error building ConversationalChain"); let input_variables = prompt_args! { "input" => "Soy de peru", }; match chain.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } let input_variables = prompt_args! { "input" => "Cuales son platos tipicos de mi pais", }; match chain.invoke(input_variables).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } } ``` -------------------------------- ### Create LLM Chain with Message History Placeholder Source: https://crates.io/crates/langchain-rust/1 Constructs an LLM chain that includes a placeholder for message history using `fmt_placeholder`. This allows for conversational context to be passed into the chain. ```rust let prompt = message_formatter![ fmt_message!(Message::new_system_message( "You are world class technical documentation writer." )), fmt_template!(HumanMessagePromptTemplate::new(template_fstring!( "{input}", "input" ))), fmt_placeholder!("history"), ]; ``` -------------------------------- ### Rust Embed Multiple Documents Asynchronously Source: https://crates.io/crates/langchain-rust/0 Demonstrates how to embed a list of documents asynchronously using the OpenAI embedder. It takes a vector of strings as input and returns a vector of embeddings. ```rust #[tokio::main] async fn main() { let documents = vec!["Hello, world!".to_string(), "How are you?".to_string()]; let embeddings = openai_embedder.embed_documents(&documents).await.unwrap(); println!("{:?}", embeddings); } ``` -------------------------------- ### Add langchain-rust with Postgres support in Rust Source: https://crates.io/crates/langchain-rust/index Demonstrates adding the `langchain-rust` crate with `postgres` feature support via Cargo. This enables integration with PostgreSQL databases. ```bash cargo add langchain-rust --features postgres ``` -------------------------------- ### Load HTML Documents with Rust Source: https://crates.io/crates/langchain-rust/3 Illustrates loading HTML documents from a local path and a URL using `HtmlLoader` in Rust. Requires `futures_util::StreamExt` and the `url` crate. ```rust 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; } ``` -------------------------------- ### Rust Embed Single Query Asynchronously Source: https://crates.io/crates/langchain-rust/0 Illustrates how to embed a single query string asynchronously using the OpenAI embedder. It takes a string as input and returns a single embedding. ```rust #[tokio::main] async fn main() { let query = "What is the meaning of life?"; let embedding = openai_embedder.embed_query(query).await.unwrap(); println!("{:?}", embedding); } ``` -------------------------------- ### Invoke LLM Source: https://crates.io/crates/langchain-rust/1 Invokes the initialized LLM with a simple text prompt and awaits the response. The result is unwrapped, and any errors during invocation will cause a panic. ```rust let resp=open_ai.invoke("how can langsmith help with testing?").await.unwrap(); ``` -------------------------------- ### Load source code files with SourceCodeLoader in Rust Source: https://crates.io/crates/langchain-rust/index Shows how to load source code files from a directory using `SourceCodeLoader` in Rust. It allows filtering by file suffixes and excluding specific directories, using `futures_util` for async operations. ```rust 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; ``` -------------------------------- ### Rust Message Formatting for Chat Source: https://crates.io/crates/langchain-rust/0 Shows how to create and use different types of messages (System, Human, AI) in LangChain Rust. These messages are used for structured communication within chat models. ```rust use serde::{Serialize, Deserialize}; use langchain::prompt::chat::{Message, MessageType}; // Creating different types of messages let system_msg = Message::new_system_message("System initialization complete."); let human_msg = Message::new_human_message("Hello, how can I assist you?"); let ai_msg = Message::new_ai_message("Analyzing input..."); println!("{:?}, {:?}, {:?}", system_msg, human_msg, ai_msg); ``` -------------------------------- ### Load PDF documents with Rust Source: https://crates.io/crates/langchain-rust/index Demonstrates how to load PDF documents using the PdfExtractLoader in Rust. It requires the `futures_util` crate for asynchronous stream processing. ```rust 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; } ``` -------------------------------- ### Invoke LLM Chain with Prompt Arguments Source: https://crates.io/crates/langchain-rust/1 Invokes an LLM chain with specific input arguments formatted using `prompt_args!`. It handles the response, printing it if successful or panicking on error. ```rust match chain.invoke(prompt_args! { "input" => "Quien es el escritor de 20000 millas de viaje submarino", }).await { Ok(result) => { println!("Result: {:?}", result); } Err(e) => panic!("Error invoking LLMChain: {:?}", e), } ``` -------------------------------- ### Add langchain-rust with SurrealDB Feature Source: https://crates.io/crates/langchain-rust/3 Explains how to add `langchain-rust` with the `surrealdb` feature using `cargo add`. This is for projects integrating with SurrealDB. ```shell cargo add langchain-rust --features surrealdb ```