Skip to main content

zai_rs/knowledge/
document_reembedding.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use validator::Validate;
5
6use crate::{ZaiResult, client::http::HttpClient};
7
8/// Request body for re-embedding a document
9#[derive(Debug, Clone, Serialize, Deserialize, Validate, Default)]
10pub struct DocumentReembeddingBody {
11    /// Optional callback URL that will be called when re-embedding completes
12    #[serde(skip_serializing_if = "Option::is_none")]
13    #[validate(url)]
14    pub callback_url: Option<String>,
15    /// Optional callback headers key-value pairs
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub callback_header: Option<BTreeMap<String, String>>,
18}
19
20/// Re-embedding request (POST /llm-application/open/document/embedding/{id})
21pub struct DocumentReembeddingRequest {
22    /// Bearer API key
23    pub key: String,
24    url: String,
25    body: DocumentReembeddingBody,
26}
27
28impl DocumentReembeddingRequest {
29    /// Create a new request for the specified document id
30    pub fn new(key: String, document_id: impl AsRef<str>) -> Self {
31        let url = format!(
32            "https://open.bigmodel.cn/api/llm-application/open/document/embedding/{}",
33            document_id.as_ref()
34        );
35        Self {
36            key,
37            url,
38            body: DocumentReembeddingBody::default(),
39        }
40    }
41
42    /// Set callback URL
43    pub fn with_callback_url(mut self, url: impl Into<String>) -> Self {
44        self.body.callback_url = Some(url.into());
45        self
46    }
47
48    /// Set callback headers
49    pub fn with_callback_header(mut self, hdr: BTreeMap<String, String>) -> Self {
50        self.body.callback_header = Some(hdr);
51        self
52    }
53
54    /// Send POST request with JSON body and parse typed response
55    pub async fn send(&self) -> ZaiResult<DocumentReembeddingResponse> {
56        // validate body
57        self.body.validate()?;
58        let resp = self.post().await?;
59        let parsed = resp.json::<DocumentReembeddingResponse>().await?;
60        Ok(parsed)
61    }
62}
63
64impl HttpClient for DocumentReembeddingRequest {
65    type Body = DocumentReembeddingBody;
66    type ApiUrl = String;
67    type ApiKey = String;
68
69    fn api_url(&self) -> &Self::ApiUrl {
70        &self.url
71    }
72    fn api_key(&self) -> &Self::ApiKey {
73        &self.key
74    }
75    fn body(&self) -> &Self::Body {
76        &self.body
77    }
78}
79
80/// Simple response envelope: { code, message, timestamp }
81#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
82pub struct DocumentReembeddingResponse {
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub code: Option<i64>,
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub message: Option<String>,
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub timestamp: Option<u64>,
89}