Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/ai_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ pub struct OpenAITranslator {
endpoint: String,
model: String,
timeout_seconds: u64,
max_tokens: u64,
}

pub struct ClaudeTranslator {
api_key: String,
endpoint: String,
model: String,
timeout_seconds: u64,
max_tokens: u64,
}

pub struct CopilotTranslator {
Expand All @@ -56,20 +58,23 @@ pub struct GeminiTranslator {
endpoint: String,
model: String,
timeout_seconds: u64,
max_tokens: u64,
}

pub struct GrokTranslator {
api_key: String,
endpoint: String,
model: String,
timeout_seconds: u64,
max_tokens: u64,
}

pub struct QwenTranslator {
api_key: String,
endpoint: String,
model: String,
timeout_seconds: u64,
max_tokens: u64,
}

impl DeepSeekTranslator {
Expand Down Expand Up @@ -101,6 +106,9 @@ impl OpenAITranslator {
timeout_seconds: crate::config::Config::load()
.map(|c| c.timeout_seconds)
.unwrap_or(20),
max_tokens: crate::config::Config::load()
.map(|c| c.max_tokens)
.unwrap_or(2048),
}
}
}
Expand All @@ -116,6 +124,9 @@ impl ClaudeTranslator {
timeout_seconds: crate::config::Config::load()
.map(|c| c.timeout_seconds)
.unwrap_or(20),
max_tokens: crate::config::Config::load()
.map(|c| c.max_tokens)
.unwrap_or(2048),
}
}
}
Expand All @@ -137,6 +148,9 @@ impl GeminiTranslator {
timeout_seconds: crate::config::Config::load()
.map(|c| c.timeout_seconds)
.unwrap_or(20),
max_tokens: crate::config::Config::load()
.map(|c| c.max_tokens)
.unwrap_or(2048),
}
}
}
Expand All @@ -152,6 +166,9 @@ impl GrokTranslator {
timeout_seconds: crate::config::Config::load()
.map(|c| c.timeout_seconds)
.unwrap_or(20),
max_tokens: crate::config::Config::load()
.map(|c| c.max_tokens)
.unwrap_or(2048),
}
}
}
Expand All @@ -167,6 +184,9 @@ impl QwenTranslator {
timeout_seconds: crate::config::Config::load()
.map(|c| c.timeout_seconds)
.unwrap_or(20),
max_tokens: crate::config::Config::load()
.map(|c| c.max_tokens)
.unwrap_or(2048),
}
}
}
Expand Down Expand Up @@ -320,7 +340,8 @@ impl AiService for OpenAITranslator {
debug!("发送给 OpenAI 的消息:\n{}", serde_json::to_string_pretty(&messages)?);
let body = serde_json::json!({
"model": self.model,
"messages": messages
"messages": messages,
"max_tokens": self.max_tokens
});

let ai_host = match url.split('/').nth(2) {
Expand Down Expand Up @@ -394,7 +415,8 @@ impl AiService for ClaudeTranslator {
debug!("发送给 Claude 的消息:\n{}", serde_json::to_string_pretty(&messages)?);
let body = serde_json::json!({
"model": self.model,
"messages": messages
"messages": messages,
"max_tokens": self.max_tokens
});

let ai_host = match url.split('/').nth(2) {
Expand Down Expand Up @@ -490,7 +512,10 @@ impl AiService for GeminiTranslator {
"parts": [{
"text": prompt
}]
}]
}],
"generationConfig": {
"maxOutputTokens": self.max_tokens
}
});

let ai_host = match url.split('/').nth(2) {
Expand Down Expand Up @@ -563,7 +588,8 @@ impl AiService for GrokTranslator {
debug!("发送给 Grok 的消息:\n{}", serde_json::to_string_pretty(&messages)?);
let body = serde_json::json!({
"model": self.model,
"messages": messages
"messages": messages,
"max_tokens": self.max_tokens
});

let ai_host = match url.split('/').nth(2) {
Expand Down Expand Up @@ -638,13 +664,8 @@ impl AiService for QwenTranslator {
let body = serde_json::json!({
"model": self.model,
"messages": messages,
// temperature 控制输出的随机性,范围 0-1
// 设置为 0.1 使输出更加确定和集中,减少不必要的发散
"temperature": 0.1,
// max_tokens 限制生成文本的最大长度
// 对于提交信息翻译来说,256 tokens 足够用了
// 限制长度可以显著提升响应速度
"max_tokens": 256
"max_tokens": self.max_tokens
});

let ai_host = match url.split('/').nth(2) {
Expand Down
Loading