Skip to main content

zai_rs/model/text_tokenizer/
data.rs

1use super::{
2    request::{TokenizerBody, TokenizerMessage, TokenizerModel},
3    response::TokenizerResponse,
4};
5use crate::client::ZaiClient;
6
7/// Text Tokenizer request client (JSON POST)
8///
9/// Builder for the tokenizer endpoint. Construct with
10/// [`TokenizerRequest::new`], tune with the `with_*` methods, then call
11/// [`TokenizerRequest::send_via`].
12pub struct TokenizerRequest {
13    body: TokenizerBody,
14}
15
16impl TokenizerRequest {
17    /// Create a new tokenizer request for the given model and messages.
18    pub fn new(model: TokenizerModel, messages: Vec<TokenizerMessage>) -> Self {
19        let body = TokenizerBody::new(model, messages);
20        Self { body }
21    }
22
23    /// Set the client-side request id.
24    pub fn with_request_id(mut self, v: impl Into<String>) -> Self {
25        self.body = self.body.with_request_id(v);
26        self
27    }
28    /// Set the end-user id.
29    pub fn with_user_id(mut self, v: impl Into<String>) -> Self {
30        self.body = self.body.with_user_id(v);
31        self
32    }
33
34    /// Validate request constraints before sending.
35    pub fn validate(&self) -> crate::ZaiResult<()> {
36        self.body.validate()
37    }
38
39    /// Send via a [`ZaiClient`] and parse the typed response.
40    /// Automatically runs `validate()` before sending.
41    pub async fn send_via(&self, client: &ZaiClient) -> crate::ZaiResult<TokenizerResponse> {
42        self.validate()?;
43        let route = crate::client::routes::TOKENIZER_COUNT;
44        let url = client.endpoints().resolve_route(route, &[])?;
45        client
46            .send_json::<_, TokenizerResponse>(route.method(), url, &self.body)
47            .await
48    }
49}