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 | 84 | "gpui", |
| 85 | 85 | "isahc", |
| 86 | 86 | "language", |
| 87 | − "lazy_static", | |
| 88 | 87 | "log", |
| 89 | 88 | "matrixmultiply", |
| 90 | 89 | "ordered-float 2.10.0", |
| @@ -20,7 +20,6 @@ futures.workspace = true · 19 unmodified lines | ||
| 20 | 20 | gpui.workspace = true |
| 21 | 21 | isahc.workspace = true |
| 22 | 22 | language.workspace = true |
| 23 | −lazy_static.workspace = true | |
| 24 | 23 | log.workspace = true |
| 25 | 24 | matrixmultiply = "0.3.7" |
| 26 | 25 | ordered-float.workspace = true |
| @@ -8,7 +8,6 @@ use gpui::BackgroundExecutor; · 7 unmodified lines | ||
| 8 | 8 | use isahc::http::StatusCode; |
| 9 | 9 | use isahc::prelude::Configurable; |
| 10 | 10 | use isahc::{AsyncBody, Response}; |
| 11 | −use lazy_static::lazy_static; | |
| 12 | 11 | use parking_lot::{Mutex, RwLock}; |
| 13 | 12 | use parse_duration::parse; |
| 14 | 13 | use postage::watch; |
| @@ -16,7 +15,7 @@ use serde::{Deserialize, Serialize}; · 1 unmodified line | ||
| 16 | 15 | use serde_json; |
| 17 | 16 | use std::env; |
| 18 | 17 | use std::ops::Add; |
| 19 | −use std::sync::Arc; | |
| 18 | +use std::sync::{Arc, OnceLock}; | |
| 20 | 19 | use std::time::{Duration, Instant}; |
| 21 | 20 | use tiktoken_rs::{cl100k_base, CoreBPE}; |
| 22 | 21 | use util::http::{HttpClient, Request}; |
| @@ -29,8 +28,9 @@ use crate::providers::open_ai::OpenAiLanguageModel; · 6 unmodified lines | ||
| 29 | 28 | |
| 30 | 29 | use crate::providers::open_ai::OPEN_AI_API_URL; |
| 31 | 30 | |
| 32 | −lazy_static! { | |
| 33 | − pub(crate) static ref OPEN_AI_BPE_TOKENIZER: CoreBPE = cl100k_base().unwrap(); | |
| 31 | +pub(crate) fn open_ai_bpe_tokenizer() -> &'static CoreBPE { | |
| 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 | 36 | #[derive(Clone)] |
| @@ -3,7 +3,7 @@ use tiktoken_rs::CoreBPE; · 2 unmodified lines | ||
| 3 | 3 | |
| 4 | 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 | 8 | #[derive(Clone)] |
| 9 | 9 | pub struct OpenAiLanguageModel { |
| @@ -13,8 +13,8 @@ pub struct OpenAiLanguageModel { · 3 unmodified lines | ||
| 13 | 13 | |
| 14 | 14 | impl OpenAiLanguageModel { |
| 15 | 15 | pub fn load(model_name: &str) -> Self { |
| 16 | − let bpe = | |
| 17 | − tiktoken_rs::get_bpe_from_model(model_name).unwrap_or(OPEN_AI_BPE_TOKENIZER.to_owned()); | |
| 16 | + let bpe = tiktoken_rs::get_bpe_from_model(model_name) | |
| 17 | + .unwrap_or(open_ai_bpe_tokenizer().to_owned()); | |
| 18 | 18 | OpenAiLanguageModel { |
| 19 | 19 | name: model_name.to_string(), |
| 20 | 20 | bpe: Some(bpe), |