zai_rs/knowledge/
document_reembedding.rs

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