open_ai.rs

  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::{Model, 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, 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: &Model,
337    max_output_tokens: Option<u32>,
338) -> open_ai::Request {
339    let stream = !model.id().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: model.id().into(),
393        messages,
394        stream,
395        stop: request.stop,
396        temperature: request.temperature.unwrap_or(1.0),
397        max_tokens: max_output_tokens,
398        parallel_tool_calls: if model.supports_parallel_tool_calls() && !request.tools.is_empty() {
399            // Disable parallel tool calls, as the Agent currently expects a maximum of one per turn.
400            Some(false)
401        } else {
402            None
403        },
404        tools: request
405            .tools
406            .into_iter()
407            .map(|tool| open_ai::ToolDefinition::Function {
408                function: open_ai::FunctionDefinition {
409                    name: tool.name,
410                    description: Some(tool.description),
411                    parameters: Some(tool.input_schema),
412                },
413            })
414            .collect(),
415        tool_choice: None,
416    }
417}
418
419pub fn map_to_language_model_completion_events(
420    events: Pin<Box<dyn Send + Stream<Item = Result<ResponseStreamEvent>>>>,
421) -> impl Stream<Item = Result<LanguageModelCompletionEvent>> {
422    #[derive(Default)]
423    struct RawToolCall {
424        id: String,
425        name: String,
426        arguments: String,
427    }
428
429    struct State {
430        events: Pin<Box<dyn Send + Stream<Item = Result<ResponseStreamEvent>>>>,
431        tool_calls_by_index: HashMap<usize, RawToolCall>,
432    }
433
434    futures::stream::unfold(
435        State {
436            events,
437            tool_calls_by_index: HashMap::default(),
438        },
439        |mut state| async move {
440            if let Some(event) = state.events.next().await {
441                match event {
442                    Ok(event) => {
443                        let Some(choice) = event.choices.first() else {
444                            return Some((
445                                vec![Err(anyhow!("Response contained no choices"))],
446                                state,
447                            ));
448                        };
449
450                        let mut events = Vec::new();
451                        if let Some(content) = choice.delta.content.clone() {
452                            events.push(Ok(LanguageModelCompletionEvent::Text(content)));
453                        }
454
455                        if let Some(tool_calls) = choice.delta.tool_calls.as_ref() {
456                            for tool_call in tool_calls {
457                                let entry = state
458                                    .tool_calls_by_index
459                                    .entry(tool_call.index)
460                                    .or_default();
461
462                                if let Some(tool_id) = tool_call.id.clone() {
463                                    entry.id = tool_id;
464                                }
465
466                                if let Some(function) = tool_call.function.as_ref() {
467                                    if let Some(name) = function.name.clone() {
468                                        entry.name = name;
469                                    }
470
471                                    if let Some(arguments) = function.arguments.clone() {
472                                        entry.arguments.push_str(&arguments);
473                                    }
474                                }
475                            }
476                        }
477
478                        match choice.finish_reason.as_deref() {
479                            Some("stop") => {
480                                events.push(Ok(LanguageModelCompletionEvent::Stop(
481                                    StopReason::EndTurn,
482                                )));
483                            }
484                            Some("tool_calls") => {
485                                events.extend(state.tool_calls_by_index.drain().map(
486                                    |(_, tool_call)| {
487                                        maybe!({
488                                            Ok(LanguageModelCompletionEvent::ToolUse(
489                                                LanguageModelToolUse {
490                                                    id: tool_call.id.into(),
491                                                    name: tool_call.name.as_str().into(),
492                                                    input: serde_json::Value::from_str(
493                                                        &tool_call.arguments,
494                                                    )?,
495                                                },
496                                            ))
497                                        })
498                                    },
499                                ));
500
501                                events.push(Ok(LanguageModelCompletionEvent::Stop(
502                                    StopReason::ToolUse,
503                                )));
504                            }
505                            Some(stop_reason) => {
506                                log::error!("Unexpected OpenAI stop_reason: {stop_reason:?}",);
507                                events.push(Ok(LanguageModelCompletionEvent::Stop(
508                                    StopReason::EndTurn,
509                                )));
510                            }
511                            None => {}
512                        }
513
514                        return Some((events, state));
515                    }
516                    Err(err) => return Some((vec![Err(err)], state)),
517                }
518            }
519
520            None
521        },
522    )
523    .flat_map(futures::stream::iter)
524}
525
526pub fn count_open_ai_tokens(
527    request: LanguageModelRequest,
528    model: open_ai::Model,
529    cx: &App,
530) -> BoxFuture<'static, Result<usize>> {
531    cx.background_spawn(async move {
532        let messages = request
533            .messages
534            .into_iter()
535            .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
536                role: match message.role {
537                    Role::User => "user".into(),
538                    Role::Assistant => "assistant".into(),
539                    Role::System => "system".into(),
540                },
541                content: Some(message.string_contents()),
542                name: None,
543                function_call: None,
544            })
545            .collect::<Vec<_>>();
546
547        match model {
548            open_ai::Model::Custom { .. }
549            | open_ai::Model::O1Mini
550            | open_ai::Model::O1
551            | open_ai::Model::O3Mini => tiktoken_rs::num_tokens_from_messages("gpt-4", &messages),
552            _ => tiktoken_rs::num_tokens_from_messages(model.id(), &messages),
553        }
554    })
555    .boxed()
556}
557
558struct ConfigurationView {
559    api_key_editor: Entity<Editor>,
560    state: gpui::Entity<State>,
561    load_credentials_task: Option<Task<()>>,
562}
563
564impl ConfigurationView {
565    fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
566        let api_key_editor = cx.new(|cx| {
567            let mut editor = Editor::single_line(window, cx);
568            editor.set_placeholder_text("sk-000000000000000000000000000000000000000000000000", cx);
569            editor
570        });
571
572        cx.observe(&state, |_, _, cx| {
573            cx.notify();
574        })
575        .detach();
576
577        let load_credentials_task = Some(cx.spawn_in(window, {
578            let state = state.clone();
579            async move |this, cx| {
580                if let Some(task) = state
581                    .update(cx, |state, cx| state.authenticate(cx))
582                    .log_err()
583                {
584                    // We don't log an error, because "not signed in" is also an error.
585                    let _ = task.await;
586                }
587
588                this.update(cx, |this, cx| {
589                    this.load_credentials_task = None;
590                    cx.notify();
591                })
592                .log_err();
593            }
594        }));
595
596        Self {
597            api_key_editor,
598            state,
599            load_credentials_task,
600        }
601    }
602
603    fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
604        let api_key = self.api_key_editor.read(cx).text(cx);
605        if api_key.is_empty() {
606            return;
607        }
608
609        let state = self.state.clone();
610        cx.spawn_in(window, async move |_, cx| {
611            state
612                .update(cx, |state, cx| state.set_api_key(api_key, cx))?
613                .await
614        })
615        .detach_and_log_err(cx);
616
617        cx.notify();
618    }
619
620    fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
621        self.api_key_editor
622            .update(cx, |editor, cx| editor.set_text("", window, cx));
623
624        let state = self.state.clone();
625        cx.spawn_in(window, async move |_, cx| {
626            state.update(cx, |state, cx| state.reset_api_key(cx))?.await
627        })
628        .detach_and_log_err(cx);
629
630        cx.notify();
631    }
632
633    fn render_api_key_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
634        let settings = ThemeSettings::get_global(cx);
635        let text_style = TextStyle {
636            color: cx.theme().colors().text,
637            font_family: settings.ui_font.family.clone(),
638            font_features: settings.ui_font.features.clone(),
639            font_fallbacks: settings.ui_font.fallbacks.clone(),
640            font_size: rems(0.875).into(),
641            font_weight: settings.ui_font.weight,
642            font_style: FontStyle::Normal,
643            line_height: relative(1.3),
644            white_space: WhiteSpace::Normal,
645            ..Default::default()
646        };
647        EditorElement::new(
648            &self.api_key_editor,
649            EditorStyle {
650                background: cx.theme().colors().editor_background,
651                local_player: cx.theme().players().local(),
652                text: text_style,
653                ..Default::default()
654            },
655        )
656    }
657
658    fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
659        !self.state.read(cx).is_authenticated()
660    }
661}
662
663impl Render for ConfigurationView {
664    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
665        let env_var_set = self.state.read(cx).api_key_from_env;
666
667        if self.load_credentials_task.is_some() {
668            div().child(Label::new("Loading credentials...")).into_any()
669        } else if self.should_render_editor(cx) {
670            v_flex()
671                .size_full()
672                .on_action(cx.listener(Self::save_api_key))
673                .child(Label::new("To use Zed's assistant with OpenAI, you need to add an API key. Follow these steps:"))
674                .child(
675                    List::new()
676                        .child(InstructionListItem::new(
677                            "Create one by visiting",
678                            Some("OpenAI's console"),
679                            Some("https://platform.openai.com/api-keys"),
680                        ))
681                        .child(InstructionListItem::text_only(
682                            "Ensure your OpenAI account has credits",
683                        ))
684                        .child(InstructionListItem::text_only(
685                            "Paste your API key below and hit enter to start using the assistant",
686                        )),
687                )
688                .child(
689                    h_flex()
690                        .w_full()
691                        .my_2()
692                        .px_2()
693                        .py_1()
694                        .bg(cx.theme().colors().editor_background)
695                        .border_1()
696                        .border_color(cx.theme().colors().border_variant)
697                        .rounded_sm()
698                        .child(self.render_api_key_editor(cx)),
699                )
700                .child(
701                    Label::new(
702                        format!("You can also assign the {OPENAI_API_KEY_VAR} environment variable and restart Zed."),
703                    )
704                    .size(LabelSize::Small).color(Color::Muted),
705                )
706                .child(
707                    Label::new(
708                        "Note that having a subscription for another service like GitHub Copilot won't work.".to_string(),
709                    )
710                    .size(LabelSize::Small).color(Color::Muted),
711                )
712                .into_any()
713        } else {
714            h_flex()
715                .size_full()
716                .justify_between()
717                .child(
718                    h_flex()
719                        .gap_1()
720                        .child(Icon::new(IconName::Check).color(Color::Success))
721                        .child(Label::new(if env_var_set {
722                            format!("API key set in {OPENAI_API_KEY_VAR} environment variable.")
723                        } else {
724                            "API key configured.".to_string()
725                        })),
726                )
727                .child(
728                    Button::new("reset-key", "Reset key")
729                        .icon(Some(IconName::Trash))
730                        .icon_size(IconSize::Small)
731                        .icon_position(IconPosition::Start)
732                        .disabled(env_var_set)
733                        .when(env_var_set, |this| {
734                            this.tooltip(Tooltip::text(format!("To reset your API key, unset the {OPENAI_API_KEY_VAR} environment variable.")))
735                        })
736                        .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
737                )
738                .into_any()
739        }
740    }
741}