History

Slightly tidy up vector_db code

Avoid panicking when truncating code with special chars

Kirill Bulatov committed 3 years ago

Commit a884bd71 parent 96abba22 files · +9 −10Browse files

Showing the changes to crates (2 of 2 files). Show all

Unified · Split

2 files
  1. crates/vector_store/src/embedding.rs +7 −5
  2. crates/vector_store/src/parsing.rs +2 −5
crates/vector_store/src/embedding.rs+7−5
@@ -67,11 +67,13 @@ impl EmbeddingProvider for DummyEmbeddings { · 66 unmodified lines
6767 }
6868 }
6969
70+const INPUT_LIMIT: usize = 8190;
71+
7072 impl OpenAIEmbeddings {
71− async fn truncate(span: String) -> String {
73+ fn truncate(span: String) -> String {
7274 let mut tokens = OPENAI_BPE_TOKENIZER.encode_with_special_tokens(span.as_ref());
73− if tokens.len() > 8190 {
74− tokens.truncate(8190);
75+ if tokens.len() > INPUT_LIMIT {
76+ tokens.truncate(INPUT_LIMIT);
7577 let result = OPENAI_BPE_TOKENIZER.decode(tokens.clone());
7678 if result.is_ok() {
7779 let transformed = result.unwrap();
@@ -80,7 +82,7 @@ impl OpenAIEmbeddings { · 2 unmodified lines
8082 }
8183 }
8284
83− return span.to_string();
85+ span
8486 }
8587
8688 async fn send_request(&self, api_key: &str, spans: Vec<&str>) -> Result<Response<AsyncBody>> {
@@ -137,7 +139,7 @@ impl EmbeddingProvider for OpenAIEmbeddings { · 50 unmodified lines
137139 // Don't worry about delaying bad request, as we can assume
138140 // we haven't been rate limited yet.
139141 for span in spans.iter_mut() {
140− *span = Self::truncate(span.to_string()).await;
142+ *span = Self::truncate(span.to_string());
141143 }
142144 }
143145 StatusCode::OK => {
crates/vector_store/src/parsing.rs+2−5
@@ -63,7 +63,7 @@ impl CodeContextRetriever { · 62 unmodified lines
6363 ) {
6464 // log::info!("-----MATCH-----");
6565
66− let mut name: Vec<&str> = vec![];
66+ let mut name = Vec::new();
6767 let mut item: Option<&str> = None;
6868 let mut offset: Option<usize> = None;
6969 for capture in mat.captures {
@@ -91,11 +91,8 @@ impl CodeContextRetriever { · 21 unmodified lines
9191 .replace("<language>", &pending_file.language.name().to_lowercase())
9292 .replace("<item>", item.unwrap());
9393
94− let mut truncated_span = context_span.clone();
95− truncated_span.truncate(100);
96−
9794 // log::info!("Name: {:?}", name);
98− // log::info!("Span: {:?}", truncated_span);
95+ // log::info!("Span: {:?}", util::truncate(&context_span, 100));
9996
10097 context_spans.push(context_span);
10198 documents.push(Document {

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