deepseek-sdk 0.4.0

DeepSeek API client for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use super::*;
use derive_builder::Builder;

pub(crate) fn is_none_or_empty_stop(opt: &Option<Stop>) -> bool {
    opt.as_ref().map(|stop| stop.is_empty()).unwrap_or(true)
}

/// Chat completion request body.
#[derive(Clone, Debug, PartialEq, Serialize, Builder)]
#[builder(
    pattern = "owned",
    setter(into, strip_option),
    build_fn(validate = "Self::validate"),
    name = "ChatRequestBuilder"
)]
pub struct ChatRequest {
    #[serde(skip_serializing)]
    pub client: DeepSeekClient,

    /// A list of messages comprising the conversation so far.
    #[builder(setter(each(name = "message", into)))]
    pub messages: Vec<ChatMessage>,

    /// Possible values: \[`deepseek-v4-flash`, `deepseek-v4-pro`, `deepseek-v4-flash-vision-exp`\]
    ///
    /// ID of the model to use.
    pub model: String,

    /// Controls the switch between thinking and non-thinking mode.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<Thinking>,

    /// Possible values: [`high`, `max`]
    ///
    /// Controls the reasoning effort of the model.
    /// The default effort is `high` for regular requests;
    /// for some complex agent requests (such as Claude Code, OpenCode),
    /// effort is automatically set to `max`.
    /// For compatibility, `low` and `medium` are mapped to `high`,
    /// and `xhigh` is mapped to `max`.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_effort: Option<ReasoningEffort>,

    /// The maximum number of tokens that can be generated in the chat completion.
    ///
    /// The total length of input tokens and generated tokens is limited by the model's context length.
    ///
    /// For the value range and default value, please refer to the [documentation](https://api-docs.deepseek.com/quick_start/pricing).
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,

    /// An object specifying the format that the model must output.
    /// Setting to { "type": "json_object" } enables JSON Output,
    /// which guarantees the message the model generates is valid JSON.
    ///
    /// **Important**: When using JSON Output, you must also instruct the model to produce JSON yourself via a system or user message.
    /// Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<ResponseFormat>,

    /// Up to 16 sequences where the API will stop generating further tokens.
    #[builder(default)]
    #[serde(skip_serializing_if = "is_none_or_empty_stop")]
    pub stop: Option<Stop>,

    /// If set, partial message deltas will be sent.
    /// Tokens will be sent as data-only server-sent events (SSE) as they become available,
    /// with the stream terminated by a \`data: \[DONE\]\` message.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    /// Options for streaming response. Only set this when you set `stream: true`.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream_options: Option<StreamOptions>,

    /// Possible values: `<= 2`
    ///
    /// Default value: `1`
    ///
    /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
    /// We generally recommend altering this or `top_p` but not both.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f64>,

    /// Possible values: `<= 1`
    ///
    /// Default value: `1`
    ///
    /// An alternative to sampling with temperature, called nucleus sampling,
    /// where the model considers the results of the tokens with top_p probability mass.
    /// So 0.1 means only the tokens comprising the top 10% probability mass are considered.
    ///
    /// We generally recommend altering this or `temperature` but not both.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f64>,

    /// A list of tools the model may call. Currently, only functions are supported as a tool.
    /// Use this to provide a list of functions the model may generate JSON inputs for.
    /// A max of 128 functions are supported.
    #[builder(default, setter(each(name = "tool", into)))]
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tools: Vec<Tool>,

    /// Controls which (if any) tool is called by the model.
    /// `none` means the model will not call any tool and instead generates a message.
    /// `auto` means the model can pick between generating a message or calling one or more tools.
    /// `required` means the model must call one or more tools.
    /// Specifying a particular tool via `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool.
    /// `none` is the default when no tools are present. `auto` is the default if tools are present.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ToolChoice>,

    /// Whether to return log probabilities of the output tokens or not.
    /// If true, returns the log probabilities of each output token returned in the `content` of `message`.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<bool>,

    /// Possible values: `<= 20`
    ///
    /// An integer between 0 and 20 specifying the number of most likely tokens to return at each token position,
    /// each with an associated log probability. `logprobs` must be set to `true` if this parameter is used.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_logprobs: Option<u32>,

