History

Replace `lazy_static!` with `OnceLock` in `ai` crate (#8647)

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

Marshall Bowers committed 2 years ago

Commit 0d0ce951 parent eb1ab694 files · +7 −9Browse files

Unified · Split

4 files
  1. Cargo.lock +0 −1
  2. crates/ai/Cargo.toml +0 −1
  3. crates/ai/src/providers/open_ai/embedding.rs +4 −4
  4. crates/ai/src/providers/open_ai/model.rs +3 −3
Cargo.lock+0−1
@@ -84,7 +84,6 @@ dependencies = [ · 83 unmodified lines
8484 "gpui",
8585 "isahc",
8686 "language",
87− "lazy_static",
8887 "log",
8988 "matrixmultiply",
9089 "ordered-float 2.10.0",
crates/ai/Cargo.toml+0−1
@@ -20,7 +20,6 @@ futures.workspace = true · 19 unmodified lines
2020 gpui.workspace = true
2121 isahc.workspace = true
2222 language.workspace = true
23−lazy_static.workspace = true
2423 log.workspace = true
2524 matrixmultiply = "0.3.7"
2625 ordered-float.workspace = true
crates/ai/src/providers/open_ai/embedding.rs+4−4
@@ -8,7 +8,6 @@ use gpui::BackgroundExecutor; · 7 unmodified lines
88 use isahc::http::StatusCode;
99 use isahc::prelude::Configurable;
1010 use isahc::{AsyncBody, Response};
11−use lazy_static::lazy_static;
1211 use parking_lot::{Mutex, RwLock};
1312 use parse_duration::parse;
1413 use postage::watch;
@@ -16,7 +15,7 @@ use serde::{Deserialize, Serialize}; · 1 unmodified line
1615 use serde_json;
1716 use std::env;
1817 use std::ops::Add;
19−use std::sync::Arc;
18+use std::sync::{Arc, OnceLock};
2019 use std::time::{Duration, Instant};
2120 use tiktoken_rs::{cl100k_base, CoreBPE};
2221 use util::http::{HttpClient, Request};
@@ -29,8 +28,9 @@ use crate::providers::open_ai::OpenAiLanguageModel; · 6 unmodified lines
2928
3029 use crate::providers::open_ai::OPEN_AI_API_URL;
3130
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())
3434 }
3535
3636 #[derive(Clone)]
crates/ai/src/providers/open_ai/model.rs+3−3
@@ -3,7 +3,7 @@ use tiktoken_rs::CoreBPE; · 2 unmodified lines
33
44 use crate::models::{LanguageModel, TruncationDirection};
55
6−use super::OPEN_AI_BPE_TOKENIZER;
6+use super::open_ai_bpe_tokenizer;
77
88 #[derive(Clone)]
99 pub struct OpenAiLanguageModel {
@@ -13,8 +13,8 @@ pub struct OpenAiLanguageModel { · 3 unmodified lines
1313
1414 impl OpenAiLanguageModel {
1515 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());
1818 OpenAiLanguageModel {
1919 name: model_name.to_string(),
2020 bpe: Some(bpe),

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t