History

add should_truncate to embedding providers

KCaverly committed 3 years ago

Commit 76caea81 parent e377ada2 files · +23 −0Browse files

Unified · Split

2 files
  1. crates/semantic_index/src/embedding.rs +19 −0
  2. crates/semantic_index/src/semantic_index_tests.rs +4 −0
crates/semantic_index/src/embedding.rs+19−0
@@ -55,6 +55,7 @@ struct OpenAIEmbeddingUsage { · 54 unmodified lines
5555 pub trait EmbeddingProvider: Sync + Send {
5656 async fn embed_batch(&self, spans: Vec<&str>) -> Result<Vec<Vec<f32>>>;
5757 fn count_tokens(&self, span: &str) -> usize;
58+ fn should_truncate(&self, span: &str) -> bool;
5859 // fn truncate(&self, span: &str) -> Result<&str>;
5960 }
6061
@@ -74,6 +75,20 @@ impl EmbeddingProvider for DummyEmbeddings { · 13 unmodified lines
7475 let tokens = OPENAI_BPE_TOKENIZER.encode_with_special_tokens(span);
7576 tokens.len()
7677 }
78+
79+ fn should_truncate(&self, span: &str) -> bool {
80+ self.count_tokens(span) > OPENAI_INPUT_LIMIT
81+
82+ // let tokens = OPENAI_BPE_TOKENIZER.encode_with_special_tokens(span);
83+ // let Ok(output) = {
84+ // if tokens.len() > OPENAI_INPUT_LIMIT {
85+ // tokens.truncate(OPENAI_INPUT_LIMIT);
86+ // OPENAI_BPE_TOKENIZER.decode(tokens)
87+ // } else {
88+ // Ok(span)
89+ // }
90+ // };
91+ }
7792 }
7893
7994 const OPENAI_INPUT_LIMIT: usize = 8190;
@@ -125,6 +140,10 @@ impl EmbeddingProvider for OpenAIEmbeddings { · 45 unmodified lines
125140 tokens.len()
126141 }
127142
143+ fn should_truncate(&self, span: &str) -> bool {
144+ self.count_tokens(span) > OPENAI_INPUT_LIMIT
145+ }
146+
128147 async fn embed_batch(&self, spans: Vec<&str>) -> Result<Vec<Vec<f32>>> {
129148 const BACKOFF_SECONDS: [usize; 4] = [3, 5, 15, 45];
130149 const MAX_RETRIES: usize = 4;
crates/semantic_index/src/semantic_index_tests.rs+4−0
@@ -1228,6 +1228,10 @@ impl EmbeddingProvider for FakeEmbeddingProvider { · 1227 unmodified lines
12281228 span.len()
12291229 }
12301230
1231+ fn should_truncate(&self, span: &str) -> bool {
1232+ false
1233+ }
1234+
12311235 async fn embed_batch(&self, spans: Vec<&str>) -> Result<Vec<Vec<f32>>> {
12321236 self.embedding_count
12331237 .fetch_add(spans.len(), atomic::Ordering::SeqCst);

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