    /// A custom `user_id`. Allowed character set is `[a-zA-Z0-9\-_]`, with a maximum length of 512.
    /// Do not include user privacy information in the `user_id`.

    /// `user_id` can be used to distinguish user identities on your side to help us with content safety review.
    /// `user_id` can be used for KVCache isolation for privacy management.
    /// `user_id` can be used for scheduling isolation of users on your business side.
    /// For more details on the `user_id` parameter, please refer to [Rate Limit & Isolation](https://api-docs.deepseek.com/quick_start/rate_limit)
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,
}
/// Chat message variants.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(tag = "role", rename_all = "snake_case")]
pub enum ChatMessage {
    System {
        /// The contents of the system message.
        content: String,
        /// An optional name for the participant. Provides the model information to differentiate between participants of the same role.
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
    },
    User {
        /// The contents of the user message. Either a plain string, or an array of content parts
        /// (for image input with the `deepseek-v4-flash-vision-exp` model).
        content: UserContent,
        /// An optional name for the participant. Provides the model information to differentiate between participants of the same role.
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
    },
    Assistant {
        /// The contents of the assistant message.
        #[serde(skip_serializing_if = "Option::is_none")]
        content: Option<String>,
        /// An optional name for the participant. Provides the model information to differentiate between participants of the same role.
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,

        #[serde(skip_serializing_if = "super::is_none_or_empty_vec")]
        tool_calls: Option<Vec<super::response::ToolCall>>,
    },
    Tool {
        /// The contents of the tool message.
        content: String,
        /// Tool call that this message is responding to.
        tool_call_id: String,
    },
}

/// Content of a user message.
///
/// Either a plain string or an array of content parts (for multimodal input).
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum UserContent {
    /// Plain text content.
    Text(String),
    /// Array of content parts (text, image, file).
    Parts(Vec<UserContentPart>),
}

impl UserContent {
    /// Create a plain text content.
    pub fn text(s: impl Into<String>) -> Self {
        UserContent::Text(s.into())
    }

    /// Create content from a list of parts.
    pub fn parts(parts: Vec<UserContentPart>) -> Self {
        UserContent::Parts(parts)
    }

    /// Create content with a single image URL.
    pub fn image_url(url: impl Into<String>) -> Self {
        UserContent::Parts(vec![UserContentPart::image_url(url)])
    }

    /// Create content with a single image URL and detail level.
    pub fn image_url_with_detail(url: impl Into<String>, detail: ImageDetail) -> Self {
        UserContent::Parts(vec![UserContentPart::image_url_with_detail(url, detail)])
    }

    /// Create content referencing an uploaded file by ID.
    pub fn file_id(id: impl Into<String>) -> Self {
        UserContent::Parts(vec![UserContentPart::file_id(id)])
    }

    /// Create content with an inline base64 image.
    pub fn file_data(data: impl Into<String>, filename: impl Into<String>) -> Self {
        UserContent::Parts(vec![UserContentPart::file_data(data, filename)])
    }
}

impl From<String> for UserContent {
    fn from(s: String) -> Self {
        UserContent::Text(s)
    }
}

impl From<&str> for UserContent {
    fn from(s: &str) -> Self {
        UserContent::Text(s.to_string())
    }
}

impl From<Vec<UserContentPart>> for UserContent {
    fn from(parts: Vec<UserContentPart>) -> Self {
        UserContent::Parts(parts)
    }
}

/// A content part within a multimodal user message.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum UserContentPart {
    /// Text content part.
    Text {
        /// The text content.
        text: String,
    },
    /// Image content part (URL or base64).
    ImageUrl {
        /// Image URL details.
        image_url: ImageUrlDetail,
    },
    /// File content part (uploaded file ID or inline base64).
    File {
        /// The ID of a file uploaded via the Files API.
        #[serde(skip_serializing_if = "Option::is_none")]
        file_id: Option<String>,
        /// A base64-encoded data URL of the image.
        #[serde(skip_serializing_if = "Option::is_none")]
        file_data: Option<String>,
        /// An optional filename (only valid with `file_data`).
        #[serde(skip_serializing_if = "Option::is_none")]
        filename: Option<String>,
    },
}

