deepseek-sdk 0.7.1

DeepSeek API client for Rust.
Documentation
//! Model list API.
//!
//! Maps to `GET /models`.
use crate::DeepSeekClient;
use crate::DeepSeekError;
use crate::api_get;
use serde::Deserialize;

/// Model list response.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
pub struct Models {
    /// Possible values: \[`list`\]
    pub object: String,
    pub data: Vec<ModelInfo>,
}

/// Model info entry.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
pub struct ModelInfo {
    /// The model identifier, which can be referenced in the API endpoints.
    pub id: String,

    /// Possible values: \[`model`\]
    ///
    /// The object type, which is always "model".
    pub object: String,

    /// The organization that owns the model.
    pub owned_by: String,

    /// The display name of the model, for use in model pickers and model
    /// discovery tools.
    pub name: Option<String>,

    /// The total token capacity of the context window, counting both input and
    /// output tokens.
    pub context_window: Option<u64>,

    /// The maximum number of output tokens the server allows in a single
    /// response, i.e. the maximum accepted value of `max_tokens`.
    pub max_output_tokens: Option<u64>,

    /// The input types the model accepts.
    ///
    /// Possible values: \[`text`, `image`\]
    #[serde(default)]
    pub input_modalities: Vec<String>,

    /// The media types the model can generate.
    ///
    /// Possible values: \[`text`\]
    #[serde(default)]
    pub output_modalities: Vec<String>,

    /// The effort levels available when thinking mode is enabled.
    pub effort: Option<ModelEffort>,

    /// Model behavior declared per API protocol.
    pub api_capabilities: Option<ApiCapabilities>,
}

/// Effort levels supported by a model when thinking mode is enabled.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
pub struct ModelEffort {
    /// The effort levels the model supports when thinking mode is enabled, in
    /// the recommended display order.
    ///
    /// These are the values accepted by the `reasoning_effort` parameter;
    /// `none`, which turns thinking mode off, is not included.
    pub supported_levels: Vec<String>,

    /// The effort level the server uses when thinking mode is enabled and the
    /// request does not specify one. Always one of `supported_levels`.
    ///
    /// Only present when the server defines a default for the model.
    pub default_level: Option<String>,
}

/// Model behavior declared per API protocol.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
pub struct ApiCapabilities {
    /// Capabilities that apply when the model is used through the Anthropic
    /// Messages API.
    pub anthropic_messages: Option<AnthropicMessagesCapabilities>,
}

/// Capabilities that apply when the model is used through the Anthropic
/// Messages API.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize)]
pub struct AnthropicMessagesCapabilities {
    /// How the model picks up an updated system prompt during a conversation.
    ///
    /// Possible values: `leading-only`, `in-history`
    ///
    /// - `leading-only`: only the system prompt at the start of the
    ///   conversation takes effect. To change the system prompt, modify that
    ///   leading system prompt; system messages appearing later in the history
    ///   are not treated as system prompt updates.
    /// - `in-history`: a `system` message may be appended to the conversation
    ///   history. The latest `system` message in the history provides the
    ///   complete effective system prompt and replaces all earlier ones.
    pub system_prompt_update: String,
}

impl Models {
    /// List available models.
    pub async fn list(client: DeepSeekClient) -> Result<Self, DeepSeekError> {
        api_get("/models", client).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DEFAULT_BASE_URL;

    fn get_client() -> DeepSeekClient {
        DeepSeekClient::new(
            std::env::var("DEEPSEEK_API_KEY").expect("DEEPSEEK_API_KEY is not set"),
            DEFAULT_BASE_URL.clone(),
        )
    }

    #[test]
    fn models_response_deserializes_metadata() {
        let response: Models = serde_json::from_str(
            r#"{
                "object": "list",
                "data": [
                    {
                        "id": "deepseek-flash",
                        "object": "model",
                        "owned_by": "deepseek",
                        "name": "DeepSeek-V4.1-Flash",
                        "context_window": 1048576,
                        "max_output_tokens": 393216,
                        "input_modalities": ["text", "image"],
                        "output_modalities": ["text"],
                        "effort": {
                            "supported_levels": ["low", "high", "max"],
                            "default_level": "high"
                        },
                        "api_capabilities": {
                            "anthropic_messages": {
                                "system_prompt_update": "in-history"
                            }
                        }
                    },
                    {
                        "id": "deepseek-v4-pro",
                        "object": "model",
                        "owned_by": "deepseek"
                    }
                ]
            }"#,
        )
        .unwrap();

        assert_eq!(response.object, "list");
        assert_eq!(response.data.len(), 2);

        let flash = &response.data[0];
        assert_eq!(flash.name.as_deref(), Some("DeepSeek-V4.1-Flash"));
        assert_eq!(flash.context_window, Some(1_048_576));
        assert_eq!(flash.max_output_tokens, Some(393_216));
        assert_eq!(flash.input_modalities, ["text", "image"]);
        assert_eq!(flash.output_modalities, ["text"]);
        let effort = flash.effort.as_ref().unwrap();
        assert_eq!(effort.supported_levels, ["low", "high", "max"]);
        assert_eq!(effort.default_level.as_deref(), Some("high"));
        let capabilities = flash.api_capabilities.as_ref().unwrap();
        let anthropic = capabilities.anthropic_messages.as_ref().unwrap();
        assert_eq!(anthropic.system_prompt_update, "in-history");

        let minimal = &response.data[1];
        assert_eq!(minimal.name, None);
        assert_eq!(minimal.context_window, None);
        assert_eq!(minimal.max_output_tokens, None);
        assert!(minimal.input_modalities.is_empty());
        assert!(minimal.output_modalities.is_empty());
        assert_eq!(minimal.effort, None);
        assert_eq!(minimal.api_capabilities, None);
    }

    #[tokio::test]
    async fn test_list_models() {
        let client = get_client();
        let models = Models::list(client).await.unwrap();
        println!("{:#?}", models);
        assert_eq!(models.object, "list");
        assert!(!models.data.is_empty());
    }
}