Skip to main content

ds_api/raw/response/non_streaming/
mod.rs

1pub mod chat_completion_response;
2pub mod choice;
3pub mod finish_reason;
4pub mod logprobs;
5pub mod object_type;
6pub mod usage;
7
8pub use chat_completion_response::ChatCompletionResponse;
9pub use choice::Choice;
10pub use finish_reason::FinishReason;
11pub use logprobs::{Logprobs, TokenLogprob, TopLogprob};
12pub use object_type::ObjectType;
13pub use usage::{CompletionTokensDetails, Usage};
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[test]
20    fn test_usage_deserialization() {
21        let json = r#"
22            {
23              "id": "dbdd2075-d78a-494a-afc9-b9ec5dc6bb64",
24              "object": "chat.completion",
25              "created": 1770982234,
26              "model": "deepseek-chat",
27              "choices": [
28                {
29                  "index": 0,
30                  "message": {
31                    "role": "assistant",
32                    "content": "Hello! How can I assist you today? 😊"
33                  },
34                  "logprobs": null,
35                  "finish_reason": "stop"
36                }
37              ],
38              "usage": {
39                "prompt_tokens": 10,
40                "completion_tokens": 11,
41                "total_tokens": 21,
42                "prompt_tokens_details": {
43                  "cached_tokens": 0
44                },
45                "prompt_cache_hit_tokens": 0,
46                "prompt_cache_miss_tokens": 10
47              },
48              "system_fingerprint": "fp_eaab8d114b_prod0820_fp8_kvcache"
49            }
50        "#;
51
52        let response: ChatCompletionResponse = serde_json::from_str(json).unwrap();
53
54        assert_eq!(
55            response.id,
56            "dbdd2075-d78a-494a-afc9-b9ec5dc6bb64".to_string()
57        );
58        assert!(matches!(response.object, ObjectType::ChatCompletion));
59        assert_eq!(response.created, 1770982234);
60        assert!(matches!(response.model, crate::raw::Model::DeepseekChat));
61        assert_eq!(response.choices.len(), 1);
62        assert_eq!(response.choices[0].index, 0);
63        assert!(matches!(
64            response.choices[0].message.role,
65            crate::raw::request::message::Role::Assistant
66        ));
67        assert_eq!(
68            response.choices[0].message.content.as_ref().unwrap(),
69            "Hello! How can I assist you today? 😊"
70        );
71        assert!(response.choices[0].logprobs.is_none());
72        assert!(matches!(
73            response.choices[0].finish_reason,
74            FinishReason::Stop
75        ));
76
77        assert_eq!(response.usage.prompt_tokens, 10);
78        assert_eq!(response.usage.completion_tokens, 11);
79        assert_eq!(response.usage.total_tokens, 21);
80        assert_eq!(response.usage.prompt_cache_hit_tokens.unwrap(), 0);
81        assert_eq!(response.usage.prompt_cache_miss_tokens.unwrap(), 10);
82    }
83}