impl UserContentPart {
    /// Create a text content part.
    pub fn text(s: impl Into<String>) -> Self {
        UserContentPart::Text { text: s.into() }
    }

    /// Create an image URL content part.
    pub fn image_url(url: impl Into<String>) -> Self {
        UserContentPart::ImageUrl {
            image_url: ImageUrlDetail {
                url: url.into(),
                detail: None,
            },
        }
    }

    /// Create an image URL content part with detail level.
    pub fn image_url_with_detail(url: impl Into<String>, detail: ImageDetail) -> Self {
        UserContentPart::ImageUrl {
            image_url: ImageUrlDetail {
                url: url.into(),
                detail: Some(detail),
            },
        }
    }

    /// Create a file content part referencing an uploaded file.
    pub fn file_id(id: impl Into<String>) -> Self {
        UserContentPart::File {
            file_id: Some(id.into()),
            file_data: None,
            filename: None,
        }
    }

    /// Create a file content part with inline base64 data.
    pub fn file_data(data: impl Into<String>, filename: impl Into<String>) -> Self {
        UserContentPart::File {
            file_id: None,
            file_data: Some(data.into()),
            filename: Some(filename.into()),
        }
    }
}

/// Image URL detail for vision requests.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ImageUrlDetail {
    /// The image URL (http(s) URL or base64 data URL).
    pub url: String,
    /// Controls how the image is processed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<ImageDetail>,
}

/// Image detail level.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ImageDetail {
    /// Downscale to 512x512 (faster, cheaper).
    Low,
    /// Process at high resolution with image tiling (slower, more detail).
    High,
    /// Use the original resolution without downscaling.
    Original,
    /// Let the model choose the processing level.
    Auto,
}

/// Reasoning effort hints for the model.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ReasoningEffort {
    High,
    Max,
}
/// Response format configuration.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ResponseFormat {
    /// Default value: `text`
    /// Must be one of `text` or `json_object`.
    #[serde(rename = "type")]
    pub(crate) typ: ResponseFormatType,
}
/// Supported response format types.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ResponseFormatType {
    Text,
    JsonObject,
}

impl ResponseFormat {
    pub fn text() -> Self {
        ResponseFormat {
            typ: ResponseFormatType::Text,
        }
    }

    pub fn json_object() -> Self {
        ResponseFormat {
            typ: ResponseFormatType::JsonObject,
        }
    }
}

/// Stop sequences for generation.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Stop {
    One(String),
    Many(Vec<String>),
}

impl Stop {
    fn is_empty(&self) -> bool {
        match self {
            Stop::One(value) => value.is_empty(),
            Stop::Many(values) => values.is_empty(),
        }
    }
}

impl From<String> for Stop {
    fn from(value: String) -> Self {
        Stop::One(value)
    }
}

impl From<&str> for Stop {
    fn from(value: &str) -> Self {
        Stop::One(value.to_string())
    }
}

impl<T> From<Vec<T>> for Stop
where
    T: Into<String>,
{
    fn from(values: Vec<T>) -> Self {
        Stop::Many(values.into_iter().map(Into::into).collect())
    }
}
/// Streaming options for SSE responses.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct StreamOptions {
    /// If set, an additional chunk will be streamed before the \`data: \[DONE\]\` message.
    /// The `usage` field on this chunk shows the token usage statistics for the entire request,
    /// and the `choices` field will always be an empty array.
    /// All other chunks will also include a `usage` field, but with a null value.
    pub include_usage: bool,
}
/// Tool definition used by the model.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Tool {
    /// The type of the tool. Currently, only `function` is supported.
    #[serde(rename = "type")]
    pub typ: ToolType,
    pub function: ToolFunctionDefinition,
}

impl Tool {
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        parameters: Option<serde_json::Value>,
    ) -> Self {
        Tool {
            typ: ToolType::Function,
            function: ToolFunctionDefinition {
                name: name.into(),
                description: description.into(),
                parameters,
            },
        }
    }
}

