History

add base model to EmbeddingProvider, not yet leveraged for truncation

KCaverly committed 3 years ago

Commit 2b780ee1 parent d1dec835 files · +57 −2Browse files

Unified · Split

5 files
  1. crates/ai/src/embedding.rs +3 −0
  2. crates/ai/src/providers/dummy.rs +35 −0
  3. crates/ai/src/providers/open_ai/embedding.rs +10 −0
  4. crates/ai/src/providers/open_ai/model.rs +1 −0
  5. crates/semantic_index/src/semantic_index_tests.rs +8 −2
crates/ai/src/embedding.rs+3−0
@@ -5,6 +5,8 @@ use rusqlite::types::{FromSql, FromSqlResult, ToSqlOutput, ValueRef}; · 4 unmodified lines
55 use rusqlite::ToSql;
66 use std::time::Instant;
77
8+use crate::models::LanguageModel;
9+
810 #[derive(Debug, PartialEq, Clone)]
911 pub struct Embedding(pub Vec<f32>);
1012
@@ -66,6 +68,7 @@ impl Embedding { · 55 unmodified lines
6668
6769 #[async_trait]
6870 pub trait EmbeddingProvider: Sync + Send {
71+ fn base_model(&self) -> Box<dyn LanguageModel>;
6972 fn is_authenticated(&self) -> bool;
7073 async fn embed_batch(&self, spans: Vec<String>) -> Result<Vec<Embedding>>;
7174 fn max_tokens_per_batch(&self) -> usize;
crates/ai/src/providers/dummy.rs+35−0
@@ -3,10 +3,42 @@ use std::time::Instant; · 2 unmodified lines
33 use crate::{
44 completion::CompletionRequest,
55 embedding::{Embedding, EmbeddingProvider},
6+ models::{LanguageModel, TruncationDirection},
67 };
78 use async_trait::async_trait;
89 use serde::Serialize;
910
11+pub struct DummyLanguageModel {}
12+
13+impl LanguageModel for DummyLanguageModel {
14+ fn name(&self) -> String {
15+ "dummy".to_string()
16+ }
17+ fn capacity(&self) -> anyhow::Result<usize> {
18+ anyhow::Ok(1000)
19+ }
20+ fn truncate(
21+ &self,
22+ content: &str,
23+ length: usize,
24+ direction: crate::models::TruncationDirection,
25+ ) -> anyhow::Result<String> {
26+ let truncated = match direction {
27+ TruncationDirection::End => content.chars().collect::<Vec<char>>()[..length]
28+ .iter()
29+ .collect::<String>(),
30+ TruncationDirection::Start => content.chars().collect::<Vec<char>>()[..length]
31+ .iter()
32+ .collect::<String>(),
33+ };
34+
35+ anyhow::Ok(truncated)
36+ }
37+ fn count_tokens(&self, content: &str) -> anyhow::Result<usize> {
38+ anyhow::Ok(content.chars().collect::<Vec<char>>().len())
39+ }
40+}
41+
1042 #[derive(Serialize)]
1143 pub struct DummyCompletionRequest {
1244 pub name: String,
@@ -22,6 +54,9 @@ pub struct DummyEmbeddingProvider {} · 9 unmodified lines
2254
2355 #[async_trait]
2456 impl EmbeddingProvider for DummyEmbeddingProvider {
57+ fn base_model(&self) -> Box<dyn LanguageModel> {
58+ Box::new(DummyLanguageModel {})
59+ }
2560 fn is_authenticated(&self) -> bool {
2661 true
2762 }
crates/ai/src/providers/open_ai/embedding.rs+10−0
@@ -19,6 +19,8 @@ use tiktoken_rs::{cl100k_base, CoreBPE}; · 18 unmodified lines
1919 use util::http::{HttpClient, Request};
2020
2121 use crate::embedding::{Embedding, EmbeddingProvider};
22+use crate::models::LanguageModel;
23+use crate::providers::open_ai::OpenAILanguageModel;
2224
2325 lazy_static! {
2426 static ref OPENAI_API_KEY: Option<String> = env::var("OPENAI_API_KEY").ok();
@@ -27,6 +29,7 @@ lazy_static! { · 2 unmodified lines
2729
2830 #[derive(Clone)]
2931 pub struct OpenAIEmbeddingProvider {
32+ model: OpenAILanguageModel,
3033 pub client: Arc<dyn HttpClient>,
3134 pub executor: Arc<Background>,
3235 rate_limit_count_rx: watch::Receiver<Option<Instant>>,
@@ -65,7 +68,10 @@ impl OpenAIEmbeddingProvider { · 32 unmodified lines
6568 let (rate_limit_count_tx, rate_limit_count_rx) = watch::channel_with(None);
6669 let rate_limit_count_tx = Arc::new(Mutex::new(rate_limit_count_tx));
6770
71+ let model = OpenAILanguageModel::load("text-embedding-ada-002");
72+
6873 OpenAIEmbeddingProvider {
74+ model,
6975 client,
7076 executor,
7177 rate_limit_count_rx,
@@ -131,6 +137,10 @@ impl OpenAIEmbeddingProvider { · 59 unmodified lines
131137
132138 #[async_trait]
133139 impl EmbeddingProvider for OpenAIEmbeddingProvider {
140+ fn base_model(&self) -> Box<dyn LanguageModel> {
141+ let model: Box<dyn LanguageModel> = Box::new(self.model.clone());
142+ model
143+ }
134144 fn is_authenticated(&self) -> bool {
135145 OPENAI_API_KEY.as_ref().is_some()
136146 }
crates/ai/src/providers/open_ai/model.rs+1−0
@@ -4,6 +4,7 @@ use util::ResultExt; · 3 unmodified lines
44
55 use crate::models::{LanguageModel, TruncationDirection};
66
7+#[derive(Clone)]
78 pub struct OpenAILanguageModel {
89 name: String,
910 bpe: Option<CoreBPE>,
crates/semantic_index/src/semantic_index_tests.rs+8−2
@@ -4,8 +4,11 @@ use crate::{ · 3 unmodified lines
44 semantic_index_settings::SemanticIndexSettings,
55 FileToEmbed, JobHandle, SearchResult, SemanticIndex, EMBEDDING_QUEUE_FLUSH_TIMEOUT,
66 };
7−use ai::embedding::{Embedding, EmbeddingProvider};
8−use ai::providers::dummy::DummyEmbeddingProvider;
7+use ai::providers::dummy::{DummyEmbeddingProvider, DummyLanguageModel};
8+use ai::{
9+ embedding::{Embedding, EmbeddingProvider},
10+ models::LanguageModel,
11+};
912 use anyhow::Result;
1013 use async_trait::async_trait;
1114 use gpui::{executor::Deterministic, Task, TestAppContext};
@@ -1282,6 +1285,9 @@ impl FakeEmbeddingProvider { · 1270 unmodified lines
12821285
12831286 #[async_trait]
12841287 impl EmbeddingProvider for FakeEmbeddingProvider {
1288+ fn base_model(&self) -> Box<dyn LanguageModel> {
1289+ Box::new(DummyLanguageModel {})
1290+ }
12851291 fn is_authenticated(&self) -> bool {
12861292 true
12871293 }

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