deepseekClient_rs/chat/
response.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Debug, Clone)]
4pub struct ChatResponse {
5    // 该对话的唯一标识符。
6    id: String,
7    // 模型生成的 completion 的选择列表。
8    pub choices: Vec<Choice>,
9    // 创建聊天完成时的 Unix 时间戳(以秒为单位)。
10    created: isize,
11    // 生成该 completion 的模型名。
12    model: String,
13    // This fingerprint represents the backend configuration that the model runs with
14    system_fingerprint: String,
15    // 对象的类型, 其值为 chat.completion。
16    object: String,
17    // 该对话补全请求的用量信息。
18    usage: Option<Usage>,
19}
20
21impl ChatResponse {
22    pub fn content(&self) -> Vec<&str> {
23        self.choices.iter().map(|c| c.content()).collect()
24    }
25    pub fn role(&self) -> Vec<&str> {
26        self.choices.iter().map(|c| c.role()).collect()
27    }
28}
29
30#[derive(Serialize, Deserialize, Debug, Clone)]
31pub struct Choice {
32    // 模型停止生成 token 的原因。
33    // stop:模型自然停止生成,或遇到 stop 序列中列出的字符串。
34    // length :输出长度达到了模型上下文长度限制,或达到了 max_tokens 的限制。
35    // content_filter:输出内容因触发过滤策略而被过滤。
36    // insufficient_system_resource:系统推理资源不足,生成被打断。
37    finish_reason: String,
38    // 该 completion 在模型生成的 completion 的选择列表中的索引。
39    index: usize,
40    // 模型生成的 completion 消息。
41    #[serde(rename = "message")]
42    response_content: ResponseMessage,
43    // 该 choice 的对数概率信息。
44    logprobs: Option<Logprobs>,
45}
46
47impl Choice {
48    pub fn content(&self) -> &str {
49        self.response_content.cotent()
50    }
51    pub fn role(&self) -> &str {
52        self.response_content.role()
53    }
54}
55
56#[derive(Serialize, Deserialize, Debug, Clone)]
57pub struct ResponseMessage {
58    // 该 completion 的内容。
59    #[serde(skip_serializing_if = "Option::is_none")]
60    content: Option<String>,
61    // 仅适用于 deepseek-reasoner 模型。内容为 assistant 消息中在最终答案之前的推理内容。
62    reasoning_content: Option<String>,
63    // 模型生成的 tool 调用,例如 function 调用。
64    tool_calls: Option<Vec<ToolCall>>,
65    // 生成这条消息的角色。
66    role: String,
67}
68
69impl ResponseMessage {
70    pub fn cotent(&self) -> &str {
71        self.content.as_deref().unwrap_or("没有回复content")
72    }
73
74    pub fn role(&self) -> &str {
75        self.role.as_str()
76    }
77}
78
79#[derive(Serialize, Deserialize, Debug, Clone)]
80pub struct ToolCall {
81    // tool 调用的 ID。
82    id: String,
83    // tool 的类型。目前仅支持 function。
84    #[serde(rename = "type")]
85    type_name: String,
86    #[serde(rename = "function")]
87    response_function: ResponseFunction,
88}
89
90#[derive(Serialize, Deserialize, Debug, Clone)]
91pub struct ResponseFunction {
92    // 模型调用的 function。
93    name: String,
94    // 要调用的 function 的参数,由模型生成,格式为 JSON。请注意,模型并不总是生成有效的 JSON,
95    // 并且可能会臆造出你函数模式中未定义的参数。在调用函数之前,请在代码中验证这些参数。
96    arguments: String,
97}
98
99#[derive(Serialize, Deserialize, Debug, Clone)]
100pub struct Logprobs {
101    // 一个包含输出 token 对数概率信息的列表。
102    content: LogprobsContent,
103}
104
105#[derive(Serialize, Deserialize, Debug, Clone)]
106pub struct LogprobsContent {
107    // 输出的 token。
108    tokens: String,
109    // 该 token 的对数概率。-9999.0 代表该 token 的输出概率极小,不在 top 20 最可能输出的 token 中。
110    logprob: isize,
111    // 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。如果 token 没有对应的字节表示,则该值为 null。
112    bytes: Option<isize>,
113    // 一个包含在该输出位置上,输出概率 top N 的 token 的列表,以及它们的对数概率。
114    // 在罕见情况下,返回的 token 数量可能少于请求参数中指定的 top_logprobs 值。
115    top_logprobs: TopLogprobs,
116}
117
118#[derive(Serialize, Deserialize, Debug, Clone)]
119pub struct TopLogprobs {
120    // token。
121    token: String,
122    // 该 token 的对数概率。
123    logprob: isize,
124    // 一个包含该 token UTF-8 字节表示的整数列表。一般在一个 UTF-8 字符被拆分成多个 token 来表示时有用。
125    // 如果 token 没有对应的字节表示,则该值为 null。
126    bytes: Option<isize>,
127}
128
129#[derive(Serialize, Deserialize, Debug, Clone)]
130pub struct Usage {
131    // 模型 completion 产生的 token 数。
132    completion_tokens: isize,
133    // 用户 prompt 所包含的 token 数。该值等于 prompt_cache_hit_tokens + prompt_cache_miss_tokens
134    prompt_tokens: isize,
135    // 用户 prompt 中,命中上下文缓存的 token 数。
136    prompt_cache_hit_tokens: isize,
137    // 用户 prompt 中,未命中上下文缓存的 token 数。
138    prompt_cache_miss_tokens: isize,
139    // 请求的 token 数量。
140    total_tokens: usize,
141    // 该请求中,所有 token 的数量(prompt + completion)
142    prompt_tokens_details: PormptTokensDetails,
143}
144
145#[derive(Serialize, Deserialize, Debug, Clone)]
146pub struct PormptTokensDetails {
147    // 推理模型所产生的思维链 token 数量
148    cached_tokens: isize,
149}