This PR replaces a `lazy_static!` usage in the `ai` crate with `OnceLock` from the standard library. This allows us to drop the `lazy_static` dependency from this crate. Release Notes: - N/A
Unified · Split
| @@ -84,7 +84,6 @@ dependencies = [ · 83 unmodified lines | |||
| 84 | "gpui", | 84 | "gpui", |
| 85 | "isahc", | 85 | "isahc", |
| 86 | "language", | 86 | "language", |
| 87 | "lazy_static", | ||
| 88 | "log", | 87 | "log", |
| 89 | "matrixmultiply", | 88 | "matrixmultiply", |
| 90 | "ordered-float 2.10.0", | 89 | "ordered-float 2.10.0", |
| @@ -20,7 +20,6 @@ futures.workspace = true · 19 unmodified lines | |||
| 20 | gpui.workspace = true | 20 | gpui.workspace = true |
| 21 | isahc.workspace = true | 21 | isahc.workspace = true |
| 22 | language.workspace = true | 22 | language.workspace = true |
| 23 | lazy_static.workspace = true | ||
| 24 | log.workspace = true | 23 | log.workspace = true |
| 25 | matrixmultiply = "0.3.7" | 24 | matrixmultiply = "0.3.7" |
| 26 | ordered-float.workspace = true | 25 | ordered-float.workspace = true |
| @@ -8,7 +8,6 @@ use gpui::BackgroundExecutor; · 7 unmodified lines | |||
| 8 | use isahc::http::StatusCode; | 8 | use isahc::http::StatusCode; |
| 9 | use isahc::prelude::Configurable; | 9 | use isahc::prelude::Configurable; |
| 10 | use isahc::{AsyncBody, Response}; | 10 | use isahc::{AsyncBody, Response}; |
| 11 | use lazy_static::lazy_static; | ||
| 12 | use parking_lot::{Mutex, RwLock}; | 11 | use parking_lot::{Mutex, RwLock}; |
| 13 | use parse_duration::parse; | 12 | use parse_duration::parse; |
| 14 | use postage::watch; | 13 | use postage::watch; |
| @@ -16,7 +15,7 @@ use serde::{Deserialize, Serialize}; · 1 unmodified line | |||
| 16 | use serde_json; | 15 | use serde_json; |
| 17 | use std::env; | 16 | use std::env; |
| 18 | use std::ops::Add; | 17 | use std::ops::Add; |
| 19 | use std::sync::Arc; | 18 | use std::sync::{Arc, OnceLock}; |
| 20 | use std::time::{Duration, Instant}; | 19 | use std::time::{Duration, Instant}; |
| 21 | use tiktoken_rs::{cl100k_base, CoreBPE}; | 20 | use tiktoken_rs::{cl100k_base, CoreBPE}; |
| 22 | use util::http::{HttpClient, Request}; | 21 | use util::http::{HttpClient, Request}; |
| @@ -29,8 +28,9 @@ use crate::providers::open_ai::OpenAiLanguageModel; · 6 unmodified lines | |||
| 29 | 28 | ||
| 30 | use crate::providers::open_ai::OPEN_AI_API_URL; | 29 | use crate::providers::open_ai::OPEN_AI_API_URL; |
| 31 | 30 | ||
| 32 | lazy_static! { | 31 | pub(crate) fn open_ai_bpe_tokenizer() -> &'static CoreBPE { |
| 33 | pub(crate) static ref OPEN_AI_BPE_TOKENIZER: CoreBPE = cl100k_base().unwrap(); | 32 | static OPEN_AI_BPE_TOKENIZER: OnceLock<CoreBPE> = OnceLock::new(); |
| 33 | OPEN_AI_BPE_TOKENIZER.get_or_init(|| cl100k_base().unwrap()) | ||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | #[derive(Clone)] | 36 | #[derive(Clone)] |
| @@ -3,7 +3,7 @@ use tiktoken_rs::CoreBPE; · 2 unmodified lines | |||
| 3 | 3 | ||
| 4 | use crate::models::{LanguageModel, TruncationDirection}; | 4 | use crate::models::{LanguageModel, TruncationDirection}; |
| 5 | 5 | ||
| 6 | use super::OPEN_AI_BPE_TOKENIZER; | 6 | use super::open_ai_bpe_tokenizer; |
| 7 | 7 | ||
| 8 | #[derive(Clone)] | 8 | #[derive(Clone)] |
| 9 | pub struct OpenAiLanguageModel { | 9 | pub struct OpenAiLanguageModel { |
| @@ -13,8 +13,8 @@ pub struct OpenAiLanguageModel { · 3 unmodified lines | |||
| 13 | 13 | ||
| 14 | impl OpenAiLanguageModel { | 14 | impl OpenAiLanguageModel { |
| 15 | pub fn load(model_name: &str) -> Self { | 15 | pub fn load(model_name: &str) -> Self { |
| 16 | let bpe = | 16 | let bpe = tiktoken_rs::get_bpe_from_model(model_name) |
| 17 | tiktoken_rs::get_bpe_from_model(model_name).unwrap_or(OPEN_AI_BPE_TOKENIZER.to_owned()); | 17 | .unwrap_or(open_ai_bpe_tokenizer().to_owned()); |
| 18 | OpenAiLanguageModel { | 18 | OpenAiLanguageModel { |
| 19 | name: model_name.to_string(), | 19 | name: model_name.to_string(), |
| 20 | bpe: Some(bpe), | 20 | bpe: Some(bpe), |