deepseek-sdk 0.3.0

DeepSeek API client for Rust.
Documentation
use super::*;

/// Token usage statistics for a request.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct Usage {
    /// Number of tokens in the generated completion.
    pub completion_tokens: u64,

    /// Number of tokens in the prompt. It equals prompt_cache_hit_tokens + prompt_cache_miss_tokens.
    pub prompt_tokens: u64,

    /// Number of tokens in the prompt that hits the context cache.
    pub prompt_cache_hit_tokens: u64,

    /// Number of tokens in the prompt that misses the context cache.
    pub prompt_cache_miss_tokens: u64,

    /// Total number of tokens used in the request (prompt + completion).
    pub total_tokens: u64,

    /// Breakdown of tokens used in a completion.
    pub completion_tokens_details: Option<CompletionTokensDetails>,
}
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct CompletionTokensDetails {
    /// Tokens generated by the model for reasoning.
    pub reasoning_tokens: u64,
}

/// Generic chat response container.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct ChatGeneric<C> {
    /// A unique identifier for the chat completion.
    pub id: String,

    pub choices: Vec<C>,

    /// The Unix timestamp (in seconds) of when the chat completion was created.
    pub created: u64,

    /// The model used for the chat completion.
    pub model: String,
    /// This fingerprint represents the backend configuration that the model runs with.
    pub system_fingerprint: String,

    /// Possible values: \`chat.completion\`
    ///
    /// The object type, which is always `chat.completion`.
    pub object: String,

    /// Usage statistics for the completion request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<Usage>,
}

/// Non-streaming choice result.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct ChatChoice {
    /// Possible values: [`stop`, `length`, `content_filter`, `tool_calls`,
    /// `insufficient_system_resource`]
    ///
    /// The reason the model stopped generating tokens.
    /// This will be `stop` if the model hit a natural stop point or a provided stop sequence,
    /// `length` if the maximum number of tokens specified in the request was reached,
    /// `content_filter` if content was omitted due to a flag from our content filters,
    /// `tool_calls` if the model called a tool,
    /// or `insufficient_system_resource` if the request is interrupted due to insufficient resource of the inference system.
    pub finish_reason: FinishReason,

    /// The index of the choice in the list of choices.
    pub index: u64,

    /// A chat completion message generated by the model.
    pub message: ChoiceMessage,

    /// Log probability information for the choice.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<Logprobs>,
}

/// Streaming choice delta.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct ChatChoiceStream {
    /// Possible values: [`stop`, `length`, `content_filter`, `tool_calls`, `insufficient_system_resource`]
    ///
    /// The reason the model stopped generating tokens.
    /// This will be `stop` if the model hit a natural stop point or a provided stop sequence,
    /// `length` if the maximum number of tokens specified in the request was reached,
    /// `content_filter` if content was omitted due to a flag from our content filters,
    /// `tool_calls` if the model called a tool,
    /// or `insufficient_system_resource` if the request is interrupted due to insufficient resource of the inference system.
    pub finish_reason: Option<FinishReason>,

    /// The index of the choice in the list of choices.
    pub index: u64,

    /// A chat completion delta generated by streamed model responses.
    pub delta: ChoiceMessageDelta,

    /// Log probability information for the choice.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<Logprobs>,
}

/// Assistant message content in non-streaming responses.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ChoiceMessage {
    /// The contents of the message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,

    /// For thinking mode only. The reasoning contents of the assistant message, before the final answer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_content: Option<String>,

    /// The tool calls generated by the model.
    #[serde(skip_serializing_if = "is_none_or_empty_vec")]
    pub tool_calls: Option<Vec<ToolCall>>,

    /// The role of the author of this message.
    pub role: Role,
}

/// Assistant message delta in streaming responses.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ChoiceMessageDelta {
    /// The contents of the chunk message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,

    /// For thinking mode only. The reasoning contents of the assistant message, before the final answer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_content: Option<String>,
    #[serde(skip_serializing_if = "is_none_or_empty_vec")]
    pub tool_calls: Option<Vec<ToolCall>>,
    /// Possible values: \`assistant\`
    ///
    /// The role of the author of this message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<Role>,
}

/// Role of a chat message.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Role {
    System,
    User,
    Assistant,
    Tool,
}

/// Tool call emitted by the model.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ToolCall {
    /// The ID of the tool call.
    pub id: String,
    #[serde(rename = "type")]

    /// Possible values: \`function\`
    ///
    ///The type of the tool. Currently, only `function` is supported.
    pub typ: ToolCallType,

    /// The function that the model called.
    pub function: ToolCallFunction,
}

impl ToolCall {
    /// Build a function tool call with an id, name, and arguments JSON string.
    pub fn new(
        id: impl Into<String>,
        name: impl Into<String>,
        arguments: impl Into<String>,
    ) -> Self {
        ToolCall {
            id: id.into(),
            typ: ToolCallType::Function,
            function: ToolCallFunction {
                name: name.into(),
                arguments: arguments.into(),
            },
        }
    }
}

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

/// Tool call function payload.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ToolCallFunction {
    /// The name of the function to call.
    pub name: String,
    /// The arguments to call the function with, as generated by the model in JSON format.
    /// Note that the model does not always generate valid JSON,
    /// and may hallucinate parameters not defined by your function schema.
    /// Validate the arguments in your code before calling your function.
    pub arguments: String,
}
/// Reason for completion termination.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum FinishReason {
    Stop,
    Length,
    ContentFilter,
    ToolCalls,
    InsufficientSystemResources,
}
/// Token-level log probability data.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Logprobs {
    #[serde(skip_serializing_if = "is_none_or_empty_vec")]
    pub content: Option<Vec<LogprobsContent>>,
    #[serde(skip_serializing_if = "is_none_or_empty_vec")]
    pub reasoning_content: Option<Vec<LogprobsReasoningContent>>,
}
/// Logprobs for content tokens.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct LogprobsContent {
    pub token: String,
    pub logprob: f64,
    pub bytes: Option<Vec<u8>>,
    pub top_logprobs: Vec<TopLogprobs>,
}

/// Top logprob candidates for a token.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct TopLogprobs {
    pub token: String,
    pub logprob: f64,
    pub bytes: Option<Vec<u8>>,
}
/// Logprobs for reasoning tokens.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct LogprobsReasoningContent {
    pub token: String,
    pub logprob: f64,
    pub bytes: Option<Vec<u8>>,
    pub top_logprobs: Vec<TopLogprobs>,
}