Skip to content

Commit

Permalink
feat: send warning to client when unauthenticated (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
McPatate authored Oct 10, 2023
1 parent f90bb13 commit b6d6c6c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/llm-ls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "llm-ls"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[[bin]]
Expand Down
19 changes: 18 additions & 1 deletion crates/llm-ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashMap;
use std::fmt::Display;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;
use std::time::{Duration, Instant};
use tokenizers::Tokenizer;
use tokio::io::AsyncWriteExt;
use tokio::sync::RwLock;
Expand All @@ -16,6 +16,7 @@ use tracing::{debug, error, info};
use tracing_appender::rolling;
use tracing_subscriber::EnvFilter;

const MAX_WARNING_REPEAT: Duration = Duration::from_secs(3_600);
const NAME: &str = "llm-ls";
const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -121,6 +122,7 @@ struct Backend {
unsafe_http_client: reqwest::Client,
workspace_folders: Arc<RwLock<Option<Vec<WorkspaceFolder>>>>,
tokenizer_map: Arc<RwLock<HashMap<String, Arc<Tokenizer>>>>,
unauthenticated_warn_at: Arc<RwLock<Instant>>,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -454,6 +456,16 @@ impl Backend {
let document = document_map
.get(params.text_document_position.text_document.uri.as_str())
.ok_or_else(|| internal_error("failed to find document"))?;
if params.api_token.is_none() {
let now = Instant::now();
let unauthenticated_warn_at = self.unauthenticated_warn_at.read().await;
if now.duration_since(*unauthenticated_warn_at) > MAX_WARNING_REPEAT {
drop(unauthenticated_warn_at);
self.client.show_message(MessageType::WARNING, "You are currently unauthenticated and will get rate limited. To reduce rate limiting, login with your API Token and consider subscribing to PRO: https://huggingface.co/pricing#pro").await;
let mut unauthenticated_warn_at = self.unauthenticated_warn_at.write().await;
*unauthenticated_warn_at = Instant::now();
}
}
let tokenizer = get_tokenizer(
&params.model,
&mut *self.tokenizer_map.write().await,
Expand Down Expand Up @@ -631,6 +643,11 @@ async fn main() {
unsafe_http_client,
workspace_folders: Arc::new(RwLock::new(None)),
tokenizer_map: Arc::new(RwLock::new(HashMap::new())),
unauthenticated_warn_at: Arc::new(RwLock::new(
Instant::now()
.checked_sub(MAX_WARNING_REPEAT)
.expect("instant to be in bounds"),
)),
})
.custom_method("llm-ls/getCompletions", Backend::get_completions)
.finish();
Expand Down

0 comments on commit b6d6c6c

Please sign in to comment.