-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tabby): support voyage embedding api (#2355)
* feat(tabby): support voyage embedding api Signed-off-by: TennyZhuang <[email protected]> * [autofix.ci] apply automated fixes * exit if necessary args are missing Signed-off-by: TennyZhuang <[email protected]> --------- Signed-off-by: TennyZhuang <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
2d8d746
commit beb7d90
Showing
3 changed files
with
122 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use anyhow::Context; | ||
use async_trait::async_trait; | ||
use reqwest::Client; | ||
use serde::{Deserialize, Serialize}; | ||
use tabby_inference::Embedding; | ||
|
||
const DEFAULT_VOYAGE_API_ENDPOINT: &str = "https://api.voyageai.com"; | ||
|
||
pub struct VoyageEmbeddingEngine { | ||
client: Client, | ||
api_endpoint: String, | ||
api_key: String, | ||
model_name: String, | ||
} | ||
|
||
impl VoyageEmbeddingEngine { | ||
pub fn create(api_endpoint: &str, model_name: &str, api_key: String) -> Self { | ||
let endpoint = if api_endpoint.is_empty() { | ||
DEFAULT_VOYAGE_API_ENDPOINT | ||
} else { | ||
api_endpoint | ||
}; | ||
let client = Client::new(); | ||
Self { | ||
client, | ||
api_endpoint: format!("{}/v1/embeddings", endpoint), | ||
api_key, | ||
model_name: model_name.to_owned(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize)] | ||
struct EmbeddingRequest { | ||
input: Vec<String>, | ||
model: String, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct EmbeddingResponse { | ||
data: Vec<EmbeddingData>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct EmbeddingData { | ||
embedding: Vec<f32>, | ||
} | ||
|
||
#[async_trait] | ||
impl Embedding for VoyageEmbeddingEngine { | ||
async fn embed(&self, prompt: &str) -> anyhow::Result<Vec<f32>> { | ||
let request = EmbeddingRequest { | ||
input: vec![prompt.to_owned()], | ||
model: self.model_name.clone(), | ||
}; | ||
|
||
let request_builder = self | ||
.client | ||
.post(&self.api_endpoint) | ||
.json(&request) | ||
.header("content-type", "application/json") | ||
.bearer_auth(&self.api_key); | ||
|
||
let response = request_builder.send().await?; | ||
if response.status().is_server_error() { | ||
let error = response.text().await?; | ||
return Err(anyhow::anyhow!("Error from server: {}", error)); | ||
} | ||
|
||
let response_body = response | ||
.json::<EmbeddingResponse>() | ||
.await | ||
.context("Failed to parse response body")?; | ||
|
||
response_body | ||
.data | ||
.into_iter() | ||
.next() | ||
.map(|data| data.embedding) | ||
.ok_or_else(|| anyhow::anyhow!("No embedding data found")) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
/// Make sure you have set the VOYAGE_API_KEY environment variable before running the test | ||
#[tokio::test] | ||
#[ignore] | ||
async fn test_voyage_embedding() { | ||
let api_key = std::env::var("VOYAGE_API_KEY").expect("VOYAGE_API_KEY must be set"); | ||
let engine = | ||
VoyageEmbeddingEngine::create(DEFAULT_VOYAGE_API_ENDPOINT, "voyage-code-2", api_key); | ||
let embedding = engine.embed("Hello, world!").await.unwrap(); | ||
assert_eq!(embedding.len(), 1536); | ||
} | ||
} |
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