open_ai_compatible.rs

  1use anyhow::Result;
  2use convert_case::{Case, Casing};
  3use futures::{FutureExt, StreamExt, future::BoxFuture};
  4use gpui::{AnyView, App, AsyncApp, Context, Entity, SharedString, Task, Window};
  5use http_client::HttpClient;
  6use language_model::{
  7    AuthenticateError, LanguageModel, LanguageModelCompletionError, LanguageModelCompletionEvent,
  8    LanguageModelId, LanguageModelName, LanguageModelProvider, LanguageModelProviderId,
  9    LanguageModelProviderName, LanguageModelProviderState, LanguageModelRequest,
 10    LanguageModelToolChoice, LanguageModelToolSchemaFormat, RateLimiter,
 11};
 12use menu;
 13use open_ai::{ResponseStreamEvent, stream_completion};
 14use schemars::JsonSchema;
 15use serde::{Deserialize, Serialize};
 16use settings::{Settings, SettingsStore};
 17use std::sync::Arc;
 18use ui::{ElevationIndex, Tooltip, prelude::*};
 19use ui_input::SingleLineInput;
 20use util::ResultExt;
 21use zed_env_vars::EnvVar;
 22
 23use crate::provider::open_ai::{OpenAiEventMapper, into_open_ai};
 24use crate::{AllLanguageModelSettings, api_key::ApiKeyState};
 25
 26#[derive(Default, Clone, Debug, PartialEq)]
 27pub struct OpenAiCompatibleSettings {
 28    pub api_url: String,
 29    pub available_models: Vec<AvailableModel>,
 30}
 31
 32#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 33pub struct AvailableModel {
 34    pub name: String,
 35    pub display_name: Option<String>,
 36    pub max_tokens: u64,
 37    pub max_output_tokens: Option<u64>,
 38    pub max_completion_tokens: Option<u64>,
 39    #[serde(default)]
 40    pub capabilities: ModelCapabilities,
 41}
 42
 43#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 44pub struct ModelCapabilities {
 45    pub tools: bool,
 46    pub images: bool,
 47    pub parallel_tool_calls: bool,
 48    pub prompt_cache_key: bool,
 49}
 50
 51impl Default for ModelCapabilities {
 52    fn default() -> Self {
 53        Self {
 54            tools: true,
 55            images: false,
 56            parallel_tool_calls: false,
 57            prompt_cache_key: false,
 58        }
 59    }
 60}
 61
 62pub struct OpenAiCompatibleLanguageModelProvider {
 63    id: LanguageModelProviderId,
 64    name: LanguageModelProviderName,
 65    http_client: Arc<dyn HttpClient>,
 66    state: gpui::Entity<State>,
 67}
 68
 69pub struct State {
 70    id: Arc<str>,
 71    api_key_env_var: EnvVar,
 72    api_key_state: ApiKeyState,
 73    settings: OpenAiCompatibleSettings,
 74}
 75
 76impl State {
 77    fn is_authenticated(&self) -> bool {
 78        self.api_key_state.has_key()
 79    }
 80
 81    fn set_api_key(&mut self, api_key: Option<String>, cx: &mut Context<Self>) -> Task<Result<()>> {
 82        let api_url = SharedString::new(self.settings.api_url.as_str());
 83        self.api_key_state
 84            .store(api_url, api_key, |this| &mut this.api_key_state, cx)
 85    }
 86
 87    fn authenticate(&mut self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
 88        let api_url = SharedString::new(self.settings.api_url.clone());
 89        self.api_key_state.load_if_needed(
 90            api_url,
 91            &self.api_key_env_var,
 92            |this| &mut this.api_key_state,
 93            cx,
 94        )
 95    }
 96}
 97
 98impl OpenAiCompatibleLanguageModelProvider {
 99    pub fn new(id: Arc<str>, http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
100        fn resolve_settings<'a>(id: &'a str, cx: &'a App) -> Option<&'a OpenAiCompatibleSettings> {
101            AllLanguageModelSettings::get_global(cx)
102                .openai_compatible
103                .get(id)
104        }
105
106        let api_key_env_var_name = format!("{}_API_KEY", id).to_case(Case::UpperSnake).into();
107        let state = cx.new(|cx| {
108            cx.observe_global::<SettingsStore>(|this: &mut State, cx| {
109                let Some(settings) = resolve_settings(&this.id, cx).cloned() else {
110                    return;
111                };
112                if &this.settings != &settings {
113                    let api_url = SharedString::new(settings.api_url.as_str());
114                    this.api_key_state.handle_url_change(
115                        api_url,
116                        &this.api_key_env_var,
117                        |this| &mut this.api_key_state,
118                        cx,
119                    );
120                    this.settings = settings;
121                    cx.notify();
122                }
123            })
124            .detach();
125            let settings = resolve_settings(&id, cx).cloned().unwrap_or_default();
126            State {
127                id: id.clone(),
128                api_key_env_var: EnvVar::new(api_key_env_var_name),
129                api_key_state: ApiKeyState::new(SharedString::new(settings.api_url.as_str())),
130                settings,
131            }
132        });
133
134        Self {
135            id: id.clone().into(),
136            name: id.into(),
137            http_client,
138            state,
139        }
140    }
141
142    fn create_language_model(&self, model: AvailableModel) -> Arc<dyn LanguageModel> {
143        Arc::new(OpenAiCompatibleLanguageModel {
144            id: LanguageModelId::from(model.name.clone()),
145            provider_id: self.id.clone(),
146            provider_name: self.name.clone(),
147            model,
148            state: self.state.clone(),
149            http_client: self.http_client.clone(),
150            request_limiter: RateLimiter::new(4),
151        })
152    }
153}
154
155impl LanguageModelProviderState for OpenAiCompatibleLanguageModelProvider {
156    type ObservableEntity = State;
157
158    fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
159        Some(self.state.clone())
160    }
161}
162
163impl LanguageModelProvider for OpenAiCompatibleLanguageModelProvider {
164    fn id(&self) -> LanguageModelProviderId {
165        self.id.clone()
166    }
167
168    fn name(&self) -> LanguageModelProviderName {
169        self.name.clone()
170    }
171
172    fn icon(&self) -> IconName {
173        IconName::AiOpenAiCompat
174    }
175
176    fn default_model(&self, cx: &App) -> Option<Arc<dyn LanguageModel>> {
177        self.state
178            .read(cx)
179            .settings
180            .available_models
181            .first()
182            .map(|model| self.create_language_model(model.clone()))
183    }
184
185    fn default_fast_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
186        None
187    }
188
189    fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
190        self.state
191            .read(cx)
192            .settings
193            .available_models
194            .iter()
195            .map(|model| self.create_language_model(model.clone()))
196            .collect()
197    }
198
199    fn is_authenticated(&self, cx: &App) -> bool {
200        self.state.read(cx).is_authenticated()
201    }
202
203    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
204        self.state.update(cx, |state, cx| state.authenticate(cx))
205    }
206
207    fn configuration_view(
208        &self,
209        _target_agent: language_model::ConfigurationViewTargetAgent,
210        window: &mut Window,
211        cx: &mut App,
212    ) -> AnyView {
213        cx.new(|cx| ConfigurationView::new(self.state.clone(), window, cx))
214            .into()
215    }
216
217    fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
218        self.state
219            .update(cx, |state, cx| state.set_api_key(None, cx))
220    }
221}
222
223pub struct OpenAiCompatibleLanguageModel {
224    id: LanguageModelId,
225    provider_id: LanguageModelProviderId,
226    provider_name: LanguageModelProviderName,
227    model: AvailableModel,
228    state: gpui::Entity<State>,
229    http_client: Arc<dyn HttpClient>,
230    request_limiter: RateLimiter,
231}
232
233impl OpenAiCompatibleLanguageModel {
234    fn stream_completion(
235        &self,
236        request: open_ai::Request,
237        cx: &AsyncApp,
238    ) -> BoxFuture<'static, Result<futures::stream::BoxStream<'static, Result<ResponseStreamEvent>>>>
239    {
240        let http_client = self.http_client.clone();
241
242        let api_key_and_url = self.state.read_with(cx, |state, _cx| {
243            let api_url = &state.settings.api_url;
244            let api_key = state.api_key_state.key(api_url);
245            (api_key, state.settings.api_url.clone())
246        });
247        let (api_key, api_url) = match api_key_and_url {
248            Ok(api_key_and_url) => api_key_and_url,
249            Err(err) => {
250                return futures::future::ready(Err(err)).boxed();
251            }
252        };
253
254        let provider = self.provider_name.clone();
255        let future = self.request_limiter.stream(async move {
256            let Some(api_key) = api_key else {
257                return Err(LanguageModelCompletionError::NoApiKey { provider });
258            };
259            let request = stream_completion(http_client.as_ref(), &api_url, &api_key, request);
260            let response = request.await?;
261            Ok(response)
262        });
263
264        async move { Ok(future.await?.boxed()) }.boxed()
265    }
266}
267
268impl LanguageModel for OpenAiCompatibleLanguageModel {
269    fn id(&self) -> LanguageModelId {
270        self.id.clone()
271    }
272
273    fn name(&self) -> LanguageModelName {
274        LanguageModelName::from(
275            self.model
276                .display_name
277                .clone()
278                .unwrap_or_else(|| self.model.name.clone()),
279        )
280    }
281
282    fn provider_id(&self) -> LanguageModelProviderId {
283        self.provider_id.clone()
284    }
285
286    fn provider_name(&self) -> LanguageModelProviderName {
287        self.provider_name.clone()
288    }
289
290    fn supports_tools(&self) -> bool {
291        self.model.capabilities.tools
292    }
293
294    fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
295        LanguageModelToolSchemaFormat::JsonSchemaSubset
296    }
297
298    fn supports_images(&self) -> bool {
299        self.model.capabilities.images
300    }
301
302    fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
303        match choice {
304            LanguageModelToolChoice::Auto => self.model.capabilities.tools,
305            LanguageModelToolChoice::Any => self.model.capabilities.tools,
306            LanguageModelToolChoice::None => true,
307        }
308    }
309
310    fn telemetry_id(&self) -> String {
311        format!("openai/{}", self.model.name)
312    }
313
314    fn max_token_count(&self) -> u64 {
315        self.model.max_tokens
316    }
317
318    fn max_output_tokens(&self) -> Option<u64> {
319        self.model.max_output_tokens
320    }
321
322    fn count_tokens(
323        &self,
324        request: LanguageModelRequest,
325        cx: &App,
326    ) -> BoxFuture<'static, Result<u64>> {
327        let max_token_count = self.max_token_count();
328        cx.background_spawn(async move {
329            let messages = super::open_ai::collect_tiktoken_messages(request);
330            let model = if max_token_count >= 100_000 {
331                // If the max tokens is 100k or more, it is likely the o200k_base tokenizer from gpt4o
332                "gpt-4o"
333            } else {
334                // Otherwise fallback to gpt-4, since only cl100k_base and o200k_base are
335                // supported with this tiktoken method
336                "gpt-4"
337            };
338            tiktoken_rs::num_tokens_from_messages(model, &messages).map(|tokens| tokens as u64)
339        })
340        .boxed()
341    }
342
343    fn stream_completion(
344        &self,
345        request: LanguageModelRequest,
346        cx: &AsyncApp,
347    ) -> BoxFuture<
348        'static,
349        Result<
350            futures::stream::BoxStream<
351                'static,
352                Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
353            >,
354            LanguageModelCompletionError,
355        >,
356    > {
357        let request = into_open_ai(
358            request,
359            &self.model.name,
360            self.model.capabilities.parallel_tool_calls,
361            self.model.capabilities.prompt_cache_key,
362            self.max_output_tokens(),
363            None,
364        );
365        let completions = self.stream_completion(request, cx);
366        async move {
367            let mapper = OpenAiEventMapper::new();
368            Ok(mapper.map_stream(completions.await?).boxed())
369        }
370        .boxed()
371    }
372}
373
374struct ConfigurationView {
375    api_key_editor: Entity<SingleLineInput>,
376    state: gpui::Entity<State>,
377    load_credentials_task: Option<Task<()>>,
378}
379
380impl ConfigurationView {
381    fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
382        let api_key_editor = cx.new(|cx| {
383            SingleLineInput::new(
384                window,
385                cx,
386                "000000000000000000000000000000000000000000000000000",
387            )
388        });
389
390        cx.observe(&state, |_, _, cx| {
391            cx.notify();
392        })
393        .detach();
394
395        let load_credentials_task = Some(cx.spawn_in(window, {
396            let state = state.clone();
397            async move |this, cx| {
398                if let Some(task) = state
399                    .update(cx, |state, cx| state.authenticate(cx))
400                    .log_err()
401                {
402                    // We don't log an error, because "not signed in" is also an error.
403                    let _ = task.await;
404                }
405                this.update(cx, |this, cx| {
406                    this.load_credentials_task = None;
407                    cx.notify();
408                })
409                .log_err();
410            }
411        }));
412
413        Self {
414            api_key_editor,
415            state,
416            load_credentials_task,
417        }
418    }
419
420    fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
421        let api_key = self.api_key_editor.read(cx).text(cx).trim().to_string();
422        if api_key.is_empty() {
423            return;
424        }
425
426        let state = self.state.clone();
427        cx.spawn_in(window, async move |_, cx| {
428            state
429                .update(cx, |state, cx| state.set_api_key(Some(api_key), cx))?
430                .await
431        })
432        .detach_and_log_err(cx);
433    }
434
435    fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
436        self.api_key_editor.update(cx, |input, cx| {
437            input.editor.update(cx, |editor, cx| {
438                editor.set_text("", window, cx);
439            });
440        });
441
442        let state = self.state.clone();
443        cx.spawn_in(window, async move |_, cx| {
444            state
445                .update(cx, |state, cx| state.set_api_key(None, cx))?
446                .await
447        })
448        .detach_and_log_err(cx);
449    }
450
451    fn should_render_editor(&self, cx: &Context<Self>) -> bool {
452        !self.state.read(cx).is_authenticated()
453    }
454}
455
456impl Render for ConfigurationView {
457    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
458        let state = self.state.read(cx);
459        let env_var_set = state.api_key_state.is_from_env_var();
460        let env_var_name = &state.api_key_env_var.name;
461
462        let api_key_section = if self.should_render_editor(cx) {
463            v_flex()
464                .on_action(cx.listener(Self::save_api_key))
465                .child(Label::new("To use Zed's agent with an OpenAI-compatible provider, you need to add an API key."))
466                .child(
467                    div()
468                        .pt(DynamicSpacing::Base04.rems(cx))
469                        .child(self.api_key_editor.clone())
470                )
471                .child(
472                    Label::new(
473                        format!("You can also assign the {env_var_name} environment variable and restart Zed."),
474                    )
475                    .size(LabelSize::Small).color(Color::Muted),
476                )
477                .into_any()
478        } else {
479            h_flex()
480                .mt_1()
481                .p_1()
482                .justify_between()
483                .rounded_md()
484                .border_1()
485                .border_color(cx.theme().colors().border)
486                .bg(cx.theme().colors().background)
487                .child(
488                    h_flex()
489                        .gap_1()
490                        .child(Icon::new(IconName::Check).color(Color::Success))
491                        .child(Label::new(if env_var_set {
492                            format!("API key set in {env_var_name} environment variable.")
493                        } else {
494                            "API key configured.".to_string()
495                        })),
496                )
497                .child(
498                    Button::new("reset-api-key", "Reset API Key")
499                        .label_size(LabelSize::Small)
500                        .icon(IconName::Undo)
501                        .icon_size(IconSize::Small)
502                        .icon_position(IconPosition::Start)
503                        .layer(ElevationIndex::ModalSurface)
504                        .when(env_var_set, |this| {
505                            this.tooltip(Tooltip::text(format!("To reset your API key, unset the {env_var_name} environment variable.")))
506                        })
507                        .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
508                )
509                .into_any()
510        };
511
512        if self.load_credentials_task.is_some() {
513            div().child(Label::new("Loading credentials…")).into_any()
514        } else {
515            v_flex().size_full().child(api_key_section).into_any()
516        }
517    }
518}