From 070890d36111a5265fc2336180747bf33d1006f1 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Tue, 28 Jan 2025 10:56:05 -0500 Subject: [PATCH] anthropic: Don't bail out on unknown model ID (#23782) This PR fixes an issue introduced in https://github.com/zed-industries/zed/pull/20551/ that would prevent models with unknown IDs from working in the LLM service. We only need to look up a model from its ID for the beta headers, and if we can't find that particular model we should fall back to the default beta headers instead of bailing out completely, Release Notes: - N/A --- crates/anthropic/src/anthropic.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/crates/anthropic/src/anthropic.rs b/crates/anthropic/src/anthropic.rs index 28e306d3141493e211d70e3225ef960919c73b87..1dd447a1c98e4935e1b412fc1b1d94e004f1ce90 100644 --- a/crates/anthropic/src/anthropic.rs +++ b/crates/anthropic/src/anthropic.rs @@ -148,8 +148,13 @@ impl Model { } } + pub const DEFAULT_BETA_HEADERS: &[&str] = &["prompt-caching-2024-07-31"]; + pub fn beta_headers(&self) -> String { - let mut headers = vec!["prompt-caching-2024-07-31".to_string()]; + let mut headers = Self::DEFAULT_BETA_HEADERS + .into_iter() + .map(|header| header.to_string()) + .collect::>(); if let Self::Custom { extra_beta_headers, .. @@ -186,12 +191,14 @@ pub async fn complete( request: Request, ) -> Result { let uri = format!("{api_url}/v1/messages"); - let model = Model::from_id(&request.model)?; + let beta_headers = Model::from_id(&request.model) + .map(|model| model.beta_headers()) + .unwrap_or_else(|_err| Model::DEFAULT_BETA_HEADERS.join(",")); let request_builder = HttpRequest::builder() .method(Method::POST) .uri(uri) .header("Anthropic-Version", "2023-06-01") - .header("Anthropic-Beta", model.beta_headers()) + .header("Anthropic-Beta", beta_headers) .header("X-Api-Key", api_key) .header("Content-Type", "application/json"); @@ -302,12 +309,14 @@ pub async fn stream_completion_with_rate_limit_info( stream: true, }; let uri = format!("{api_url}/v1/messages"); - let model = Model::from_id(&request.base.model)?; + let beta_headers = Model::from_id(&request.base.model) + .map(|model| model.beta_headers()) + .unwrap_or_else(|_err| Model::DEFAULT_BETA_HEADERS.join(",")); let request_builder = HttpRequest::builder() .method(Method::POST) .uri(uri) .header("Anthropic-Version", "2023-06-01") - .header("Anthropic-Beta", model.beta_headers()) + .header("Anthropic-Beta", beta_headers) .header("X-Api-Key", api_key) .header("Content-Type", "application/json"); let serialized_request =