/// Tool type.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolType {
    Function,
}

/// Tool function definition.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ToolFunctionDefinition {
    /// A description of what the function does,
    /// used by the model to choose when and how to call the function.
    pub description: String,
    /// The name of the function to be called. Must be a-z, A-Z, 0-9,
    /// or contain underscores and dashes, with a maximum length of 64.
    pub name: String,
    /// The parameters the functions accepts, described as a JSON Schema object.
    /// See the [Tool Calls Guide](https://api-docs.deepseek.com/guides/tool_calls) for examples,
    /// and the [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for documentation about the format.
    ///
    /// Omitting `parameters` defines a function with an empty parameter list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parameters: Option<serde_json::Value>,
}
/// Tool choice configuration.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum ToolChoice {
    /// Possible values: [`none`, `auto`, `required`]
    Simple(ChatToolChoice),
    /// {"type":"function","function":{...}}
    Named(ChatNamedToolChoice),
}

impl ToolChoice {
    pub fn named(function: serde_json::Value) -> Self {
        ToolChoice::Named(ChatNamedToolChoice {
            typ: ToolType::Function,
            function,
        })
    }

    pub fn none() -> Self {
        ToolChoice::Simple(ChatToolChoice::None)
    }

    pub fn auto() -> Self {
        ToolChoice::Simple(ChatToolChoice::Auto)
    }

    pub fn required() -> Self {
        ToolChoice::Simple(ChatToolChoice::Required)
    }
}

/// Tool choice values.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ChatToolChoice {
    None,
    Auto,
    Required,
}
/// Named tool choice configuration.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ChatNamedToolChoice {
    /// Possible values: \`function\`
    ///
    /// The type of the tool. Currently, only `function` is supported.
    #[serde(rename = "type")]
    pub typ: ToolType,

    pub function: serde_json::Value,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct Thinking {
    /// Possible values: [`enabled`, `disabled`]
    ///
    /// Default value: `enabled`
    ///
    /// If set to `enabled`, then use thinking mode. If set to `disabled`, then use non-thinking model.
    #[serde(rename = "type")]
    pub(crate) typ: ThinkingType,
}

impl Thinking {
    pub fn enabled() -> Self {
        Thinking {
            typ: ThinkingType::Enabled,
        }
    }

    pub fn disabled() -> Self {
        Thinking {
            typ: ThinkingType::Disabled,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ThinkingType {
    Enabled,
    Disabled,
}

impl ChatRequestBuilder {
    fn validate(&self) -> Result<(), String> {
        // derive_builder + strip_option makes Option<T> fields become Option<Option<T>> here;
        // flatten() treats "unset" and "explicit None" uniformly for validation.
        if let Some(temperature) = self.temperature.flatten()
            && !(0.0..=2.0).contains(&temperature)
        {
            return Err("temperature must be between 0 and 2".to_string());
        }

        if let Some(top_p) = self.top_p.flatten()
            && !(0.0..=1.0).contains(&top_p)
        {
            return Err("top_p must be between 0 and 1".to_string());
        }

        if let Some(top_logprobs) = self.top_logprobs.flatten() {
            if top_logprobs > 20 {
                return Err("top_logprobs must be <= 20".to_string());
            }
            if self.logprobs.flatten() != Some(true) {
                return Err("top_logprobs requires logprobs=true".to_string());
            }
        }

        if let Some(stream) = self.stream.flatten()
            && !stream
            && self.stream_options.is_some()
        {
            return Err("stream_options cannot be set when stream is false".to_string());
        }

        if let Some(stop) = self.stop.as_ref().and_then(|s| s.as_ref())
            && let Stop::Many(values) = stop
            && values.len() > 16
        {
            return Err("a maximum of 16 stop sequences are allowed".to_string());
        }

        if let Some(user_id) = self.user_id.as_ref().and_then(|u| u.as_ref()) {
            if user_id.len() > 512 {
                return Err("user_id must be at most 512 characters".to_string());
            }
            if !user_id
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
            {
                return Err("user_id must only contain [a-zA-Z0-9\\-_]".to_string());
            }
        }

        Ok(())
    }
}