language_model.rs

  1mod model;
  2pub mod provider;
  3mod rate_limiter;
  4mod registry;
  5mod request;
  6mod role;
  7pub mod settings;
  8
  9use anyhow::Result;
 10use client::{Client, UserStore};
 11use futures::{future::BoxFuture, stream::BoxStream, TryStreamExt as _};
 12use gpui::{
 13    AnyElement, AnyView, AppContext, AsyncAppContext, Model, SharedString, Task, WindowContext,
 14};
 15pub use model::*;
 16use project::Fs;
 17use proto::Plan;
 18pub(crate) use rate_limiter::*;
 19pub use registry::*;
 20pub use request::*;
 21pub use role::*;
 22use schemars::JsonSchema;
 23use serde::{de::DeserializeOwned, Deserialize, Serialize};
 24use std::{future::Future, sync::Arc};
 25use ui::IconName;
 26
 27pub fn init(
 28    user_store: Model<UserStore>,
 29    client: Arc<Client>,
 30    fs: Arc<dyn Fs>,
 31    cx: &mut AppContext,
 32) {
 33    settings::init(fs, cx);
 34    registry::init(user_store, client, cx);
 35}
 36
 37/// The availability of a [`LanguageModel`].
 38#[derive(Debug, PartialEq, Eq, Clone, Copy)]
 39pub enum LanguageModelAvailability {
 40    /// The language model is available to the general public.
 41    Public,
 42    /// The language model is available to users on the indicated plan.
 43    RequiresPlan(Plan),
 44}
 45
 46/// Configuration for caching language model messages.
 47#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 48pub struct LanguageModelCacheConfiguration {
 49    pub max_cache_anchors: usize,
 50    pub should_speculate: bool,
 51    pub min_total_token: usize,
 52}
 53
 54pub trait LanguageModel: Send + Sync {
 55    fn id(&self) -> LanguageModelId;
 56    fn name(&self) -> LanguageModelName;
 57    fn provider_id(&self) -> LanguageModelProviderId;
 58    fn provider_name(&self) -> LanguageModelProviderName;
 59    fn telemetry_id(&self) -> String;
 60
 61    /// Returns the availability of this language model.
 62    fn availability(&self) -> LanguageModelAvailability {
 63        LanguageModelAvailability::Public
 64    }
 65
 66    fn max_token_count(&self) -> usize;
 67    fn max_output_tokens(&self) -> Option<u32> {
 68        None
 69    }
 70
 71    fn count_tokens(
 72        &self,
 73        request: LanguageModelRequest,
 74        cx: &AppContext,
 75    ) -> BoxFuture<'static, Result<usize>>;
 76
 77    fn stream_completion(
 78        &self,
 79        request: LanguageModelRequest,
 80        cx: &AsyncAppContext,
 81    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>>;
 82
 83    fn use_any_tool(
 84        &self,
 85        request: LanguageModelRequest,
 86        name: String,
 87        description: String,
 88        schema: serde_json::Value,
 89        cx: &AsyncAppContext,
 90    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>>;
 91
 92    fn cache_configuration(&self) -> Option<LanguageModelCacheConfiguration> {
 93        None
 94    }
 95
 96    #[cfg(any(test, feature = "test-support"))]
 97    fn as_fake(&self) -> &provider::fake::FakeLanguageModel {
 98        unimplemented!()
 99    }
100}
101
102impl dyn LanguageModel {
103    pub fn use_tool<T: LanguageModelTool>(
104        &self,
105        request: LanguageModelRequest,
106        cx: &AsyncAppContext,
107    ) -> impl 'static + Future<Output = Result<T>> {
108        let schema = schemars::schema_for!(T);
109        let schema_json = serde_json::to_value(&schema).unwrap();
110        let stream = self.use_any_tool(request, T::name(), T::description(), schema_json, cx);
111        async move {
112            let stream = stream.await?;
113            let response = stream.try_collect::<String>().await?;
114            Ok(serde_json::from_str(&response)?)
115        }
116    }
117
118    pub fn use_tool_stream<T: LanguageModelTool>(
119        &self,
120        request: LanguageModelRequest,
121        cx: &AsyncAppContext,
122    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
123        let schema = schemars::schema_for!(T);
124        let schema_json = serde_json::to_value(&schema).unwrap();
125        self.use_any_tool(request, T::name(), T::description(), schema_json, cx)
126    }
127}
128
129pub trait LanguageModelTool: 'static + DeserializeOwned + JsonSchema {
130    fn name() -> String;
131    fn description() -> String;
132}
133
134pub trait LanguageModelProvider: 'static {
135    fn id(&self) -> LanguageModelProviderId;
136    fn name(&self) -> LanguageModelProviderName;
137    fn icon(&self) -> IconName {
138        IconName::ZedAssistant
139    }
140    fn provided_models(&self, cx: &AppContext) -> Vec<Arc<dyn LanguageModel>>;
141    fn load_model(&self, _model: Arc<dyn LanguageModel>, _cx: &AppContext) {}
142    fn is_authenticated(&self, cx: &AppContext) -> bool;
143    fn authenticate(&self, cx: &mut AppContext) -> Task<Result<()>>;
144    fn configuration_view(&self, cx: &mut WindowContext) -> AnyView;
145    fn must_accept_terms(&self, _cx: &AppContext) -> bool {
146        false
147    }
148    fn render_accept_terms(&self, _cx: &mut WindowContext) -> Option<AnyElement> {
149        None
150    }
151    fn reset_credentials(&self, cx: &mut AppContext) -> Task<Result<()>>;
152}
153
154pub trait LanguageModelProviderState: 'static {
155    type ObservableEntity;
156
157    fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>>;
158
159    fn subscribe<T: 'static>(
160        &self,
161        cx: &mut gpui::ModelContext<T>,
162        callback: impl Fn(&mut T, &mut gpui::ModelContext<T>) + 'static,
163    ) -> Option<gpui::Subscription> {
164        let entity = self.observable_entity()?;
165        Some(cx.observe(&entity, move |this, _, cx| {
166            callback(this, cx);
167        }))
168    }
169}
170
171#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
172pub struct LanguageModelId(pub SharedString);
173
174#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
175pub struct LanguageModelName(pub SharedString);
176
177#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
178pub struct LanguageModelProviderId(pub SharedString);
179
180#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
181pub struct LanguageModelProviderName(pub SharedString);
182
183impl From<String> for LanguageModelId {
184    fn from(value: String) -> Self {
185        Self(SharedString::from(value))
186    }
187}
188
189impl From<String> for LanguageModelName {
190    fn from(value: String) -> Self {
191        Self(SharedString::from(value))
192    }
193}
194
195impl From<String> for LanguageModelProviderId {
196    fn from(value: String) -> Self {
197        Self(SharedString::from(value))
198    }
199}
200
201impl From<String> for LanguageModelProviderName {
202    fn from(value: String) -> Self {
203        Self(SharedString::from(value))
204    }
205}