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, Subscription, Task, TextStyle, View,
7 WhiteSpace,
8};
9use http_client::HttpClient;
10use open_ai::stream_completion;
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13use settings::{Settings, SettingsStore};
14use std::{future, sync::Arc, time::Duration};
15use strum::IntoEnumIterator;
16use theme::ThemeSettings;
17use ui::prelude::*;
18use util::ResultExt;
19
20use crate::{
21 settings::AllLanguageModelSettings, LanguageModel, LanguageModelId, LanguageModelName,
22 LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,
23 LanguageModelProviderState, LanguageModelRequest, RateLimiter, Role,
24};
25
26const PROVIDER_ID: &str = "openai";
27const PROVIDER_NAME: &str = "OpenAI";
28
29#[derive(Default, Clone, Debug, PartialEq)]
30pub struct OpenAiSettings {
31 pub api_url: String,
32 pub low_speed_timeout: Option<Duration>,
33 pub available_models: Vec<AvailableModel>,
34 pub needs_setting_migration: bool,
35}
36
37#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
38pub struct AvailableModel {
39 pub name: String,
40 pub max_tokens: usize,
41}
42
43pub struct OpenAiLanguageModelProvider {
44 http_client: Arc<dyn HttpClient>,
45 state: gpui::Model<State>,
46}
47
48pub struct State {
49 api_key: Option<String>,
50 _subscription: Subscription,
51}
52
53impl OpenAiLanguageModelProvider {
54 pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut AppContext) -> Self {
55 let state = cx.new_model(|cx| State {
56 api_key: None,
57 _subscription: cx.observe_global::<SettingsStore>(|_this: &mut State, cx| {
58 cx.notify();
59 }),
60 });
61
62 Self { http_client, state }
63 }
64}
65
66impl LanguageModelProviderState for OpenAiLanguageModelProvider {
67 type ObservableEntity = State;
68
69 fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> {
70 Some(self.state.clone())
71 }
72}
73
74impl LanguageModelProvider for OpenAiLanguageModelProvider {
75 fn id(&self) -> LanguageModelProviderId {
76 LanguageModelProviderId(PROVIDER_ID.into())
77 }
78
79 fn name(&self) -> LanguageModelProviderName {
80 LanguageModelProviderName(PROVIDER_NAME.into())
81 }
82
83 fn provided_models(&self, cx: &AppContext) -> Vec<Arc<dyn LanguageModel>> {
84 let mut models = BTreeMap::default();
85
86 // Add base models from open_ai::Model::iter()
87 for model in open_ai::Model::iter() {
88 if !matches!(model, open_ai::Model::Custom { .. }) {
89 models.insert(model.id().to_string(), model);
90 }
91 }
92
93 // Override with available models from settings
94 for model in &AllLanguageModelSettings::get_global(cx)
95 .openai
96 .available_models
97 {
98 models.insert(
99 model.name.clone(),
100 open_ai::Model::Custom {
101 name: model.name.clone(),
102 max_tokens: model.max_tokens,
103 },
104 );
105 }
106
107 models
108 .into_values()
109 .map(|model| {
110 Arc::new(OpenAiLanguageModel {
111 id: LanguageModelId::from(model.id().to_string()),
112 model,
113 state: self.state.clone(),
114 http_client: self.http_client.clone(),
115 request_limiter: RateLimiter::new(4),
116 }) as Arc<dyn LanguageModel>
117 })
118 .collect()
119 }
120
121 fn is_authenticated(&self, cx: &AppContext) -> bool {
122 self.state.read(cx).api_key.is_some()
123 }
124
125 fn authenticate(&self, cx: &mut AppContext) -> Task<Result<()>> {
126 if self.is_authenticated(cx) {
127 Task::ready(Ok(()))
128 } else {
129 let api_url = AllLanguageModelSettings::get_global(cx)
130 .openai
131 .api_url
132 .clone();
133 let state = self.state.clone();
134 cx.spawn(|mut cx| async move {
135 let api_key = if let Ok(api_key) = std::env::var("OPENAI_API_KEY") {
136 api_key
137 } else {
138 let (_, api_key) = cx
139 .update(|cx| cx.read_credentials(&api_url))?
140 .await?
141 .ok_or_else(|| anyhow!("credentials not found"))?;
142 String::from_utf8(api_key)?
143 };
144 state.update(&mut cx, |this, cx| {
145 this.api_key = Some(api_key);
146 cx.notify();
147 })
148 })
149 }
150 }
151
152 fn authentication_prompt(&self, cx: &mut WindowContext) -> AnyView {
153 cx.new_view(|cx| AuthenticationPrompt::new(self.state.clone(), cx))
154 .into()
155 }
156
157 fn reset_credentials(&self, cx: &mut AppContext) -> Task<Result<()>> {
158 let settings = &AllLanguageModelSettings::get_global(cx).openai;
159 let delete_credentials = cx.delete_credentials(&settings.api_url);
160 let state = self.state.clone();
161 cx.spawn(|mut cx| async move {
162 delete_credentials.await.log_err();
163 state.update(&mut cx, |this, cx| {
164 this.api_key = None;
165 cx.notify();
166 })
167 })
168 }
169}
170
171pub struct OpenAiLanguageModel {
172 id: LanguageModelId,
173 model: open_ai::Model,
174 state: gpui::Model<State>,
175 http_client: Arc<dyn HttpClient>,
176 request_limiter: RateLimiter,
177}
178
179impl LanguageModel for OpenAiLanguageModel {
180 fn id(&self) -> LanguageModelId {
181 self.id.clone()
182 }
183
184 fn name(&self) -> LanguageModelName {
185 LanguageModelName::from(self.model.display_name().to_string())
186 }
187
188 fn provider_id(&self) -> LanguageModelProviderId {
189 LanguageModelProviderId(PROVIDER_ID.into())
190 }
191
192 fn provider_name(&self) -> LanguageModelProviderName {
193 LanguageModelProviderName(PROVIDER_NAME.into())
194 }
195
196 fn telemetry_id(&self) -> String {
197 format!("openai/{}", self.model.id())
198 }
199
200 fn max_token_count(&self) -> usize {
201 self.model.max_token_count()
202 }
203
204 fn count_tokens(
205 &self,
206 request: LanguageModelRequest,
207 cx: &AppContext,
208 ) -> BoxFuture<'static, Result<usize>> {
209 count_open_ai_tokens(request, self.model.clone(), cx)
210 }
211
212 fn stream_completion(
213 &self,
214 request: LanguageModelRequest,
215 cx: &AsyncAppContext,
216 ) -> BoxFuture<'static, Result<futures::stream::BoxStream<'static, Result<String>>>> {
217 let request = request.into_open_ai(self.model.id().into());
218
219 let http_client = self.http_client.clone();
220 let Ok((api_key, api_url, low_speed_timeout)) = cx.read_model(&self.state, |state, cx| {
221 let settings = &AllLanguageModelSettings::get_global(cx).openai;
222 (
223 state.api_key.clone(),
224 settings.api_url.clone(),
225 settings.low_speed_timeout,
226 )
227 }) else {
228 return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
229 };
230
231 let future = self.request_limiter.stream(async move {
232 let api_key = api_key.ok_or_else(|| anyhow!("missing api key"))?;
233 let request = stream_completion(
234 http_client.as_ref(),
235 &api_url,
236 &api_key,
237 request,
238 low_speed_timeout,
239 );
240 let response = request.await?;
241 Ok(open_ai::extract_text_from_events(response).boxed())
242 });
243
244 async move { Ok(future.await?.boxed()) }.boxed()
245 }
246
247 fn use_any_tool(
248 &self,
249 _request: LanguageModelRequest,
250 _name: String,
251 _description: String,
252 _schema: serde_json::Value,
253 _cx: &AsyncAppContext,
254 ) -> BoxFuture<'static, Result<serde_json::Value>> {
255 future::ready(Err(anyhow!("not implemented"))).boxed()
256 }
257}
258
259pub fn count_open_ai_tokens(
260 request: LanguageModelRequest,
261 model: open_ai::Model,
262 cx: &AppContext,
263) -> BoxFuture<'static, Result<usize>> {
264 cx.background_executor()
265 .spawn(async move {
266 let messages = request
267 .messages
268 .into_iter()
269 .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
270 role: match message.role {
271 Role::User => "user".into(),
272 Role::Assistant => "assistant".into(),
273 Role::System => "system".into(),
274 },
275 content: Some(message.content),
276 name: None,
277 function_call: None,
278 })
279 .collect::<Vec<_>>();
280
281 if let open_ai::Model::Custom { .. } = model {
282 tiktoken_rs::num_tokens_from_messages("gpt-4", &messages)
283 } else {
284 tiktoken_rs::num_tokens_from_messages(model.id(), &messages)
285 }
286 })
287 .boxed()
288}
289
290struct AuthenticationPrompt {
291 api_key: View<Editor>,
292 state: gpui::Model<State>,
293}
294
295impl AuthenticationPrompt {
296 fn new(state: gpui::Model<State>, cx: &mut WindowContext) -> Self {
297 Self {
298 api_key: cx.new_view(|cx| {
299 let mut editor = Editor::single_line(cx);
300 editor.set_placeholder_text(
301 "sk-000000000000000000000000000000000000000000000000",
302 cx,
303 );
304 editor
305 }),
306 state,
307 }
308 }
309
310 fn save_api_key(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
311 let api_key = self.api_key.read(cx).text(cx);
312 if api_key.is_empty() {
313 return;
314 }
315
316 let settings = &AllLanguageModelSettings::get_global(cx).openai;
317 let write_credentials =
318 cx.write_credentials(&settings.api_url, "Bearer", api_key.as_bytes());
319 let state = self.state.clone();
320 cx.spawn(|_, mut cx| async move {
321 write_credentials.await?;
322 state.update(&mut cx, |this, cx| {
323 this.api_key = Some(api_key);
324 cx.notify();
325 })
326 })
327 .detach_and_log_err(cx);
328 }
329
330 fn render_api_key_editor(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
331 let settings = ThemeSettings::get_global(cx);
332 let text_style = TextStyle {
333 color: cx.theme().colors().text,
334 font_family: settings.ui_font.family.clone(),
335 font_features: settings.ui_font.features.clone(),
336 font_fallbacks: settings.ui_font.fallbacks.clone(),
337 font_size: rems(0.875).into(),
338 font_weight: settings.ui_font.weight,
339 font_style: FontStyle::Normal,
340 line_height: relative(1.3),
341 background_color: None,
342 underline: None,
343 strikethrough: None,
344 white_space: WhiteSpace::Normal,
345 };
346 EditorElement::new(
347 &self.api_key,
348 EditorStyle {
349 background: cx.theme().colors().editor_background,
350 local_player: cx.theme().players().local(),
351 text: text_style,
352 ..Default::default()
353 },
354 )
355 }
356}
357
358impl Render for AuthenticationPrompt {
359 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
360 const INSTRUCTIONS: [&str; 6] = [
361 "To use the assistant panel or inline assistant, you need to add your OpenAI API key.",
362 " - You can create an API key at: platform.openai.com/api-keys",
363 " - Make sure your OpenAI account has credits",
364 " - Having a subscription for another service like GitHub Copilot won't work.",
365 "",
366 "Paste your OpenAI API key below and hit enter to use the assistant:",
367 ];
368
369 v_flex()
370 .p_4()
371 .size_full()
372 .on_action(cx.listener(Self::save_api_key))
373 .children(
374 INSTRUCTIONS.map(|instruction| Label::new(instruction).size(LabelSize::Small)),
375 )
376 .child(
377 h_flex()
378 .w_full()
379 .my_2()
380 .px_2()
381 .py_1()
382 .bg(cx.theme().colors().editor_background)
383 .rounded_md()
384 .child(self.render_api_key_editor(cx)),
385 )
386 .child(
387 Label::new(
388 "You can also assign the OPENAI_API_KEY environment variable and restart Zed.",
389 )
390 .size(LabelSize::Small),
391 )
392 .child(
393 h_flex()
394 .gap_2()
395 .child(Label::new("Click on").size(LabelSize::Small))
396 .child(Icon::new(IconName::ZedAssistant).size(IconSize::XSmall))
397 .child(
398 Label::new("in the status bar to close this panel.").size(LabelSize::Small),
399 ),
400 )
401 .into_any()
402 }
403}