调整
This commit is contained in:
1 parent
79bded1cd2
commit
9e6d73e191
73 files changed
+6229
-1427
No files matched your search
@@ -75,8 +75,9 @@ impl AiEngine {
|
||||
}
|
||||
|
||||
/// 密钥只从系统凭据管理器读,不进配置文件、不经前端。
|
||||
/// AI 引擎走 `ai-provider-*`(含对旧 `translate-engine-*` 的回退)。
|
||||
fn api_key(cfg: &TranslateEngineConfig) -> String {
|
||||
crate::translate::engine_api_key(&cfg.id)
|
||||
crate::translate::engine_api_key_with_kind(&cfg.id, Some("ai"))
|
||||
}
|
||||
|
||||
fn require_key(cfg: &TranslateEngineConfig) -> Result<String, TranslateError> {
|
||||
@@ -592,70 +593,3 @@ struct ModelEntry {
|
||||
id: String,
|
||||
}
|
||||
|
||||
/// 通用(非翻译语义)的对话补全入口:供终端 AI 助手等模块复用引擎配置。
|
||||
///
|
||||
/// 与翻译路径共享端点归一(剥 `/chat/completions` 后缀)、密钥存取
|
||||
/// (凭据管理器)、`apply_common_params`(temperature / max_tokens / extra_body)
|
||||
/// 与响应解析,但 **消息由调用方全量给定**——这里不含任何翻译提示词语义。
|
||||
///
|
||||
/// 刻意做成关联函数而不是 `AiEngine` 的实例方法:调用方(终端助手)只持有
|
||||
/// `TranslateEngineConfig`,为它构造 `AiEngine` 还要 PromptTemplates 与 client,
|
||||
/// 属于无谓的耦合。
|
||||
pub async fn chat_once(
|
||||
cfg: &TranslateEngineConfig,
|
||||
messages: Vec<(&str, String)>,
|
||||
) -> Result<String, String> {
|
||||
let key = AiEngine::require_key(cfg).map_err(|e| e.to_string())?;
|
||||
let model = AiEngine::require_model(cfg).map_err(|e| e.to_string())?;
|
||||
let endpoint = AiEngine::chat_endpoint(cfg).map_err(|e| e.to_string())?;
|
||||
|
||||
let mut body = serde_json::Map::new();
|
||||
body.insert("model".to_string(), json!(model));
|
||||
body.insert("stream".to_string(), json!(false));
|
||||
body.insert(
|
||||
"messages".to_string(),
|
||||
json!(messages
|
||||
.into_iter()
|
||||
.map(|(role, content)| json!({ "role": role, "content": content }))
|
||||
.collect::<Vec<_>>()),
|
||||
);
|
||||
apply_common_params(&mut body, cfg);
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let resp = client
|
||||
.post(&endpoint)
|
||||
.bearer_auth(&key)
|
||||
.timeout(Duration::from_millis(cfg.timeout_ms.max(1000)))
|
||||
.json(&serde_json::Value::Object(body))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| classify_reqwest(e, &cfg.name).to_string())?;
|
||||
|
||||
let status = resp.status();
|
||||
let raw = resp
|
||||
.text()
|
||||
.await
|
||||
.map_err(|e| format!("读取「{}」响应失败: {e}", cfg.name))?;
|
||||
if !status.is_success() {
|
||||
return Err(classify_http(status.as_u16(), &raw, &cfg.name, &model).to_string());
|
||||
}
|
||||
|
||||
let parsed: ChatResponse = serde_json::from_str(&raw)
|
||||
.map_err(|e| format!("「{}」响应不是预期的 JSON: {e}", cfg.name))?;
|
||||
if let Some(err) = parsed.error {
|
||||
let msg = err
|
||||
.message
|
||||
.filter(|m| !m.trim().is_empty())
|
||||
.unwrap_or_else(|| "上游返回了错误对象".to_string());
|
||||
return Err(format!("「{}」返回错误:{msg}", cfg.name));
|
||||
}
|
||||
|
||||
parsed
|
||||
.choices
|
||||
.into_iter()
|
||||
.next()
|
||||
.and_then(|c| c.message.content)
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.ok_or_else(|| format!("「{}」返回了空内容", cfg.name))
|
||||
}
|
||||
Reference in new issue
Block a user