zai_rs/knowledge/
document_reembedding.rs1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use validator::Validate;
5
6use crate::{ZaiResult, client::http::HttpClient};
7
8#[derive(Debug, Clone, Serialize, Deserialize, Validate, Default)]
10pub struct DocumentReembeddingBody {
11 #[serde(skip_serializing_if = "Option::is_none")]
13 #[validate(url)]
14 pub callback_url: Option<String>,
15 #[serde(skip_serializing_if = "Option::is_none")]
17 pub callback_header: Option<BTreeMap<String, String>>,
18}
19
20pub struct DocumentReembeddingRequest {
22 pub key: String,
24 url: String,
25 body: DocumentReembeddingBody,
26}
27
28impl DocumentReembeddingRequest {
29 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 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 pub fn with_callback_header(mut self, hdr: BTreeMap<String, String>) -> Self {
50 self.body.callback_header = Some(hdr);
51 self
52 }
53
54 pub async fn send(&self) -> ZaiResult<DocumentReembeddingResponse> {
56 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#[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}