deepseek_api/response.rs
1use schemars::schema::SchemaObject;
2use serde::{de::DeserializeOwned, Deserialize, Serialize};
3use std::fmt;
4
5use crate::json_stream::JsonStream;
6
7/// Represents different types of models available in the deep seek.
8#[derive(Default, Clone, Serialize, Deserialize, Debug, PartialEq)]
9pub enum ModelType {
10 /// Default model type for chat-based interactions.
11 #[default]
12 #[serde(rename = "deepseek-chat")]
13 DeepSeekChat,
14
15 /// Model type for reasoning-based interactions.
16 #[serde(rename = "deepseek-reasoner")]
17 DeepSeekReasoner,
18}
19
20impl ModelType {
21 /// Retrieves the pricing information for the model.
22 ///
23 /// Returns a tuple containing:
24 /// - `hit_price`: Price for cache hit.
25 /// - `miss_price`: Price for cache miss.
26 /// - `output_price`: Price for output.
27 pub fn get_pricing_info(&self) -> (f32, f32, f32) {
28 match self {
29 ModelType::DeepSeekChat => (0.5, 2.0, 8.0),
30 ModelType::DeepSeekReasoner => (1.0, 4.0, 16.0),
31 }
32 }
33
34 /// Retrieves the limit information for the model.
35 ///
36 /// Returns a tuple containing:
37 /// - `context_len`: Maximum context length.
38 /// - `thought_chain_len`: Optional maximum thought chain length.
39 /// - `output_len`: Maximum output length.
40 pub fn get_limit_info(&self) -> (u32, Option<u32>, u32) {
41 match self {
42 ModelType::DeepSeekChat => (64, None, 8),
43 ModelType::DeepSeekReasoner => (64, Some(32), 8),
44 }
45 }
46}
47
48impl fmt::Display for ModelType {
49 /// Formats the model type into a human-readable string.
50 ///
51 /// The output includes context length, thought chain length (if applicable),
52 /// output length, and pricing information.
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 let (context_len, thought_chain_len, output_len) = self.get_limit_info();
55 let (hit_price, miss_price, output_price) = self.get_pricing_info();
56
57 match self {
58 ModelType::DeepSeekChat => {
59 write!(
60 f,
61 "DeepSeekChat: Context Length = {}K, Max Output Length = {}K, \nInput Price (Cache Hit) = {}元, Input Price (Cache Miss) = {}元, Output Price = {}元",
62 context_len, output_len, hit_price, miss_price, output_price
63 )
64 }
65 ModelType::DeepSeekReasoner => {
66 write!(
67 f,
68 "DeepSeekReasoner: Context Length = {}K, Max Thought Chain Length = {:?}K, Max Output Length = {}K, \nInput Price (Cache Hit) = {}元, Input Price (Cache Miss) = {}元, Output Price = {}元",
69 context_len, thought_chain_len, output_len, hit_price, miss_price, output_price
70 )
71 }
72 }
73 }
74}
75
76/// Represents a model with its associated metadata.
77#[derive(Clone, Serialize, Deserialize, Debug)]
78pub struct Model {
79 /// Unique identifier for the model.
80 pub id: String,
81 /// Type of the object.
82 pub object: String,
83 /// Owner of the model.
84 pub owned_by: String,
85}
86
87/// Response structure containing a list of models.
88#[derive(Clone, Serialize, Deserialize, Debug)]
89pub struct ModelResp {
90 /// Type of the object.
91 pub object: String,
92 /// List of models.
93 pub data: Vec<Model>,
94}
95
96/// Information about the balance of a user.
97#[derive(Clone, Serialize, Deserialize, Debug)]
98pub struct BalanceInfo {
99 /// Currency type.
100 pub currency: String,
101 /// Total balance available.
102 pub total_balance: String,
103 /// Granted balance.
104 pub granted_balance: String,
105 /// Topped up balance.
106 pub topped_up_balance: String,
107}
108
109/// Response structure containing balance information.
110#[derive(Clone, Serialize, Deserialize, Debug)]
111pub struct BalanceResp {
112 /// Indicates if the balance is available.
113 pub is_available: bool,
114 /// List of balance information.
115 pub balance_infos: Vec<BalanceInfo>,
116}
117
118/// Represents a function with its name and parameters.
119#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
120pub struct Function {
121 /// Name of the function.
122 pub name: String,
123 /// Parameters of the function.
124 pub parameters: SchemaObject,
125}
126
127/// Represents a tool call with its associated function.
128#[derive(Clone, Serialize, Deserialize, Debug)]
129pub struct ToolCall {
130 /// Unique identifier for the tool call.
131 pub id: String,
132 /// Type of the tool call.
133 pub r#type: String,
134 /// Function associated with the tool call.
135 pub function: Function,
136}
137
138/// Represents a message with its content and optional reasoning content and tool calls.
139#[derive(Clone, Serialize, Deserialize, Debug)]
140pub struct Message {
141 /// Content of the message.
142 pub content: String,
143 /// Optional reasoning content.
144 pub reasoning_content: Option<String>,
145 /// Optional list of tool calls.
146 pub tool_calls: Option<Vec<ToolCall>>,
147 /// Role of the message sender.
148 pub role: String,
149}
150
151/// Enum representing the reason for finishing a process.
152#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
153pub enum FinishReason {
154 /// Process stopped.
155 #[serde(rename = "stop")]
156 Stop,
157 /// Process finished due to length limit.
158 #[serde(rename = "length")]
159 Length,
160 /// Process finished due to content filter.
161 #[serde(rename = "content_filter")]
162 ContentFilter,
163 /// Process finished due to tool calls.
164 #[serde(rename = "tool_calls")]
165 ToolCalls,
166 /// Process finished due to insufficient system resources.
167 #[serde(rename = "insufficient_system_resource")]
168 InsufficientSystemResource,
169}
170
171/// Wrapper for log probability information.
172#[derive(Debug, Clone, Serialize, Deserialize)]
173pub struct LogProbWrap {
174 /// List of log probabilities.
175 pub content: Vec<LogProb>,
176}
177
178/// Represents log probability information for a token.
179#[derive(Debug, Clone, Serialize, Deserialize)]
180pub struct LogProb {
181 /// Token string.
182 pub token: String,
183 /// Log probability of the token.
184 pub logprob: f32,
185 /// Optional bytes representation of the token.
186 pub bytes: Option<Vec<u8>>,
187 /// List of top log probabilities.
188 pub top_logprobs: Vec<TopLogProb>,
189}
190
191/// Represents top log probability information for a token.
192#[derive(Debug, Clone, Serialize, Deserialize)]
193pub struct TopLogProb {
194 /// Token string.
195 pub token: String,
196 /// Log probability of the token.
197 pub logprob: f32,
198 /// Optional bytes representation of the token.
199 pub bytes: Option<Vec<u8>>,
200}
201
202/// Represents a choice made during a process.
203#[derive(Clone, Serialize, Deserialize, Debug)]
204pub struct Choice {
205 /// Reason for finishing the process.
206 pub finish_reason: FinishReason,
207 /// Index of the choice.
208 pub index: usize,
209 /// Message associated with the choice.
210 pub text: Option<String>,
211 /// Message associated with the choice.
212 pub message: Option<Message>,
213 /// Optional log probability information.
214 pub logprobs: Option<LogProbWrap>,
215}
216
217/// Represents usage information for a process.
218#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct Usage {
220 /// Number of completion tokens used.
221 pub completion_tokens: u64,
222 /// Number of prompt tokens used.
223 pub prompt_tokens: u64,
224 /// Number of prompt cache hit tokens.
225 pub prompt_cache_hit_tokens: u64,
226 /// Number of prompt cache miss tokens.
227 pub prompt_cache_miss_tokens: u64,
228 /// Total number of tokens used.
229 pub total_tokens: u64,
230 /// Details of completion tokens used.
231 pub completion_tokens_details: CompletionTokensDetails,
232}
233
234/// Details of completion tokens used.
235#[derive(Debug, Clone, Serialize, Deserialize)]
236pub struct CompletionTokensDetails {
237 /// Number of reasoning tokens used.
238 pub reasoning_tokens: u64,
239}
240
241/// Represents a chat completion with its associated metadata.
242#[derive(Debug, Clone, Serialize, Deserialize)]
243pub struct ChatCompletion {
244 /// Unique identifier for the chat completion.
245 pub id: String,
246 /// List of choices made during the chat completion.
247 pub choices: Vec<Choice>,
248 /// Timestamp of when the chat completion was created.
249 pub created: u32,
250 /// Model used for the chat completion.
251 pub model: String,
252 /// System fingerprint associated with the chat completion.
253 pub system_fingerprint: String,
254 /// Type of the object.
255 pub object: String,
256}
257
258/// Represents a delta change in a choice stream.
259#[derive(Clone, Serialize, Deserialize, Debug)]
260pub struct Delta {
261 /// Content of the delta change.
262 pub content: String,
263 /// Reasoning content of the delta change.
264 #[serde(default)]
265 pub reasoning_content: String,
266 /// Role of the delta change sender.
267 #[serde(default)]
268 pub role: String,
269}
270
271/// Represents a choice stream with its associated delta change.
272#[derive(Clone, Serialize, Deserialize, Debug)]
273pub struct JSONChoiceStream {
274 /// Delta change in the choice stream.
275 pub delta: Delta,
276 /// Reason for finishing the choice stream.
277 pub finish_reason: Option<FinishReason>,
278 /// Index of the choice stream.
279 pub index: usize,
280}
281
282/// Represents a choice stream with its associated delta change.
283#[derive(Clone, Serialize, Deserialize, Debug)]
284pub struct TextChoiceStream {
285 /// Delta change in the choice stream.
286 pub text: String,
287 /// Reason for finishing the choice stream.
288 pub finish_reason: Option<FinishReason>,
289 /// Index of the choice stream.
290 pub index: usize,
291}
292
293/// Represents a chat completion stream with its associated metadata.
294#[derive(Debug, Clone, Serialize, Deserialize)]
295pub struct ChatCompletionStream<T> {
296 /// Unique identifier for the chat completion stream.
297 pub id: String,
298 /// List of choice streams made during the chat completion stream.
299 pub choices: Vec<T>,
300 /// Timestamp of when the chat completion stream was created.
301 pub created: u32,
302 /// Model used for the chat completion stream.
303 pub model: String,
304 /// System fingerprint associated with the chat completion stream.
305 pub system_fingerprint: String,
306 /// Type of the object.
307 pub object: String,
308}
309
310/// Represents a chat response which can either be a full response or a stream of items.
311///
312/// This enum is generic over the response type `RESP` and the item type `ITEM`.
313///
314/// # Variants
315///
316/// - `Full(RESP)`: Represents a complete response of type `RESP`.
317/// - `Stream(JsonStream<ITEM>)`: Represents a stream of items of type `ITEM`.
318///
319/// # Type Parameters
320///
321/// - `RESP`: The type of the full response. Must implement `DeserializeOwned`.
322/// - `ITEM`: The type of the items in the stream. Must implement `DeserializeOwned`.
323///
324/// # Methods
325///
326/// - `must_response(self) -> RESP`: Consumes the enum and returns the full response if it is the `Full` variant. Panics if it is the `Stream` variant.
327/// - `must_stream(self) -> JsonStream<ITEM>`: Consumes the enum and returns the stream if it is the `Stream` variant. Panics if it is the `Full` variant.
328pub enum ChatResponse<RESP, ITEM>
329where
330 RESP: DeserializeOwned,
331 ITEM: DeserializeOwned,
332{
333 Full(RESP),
334 Stream(JsonStream<ITEM>),
335}
336
337impl<RESP, ITEM> ChatResponse<RESP, ITEM>
338where
339 RESP: DeserializeOwned,
340 ITEM: DeserializeOwned,
341{
342 pub fn must_response(self) -> RESP {
343 match self {
344 ChatResponse::Full(resp) => resp,
345 ChatResponse::Stream(_) => panic!("Expected Full variant, found Stream"),
346 }
347 }
348
349 pub fn must_stream(self) -> JsonStream<ITEM> {
350 match self {
351 ChatResponse::Stream(stream) => stream,
352 ChatResponse::Full(_) => panic!("Expected Stream variant, found Full"),
353 }
354 }
355}