open_ai.rs

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