feat: replaced ollama with litellm (#18)
Co-authored-by: Sharang Parnerkar <parnerkarsharang@gmail.com> Reviewed-on: #18
This commit was merged in pull request #18.
This commit is contained in:
+21
-21
@@ -25,8 +25,8 @@ const DEFAULT_TOPICS: &[&str] = &[
|
||||
///
|
||||
/// State is persisted across sessions using localStorage:
|
||||
/// - `certifai_topics`: custom user-defined search topics
|
||||
/// - `certifai_ollama_url`: Ollama instance URL for summarization
|
||||
/// - `certifai_ollama_model`: Ollama model ID for summarization
|
||||
/// - `certifai_litellm_url`: LiteLLM proxy URL for summarization
|
||||
/// - `certifai_litellm_model`: LiteLLM model ID for summarization
|
||||
#[component]
|
||||
pub fn DashboardPage() -> Element {
|
||||
let locale = use_context::<Signal<Locale>>();
|
||||
@@ -34,11 +34,11 @@ pub fn DashboardPage() -> Element {
|
||||
|
||||
// Persistent state stored in localStorage
|
||||
let mut custom_topics = use_persistent("certifai_topics".to_string(), Vec::<String>::new);
|
||||
// Default to empty so the server functions use OLLAMA_URL / OLLAMA_MODEL
|
||||
// Default to empty so the server functions use LITELLM_URL / LITELLM_MODEL
|
||||
// from .env. Only stores a non-empty value when the user explicitly saves
|
||||
// an override via the Settings panel.
|
||||
let mut ollama_url = use_persistent("certifai_ollama_url".to_string(), String::new);
|
||||
let mut ollama_model = use_persistent("certifai_ollama_model".to_string(), String::new);
|
||||
let mut litellm_url = use_persistent("certifai_litellm_url".to_string(), String::new);
|
||||
let mut litellm_model = use_persistent("certifai_litellm_model".to_string(), String::new);
|
||||
|
||||
// Reactive signals for UI state
|
||||
let mut active_topic = use_signal(|| "AI".to_string());
|
||||
@@ -235,8 +235,8 @@ pub fn DashboardPage() -> Element {
|
||||
onclick: move |_| {
|
||||
let currently_shown = *show_settings.read();
|
||||
if !currently_shown {
|
||||
settings_url.set(ollama_url.read().clone());
|
||||
settings_model.set(ollama_model.read().clone());
|
||||
settings_url.set(litellm_url.read().clone());
|
||||
settings_model.set(litellm_model.read().clone());
|
||||
}
|
||||
show_settings.set(!currently_shown);
|
||||
},
|
||||
@@ -247,16 +247,16 @@ pub fn DashboardPage() -> Element {
|
||||
// Settings panel (collapsible)
|
||||
if *show_settings.read() {
|
||||
div { class: "settings-panel",
|
||||
h4 { class: "settings-panel-title", "{t(l, \"dashboard.ollama_settings\")}" }
|
||||
h4 { class: "settings-panel-title", "{t(l, \"dashboard.litellm_settings\")}" }
|
||||
p { class: "settings-hint",
|
||||
"{t(l, \"dashboard.settings_hint\")}"
|
||||
}
|
||||
div { class: "settings-field",
|
||||
label { "{t(l, \"dashboard.ollama_url\")}" }
|
||||
label { "{t(l, \"dashboard.litellm_url\")}" }
|
||||
input {
|
||||
class: "settings-input",
|
||||
r#type: "text",
|
||||
placeholder: "{t(l, \"dashboard.ollama_url_placeholder\")}",
|
||||
placeholder: "{t(l, \"dashboard.litellm_url_placeholder\")}",
|
||||
value: "{settings_url}",
|
||||
oninput: move |e| settings_url.set(e.value()),
|
||||
}
|
||||
@@ -274,8 +274,8 @@ pub fn DashboardPage() -> Element {
|
||||
button {
|
||||
class: "btn btn-primary",
|
||||
onclick: move |_| {
|
||||
*ollama_url.write() = settings_url.read().trim().to_string();
|
||||
*ollama_model.write() = settings_model.read().trim().to_string();
|
||||
*litellm_url.write() = settings_url.read().trim().to_string();
|
||||
*litellm_model.write() = settings_model.read().trim().to_string();
|
||||
show_settings.set(false);
|
||||
},
|
||||
"{t(l, \"common.save\")}"
|
||||
@@ -320,14 +320,14 @@ pub fn DashboardPage() -> Element {
|
||||
news_session_id.set(None);
|
||||
|
||||
|
||||
let oll_url = ollama_url.read().clone();
|
||||
let mdl = ollama_model.read().clone();
|
||||
let ll_url = litellm_url.read().clone();
|
||||
let mdl = litellm_model.read().clone();
|
||||
spawn(async move {
|
||||
is_summarizing.set(true);
|
||||
match crate::infrastructure::llm::summarize_article(
|
||||
snippet.clone(),
|
||||
article_url,
|
||||
oll_url,
|
||||
ll_url,
|
||||
mdl,
|
||||
)
|
||||
.await
|
||||
@@ -373,8 +373,8 @@ pub fn DashboardPage() -> Element {
|
||||
chat_messages: chat_messages.read().clone(),
|
||||
is_chatting: *is_chatting.read(),
|
||||
on_chat_send: move |question: String| {
|
||||
let oll_url = ollama_url.read().clone();
|
||||
let mdl = ollama_model.read().clone();
|
||||
let ll_url = litellm_url.read().clone();
|
||||
let mdl = litellm_model.read().clone();
|
||||
let ctx = article_context.read().clone();
|
||||
// Capture article info for News session creation
|
||||
let card_title = selected_card
|
||||
@@ -394,7 +394,7 @@ pub fn DashboardPage() -> Element {
|
||||
content: question.clone(),
|
||||
});
|
||||
|
||||
// Build full message history for Ollama
|
||||
// Build full message history for LiteLLM
|
||||
let system_msg = format!(
|
||||
"You are a helpful assistant. The user is reading \
|
||||
a news article. Use the following context to answer \
|
||||
@@ -422,7 +422,7 @@ pub fn DashboardPage() -> Element {
|
||||
match create_chat_session(
|
||||
card_title,
|
||||
"News".to_string(),
|
||||
"ollama".to_string(),
|
||||
"litellm".to_string(),
|
||||
mdl.clone(),
|
||||
card_url,
|
||||
)
|
||||
@@ -458,7 +458,7 @@ pub fn DashboardPage() -> Element {
|
||||
}
|
||||
|
||||
match crate::infrastructure::llm::chat_followup(
|
||||
msgs, oll_url, mdl,
|
||||
msgs, ll_url, mdl,
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -495,7 +495,7 @@ pub fn DashboardPage() -> Element {
|
||||
// Right: sidebar (when no card selected)
|
||||
if !has_selection {
|
||||
DashboardSidebar {
|
||||
ollama_url: ollama_url.read().clone(),
|
||||
litellm_url: litellm_url.read().clone(),
|
||||
trending: trending_topics.clone(),
|
||||
recent_searches: recent_searches.read().clone(),
|
||||
on_topic_click: move |topic: String| {
|
||||
|
||||
@@ -2,12 +2,14 @@ use dioxus::prelude::*;
|
||||
|
||||
use crate::components::{MemberRow, PageHeader};
|
||||
use crate::i18n::{t, tw, Locale};
|
||||
use crate::models::{BillingUsage, MemberRole, OrgMember};
|
||||
use crate::infrastructure::litellm::get_litellm_usage;
|
||||
use crate::models::{BillingUsage, LitellmUsageStats, MemberRole, OrgMember};
|
||||
|
||||
/// Organization dashboard with billing stats, member table, and invite modal.
|
||||
///
|
||||
/// Shows current billing usage, a table of organization members
|
||||
/// with role management, and a button to invite new members.
|
||||
/// Shows current billing usage (fetched from LiteLLM), a per-model
|
||||
/// breakdown table, a table of organization members with role
|
||||
/// management, and a button to invite new members.
|
||||
#[component]
|
||||
pub fn OrgDashboardPage() -> Element {
|
||||
let locale = use_context::<Signal<Locale>>();
|
||||
@@ -20,6 +22,20 @@ pub fn OrgDashboardPage() -> Element {
|
||||
|
||||
let members_list = members.read().clone();
|
||||
|
||||
// Compute date range: 1st of current month to today
|
||||
let (start_date, end_date) = current_month_range();
|
||||
|
||||
// Fetch real usage stats from LiteLLM via server function.
|
||||
// use_resource memoises and won't re-fire on parent re-renders.
|
||||
let usage_resource = use_resource(move || {
|
||||
let start = start_date.clone();
|
||||
let end = end_date.clone();
|
||||
async move { get_litellm_usage(start, end).await }
|
||||
});
|
||||
|
||||
// Clone out of Signal to avoid holding the borrow across rsx!
|
||||
let usage_snapshot = usage_resource.read().clone();
|
||||
|
||||
// Format token counts for display
|
||||
let tokens_display = format_tokens(usage.tokens_used);
|
||||
let tokens_limit_display = format_tokens(usage.tokens_limit);
|
||||
@@ -30,26 +46,39 @@ pub fn OrgDashboardPage() -> Element {
|
||||
title: t(l, "org.title"),
|
||||
subtitle: t(l, "org.subtitle"),
|
||||
actions: rsx! {
|
||||
button { class: "btn-primary", onclick: move |_| show_invite.set(true), {t(l, "org.invite_member")} }
|
||||
button {
|
||||
class: "btn-primary",
|
||||
onclick: move |_| show_invite.set(true),
|
||||
{t(l, "org.invite_member")}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// Stats bar
|
||||
div { class: "org-stats-bar",
|
||||
div { class: "org-stat",
|
||||
span { class: "org-stat-value", "{usage.seats_used}/{usage.seats_total}" }
|
||||
span { class: "org-stat-value",
|
||||
"{usage.seats_used}/{usage.seats_total}"
|
||||
}
|
||||
span { class: "org-stat-label", {t(l, "org.seats_used")} }
|
||||
}
|
||||
div { class: "org-stat",
|
||||
span { class: "org-stat-value", "{tokens_display}" }
|
||||
span { class: "org-stat-label", {tw(l, "org.of_tokens", &[("limit", &tokens_limit_display)])} }
|
||||
span { class: "org-stat-label",
|
||||
{tw(l, "org.of_tokens", &[("limit", &tokens_limit_display)])}
|
||||
}
|
||||
}
|
||||
div { class: "org-stat",
|
||||
span { class: "org-stat-value", "{usage.billing_cycle_end}" }
|
||||
span { class: "org-stat-value",
|
||||
"{usage.billing_cycle_end}"
|
||||
}
|
||||
span { class: "org-stat-label", {t(l, "org.cycle_ends")} }
|
||||
}
|
||||
}
|
||||
|
||||
// LiteLLM usage stats section
|
||||
{render_usage_section(l, &usage_snapshot)}
|
||||
|
||||
// Members table
|
||||
div { class: "org-table-wrapper",
|
||||
table { class: "org-table",
|
||||
@@ -114,6 +143,144 @@ pub fn OrgDashboardPage() -> Element {
|
||||
}
|
||||
}
|
||||
|
||||
/// Render the LiteLLM usage stats section: totals bar + per-model table.
|
||||
///
|
||||
/// Shows a loading state while the resource is pending, an error/empty
|
||||
/// message on failure, and the full breakdown on success.
|
||||
fn render_usage_section(
|
||||
l: Locale,
|
||||
snapshot: &Option<Result<LitellmUsageStats, ServerFnError>>,
|
||||
) -> Element {
|
||||
match snapshot {
|
||||
None => rsx! {
|
||||
div { class: "org-usage-loading",
|
||||
span { {t(l, "org.loading_usage")} }
|
||||
}
|
||||
},
|
||||
Some(Err(_)) => rsx! {
|
||||
div { class: "org-usage-unavailable",
|
||||
span { {t(l, "org.usage_unavailable")} }
|
||||
}
|
||||
},
|
||||
Some(Ok(stats)) if stats.total_tokens == 0 && stats.model_breakdown.is_empty() => {
|
||||
rsx! {
|
||||
div { class: "org-usage-unavailable",
|
||||
span { {t(l, "org.usage_unavailable")} }
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Ok(stats)) => {
|
||||
let spend_display = format!("${:.2}", stats.total_spend);
|
||||
let total_display = format_tokens(stats.total_tokens);
|
||||
// Free-tier LiteLLM doesn't provide prompt/completion split
|
||||
let has_token_split =
|
||||
stats.total_prompt_tokens > 0 || stats.total_completion_tokens > 0;
|
||||
|
||||
rsx! {
|
||||
// Usage totals bar
|
||||
div { class: "org-stats-bar",
|
||||
div { class: "org-stat",
|
||||
span { class: "org-stat-value", "{spend_display}" }
|
||||
span { class: "org-stat-label",
|
||||
{t(l, "org.total_spend")}
|
||||
}
|
||||
}
|
||||
div { class: "org-stat",
|
||||
span { class: "org-stat-value",
|
||||
"{total_display}"
|
||||
}
|
||||
span { class: "org-stat-label",
|
||||
{t(l, "org.total_tokens")}
|
||||
}
|
||||
}
|
||||
// Only show prompt/completion split when available
|
||||
if has_token_split {
|
||||
div { class: "org-stat",
|
||||
span { class: "org-stat-value",
|
||||
{format_tokens(stats.total_prompt_tokens)}
|
||||
}
|
||||
span { class: "org-stat-label",
|
||||
{t(l, "org.prompt_tokens")}
|
||||
}
|
||||
}
|
||||
div { class: "org-stat",
|
||||
span { class: "org-stat-value",
|
||||
{format_tokens(stats.total_completion_tokens)}
|
||||
}
|
||||
span { class: "org-stat-label",
|
||||
{t(l, "org.completion_tokens")}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Per-model breakdown table
|
||||
if !stats.model_breakdown.is_empty() {
|
||||
h3 { class: "org-section-title",
|
||||
{t(l, "org.model_usage")}
|
||||
}
|
||||
div { class: "org-table-wrapper",
|
||||
table { class: "org-table",
|
||||
thead {
|
||||
tr {
|
||||
th { {t(l, "org.model")} }
|
||||
th { {t(l, "org.tokens")} }
|
||||
th { {t(l, "org.spend")} }
|
||||
}
|
||||
}
|
||||
tbody {
|
||||
for model in &stats.model_breakdown {
|
||||
tr { key: "{model.model}",
|
||||
td { "{model.model}" }
|
||||
td {
|
||||
{format_tokens(model.total_tokens)}
|
||||
}
|
||||
td {
|
||||
{format!(
|
||||
"${:.2}", model.spend
|
||||
)}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute the date range for the current billing month.
|
||||
///
|
||||
/// Returns `(start_date, end_date)` as `YYYY-MM-DD` strings where
|
||||
/// start_date is the 1st of the current month and end_date is today.
|
||||
///
|
||||
/// On the web target this uses `js_sys::Date` to read the browser clock.
|
||||
/// On the server target (SSR) it falls back to `chrono::Utc::now()`.
|
||||
fn current_month_range() -> (String, String) {
|
||||
#[cfg(feature = "web")]
|
||||
{
|
||||
// js_sys::Date accesses the browser's local clock in WASM.
|
||||
let now = js_sys::Date::new_0();
|
||||
let year = now.get_full_year();
|
||||
// JS months are 0-indexed, so add 1 for calendar month
|
||||
let month = now.get_month() + 1;
|
||||
let day = now.get_date();
|
||||
let start = format!("{year:04}-{month:02}-01");
|
||||
let end = format!("{year:04}-{month:02}-{day:02}");
|
||||
(start, end)
|
||||
}
|
||||
#[cfg(not(feature = "web"))]
|
||||
{
|
||||
use chrono::Datelike;
|
||||
let today = chrono::Utc::now().date_naive();
|
||||
let start = format!("{:04}-{:02}-01", today.year(), today.month());
|
||||
let end = today.format("%Y-%m-%d").to_string();
|
||||
(start, end)
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats a token count into a human-readable string (e.g. "1.2M").
|
||||
fn format_tokens(count: u64) -> String {
|
||||
const M: u64 = 1_000_000;
|
||||
|
||||
+26
-20
@@ -13,8 +13,8 @@ pub fn ProvidersPage() -> Element {
|
||||
let locale = use_context::<Signal<Locale>>();
|
||||
let l = *locale.read();
|
||||
|
||||
let mut selected_provider = use_signal(|| LlmProvider::Ollama);
|
||||
let mut selected_model = use_signal(|| "llama3.1:8b".to_string());
|
||||
let mut selected_provider = use_signal(|| LlmProvider::LiteLlm);
|
||||
let mut selected_model = use_signal(|| "qwen3-32b".to_string());
|
||||
let mut selected_embedding = use_signal(|| "nomic-embed-text".to_string());
|
||||
let mut api_key = use_signal(String::new);
|
||||
let mut saved = use_signal(|| false);
|
||||
@@ -59,12 +59,12 @@ pub fn ProvidersPage() -> Element {
|
||||
"Hugging Face" => LlmProvider::HuggingFace,
|
||||
"OpenAI" => LlmProvider::OpenAi,
|
||||
"Anthropic" => LlmProvider::Anthropic,
|
||||
_ => LlmProvider::Ollama,
|
||||
_ => LlmProvider::LiteLlm,
|
||||
};
|
||||
selected_provider.set(prov);
|
||||
saved.set(false);
|
||||
},
|
||||
option { value: "Ollama", "Ollama" }
|
||||
option { value: "LiteLLM", "LiteLLM" }
|
||||
option { value: "Hugging Face", "Hugging Face" }
|
||||
option { value: "OpenAI", "OpenAI" }
|
||||
option { value: "Anthropic", "Anthropic" }
|
||||
@@ -156,23 +156,29 @@ pub fn ProvidersPage() -> Element {
|
||||
fn mock_models() -> Vec<ModelEntry> {
|
||||
vec![
|
||||
ModelEntry {
|
||||
id: "llama3.1:8b".into(),
|
||||
name: "Llama 3.1 8B".into(),
|
||||
provider: LlmProvider::Ollama,
|
||||
context_window: 128,
|
||||
},
|
||||
ModelEntry {
|
||||
id: "llama3.1:70b".into(),
|
||||
name: "Llama 3.1 70B".into(),
|
||||
provider: LlmProvider::Ollama,
|
||||
context_window: 128,
|
||||
},
|
||||
ModelEntry {
|
||||
id: "mistral:7b".into(),
|
||||
name: "Mistral 7B".into(),
|
||||
provider: LlmProvider::Ollama,
|
||||
id: "qwen3-32b".into(),
|
||||
name: "Qwen3 32B".into(),
|
||||
provider: LlmProvider::LiteLlm,
|
||||
context_window: 32,
|
||||
},
|
||||
ModelEntry {
|
||||
id: "llama-3.3-70b".into(),
|
||||
name: "Llama 3.3 70B".into(),
|
||||
provider: LlmProvider::LiteLlm,
|
||||
context_window: 128,
|
||||
},
|
||||
ModelEntry {
|
||||
id: "mistral-small-24b".into(),
|
||||
name: "Mistral Small 24B".into(),
|
||||
provider: LlmProvider::LiteLlm,
|
||||
context_window: 32,
|
||||
},
|
||||
ModelEntry {
|
||||
id: "deepseek-r1-70b".into(),
|
||||
name: "DeepSeek R1 70B".into(),
|
||||
provider: LlmProvider::LiteLlm,
|
||||
context_window: 64,
|
||||
},
|
||||
ModelEntry {
|
||||
id: "meta-llama/Llama-3.1-8B".into(),
|
||||
name: "Llama 3.1 8B".into(),
|
||||
@@ -200,7 +206,7 @@ fn mock_embeddings() -> Vec<EmbeddingEntry> {
|
||||
EmbeddingEntry {
|
||||
id: "nomic-embed-text".into(),
|
||||
name: "Nomic Embed Text".into(),
|
||||
provider: LlmProvider::Ollama,
|
||||
provider: LlmProvider::LiteLlm,
|
||||
dimensions: 768,
|
||||
},
|
||||
EmbeddingEntry {
|
||||
|
||||
Reference in New Issue
Block a user