anthropic.rs

  1use crate::{
  2    settings::AllLanguageModelSettings, LanguageModel, LanguageModelId, LanguageModelName,
  3    LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,
  4    LanguageModelProviderState, LanguageModelRequest, RateLimiter, Role,
  5};
  6use anthropic::AnthropicError;
  7use anyhow::{anyhow, Context as _, Result};
  8use collections::BTreeMap;
  9use editor::{Editor, EditorElement, EditorStyle};
 10use futures::{future::BoxFuture, stream::BoxStream, FutureExt, StreamExt};
 11use gpui::{
 12    AnyView, AppContext, AsyncAppContext, FontStyle, ModelContext, Subscription, Task, TextStyle,
 13    View, WhiteSpace,
 14};
 15use http_client::HttpClient;
 16use schemars::JsonSchema;
 17use serde::{Deserialize, Serialize};
 18use settings::{Settings, SettingsStore};
 19use std::{sync::Arc, time::Duration};
 20use strum::IntoEnumIterator;
 21use theme::ThemeSettings;
 22use ui::{prelude::*, Icon, IconName};
 23use util::ResultExt;
 24
 25const PROVIDER_ID: &str = "anthropic";
 26const PROVIDER_NAME: &str = "Anthropic";
 27
 28#[derive(Default, Clone, Debug, PartialEq)]
 29pub struct AnthropicSettings {
 30    pub api_url: String,
 31    pub low_speed_timeout: Option<Duration>,
 32    pub available_models: Vec<AvailableModel>,
 33    pub needs_setting_migration: bool,
 34}
 35
 36#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 37pub struct AvailableModel {
 38    pub name: String,
 39    pub max_tokens: usize,
 40    pub tool_override: Option<String>,
 41}
 42
 43pub struct AnthropicLanguageModelProvider {
 44    http_client: Arc<dyn HttpClient>,
 45    state: gpui::Model<State>,
 46}
 47
 48pub struct State {
 49    api_key: Option<String>,
 50    _subscription: Subscription,
 51}
 52
 53impl State {
 54    fn reset_api_key(&self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
 55        let delete_credentials =
 56            cx.delete_credentials(&AllLanguageModelSettings::get_global(cx).anthropic.api_url);
 57        cx.spawn(|this, mut cx| async move {
 58            delete_credentials.await.ok();
 59            this.update(&mut cx, |this, cx| {
 60                this.api_key = None;
 61                cx.notify();
 62            })
 63        })
 64    }
 65
 66    fn set_api_key(&mut self, api_key: String, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
 67        let write_credentials = cx.write_credentials(
 68            AllLanguageModelSettings::get_global(cx)
 69                .anthropic
 70                .api_url
 71                .as_str(),
 72            "Bearer",
 73            api_key.as_bytes(),
 74        );
 75        cx.spawn(|this, mut cx| async move {
 76            write_credentials.await?;
 77
 78            this.update(&mut cx, |this, cx| {
 79                this.api_key = Some(api_key);
 80                cx.notify();
 81            })
 82        })
 83    }
 84
 85    fn is_authenticated(&self) -> bool {
 86        self.api_key.is_some()
 87    }
 88
 89    fn authenticate(&self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
 90        if self.is_authenticated() {
 91            Task::ready(Ok(()))
 92        } else {
 93            let api_url = AllLanguageModelSettings::get_global(cx)
 94                .anthropic
 95                .api_url
 96                .clone();
 97
 98            cx.spawn(|this, mut cx| async move {
 99                let api_key = if let Ok(api_key) = std::env::var("ANTHROPIC_API_KEY") {
100                    api_key
101                } else {
102                    let (_, api_key) = cx
103                        .update(|cx| cx.read_credentials(&api_url))?
104                        .await?
105                        .ok_or_else(|| anyhow!("credentials not found"))?;
106                    String::from_utf8(api_key)?
107                };
108
109                this.update(&mut cx, |this, cx| {
110                    this.api_key = Some(api_key);
111                    cx.notify();
112                })
113            })
114        }
115    }
116}
117
118impl AnthropicLanguageModelProvider {
119    pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut AppContext) -> Self {
120        let state = cx.new_model(|cx| State {
121            api_key: None,
122            _subscription: cx.observe_global::<SettingsStore>(|_, cx| {
123                cx.notify();
124            }),
125        });
126
127        Self { http_client, state }
128    }
129}
130
131impl LanguageModelProviderState for AnthropicLanguageModelProvider {
132    type ObservableEntity = State;
133
134    fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> {
135        Some(self.state.clone())
136    }
137}
138
139impl LanguageModelProvider for AnthropicLanguageModelProvider {
140    fn id(&self) -> LanguageModelProviderId {
141        LanguageModelProviderId(PROVIDER_ID.into())
142    }
143
144    fn name(&self) -> LanguageModelProviderName {
145        LanguageModelProviderName(PROVIDER_NAME.into())
146    }
147
148    fn icon(&self) -> IconName {
149        IconName::AiAnthropic
150    }
151
152    fn provided_models(&self, cx: &AppContext) -> Vec<Arc<dyn LanguageModel>> {
153        let mut models = BTreeMap::default();
154
155        // Add base models from anthropic::Model::iter()
156        for model in anthropic::Model::iter() {
157            if !matches!(model, anthropic::Model::Custom { .. }) {
158                models.insert(model.id().to_string(), model);
159            }
160        }
161
162        // Override with available models from settings
163        for model in AllLanguageModelSettings::get_global(cx)
164            .anthropic
165            .available_models
166            .iter()
167        {
168            models.insert(
169                model.name.clone(),
170                anthropic::Model::Custom {
171                    name: model.name.clone(),
172                    max_tokens: model.max_tokens,
173                    tool_override: model.tool_override.clone(),
174                },
175            );
176        }
177
178        models
179            .into_values()
180            .map(|model| {
181                Arc::new(AnthropicModel {
182                    id: LanguageModelId::from(model.id().to_string()),
183                    model,
184                    state: self.state.clone(),
185                    http_client: self.http_client.clone(),
186                    request_limiter: RateLimiter::new(4),
187                }) as Arc<dyn LanguageModel>
188            })
189            .collect()
190    }
191
192    fn is_authenticated(&self, cx: &AppContext) -> bool {
193        self.state.read(cx).is_authenticated()
194    }
195
196    fn authenticate(&self, cx: &mut AppContext) -> Task<Result<()>> {
197        self.state.update(cx, |state, cx| state.authenticate(cx))
198    }
199
200    fn configuration_view(&self, cx: &mut WindowContext) -> AnyView {
201        cx.new_view(|cx| ConfigurationView::new(self.state.clone(), cx))
202            .into()
203    }
204
205    fn reset_credentials(&self, cx: &mut AppContext) -> Task<Result<()>> {
206        self.state.update(cx, |state, cx| state.reset_api_key(cx))
207    }
208}
209
210pub struct AnthropicModel {
211    id: LanguageModelId,
212    model: anthropic::Model,
213    state: gpui::Model<State>,
214    http_client: Arc<dyn HttpClient>,
215    request_limiter: RateLimiter,
216}
217
218pub fn count_anthropic_tokens(
219    request: LanguageModelRequest,
220    cx: &AppContext,
221) -> BoxFuture<'static, Result<usize>> {
222    cx.background_executor()
223        .spawn(async move {
224            let messages = request
225                .messages
226                .into_iter()
227                .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
228                    role: match message.role {
229                        Role::User => "user".into(),
230                        Role::Assistant => "assistant".into(),
231                        Role::System => "system".into(),
232                    },
233                    content: Some(message.content),
234                    name: None,
235                    function_call: None,
236                })
237                .collect::<Vec<_>>();
238
239            // Tiktoken doesn't yet support these models, so we manually use the
240            // same tokenizer as GPT-4.
241            tiktoken_rs::num_tokens_from_messages("gpt-4", &messages)
242        })
243        .boxed()
244}
245
246impl AnthropicModel {
247    fn request_completion(
248        &self,
249        request: anthropic::Request,
250        cx: &AsyncAppContext,
251    ) -> BoxFuture<'static, Result<anthropic::Response>> {
252        let http_client = self.http_client.clone();
253
254        let Ok((api_key, api_url)) = cx.read_model(&self.state, |state, cx| {
255            let settings = &AllLanguageModelSettings::get_global(cx).anthropic;
256            (state.api_key.clone(), settings.api_url.clone())
257        }) else {
258            return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
259        };
260
261        async move {
262            let api_key = api_key.ok_or_else(|| anyhow!("missing api key"))?;
263            anthropic::complete(http_client.as_ref(), &api_url, &api_key, request)
264                .await
265                .context("failed to retrieve completion")
266        }
267        .boxed()
268    }
269
270    fn stream_completion(
271        &self,
272        request: anthropic::Request,
273        cx: &AsyncAppContext,
274    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<anthropic::Event, AnthropicError>>>>
275    {
276        let http_client = self.http_client.clone();
277
278        let Ok((api_key, api_url, low_speed_timeout)) = cx.read_model(&self.state, |state, cx| {
279            let settings = &AllLanguageModelSettings::get_global(cx).anthropic;
280            (
281                state.api_key.clone(),
282                settings.api_url.clone(),
283                settings.low_speed_timeout,
284            )
285        }) else {
286            return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
287        };
288
289        async move {
290            let api_key = api_key.ok_or_else(|| anyhow!("missing api key"))?;
291            let request = anthropic::stream_completion(
292                http_client.as_ref(),
293                &api_url,
294                &api_key,
295                request,
296                low_speed_timeout,
297            );
298            request.await.context("failed to stream completion")
299        }
300        .boxed()
301    }
302}
303
304impl LanguageModel for AnthropicModel {
305    fn id(&self) -> LanguageModelId {
306        self.id.clone()
307    }
308
309    fn name(&self) -> LanguageModelName {
310        LanguageModelName::from(self.model.display_name().to_string())
311    }
312
313    fn provider_id(&self) -> LanguageModelProviderId {
314        LanguageModelProviderId(PROVIDER_ID.into())
315    }
316
317    fn provider_name(&self) -> LanguageModelProviderName {
318        LanguageModelProviderName(PROVIDER_NAME.into())
319    }
320
321    fn telemetry_id(&self) -> String {
322        format!("anthropic/{}", self.model.id())
323    }
324
325    fn max_token_count(&self) -> usize {
326        self.model.max_token_count()
327    }
328
329    fn count_tokens(
330        &self,
331        request: LanguageModelRequest,
332        cx: &AppContext,
333    ) -> BoxFuture<'static, Result<usize>> {
334        count_anthropic_tokens(request, cx)
335    }
336
337    fn stream_completion(
338        &self,
339        request: LanguageModelRequest,
340        cx: &AsyncAppContext,
341    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
342        let request = request.into_anthropic(self.model.id().into());
343        let request = self.stream_completion(request, cx);
344        let future = self.request_limiter.stream(async move {
345            let response = request.await.map_err(|err| anyhow!(err))?;
346            Ok(anthropic::extract_text_from_events(response))
347        });
348        async move {
349            Ok(future
350                .await?
351                .map(|result| result.map_err(|err| anyhow!(err)))
352                .boxed())
353        }
354        .boxed()
355    }
356
357    fn use_any_tool(
358        &self,
359        request: LanguageModelRequest,
360        tool_name: String,
361        tool_description: String,
362        input_schema: serde_json::Value,
363        cx: &AsyncAppContext,
364    ) -> BoxFuture<'static, Result<serde_json::Value>> {
365        let mut request = request.into_anthropic(self.model.tool_model_id().into());
366        request.tool_choice = Some(anthropic::ToolChoice::Tool {
367            name: tool_name.clone(),
368        });
369        request.tools = vec![anthropic::Tool {
370            name: tool_name.clone(),
371            description: tool_description,
372            input_schema,
373        }];
374
375        let response = self.request_completion(request, cx);
376        self.request_limiter
377            .run(async move {
378                let response = response.await?;
379                response
380                    .content
381                    .into_iter()
382                    .find_map(|content| {
383                        if let anthropic::Content::ToolUse { name, input, .. } = content {
384                            if name == tool_name {
385                                Some(input)
386                            } else {
387                                None
388                            }
389                        } else {
390                            None
391                        }
392                    })
393                    .context("tool not used")
394            })
395            .boxed()
396    }
397}
398
399struct ConfigurationView {
400    api_key_editor: View<Editor>,
401    state: gpui::Model<State>,
402    load_credentials_task: Option<Task<()>>,
403}
404
405impl ConfigurationView {
406    const PLACEHOLDER_TEXT: &'static str = "sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
407
408    fn new(state: gpui::Model<State>, cx: &mut ViewContext<Self>) -> Self {
409        cx.observe(&state, |_, _, cx| {
410            cx.notify();
411        })
412        .detach();
413
414        let load_credentials_task = Some(cx.spawn({
415            let state = state.clone();
416            |this, mut cx| async move {
417                if let Some(task) = state
418                    .update(&mut cx, |state, cx| state.authenticate(cx))
419                    .log_err()
420                {
421                    // We don't log an error, because "not signed in" is also an error.
422                    let _ = task.await;
423                }
424                this.update(&mut cx, |this, cx| {
425                    this.load_credentials_task = None;
426                    cx.notify();
427                })
428                .log_err();
429            }
430        }));
431
432        Self {
433            api_key_editor: cx.new_view(|cx| {
434                let mut editor = Editor::single_line(cx);
435                editor.set_placeholder_text(Self::PLACEHOLDER_TEXT, cx);
436                editor
437            }),
438            state,
439            load_credentials_task,
440        }
441    }
442
443    fn save_api_key(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
444        let api_key = self.api_key_editor.read(cx).text(cx);
445        if api_key.is_empty() {
446            return;
447        }
448
449        let state = self.state.clone();
450        cx.spawn(|_, mut cx| async move {
451            state
452                .update(&mut cx, |state, cx| state.set_api_key(api_key, cx))?
453                .await
454        })
455        .detach_and_log_err(cx);
456
457        cx.notify();
458    }
459
460    fn reset_api_key(&mut self, cx: &mut ViewContext<Self>) {
461        self.api_key_editor
462            .update(cx, |editor, cx| editor.set_text("", cx));
463
464        let state = self.state.clone();
465        cx.spawn(|_, mut cx| async move {
466            state
467                .update(&mut cx, |state, cx| state.reset_api_key(cx))?
468                .await
469        })
470        .detach_and_log_err(cx);
471
472        cx.notify();
473    }
474
475    fn render_api_key_editor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
476        let settings = ThemeSettings::get_global(cx);
477        let text_style = TextStyle {
478            color: cx.theme().colors().text,
479            font_family: settings.ui_font.family.clone(),
480            font_features: settings.ui_font.features.clone(),
481            font_fallbacks: settings.ui_font.fallbacks.clone(),
482            font_size: rems(0.875).into(),
483            font_weight: settings.ui_font.weight,
484            font_style: FontStyle::Normal,
485            line_height: relative(1.3),
486            background_color: None,
487            underline: None,
488            strikethrough: None,
489            white_space: WhiteSpace::Normal,
490        };
491        EditorElement::new(
492            &self.api_key_editor,
493            EditorStyle {
494                background: cx.theme().colors().editor_background,
495                local_player: cx.theme().players().local(),
496                text: text_style,
497                ..Default::default()
498            },
499        )
500    }
501
502    fn should_render_editor(&self, cx: &mut ViewContext<Self>) -> bool {
503        !self.state.read(cx).is_authenticated()
504    }
505}
506
507impl Render for ConfigurationView {
508    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
509        const INSTRUCTIONS: [&str; 4] = [
510            "To use the assistant panel or inline assistant, you need to add your Anthropic API key.",
511            "You can create an API key at: https://console.anthropic.com/settings/keys",
512            "",
513            "Paste your Anthropic API key below and hit enter to use the assistant:",
514        ];
515
516        if self.load_credentials_task.is_some() {
517            div().child(Label::new("Loading credentials...")).into_any()
518        } else if self.should_render_editor(cx) {
519            v_flex()
520                .size_full()
521                .on_action(cx.listener(Self::save_api_key))
522                .children(
523                    INSTRUCTIONS.map(|instruction| Label::new(instruction)),
524                )
525                .child(
526                    h_flex()
527                        .w_full()
528                        .my_2()
529                        .px_2()
530                        .py_1()
531                        .bg(cx.theme().colors().editor_background)
532                        .rounded_md()
533                        .child(self.render_api_key_editor(cx)),
534                )
535                .child(
536                    Label::new(
537                        "You can also assign the ANTHROPIC_API_KEY environment variable and restart Zed.",
538                    )
539                    .size(LabelSize::Small),
540                )
541                .into_any()
542        } else {
543            h_flex()
544                .size_full()
545                .justify_between()
546                .child(
547                    h_flex()
548                        .gap_1()
549                        .child(Icon::new(IconName::Check).color(Color::Success))
550                        .child(Label::new("API key configured.")),
551                )
552                .child(
553                    Button::new("reset-key", "Reset key")
554                        .icon(Some(IconName::Trash))
555                        .icon_size(IconSize::Small)
556                        .icon_position(IconPosition::Start)
557                        .on_click(cx.listener(|this, _, cx| this.reset_api_key(cx))),
558                )
559                .into_any()
560        }
561    }
562}