-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add embedding provider for VoyageAI.
Fixes: #152
- Loading branch information
Showing
7 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
use reqwest::Client; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::{EmbeddingProvider, GenericEmbeddingRequest, GenericEmbeddingResponse}; | ||
use crate::errors::VectorizeError; | ||
use crate::transformers::http_handler::handle_response; | ||
use async_trait::async_trait; | ||
use std::env; | ||
|
||
pub const VOYAGE_BASE_URL: &str = "https://api.voyageai.com/v1"; | ||
|
||
pub struct VoyageProvider { | ||
pub url: String, | ||
pub api_key: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct VoyageEmbeddingBody { | ||
pub input: Vec<String>, | ||
pub model: String, | ||
pub input_type: String, | ||
} | ||
|
||
impl From<GenericEmbeddingRequest> for VoyageEmbeddingBody { | ||
fn from(request: GenericEmbeddingRequest) -> Self { | ||
VoyageEmbeddingBody { | ||
input: request.input, | ||
model: request.model, | ||
input_type: "document".to_string(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct VoyageEmbeddingResponse { | ||
pub data: Vec<EmbeddingObject>, | ||
} | ||
|
||
#[derive(Clone, Serialize, Deserialize, Debug)] | ||
pub struct EmbeddingObject { | ||
pub embedding: Vec<f64>, | ||
} | ||
|
||
impl From<VoyageEmbeddingResponse> for GenericEmbeddingResponse { | ||
fn from(response: VoyageEmbeddingResponse) -> Self { | ||
GenericEmbeddingResponse { | ||
embeddings: response.data.iter().map(|x| x.embedding.clone()).collect(), | ||
} | ||
} | ||
} | ||
|
||
impl VoyageProvider { | ||
pub fn new(url: Option<String>, api_key: Option<String>) -> Self { | ||
let final_url = match url { | ||
Some(url) => url, | ||
None => VOYAGE_BASE_URL.to_string(), | ||
}; | ||
let final_api_key = match api_key { | ||
Some(api_key) => api_key, | ||
None => env::var("VOYAGE_API_KEY").expect("VOYAGE_API_KEY not set"), | ||
}; | ||
VoyageProvider { | ||
url: final_url, | ||
api_key: final_api_key, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl EmbeddingProvider for VoyageProvider { | ||
async fn generate_embedding<'a>( | ||
&self, | ||
request: &'a GenericEmbeddingRequest, | ||
) -> Result<GenericEmbeddingResponse, VectorizeError> { | ||
let client = Client::new(); | ||
|
||
let req_body = VoyageEmbeddingBody::from(request.clone()); | ||
let embedding_url = format!("{}/embeddings", self.url); | ||
|
||
let response = client | ||
.post(&embedding_url) | ||
.timeout(std::time::Duration::from_secs(120_u64)) | ||
.header("Content-Type", "application/json") | ||
.header("Authorization", format!("Bearer {}", self.api_key)) | ||
.json(&req_body) | ||
.send() | ||
.await?; | ||
|
||
let embeddings = handle_response::<VoyageEmbeddingResponse>(response, "embeddings").await?; | ||
Ok(GenericEmbeddingResponse { | ||
embeddings: embeddings | ||
.data | ||
.iter() | ||
.map(|x| x.embedding.clone()) | ||
.collect(), | ||
}) | ||
} | ||
|
||
async fn model_dim(&self, model_name: &str) -> Result<u32, VectorizeError> { | ||
Ok(voyager_embedding_dim(model_name) as u32) | ||
} | ||
} | ||
|
||
pub fn voyager_embedding_dim(model_name: &str) -> i32 { | ||
match model_name { | ||
"voyage-3-lite" => 512, | ||
"voyage-3" | "voyage-finance-2" | "voyage-multilingual-2" | "voyage-law-2" => 1024, | ||
"voyage-code-2" => 1536, | ||
// older models | ||
"voyage-large-2" => 1536, | ||
"voyage-large-2-instruct" | ||
| "voyage-2" | ||
| "voyage-lite-02-instruct" | ||
| "voyage-02" | ||
| "voyage-01" | ||
| "voyage-lite-01" | ||
| "voyage-lite-01-instruct" => 1024, | ||
_ => 1536, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod integration_tests { | ||
use super::*; | ||
use std::env; | ||
|
||
#[tokio::test] | ||
async fn test_voyage_ai_embedding() { | ||
let api_key = Some(env::var("VOYAGE_API_KEY").expect("VOYAGE_API_KEY must be set")); | ||
let provider = VoyageProvider::new(Some(VOYAGE_BASE_URL.to_string()), api_key); | ||
|
||
let request = GenericEmbeddingRequest { | ||
input: vec!["hello world".to_string()], | ||
model: "voyage-3-lite".to_string(), | ||
}; | ||
|
||
let embeddings = provider.generate_embedding(&request).await.unwrap(); | ||
println!("{:?}", embeddings); | ||
assert!( | ||
!embeddings.embeddings.is_empty(), | ||
"Embeddings should not be empty" | ||
); | ||
assert!( | ||
embeddings.embeddings.len() == 1, | ||
"Embeddings should have length 1" | ||
); | ||
assert!( | ||
embeddings.embeddings[0].len() == 512, | ||
"Embeddings should have dimension 512" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters