Skip to main content

Crate dioxus_ai

Crate dioxus_ai 

Source
Expand description

§Dioxus AI

AI hooks for Dioxus applications - chat, completions, and streaming.

§Features

  • use_chat - Reactive chat with message history and streaming
  • use_completion - Single completion requests
  • Built on llm-client for provider support (OpenAI, Anthropic, OpenRouter)

§Example

use dioxus::prelude::*;
use dioxus_ai::{use_chat, ChatOptions};

#[component]
fn Chat() -> Element {
    let mut chat = use_chat(ChatOptions {
        provider: "openai".to_string(),
        api_key: "sk-...".to_string(),
        model: "gpt-4o-mini".to_string(),
        ..Default::default()
    });

    let mut input = use_signal(String::new);

    rsx! {
        div {
            for msg in chat.messages().iter() {
                p { class: "{msg.role}", "{msg.content}" }
            }

            if chat.is_loading() {
                p { "Thinking..." }
            }

            input {
                value: "{input}",
                oninput: move |e| input.set(e.value().clone())
            }
            button {
                onclick: move |_| {
                    chat.send(&input());
                    input.set(String::new());
                },
                "Send"
            }
        }
    }
}

Structs§

ChatMessage
A message in the chat conversation
ChatOptions
Options for the chat hook
CompletionOptions
Options for the completion hook
UseChatState
State for the chat hook
UseCompletionState
State for the completion hook

Enums§

DioxusAiError
Errors that can occur in dioxus-ai
Provider
Supported LLM providers.
Role
Role in a conversation.

Functions§

use_chat
Create a reactive chat interface
use_completion
Create a completion interface

Type Aliases§

Result