use crate::client::LlmClient;
use crate::error::Error;
pub async fn embed(client: &dyn LlmClient, text: &str) -> Result<Vec<f32>, Error> {
client.embed(text).await
}
#[cfg(test)]
mod tests {
use super::*;
use async_trait::async_trait;
use crate::client::{CompletionRequest, TokenStream};
struct OkClient;
#[async_trait]
impl LlmClient for OkClient {
fn default_model(&self) -> &str {
"test"
}
async fn complete(&self, _: CompletionRequest) -> Result<String, Error> {
Err(Error::Unsupported)
}
async fn complete_stream(&self, _: CompletionRequest) -> Result<TokenStream, Error> {
Err(Error::Unsupported)
}
async fn embed(&self, _: &str) -> Result<Vec<f32>, Error> {
Ok(vec![0.1, 0.2, 0.3])
}
}
struct UnsupportedClient;
#[async_trait]
impl LlmClient for UnsupportedClient {
fn default_model(&self) -> &str {
"test"
}
async fn complete(&self, _: CompletionRequest) -> Result<String, Error> {
Err(Error::Unsupported)
}
async fn complete_stream(&self, _: CompletionRequest) -> Result<TokenStream, Error> {
Err(Error::Unsupported)
}
async fn embed(&self, _: &str) -> Result<Vec<f32>, Error> {
Err(Error::Unsupported)
}
}
#[tokio::test]
async fn embed_delegates_to_client() {
let client = OkClient;
let result = embed(&client, "hello").await.unwrap();
assert_eq!(result, vec![0.1f32, 0.2, 0.3]);
}
#[tokio::test]
async fn embed_propagates_unsupported() {
let client = UnsupportedClient;
let result = embed(&client, "hello").await;
assert!(
matches!(result, Err(Error::Unsupported)),
"expected Err(Error::Unsupported), got: {result:?}"
);
}
}