From 2ea196d0ffb2cea35db37214b568c0d637d897fc Mon Sep 17 00:00:00 2001 From: Markus Hennerbichler Date: Sat, 20 Jan 2024 12:44:04 +0000 Subject: [PATCH] Only warn of rate-limits when using HF endpoint --- crates/llm-ls/src/main.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/llm-ls/src/main.rs b/crates/llm-ls/src/main.rs index 054aada..890ad9e 100644 --- a/crates/llm-ls/src/main.rs +++ b/crates/llm-ls/src/main.rs @@ -26,6 +26,7 @@ mod language_id; const MAX_WARNING_REPEAT: Duration = Duration::from_secs(3_600); pub const NAME: &str = "llm-ls"; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); +const HUGGINGFACE_INFERENCE_HOSTNAME: &str = "api-inference.huggingface.co"; fn get_position_idx(rope: &Rope, row: usize, col: usize) -> Result { Ok(rope.try_line_to_char(row).map_err(internal_error)? @@ -247,7 +248,7 @@ where } #[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] +// #[serde(rename_all = "camelCase")] struct AcceptedCompletion { request_id: Uuid, accepted_completion: u32, @@ -255,7 +256,7 @@ struct AcceptedCompletion { } #[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] +// #[serde(rename_all = "camelCase")] struct RejectedCompletion { request_id: Uuid, shown_completions: Vec, @@ -584,10 +585,14 @@ fn build_url(model: &str) -> String { if model.starts_with("http://") || model.starts_with("https://") { model.to_owned() } else { - format!("https://api-inference.huggingface.co/models/{model}") + format!("https://{HUGGINGFACE_INFERENCE_HOSTNAME}/models/{model}") } } +fn is_hf_model(model: &str) -> bool { + return build_url(model).contains(HUGGINGFACE_INFERENCE_HOSTNAME); +} + impl Backend { async fn get_completions(&self, params: CompletionParams) -> Result { let request_id = Uuid::new_v4(); @@ -613,7 +618,7 @@ impl Backend { "received completion request for {}", params.text_document_position.text_document.uri ); - if params.api_token.is_none() { + if params.api_token.is_none() && is_hf_model(¶ms.model) { let now = Instant::now(); let unauthenticated_warn_at = self.unauthenticated_warn_at.read().await; if now.duration_since(*unauthenticated_warn_at) > MAX_WARNING_REPEAT { @@ -734,8 +739,11 @@ impl LanguageServer for Backend { async fn did_change(&self, params: DidChangeTextDocumentParams) { let uri = params.text_document.uri.to_string(); + if params.content_changes.is_empty() { + return; + } self.client - .log_message(MessageType::INFO, format!("{uri} changed")) + .log_message(MessageType::LOG, format!("{uri} changed")) .await; let mut document_map = self.document_map.write().await; let doc = document_map.get_mut(&uri);