feat(deepseek): 添加 DeepSeek reasoning 模式支持

This commit is contained in:
2026-05-26 16:27:49 +08:00
parent 3a57d25a76
commit 1063369d96
6 changed files with 103 additions and 39 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "quicommit" name = "quicommit"
version = "0.2.0" version = "0.2.1"
edition = "2024" edition = "2024"
authors = ["Sidney Zhang <zly@lyzhang.me>"] authors = ["Sidney Zhang <zly@lyzhang.me>"]
description = "A powerful Git assistant tool with AI-powered commit/tag/changelog generation(alpha version)" description = "A powerful Git assistant tool with AI-powered commit/tag/changelog generation(alpha version)"

View File

@@ -993,7 +993,7 @@ impl ConfigCommand {
"deepseek" => { "deepseek" => {
println!("DeepSeek models:"); println!("DeepSeek models:");
println!(" deepseek-chat"); println!(" deepseek-chat");
println!(" deepseek-coder"); println!(" deepseek-reasoner");
} }
"openrouter" => { "openrouter" => {
println!("OpenRouter models (examples):"); println!("OpenRouter models (examples):");

View File

@@ -110,6 +110,10 @@ pub struct LlmConfig {
/// API key (stored in config for fallback, encrypted if encrypt_sensitive is true) /// API key (stored in config for fallback, encrypted if encrypt_sensitive is true)
#[serde(default)] #[serde(default)]
pub api_key: Option<String>, pub api_key: Option<String>,
/// Enable thinking/reasoning mode (deepseek, kimi)
#[serde(default)]
pub thinking_enabled: bool,
} }
fn default_api_key_storage() -> String { fn default_api_key_storage() -> String {
@@ -127,6 +131,7 @@ impl Default for LlmConfig {
timeout: default_timeout(), timeout: default_timeout(),
api_key_storage: default_api_key_storage(), api_key_storage: default_api_key_storage(),
api_key: None, api_key: None,
thinking_enabled: false,
} }
} }
} }

View File

