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