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.messages;
225 let mut tokens_from_images = 0;
226 let mut string_messages = Vec::with_capacity(messages.len());
227
228 for message in messages {
229 use crate::MessageContent;
230
231 let mut string_contents = String::new();
232
233 for content in message.content {
234 match content {
235 MessageContent::Text(string) => {
236 string_contents.push_str(&string);
237 }
238 MessageContent::Image(image) => {
239 tokens_from_images += image.estimate_tokens();
240 }
241 }
242 }
243
244 if !string_contents.is_empty() {
245 string_messages.push(tiktoken_rs::ChatCompletionRequestMessage {
246 role: match message.role {
247 Role::User => "user".into(),
248 Role::Assistant => "assistant".into(),
249 Role::System => "system".into(),
250 },
251 content: Some(string_contents),
252 name: None,
253 function_call: None,
254 });
255 }
256 }
257
258 // Tiktoken doesn't yet support these models, so we manually use the
259 // same tokenizer as GPT-4.
260 tiktoken_rs::num_tokens_from_messages("gpt-4", &string_messages)
261 .map(|tokens| tokens + tokens_from_images)
262 })
263 .boxed()
264}
265
266impl AnthropicModel {
267 fn request_completion(
268 &self,
269 request: anthropic::Request,
270 cx: &AsyncAppContext,
271 ) -> BoxFuture<'static, Result<anthropic::Response>> {
272 let http_client = self.http_client.clone();
273
274 let Ok((api_key, api_url)) = cx.read_model(&self.state, |state, cx| {
275 let settings = &AllLanguageModelSettings::get_global(cx).anthropic;
276 (state.api_key.clone(), settings.api_url.clone())
277 }) else {
278 return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
279 };
280
281 async move {
282 let api_key = api_key.ok_or_else(|| anyhow!("missing api key"))?;
283 anthropic::complete(http_client.as_ref(), &api_url, &api_key, request)
284 .await
285 .context("failed to retrieve completion")
286 }
287 .boxed()
288 }
289
290 fn stream_completion(
291 &self,
292 request: anthropic::Request,
293 cx: &AsyncAppContext,
294 ) -> BoxFuture<'static, Result<BoxStream<'static, Result<anthropic::Event, AnthropicError>>>>
295 {
296 let http_client = self.http_client.clone();
297
298 let Ok((api_key, api_url, low_speed_timeout)) = cx.read_model(&self.state, |state, cx| {
299 let settings = &AllLanguageModelSettings::get_global(cx).anthropic;
300 (
301 state.api_key.clone(),
302 settings.api_url.clone(),
303 settings.low_speed_timeout,
304 )
305 }) else {
306 return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
307 };
308
309 async move {
310 let api_key = api_key.ok_or_else(|| anyhow!("missing api key"))?;
311 let request = anthropic::stream_completion(
312 http_client.as_ref(),
313 &api_url,
314 &api_key,
315 request,
316 low_speed_timeout,
317 );
318 request.await.context("failed to stream completion")
319 }
320 .boxed()
321 }
322}
323
324impl LanguageModel for AnthropicModel {
325 fn id(&self) -> LanguageModelId {
326 self.id.clone()
327 }
328
329 fn name(&self) -> LanguageModelName {
330 LanguageModelName::from(self.model.display_name().to_string())
331 }
332
333 fn provider_id(&self) -> LanguageModelProviderId {
334 LanguageModelProviderId(PROVIDER_ID.into())
335 }
336
337 fn provider_name(&self) -> LanguageModelProviderName {
338 LanguageModelProviderName(PROVIDER_NAME.into())
339 }
340
341 fn telemetry_id(&self) -> String {
342 format!("anthropic/{}", self.model.id())
343 }
344
345 fn max_token_count(&self) -> usize {
346 self.model.max_token_count()
347 }
348
349 fn count_tokens(
350 &self,
351 request: LanguageModelRequest,
352 cx: &AppContext,
353 ) -> BoxFuture<'static, Result<usize>> {
354 count_anthropic_tokens(request, cx)
355 }
356
357 fn stream_completion(
358 &self,
359 request: LanguageModelRequest,
360 cx: &AsyncAppContext,
361 ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
362 let request = request.into_anthropic(self.model.id().into());
363 let request = self.stream_completion(request, cx);
364 let future = self.request_limiter.stream(async move {
365 let response = request.await.map_err(|err| anyhow!(err))?;
366 Ok(anthropic::extract_text_from_events(response))
367 });
368 async move {
369 Ok(future
370 .await?
371 .map(|result| result.map_err(|err| anyhow!(err)))
372 .boxed())
373 }
374 .boxed()
375 }
376
377 fn use_any_tool(
378 &self,
379 request: LanguageModelRequest,
380 tool_name: String,
381 tool_description: String,
382 input_schema: serde_json::Value,
383 cx: &AsyncAppContext,
384 ) -> BoxFuture<'static, Result<serde_json::Value>> {
385 let mut request = request.into_anthropic(self.model.tool_model_id().into());
386 request.tool_choice = Some(anthropic::ToolChoice::Tool {
387 name: tool_name.clone(),
388 });
389 request.tools = vec![anthropic::Tool {
390 name: tool_name.clone(),
391 description: tool_description,
392 input_schema,
393 }];
394
395 let response = self.request_completion(request, cx);
396 self.request_limiter
397 .run(async move {
398 let response = response.await?;
399 response
400 .content
401 .into_iter()
402 .find_map(|content| {
403 if let anthropic::Content::ToolUse { name, input, .. } = content {
404 if name == tool_name {
405 Some(input)
406 } else {
407 None
408 }
409 } else {
410 None
411 }
412 })
413 .context("tool not used")
414 })
415 .boxed()
416 }
417}
418
419struct ConfigurationView {
420 api_key_editor: View<Editor>,
421 state: gpui::Model<State>,
422 load_credentials_task: Option<Task<()>>,
423}
424
425impl ConfigurationView {
426 const PLACEHOLDER_TEXT: &'static str = "sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
427
428 fn new(state: gpui::Model<State>, cx: &mut ViewContext<Self>) -> Self {
429 cx.observe(&state, |_, _, cx| {
430 cx.notify();
431 })
432 .detach();
433
434 let load_credentials_task = Some(cx.spawn({
435 let state = state.clone();
436 |this, mut cx| async move {
437 if let Some(task) = state
438 .update(&mut cx, |state, cx| state.authenticate(cx))
439 .log_err()
440 {
441 // We don't log an error, because "not signed in" is also an error.
442 let _ = task.await;
443 }
444 this.update(&mut cx, |this, cx| {
445 this.load_credentials_task = None;
446 cx.notify();
447 })
448 .log_err();
449 }
450 }));
451
452 Self {
453 api_key_editor: cx.new_view(|cx| {
454 let mut editor = Editor::single_line(cx);
455 editor.set_placeholder_text(Self::PLACEHOLDER_TEXT, cx);
456 editor
457 }),
458 state,
459 load_credentials_task,
460 }
461 }
462
463 fn save_api_key(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
464 let api_key = self.api_key_editor.read(cx).text(cx);
465 if api_key.is_empty() {
466 return;
467 }
468
469 let state = self.state.clone();
470 cx.spawn(|_, mut cx| async move {
471 state
472 .update(&mut cx, |state, cx| state.set_api_key(api_key, cx))?
473 .await
474 })
475 .detach_and_log_err(cx);
476
477 cx.notify();
478 }
479
480 fn reset_api_key(&mut self, cx: &mut ViewContext<Self>) {
481 self.api_key_editor
482 .update(cx, |editor, cx| editor.set_text("", cx));
483
484 let state = self.state.clone();
485 cx.spawn(|_, mut cx| async move {
486 state
487 .update(&mut cx, |state, cx| state.reset_api_key(cx))?
488 .await
489 })
490 .detach_and_log_err(cx);
491
492 cx.notify();
493 }
494
495 fn render_api_key_editor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
496 let settings = ThemeSettings::get_global(cx);
497 let text_style = TextStyle {
498 color: cx.theme().colors().text,
499 font_family: settings.ui_font.family.clone(),
500 font_features: settings.ui_font.features.clone(),
501 font_fallbacks: settings.ui_font.fallbacks.clone(),
502 font_size: rems(0.875).into(),
503 font_weight: settings.ui_font.weight,
504 font_style: FontStyle::Normal,
505 line_height: relative(1.3),
506 background_color: None,
507 underline: None,
508 strikethrough: None,
509 white_space: WhiteSpace::Normal,
510 };
511 EditorElement::new(
512 &self.api_key_editor,
513 EditorStyle {
514 background: cx.theme().colors().editor_background,
515 local_player: cx.theme().players().local(),
516 text: text_style,
517 ..Default::default()
518 },
519 )
520 }
521
522 fn should_render_editor(&self, cx: &mut ViewContext<Self>) -> bool {
523 !self.state.read(cx).is_authenticated()
524 }
525}
526
527impl Render for ConfigurationView {
528 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
529 const INSTRUCTIONS: [&str; 4] = [
530 "To use the assistant panel or inline assistant, you need to add your Anthropic API key.",
531 "You can create an API key at: https://console.anthropic.com/settings/keys",
532 "",
533 "Paste your Anthropic API key below and hit enter to use the assistant:",
534 ];
535
536 if self.load_credentials_task.is_some() {
537 div().child(Label::new("Loading credentials...")).into_any()
538 } else if self.should_render_editor(cx) {
539 v_flex()
540 .size_full()
541 .on_action(cx.listener(Self::save_api_key))
542 .children(
543 INSTRUCTIONS.map(|instruction| Label::new(instruction)),
544 )
545 .child(
546 h_flex()
547 .w_full()
548 .my_2()
549 .px_2()
550 .py_1()
551 .bg(cx.theme().colors().editor_background)
552 .rounded_md()
553 .child(self.render_api_key_editor(cx)),
554 )
555 .child(
556 Label::new(
557 "You can also assign the ANTHROPIC_API_KEY environment variable and restart Zed.",
558 )
559 .size(LabelSize::Small),
560 )
561 .into_any()
562 } else {
563 h_flex()
564 .size_full()
565 .justify_between()
566 .child(
567 h_flex()
568 .gap_1()
569 .child(Icon::new(IconName::Check).color(Color::Success))
570 .child(Label::new("API key configured.")),
571 )
572 .child(
573 Button::new("reset-key", "Reset key")
574 .icon(Some(IconName::Trash))
575 .icon_size(IconSize::Small)
576 .icon_position(IconPosition::Start)
577 .on_click(cx.listener(|this, _, cx| this.reset_api_key(cx))),
578 )
579 .into_any()
580 }
581 }
582}