@@ -10,6 +10,7 @@ pub struct DeepSeekClient {
api_key: String, api_key: String,
model: String, model: String,
client: reqwest::Client, client: reqwest::Client,
thinking_enabled: bool,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@@ -21,6 +22,14 @@ struct ChatCompletionRequest {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
temperature: Option<f32>, temperature: Option<f32>,
stream: bool, stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
thinking: Option<ThinkingConfig>,
}
#[derive(Debug, Serialize)]
struct ThinkingConfig {
#[serde(rename = "type")]
thinking_type: String,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@@ -37,6 +46,8 @@ struct ChatCompletionResponse {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct Choice { struct Choice {
message: Message, message: Message,
#[serde(default)]
reasoning_content: Option<String>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@@ -55,24 +66,26 @@ impl DeepSeekClient {
/// Create new DeepSeek client /// Create new DeepSeek client
pub fn new(api_key: &str, model: &str) -> Result<Self> { pub fn new(api_key: &str, model: &str) -> Result<Self> {
let client = create_http_client(Duration::from_secs(60))?; let client = create_http_client(Duration::from_secs(60))?;
Ok(Self { Ok(Self {
base_url: "https://api.deepseek.com/v1".to_string(), base_url: "https://api.deepseek.com/".to_string(),
api_key: api_key.to_string(), api_key: api_key.to_string(),
model: model.to_string(), model: model.to_string(),
client, client,
thinking_enabled: false,
}) })
} }
/// Create with custom base URL /// Create with custom base URL
pub fn with_base_url(api_key: &str, model: &str, base_url: &str) -> Result<Self> { pub fn with_base_url(api_key: &str, model: &str, base_url: &str) -> Result<Self> {
let client = create_http_client(Duration::from_secs(60))?; let client = create_http_client(Duration::from_secs(60))?;
Ok(Self { Ok(Self {
base_url: base_url.trim_end_matches('/').to_string(), base_url: base_url.trim_end_matches('/').to_string(),
api_key: api_key.to_string(), api_key: api_key.to_string(),
model: model.to_string(), model: model.to_string(),
client, client,
thinking_enabled: false,
}) })
} }
@@ -82,6 +95,12 @@ impl DeepSeekClient {
Ok(self) Ok(self)
} }
/// Enable or disable thinking mode
pub fn with_thinking(mut self, enabled: bool) -> Self {
self.thinking_enabled = enabled;
self
}
/// List available models /// List available models
pub async fn list_models(&self) -> Result<Vec<String>> { pub async fn list_models(&self) -> Result<Vec<String>> {
let url = format!("{}/models", self.base_url); let url = format!("{}/models", self.base_url);
@@ -142,25 +161,25 @@ impl LlmProvider for DeepSeekClient {
content: prompt.to_string(), content: prompt.to_string(),
}, },
]; ];
self.chat_completion(messages).await self.chat_completion(messages).await
} }
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> { async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
let mut messages = vec![]; let mut messages = vec![];
if !system.is_empty() { if !system.is_empty() {
messages.push(Message { messages.push(Message {
role: "system".to_string(), role: "system".to_string(),
content: system.to_string(), content: system.to_string(),
}); });
} }
messages.push(Message { messages.push(Message {
role: "user".to_string(), role: "user".to_string(),
content: user.to_string(), content: user.to_string(),
}); });
self.chat_completion(messages).await self.chat_completion(messages).await
} }
@@ -176,15 +195,24 @@ impl LlmProvider for DeepSeekClient {
impl DeepSeekClient { impl DeepSeekClient {
async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> { async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> {
let url = format!("{}/chat/completions", self.base_url); let url = format!("{}/chat/completions", self.base_url);
let thinking = if self.thinking_enabled {
Some(ThinkingConfig {
thinking_type: "enabled".to_string(),
})
} else {
None
};
let request = ChatCompletionRequest { let request = ChatCompletionRequest {
model: self.model.clone(), model: self.model.clone(),
messages, messages,
max_tokens: Some(500), max_tokens: Some(500),
temperature: Some(0.7), temperature: Some(0.7),
stream: false, stream: false,
thinking,
}; };
let response = self.client let response = self.client
.post(&url) .post(&url)
.header("Authorization", format!("Bearer {}", self.api_key)) .header("Authorization", format!("Bearer {}", self.api_key))
@@ -193,25 +221,24 @@ impl DeepSeekClient {
.send() .send()
.await .await
.context("Failed to send request to DeepSeek")?; .context("Failed to send request to DeepSeek")?;
let status = response.status(); let status = response.status();
if !status.is_success() { if !status.is_success() {
let text = response.text().await.unwrap_or_default(); let text = response.text().await.unwrap_or_default();
// Try to parse error
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) { if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
bail!("DeepSeek API error: {} ({})", error.error.message, error.error.error_type); bail!("DeepSeek API error: {} ({})", error.error.message, error.error.error_type);
} }
bail!("DeepSeek API error: {} - {}", status, text); bail!("DeepSeek API error: {} - {}", status, text);
} }
let result: ChatCompletionResponse = response let result: ChatCompletionResponse = response
.json() .json()
.await .await
.context("Failed to parse DeepSeek response")?; .context("Failed to parse DeepSeek response")?;
result.choices result.choices
.into_iter() .into_iter()
.next() .next()
@@ -223,7 +250,7 @@ impl DeepSeekClient {
/// Available DeepSeek models /// Available DeepSeek models
pub const DEEPSEEK_MODELS: &[&str] = &[ pub const DEEPSEEK_MODELS: &[&str] = &[
"deepseek-chat", "deepseek-chat",
"deepseek-coder", "deepseek-reasoner",
]; ];
/// Check if a model name is valid /// Check if a model name is valid
@@ -238,6 +265,7 @@ mod tests {
#[test] #[test]
fn test_model_validation() { fn test_model_validation() {
assert!(is_valid_model("deepseek-chat")); assert!(is_valid_model("deepseek-chat"));
assert!(is_valid_model("deepseek-reasoner"));
assert!(!is_valid_model("invalid-model")); assert!(!is_valid_model("invalid-model"));
} }
} }

View File

@@ -10,6 +10,7 @@ pub struct KimiClient {
api_key: String, api_key: String,
model: String, model: String,
client: reqwest::Client, client: reqwest::Client,
thinking_enabled: bool,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@@ -21,6 +22,14 @@ struct ChatCompletionRequest {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
temperature: Option<f32>, temperature: Option<f32>,
stream: bool, stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
thinking: Option<ThinkingConfig>,
}
#[derive(Debug, Serialize)]
struct ThinkingConfig {
#[serde(rename = "type")]
thinking_type: String,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@@ -37,6 +46,8 @@ struct ChatCompletionResponse {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct Choice { struct Choice {
message: Message, message: Message,
#[serde(default)]
reasoning_content: Option<String>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@@ -55,24 +66,26 @@ impl KimiClient {
/// Create new Kimi client /// Create new Kimi client
pub fn new(api_key: &str, model: &str) -> Result<Self> { pub fn new(api_key: &str, model: &str) -> Result<Self> {
let client = create_http_client(Duration::from_secs(60))?; let client = create_http_client(Duration::from_secs(60))?;
Ok(Self { Ok(Self {
base_url: "https://api.moonshot.cn/v1".to_string(), base_url: "https://api.moonshot.cn/v1".to_string(),
api_key: api_key.to_string(), api_key: api_key.to_string(),
model: model.to_string(), model: model.to_string(),
client, client,
thinking_enabled: false,
}) })
} }
/// Create with custom base URL /// Create with custom base URL
pub fn with_base_url(api_key: &str, model: &str, base_url: &str) -> Result<Self> { pub fn with_base_url(api_key: &str, model: &str, base_url: &str) -> Result<Self> {
let client = create_http_client(Duration::from_secs(60))?; let client = create_http_client(Duration::from_secs(60))?;
Ok(Self { Ok(Self {
base_url: base_url.trim_end_matches('/').to_string(), base_url: base_url.trim_end_matches('/').to_string(),
api_key: api_key.to_string(), api_key: api_key.to_string(),
model: model.to_string(), model: model.to_string(),
client, client,
thinking_enabled: false,
}) })
} }
@@ -82,6 +95,12 @@ impl KimiClient {
Ok(self) Ok(self)
} }
/// Enable or disable thinking mode
pub fn with_thinking(mut self, enabled: bool) -> Self {
self.thinking_enabled = enabled;
self
}
/// List available models /// List available models
pub async fn list_models(&self) -> Result<Vec<String>> { pub async fn list_models(&self) -> Result<Vec<String>> {
let url = format!("{}/models", self.base_url); let url = format!("{}/models", self.base_url);
@@ -142,25 +161,25 @@ impl LlmProvider for KimiClient {
content: prompt.to_string(), content: prompt.to_string(),
}, },
]; ];
self.chat_completion(messages).await self.chat_completion(messages).await
} }
async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> { async fn generate_with_system(&self, system: &str, user: &str) -> Result<String> {
let mut messages = vec![]; let mut messages = vec![];
if !system.is_empty() { if !system.is_empty() {
messages.push(Message { messages.push(Message {
role: "system".to_string(), role: "system".to_string(),
content: system.to_string(), content: system.to_string(),
}); });
} }
messages.push(Message { messages.push(Message {
role: "user".to_string(), role: "user".to_string(),
content: user.to_string(), content: user.to_string(),
}); });
self.chat_completion(messages).await self.chat_completion(messages).await
} }
@@ -176,15 +195,24 @@ impl LlmProvider for KimiClient {
impl KimiClient { impl KimiClient {
async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> { async fn chat_completion(&self, messages: Vec<Message>) -> Result<String> {
let url = format!("{}/chat/completions", self.base_url); let url = format!("{}/chat/completions", self.base_url);
let thinking = if self.thinking_enabled {
Some(ThinkingConfig {
thinking_type: "enabled".to_string(),
})
} else {
None
};
let request = ChatCompletionRequest { let request = ChatCompletionRequest {
model: self.model.clone(), model: self.model.clone(),
messages, messages,
max_tokens: Some(500), max_tokens: Some(500),
temperature: Some(0.7), temperature: Some(1.0),
stream: false, stream: false,
thinking,
}; };
let response = self.client let response = self.client
.post(&url) .post(&url)
.header("Authorization", format!("Bearer {}", self.api_key)) .header("Authorization", format!("Bearer {}", self.api_key))
@@ -193,25 +221,24 @@ impl KimiClient {
.send() .send()
.await .await
.context("Failed to send request to Kimi")?; .context("Failed to send request to Kimi")?;
let status = response.status(); let status = response.status();
if !status.is_success() { if !status.is_success() {
let text = response.text().await.unwrap_or_default(); let text = response.text().await.unwrap_or_default();
// Try to parse error
if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) { if let Ok(error) = serde_json::from_str::<ErrorResponse>(&text) {
bail!("Kimi API error: {} ({})", error.error.message, error.error.error_type); bail!("Kimi API error: {} ({})", error.error.message, error.error.error_type);
} }
bail!("Kimi API error: {} - {}", status, text); bail!("Kimi API error: {} - {}", status, text);
} }
let result: ChatCompletionResponse = response let result: ChatCompletionResponse = response
.json() .json()
.await .await
.context("Failed to parse Kimi response")?; .context("Failed to parse Kimi response")?;
result.choices result.choices
.into_iter() .into_iter()
.next() .next()
@@ -241,4 +268,4 @@ mod tests {
assert!(is_valid_model("moonshot-v1-8k")); assert!(is_valid_model("moonshot-v1-8k"));
assert!(!is_valid_model("invalid-model")); assert!(!is_valid_model("invalid-model"));
} }
} }

View File

@@ -44,6 +44,7 @@ pub struct LlmClientConfig {
pub max_tokens: u32, pub max_tokens: u32,
pub temperature: f32, pub temperature: f32,
pub timeout: Duration, pub timeout: Duration,
pub thinking_enabled: bool,
} }
impl Default for LlmClientConfig { impl Default for LlmClientConfig {
@@ -52,6 +53,7 @@ impl Default for LlmClientConfig {
max_tokens: 500, max_tokens: 500,
temperature: 0.7, temperature: 0.7,
timeout: Duration::from_secs(30), timeout: Duration::from_secs(30),
thinking_enabled: false,
} }
} }
} }
@@ -64,12 +66,14 @@ impl LlmClient {
max_tokens: config.llm.max_tokens, max_tokens: config.llm.max_tokens,
temperature: config.llm.temperature, temperature: config.llm.temperature,
timeout: Duration::from_secs(config.llm.timeout), timeout: Duration::from_secs(config.llm.timeout),
thinking_enabled: config.llm.thinking_enabled,
}; };
let provider = config.llm.provider.as_str(); let provider = config.llm.provider.as_str();
let model = config.llm.model.as_str(); let model = config.llm.model.as_str();
let base_url = manager.llm_base_url(); let base_url = manager.llm_base_url();
let api_key = manager.get_api_key(); let api_key = manager.get_api_key();
let thinking_enabled = config.llm.thinking_enabled;
let provider: Box<dyn LlmProvider> = match provider { let provider: Box<dyn LlmProvider> = match provider {
"ollama" => { "ollama" => {
@@ -88,12 +92,12 @@ impl LlmClient {
"kimi" => { "kimi" => {
let key = api_key.as_ref() let key = api_key.as_ref()
.ok_or_else(|| anyhow::anyhow!("Kimi API key not configured"))?; .ok_or_else(|| anyhow::anyhow!("Kimi API key not configured"))?;
Box::new(KimiClient::with_base_url(key, model, &base_url)?) Box::new(KimiClient::with_base_url(key, model, &base_url)?.with_thinking(thinking_enabled))
} }
"deepseek" => { "deepseek" => {
let key = api_key.as_ref() let key = api_key.as_ref()
.ok_or_else(|| anyhow::anyhow!("DeepSeek API key not configured"))?; .ok_or_else(|| anyhow::anyhow!("DeepSeek API key not configured"))?;
Box::new(DeepSeekClient::with_base_url(key, model, &base_url)?) Box::new(DeepSeekClient::with_base_url(key, model, &base_url)?.with_thinking(thinking_enabled))
} }
"openrouter" => { "openrouter" => {
let key = api_key.as_ref() let key = api_key.as_ref()