Skip to main content

leptos_ai/
lib.rs

1//! # Leptos AI
2//!
3//! AI hooks for Leptos applications - chat, completions, and streaming.
4//!
5//! ## Features
6//!
7//! - `use_chat` - Reactive chat with message history and streaming
8//! - `use_completion` - Single completion requests
9//! - Built on `llm-client` for provider support (OpenAI, Anthropic, OpenRouter)
10//!
11//! ## Example
12//!
13//! ```rust,ignore
14//! use leptos::*;
15//! use leptos_ai::{use_chat, ChatOptions};
16//!
17//! #[component]
18//! fn Chat() -> impl IntoView {
19//!     let chat = use_chat(ChatOptions {
20//!         provider: "openai".to_string(),
21//!         api_key: "sk-...".to_string(),
22//!         model: "gpt-4o-mini".to_string(),
23//!         ..Default::default()
24//!     });
25//!
26//!     let input = create_node_ref::<html::Input>();
27//!
28//!     let on_submit = move |ev: ev::SubmitEvent| {
29//!         ev.prevent_default();
30//!         if let Some(input_el) = input.get() {
31//!             let value = input_el.value();
32//!             if !value.is_empty() {
33//!                 chat.send(&value);
34//!                 input_el.set_value("");
35//!             }
36//!         }
37//!     };
38//!
39//!     view! {
40//!         <div class="chat">
41//!             <For
42//!                 each=move || chat.messages.get()
43//!                 key=|msg| msg.id.clone()
44//!                 children=move |msg| {
45//!                     view! {
46//!                         <div class=format!("message {}", msg.role)>
47//!                             {msg.content.clone()}
48//!                         </div>
49//!                     }
50//!                 }
51//!             />
52//!             <Show when=move || chat.is_loading.get()>
53//!                 <div class="loading">"Thinking..."</div>
54//!             </Show>
55//!             <form on:submit=on_submit>
56//!                 <input type="text" node_ref=input placeholder="Type a message..." />
57//!                 <button type="submit">"Send"</button>
58//!             </form>
59//!         </div>
60//!     }
61//! }
62//! ```
63
64mod error;
65mod hooks;
66mod types;
67
68pub use error::*;
69pub use hooks::*;
70pub use types::*;
71
72// Re-export useful types from llm-client
73pub use cortexai_llm_client::{Provider, Role};