zai_rs/model/text_embedded/
data.rs1use super::request::{EmbeddingBody, EmbeddingDimensions, EmbeddingInput, EmbeddingModel};
2use super::response::EmbeddingResponse;
3use crate::client::http::HttpClient;
4
5pub struct EmbeddingRequest {
7 pub key: String,
8 body: EmbeddingBody,
9}
10
11impl EmbeddingRequest {
12 pub fn new(key: String, model: EmbeddingModel, input: EmbeddingInput) -> Self {
13 let body = EmbeddingBody::new(model, input);
14 Self { key, body }
15 }
16
17 pub fn with_dimensions(mut self, dims: EmbeddingDimensions) -> Self {
18 self.body = self.body.with_dimensions(dims);
19 self
20 }
21
22 pub fn validate(&self) -> Result<(), validator::ValidationError> {
24 self.body.validate_model_constraints()
25 }
26
27 pub async fn send(&self) -> anyhow::Result<EmbeddingResponse> {
30 if let Err(e) = self.validate() {
31 return Err(anyhow::anyhow!("validation failed: {}", e));
32 }
33 let resp: reqwest::Response = self.post().await?;
34 let parsed = resp.json::<EmbeddingResponse>().await?;
35 Ok(parsed)
36 }
37
38 #[deprecated(note = "Use send() instead")]
39 pub async fn execute(&self) -> anyhow::Result<EmbeddingResponse> {
41 self.send().await
42 }
43}
44
45impl HttpClient for EmbeddingRequest {
46 type Body = EmbeddingBody;
47 type ApiUrl = &'static str;
48 type ApiKey = String;
49
50 fn api_url(&self) -> &Self::ApiUrl {
51 &"https://open.bigmodel.cn/api/paas/v4/embeddings"
52 }
53 fn api_key(&self) -> &Self::ApiKey {
54 &self.key
55 }
56 fn body(&self) -> &Self::Body {
57 &self.body
58 }
59}