1use anyhow::{Context as _, Result, anyhow};
2use collections::{BTreeMap, HashMap};
3use credentials_provider::CredentialsProvider;
4use editor::{Editor, EditorElement, EditorStyle};
5use futures::Stream;
6use futures::{FutureExt, StreamExt, future::BoxFuture};
7use gpui::{
8 AnyView, App, AsyncApp, Context, Entity, FontStyle, Subscription, Task, TextStyle, WhiteSpace,
9};
10use http_client::HttpClient;
11use language_model::{
12 AuthenticateError, LanguageModel, LanguageModelCompletionEvent, LanguageModelId,
13 LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,
14 LanguageModelProviderState, LanguageModelRequest, LanguageModelToolUse, MessageContent,
15 RateLimiter, Role, StopReason,
16};
17use open_ai::{ResponseStreamEvent, stream_completion};
18use schemars::JsonSchema;
19use serde::{Deserialize, Serialize};
20use settings::{Settings, SettingsStore};
21use std::pin::Pin;
22use std::str::FromStr as _;
23use std::sync::Arc;
24use strum::IntoEnumIterator;
25use theme::ThemeSettings;
26use ui::{Icon, IconName, List, Tooltip, prelude::*};
27use util::{ResultExt, maybe};
28
29use crate::{AllLanguageModelSettings, ui::InstructionListItem};
30
31const PROVIDER_ID: &str = "openai";
32const PROVIDER_NAME: &str = "OpenAI";
33
34#[derive(Default, Clone, Debug, PartialEq)]
35pub struct OpenAiSettings {
36 pub api_url: String,
37 pub available_models: Vec<AvailableModel>,
38 pub needs_setting_migration: bool,
39}
40
41#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
42pub struct AvailableModel {
43 pub name: String,
44 pub display_name: Option<String>,
45 pub max_tokens: usize,
46 pub max_output_tokens: Option<u32>,
47 pub max_completion_tokens: Option<u32>,
48}
49
50pub struct OpenAiLanguageModelProvider {
51 http_client: Arc<dyn HttpClient>,
52 state: gpui::Entity<State>,
53}
54
55pub struct State {
56 api_key: Option<String>,
57 api_key_from_env: bool,
58 _subscription: Subscription,
59}
60
61const OPENAI_API_KEY_VAR: &str = "OPENAI_API_KEY";
62
63impl State {
64 fn is_authenticated(&self) -> bool {
65 self.api_key.is_some()
66 }
67
68 fn reset_api_key(&self, cx: &mut Context<Self>) -> Task<Result<()>> {
69 let credentials_provider = <dyn CredentialsProvider>::global(cx);
70 let api_url = AllLanguageModelSettings::get_global(cx)
71 .openai
72 .api_url
73 .clone();
74 cx.spawn(async move |this, cx| {
75 credentials_provider
76 .delete_credentials(&api_url, &cx)
77 .await
78 .log_err();
79 this.update(cx, |this, cx| {
80 this.api_key = None;
81 this.api_key_from_env = false;
82 cx.notify();
83 })
84 })
85 }
86
87 fn set_api_key(&mut self, api_key: String, cx: &mut Context<Self>) -> Task<Result<()>> {
88 let credentials_provider = <dyn CredentialsProvider>::global(cx);
89 let api_url = AllLanguageModelSettings::get_global(cx)
90 .openai
91 .api_url
92 .clone();
93 cx.spawn(async move |this, cx| {
94 credentials_provider
95 .write_credentials(&api_url, "Bearer", api_key.as_bytes(), &cx)
96 .await
97 .log_err();
98 this.update(cx, |this, cx| {
99 this.api_key = Some(api_key);
100 cx.notify();
101 })
102 })
103 }
104
105 fn authenticate(&self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
106 if self.is_authenticated() {
107 return Task::ready(Ok(()));
108 }
109
110 let credentials_provider = <dyn CredentialsProvider>::global(cx);
111 let api_url = AllLanguageModelSettings::get_global(cx)
112 .openai
113 .api_url
114 .clone();
115 cx.spawn(async move |this, cx| {
116 let (api_key, from_env) = if let Ok(api_key) = std::env::var(OPENAI_API_KEY_VAR) {
117 (api_key, true)
118 } else {
119 let (_, api_key) = credentials_provider
120 .read_credentials(&api_url, &cx)
121 .await?
122 .ok_or(AuthenticateError::CredentialsNotFound)?;
123 (
124 String::from_utf8(api_key).context("invalid {PROVIDER_NAME} API key")?,
125 false,
126 )
127 };
128 this.update(cx, |this, cx| {
129 this.api_key = Some(api_key);
130 this.api_key_from_env = from_env;
131 cx.notify();
132 })?;
133
134 Ok(())
135 })
136 }
137}
138
139impl OpenAiLanguageModelProvider {
140 pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
141 let state = cx.new(|cx| State {
142 api_key: None,
143 api_key_from_env: false,
144 _subscription: cx.observe_global::<SettingsStore>(|_this: &mut State, cx| {
145 cx.notify();
146 }),
147 });
148
149 Self { http_client, state }
150 }
151}
152
153impl LanguageModelProviderState for OpenAiLanguageModelProvider {
154 type ObservableEntity = State;
155
156 fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
157 Some(self.state.clone())
158 }
159}
160
161impl LanguageModelProvider for OpenAiLanguageModelProvider {
162 fn id(&self) -> LanguageModelProviderId {
163 LanguageModelProviderId(PROVIDER_ID.into())
164 }
165
166 fn name(&self) -> LanguageModelProviderName {
167 LanguageModelProviderName(PROVIDER_NAME.into())
168 }
169
170 fn icon(&self) -> IconName {
171 IconName::AiOpenAi
172 }
173
174 fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
175 let model = open_ai::Model::default();
176 Some(Arc::new(OpenAiLanguageModel {
177 id: LanguageModelId::from(model.id().to_string()),
178 model,
179 state: self.state.clone(),
180 http_client: self.http_client.clone(),
181 request_limiter: RateLimiter::new(4),
182 }))
183 }
184
185 fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
186 let mut models = BTreeMap::default();
187
188 // Add base models from open_ai::Model::iter()
189 for model in open_ai::Model::iter() {
190 if !matches!(model, open_ai::Model::Custom { .. }) {
191 models.insert(model.id().to_string(), model);
192 }
193 }
194
195 // Override with available models from settings
196 for model in &AllLanguageModelSettings::get_global(cx)
197 .openai
198 .available_models
199 {
200 models.insert(
201 model.name.clone(),
202 open_ai::Model::Custom {
203 name: model.name.clone(),
204 display_name: model.display_name.clone(),
205 max_tokens: model.max_tokens,
206 max_output_tokens: model.max_output_tokens,
207 max_completion_tokens: model.max_completion_tokens,
208 },
209 );
210 }
211
212 models
213 .into_values()
214 .map(|model| {
215 Arc::new(OpenAiLanguageModel {
216 id: LanguageModelId::from(model.id().to_string()),
217 model,
218 state: self.state.clone(),
219 http_client: self.http_client.clone(),
220 request_limiter: RateLimiter::new(4),
221 }) as Arc<dyn LanguageModel>
222 })
223 .collect()
224 }
225
226 fn is_authenticated(&self, cx: &App) -> bool {
227 self.state.read(cx).is_authenticated()
228 }
229
230 fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
231 self.state.update(cx, |state, cx| state.authenticate(cx))
232 }
233
234 fn configuration_view(&self, window: &mut Window, cx: &mut App) -> AnyView {
235 cx.new(|cx| ConfigurationView::new(self.state.clone(), window, cx))
236 .into()
237 }
238
239 fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
240 self.state.update(cx, |state, cx| state.reset_api_key(cx))
241 }
242}
243
244pub struct OpenAiLanguageModel {
245 id: LanguageModelId,
246 model: open_ai::Model,
247 state: gpui::Entity<State>,
248 http_client: Arc<dyn HttpClient>,
249 request_limiter: RateLimiter,
250}
251
252impl OpenAiLanguageModel {
253 fn stream_completion(
254 &self,
255 request: open_ai::Request,
256 cx: &AsyncApp,
257 ) -> BoxFuture<'static, Result<futures::stream::BoxStream<'static, Result<ResponseStreamEvent>>>>
258 {
259 let http_client = self.http_client.clone();
260 let Ok((api_key, api_url)) = cx.read_entity(&self.state, |state, cx| {
261 let settings = &AllLanguageModelSettings::get_global(cx).openai;
262 (state.api_key.clone(), settings.api_url.clone())
263 }) else {
264 return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
265 };
266
267 let future = self.request_limiter.stream(async move {
268 let api_key = api_key.ok_or_else(|| anyhow!("Missing OpenAI API Key"))?;
269 let request = stream_completion(http_client.as_ref(), &api_url, &api_key, request);
270 let response = request.await?;
271 Ok(response)
272 });
273
274 async move { Ok(future.await?.boxed()) }.boxed()
275 }
276}
277
278impl LanguageModel for OpenAiLanguageModel {
279 fn id(&self) -> LanguageModelId {
280 self.id.clone()
281 }
282
283 fn name(&self) -> LanguageModelName {
284 LanguageModelName::from(self.model.display_name().to_string())
285 }
286
287 fn provider_id(&self) -> LanguageModelProviderId {
288 LanguageModelProviderId(PROVIDER_ID.into())
289 }
290
291 fn provider_name(&self) -> LanguageModelProviderName {
292 LanguageModelProviderName(PROVIDER_NAME.into())
293 }
294
295 fn supports_tools(&self) -> bool {
296 true
297 }
298
299 fn telemetry_id(&self) -> String {
300 format!("openai/{}", self.model.id())
301 }
302
303 fn max_token_count(&self) -> usize {
304 self.model.max_token_count()
305 }
306
307 fn max_output_tokens(&self) -> Option<u32> {
308 self.model.max_output_tokens()
309 }
310
311 fn count_tokens(
312 &self,
313 request: LanguageModelRequest,
314 cx: &App,
315 ) -> BoxFuture<'static, Result<usize>> {
316 count_open_ai_tokens(request, self.model.clone(), cx)
317 }
318
319 fn stream_completion(
320 &self,
321 request: LanguageModelRequest,
322 cx: &AsyncApp,
323 ) -> BoxFuture<
324 'static,
325 Result<futures::stream::BoxStream<'static, Result<LanguageModelCompletionEvent>>>,
326 > {
327 let request = into_open_ai(request, self.model.id().into(), self.max_output_tokens());
328 let completions = self.stream_completion(request, cx);
329 async move { Ok(map_to_language_model_completion_events(completions.await?).boxed()) }
330 .boxed()
331 }
332}
333
334pub fn into_open_ai(
335 request: LanguageModelRequest,
336 model: String,
337 max_output_tokens: Option<u32>,
338) -> open_ai::Request {
339 let stream = !model.starts_with("o1-");
340
341 let mut messages = Vec::new();
342 for message in request.messages {
343 for content in message.content {
344 match content {
345 MessageContent::Text(text) => messages.push(match message.role {
346 Role::User => open_ai::RequestMessage::User { content: text },
347 Role::Assistant => open_ai::RequestMessage::Assistant {
348 content: Some(text),
349 tool_calls: Vec::new(),
350 },
351 Role::System => open_ai::RequestMessage::System { content: text },
352 }),
353 MessageContent::Image(_) => {}
354 MessageContent::ToolUse(tool_use) => {
355 let tool_call = open_ai::ToolCall {
356 id: tool_use.id.to_string(),
357 content: open_ai::ToolCallContent::Function {
358 function: open_ai::FunctionContent {
359 name: tool_use.name.to_string(),
360 arguments: serde_json::to_string(&tool_use.input)
361 .unwrap_or_default(),
362 },
363 },
364 };
365
366 if let Some(last_assistant_message) = messages.iter_mut().rfind(|message| {
367 matches!(message, open_ai::RequestMessage::Assistant { .. })
368 }) {
369 if let open_ai::RequestMessage::Assistant { tool_calls, .. } =
370 last_assistant_message
371 {
372 tool_calls.push(tool_call);
373 }
374 } else {
375 messages.push(open_ai::RequestMessage::Assistant {
376 content: None,
377 tool_calls: vec![tool_call],
378 });
379 }
380 }
381 MessageContent::ToolResult(tool_result) => {
382 messages.push(open_ai::RequestMessage::Tool {
383 content: tool_result.content.to_string(),
384 tool_call_id: tool_result.tool_use_id.to_string(),
385 });
386 }
387 }
388 }
389 }
390
391 open_ai::Request {
392 model,
393 messages,
394 stream,
395 stop: request.stop,
396 temperature: request.temperature.unwrap_or(1.0),
397 max_tokens: max_output_tokens,
398 tools: request
399 .tools
400 .into_iter()
401 .map(|tool| open_ai::ToolDefinition::Function {
402 function: open_ai::FunctionDefinition {
403 name: tool.name,
404 description: Some(tool.description),
405 parameters: Some(tool.input_schema),
406 },
407 })
408 .collect(),
409 tool_choice: None,
410 }
411}
412
413pub fn map_to_language_model_completion_events(
414 events: Pin<Box<dyn Send + Stream<Item = Result<ResponseStreamEvent>>>>,
415) -> impl Stream<Item = Result<LanguageModelCompletionEvent>> {
416 #[derive(Default)]
417 struct RawToolCall {
418 id: String,
419 name: String,
420 arguments: String,
421 }
422
423 struct State {
424 events: Pin<Box<dyn Send + Stream<Item = Result<ResponseStreamEvent>>>>,
425 tool_calls_by_index: HashMap<usize, RawToolCall>,
426 }
427
428 futures::stream::unfold(
429 State {
430 events,
431 tool_calls_by_index: HashMap::default(),
432 },
433 |mut state| async move {
434 if let Some(event) = state.events.next().await {
435 match event {
436 Ok(event) => {
437 let Some(choice) = event.choices.first() else {
438 return Some((
439 vec![Err(anyhow!("Response contained no choices"))],
440 state,
441 ));
442 };
443
444 let mut events = Vec::new();
445 if let Some(content) = choice.delta.content.clone() {
446 events.push(Ok(LanguageModelCompletionEvent::Text(content)));
447 }
448
449 if let Some(tool_calls) = choice.delta.tool_calls.as_ref() {
450 for tool_call in tool_calls {
451 let entry = state
452 .tool_calls_by_index
453 .entry(tool_call.index)
454 .or_default();
455
456 if let Some(tool_id) = tool_call.id.clone() {
457 entry.id = tool_id;
458 }
459
460 if let Some(function) = tool_call.function.as_ref() {
461 if let Some(name) = function.name.clone() {
462 entry.name = name;
463 }
464
465 if let Some(arguments) = function.arguments.clone() {
466 entry.arguments.push_str(&arguments);
467 }
468 }
469 }
470 }
471
472 match choice.finish_reason.as_deref() {
473 Some("stop") => {
474 events.push(Ok(LanguageModelCompletionEvent::Stop(
475 StopReason::EndTurn,
476 )));
477 }
478 Some("tool_calls") => {
479 events.extend(state.tool_calls_by_index.drain().map(
480 |(_, tool_call)| {
481 maybe!({
482 Ok(LanguageModelCompletionEvent::ToolUse(
483 LanguageModelToolUse {
484 id: tool_call.id.into(),
485 name: tool_call.name.as_str().into(),
486 input: serde_json::Value::from_str(
487 &tool_call.arguments,
488 )?,
489 },
490 ))
491 })
492 },
493 ));
494
495 events.push(Ok(LanguageModelCompletionEvent::Stop(
496 StopReason::ToolUse,
497 )));
498 }
499 Some(stop_reason) => {
500 log::error!("Unexpected OpenAI stop_reason: {stop_reason:?}",);
501 events.push(Ok(LanguageModelCompletionEvent::Stop(
502 StopReason::EndTurn,
503 )));
504 }
505 None => {}
506 }
507
508 return Some((events, state));
509 }
510 Err(err) => return Some((vec![Err(err)], state)),
511 }
512 }
513
514 None
515 },
516 )
517 .flat_map(futures::stream::iter)
518}
519
520pub fn count_open_ai_tokens(
521 request: LanguageModelRequest,
522 model: open_ai::Model,
523 cx: &App,
524) -> BoxFuture<'static, Result<usize>> {
525 cx.background_spawn(async move {
526 let messages = request
527 .messages
528 .into_iter()
529 .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
530 role: match message.role {
531 Role::User => "user".into(),
532 Role::Assistant => "assistant".into(),
533 Role::System => "system".into(),
534 },
535 content: Some(message.string_contents()),
536 name: None,
537 function_call: None,
538 })
539 .collect::<Vec<_>>();
540
541 match model {
542 open_ai::Model::Custom { .. }
543 | open_ai::Model::O1Mini
544 | open_ai::Model::O1
545 | open_ai::Model::O3Mini => tiktoken_rs::num_tokens_from_messages("gpt-4", &messages),
546 _ => tiktoken_rs::num_tokens_from_messages(model.id(), &messages),
547 }
548 })
549 .boxed()
550}
551
552struct ConfigurationView {
553 api_key_editor: Entity<Editor>,
554 state: gpui::Entity<State>,
555 load_credentials_task: Option<Task<()>>,
556}
557
558impl ConfigurationView {
559 fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
560 let api_key_editor = cx.new(|cx| {
561 let mut editor = Editor::single_line(window, cx);
562 editor.set_placeholder_text("sk-000000000000000000000000000000000000000000000000", cx);
563 editor
564 });
565
566 cx.observe(&state, |_, _, cx| {
567 cx.notify();
568 })
569 .detach();
570
571 let load_credentials_task = Some(cx.spawn_in(window, {
572 let state = state.clone();
573 async move |this, cx| {
574 if let Some(task) = state
575 .update(cx, |state, cx| state.authenticate(cx))
576 .log_err()
577 {
578 // We don't log an error, because "not signed in" is also an error.
579 let _ = task.await;
580 }
581
582 this.update(cx, |this, cx| {
583 this.load_credentials_task = None;
584 cx.notify();
585 })
586 .log_err();
587 }
588 }));
589
590 Self {
591 api_key_editor,
592 state,
593 load_credentials_task,
594 }
595 }
596
597 fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
598 let api_key = self.api_key_editor.read(cx).text(cx);
599 if api_key.is_empty() {
600 return;
601 }
602
603 let state = self.state.clone();
604 cx.spawn_in(window, async move |_, cx| {
605 state
606 .update(cx, |state, cx| state.set_api_key(api_key, cx))?
607 .await
608 })
609 .detach_and_log_err(cx);
610
611 cx.notify();
612 }
613
614 fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
615 self.api_key_editor
616 .update(cx, |editor, cx| editor.set_text("", window, cx));
617
618 let state = self.state.clone();
619 cx.spawn_in(window, async move |_, cx| {
620 state.update(cx, |state, cx| state.reset_api_key(cx))?.await
621 })
622 .detach_and_log_err(cx);
623
624 cx.notify();
625 }
626
627 fn render_api_key_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
628 let settings = ThemeSettings::get_global(cx);
629 let text_style = TextStyle {
630 color: cx.theme().colors().text,
631 font_family: settings.ui_font.family.clone(),
632 font_features: settings.ui_font.features.clone(),
633 font_fallbacks: settings.ui_font.fallbacks.clone(),
634 font_size: rems(0.875).into(),
635 font_weight: settings.ui_font.weight,
636 font_style: FontStyle::Normal,
637 line_height: relative(1.3),
638 white_space: WhiteSpace::Normal,
639 ..Default::default()
640 };
641 EditorElement::new(
642 &self.api_key_editor,
643 EditorStyle {
644 background: cx.theme().colors().editor_background,
645 local_player: cx.theme().players().local(),
646 text: text_style,
647 ..Default::default()
648 },
649 )
650 }
651
652 fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
653 !self.state.read(cx).is_authenticated()
654 }
655}
656
657impl Render for ConfigurationView {
658 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
659 let env_var_set = self.state.read(cx).api_key_from_env;
660
661 if self.load_credentials_task.is_some() {
662 div().child(Label::new("Loading credentials...")).into_any()
663 } else if self.should_render_editor(cx) {
664 v_flex()
665 .size_full()
666 .on_action(cx.listener(Self::save_api_key))
667 .child(Label::new("To use Zed's assistant with OpenAI, you need to add an API key. Follow these steps:"))
668 .child(
669 List::new()
670 .child(InstructionListItem::new(
671 "Create one by visiting",
672 Some("OpenAI's console"),
673 Some("https://platform.openai.com/api-keys"),
674 ))
675 .child(InstructionListItem::text_only(
676 "Ensure your OpenAI account has credits",
677 ))
678 .child(InstructionListItem::text_only(
679 "Paste your API key below and hit enter to start using the assistant",
680 )),
681 )
682 .child(
683 h_flex()
684 .w_full()
685 .my_2()
686 .px_2()
687 .py_1()
688 .bg(cx.theme().colors().editor_background)
689 .border_1()
690 .border_color(cx.theme().colors().border_variant)
691 .rounded_sm()
692 .child(self.render_api_key_editor(cx)),
693 )
694 .child(
695 Label::new(
696 format!("You can also assign the {OPENAI_API_KEY_VAR} environment variable and restart Zed."),
697 )
698 .size(LabelSize::Small).color(Color::Muted),
699 )
700 .child(
701 Label::new(
702 "Note that having a subscription for another service like GitHub Copilot won't work.".to_string(),
703 )
704 .size(LabelSize::Small).color(Color::Muted),
705 )
706 .into_any()
707 } else {
708 h_flex()
709 .size_full()
710 .justify_between()
711 .child(
712 h_flex()
713 .gap_1()
714 .child(Icon::new(IconName::Check).color(Color::Success))
715 .child(Label::new(if env_var_set {
716 format!("API key set in {OPENAI_API_KEY_VAR} environment variable.")
717 } else {
718 "API key configured.".to_string()
719 })),
720 )
721 .child(
722 Button::new("reset-key", "Reset key")
723 .icon(Some(IconName::Trash))
724 .icon_size(IconSize::Small)
725 .icon_position(IconPosition::Start)
726 .disabled(env_var_set)
727 .when(env_var_set, |this| {
728 this.tooltip(Tooltip::text(format!("To reset your API key, unset the {OPENAI_API_KEY_VAR} environment variable.")))
729 })
730 .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
731 )
732 .into_any()
733 }
734 }
735}