Skip to content

Latest commit

 

History

History
76 lines (57 loc) · 2.68 KB

File metadata and controls

76 lines (57 loc) · 2.68 KB

LMM WebAssembly Guide 🌐

LMM natively targets wasm32-unknown-unknown. Because the HTTP client (reqwest) automatically switches to the browser fetch API on WASM targets, you can deploy LMM inside Rust frontend frameworks without any additional glue code.

Supported Frameworks

LMM WASM integration is actively used and tested with:

  • Yew: the most popular Rust / WASM frontend framework
  • Dioxus: cross-platform Rust UI framework
  • Leptos: fine-grained reactive framework

📦 Adding to a WASM Project

Add lmm to your Cargo.toml with the appropriate feature set. Avoid enabling rust-binary, python, or node on WASM targets.

[dependencies]
lmm = { version = "0.2.8", default-features = false, features = ["wasm-net"] }

Build with Trunk (Yew / Leptos):

trunk serve

🌍 CORS Considerations

DuckDuckGo's endpoints set permissive CORS headers on most responses, but the behaviour can vary by endpoint and region. If you encounter CORS errors:

  • Use a server-side proxy to forward requests from your own origin.
  • Use the wt-wt (worldwide) region which tends to have the broadest CORS coverage.
  • Consider caching results server-side and serving them from your own API.

🔬 Feature Flags for WASM

Feature WASM-safe Description
(default) Core symbolic engine (no networking)
wasm-net Enables reqwest fetch-based HTTP on WASM
net Native Tokio networking: incompatible with WASM
rust-binary CLI binary: incompatible with WASM
python pyo3 bindings: incompatible with WASM
node napi bindings: incompatible with WASM

📖 Example: Yew Component

use lmm::prelude::*;
use yew::prelude::*;

#[function_component(PredictorDemo)]
fn predictor_demo() -> Html {
    let output = use_state(|| String::from("Click to generate..."));
    let onclick = {
        let output = output.clone();
        Callback::from(move |_| {
            let predictor = TextPredictor::new(20, 30, 3);
            if let Ok(result) = predictor.predict_continuation("Wise AI built the first LMM", 80) {
                output.set(result.continuation);
            }
        })
    };
    html! {
        <div>
            <button {onclick}>{ "Generate" }</button>
            <p>{ (*output).clone() }</p>
        </div>
    }
}

📄 License

Licensed under the MIT License.