feat(backend): 为百炼客户端添加兼容模式API调用支持
新增generateCompatible方法实现OpenAI兼容端点的调用逻辑,适配/compatible-mode路径的百炼/MaaS接口,自动补全chat/completions后缀并处理标准OpenAI格式的请求响应
This commit is contained in:
parent
6a13b47328
commit
166e48cada
@ -8,6 +8,7 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -47,6 +48,11 @@ public class BailianClient implements LLMClient {
|
||||
url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
|
||||
}
|
||||
|
||||
// 阿里MaaS/百炼 OpenAI 兼容端点(compatible-mode)走 chat/completions 协议
|
||||
if (url.contains("/compatible-mode")) {
|
||||
return generateCompatible(url, system, prompt, maxTokens, thinkingDisabled);
|
||||
}
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("Authorization", "Bearer " + model.getApiKey());
|
||||
@ -104,6 +110,64 @@ public class BailianClient implements LLMClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用 OpenAI 兼容端点(百炼/MaaS compatible-mode)
|
||||
* URL 形如 https://xxx/compatible-mode/v1,自动补全 /chat/completions
|
||||
*/
|
||||
private String generateCompatible(String apiUrl, String system, String prompt, Integer maxTokens, boolean thinkingDisabled) {
|
||||
String url = apiUrl.endsWith("/") ? apiUrl.substring(0, apiUrl.length() - 1) : apiUrl;
|
||||
if (!url.endsWith("/chat/completions")) {
|
||||
url = url + "/chat/completions";
|
||||
}
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("Authorization", "Bearer " + model.getApiKey());
|
||||
|
||||
List<Map<String, String>> messages = new ArrayList<>();
|
||||
if (system != null && !system.isEmpty()) {
|
||||
messages.add(Map.of("role", "system", "content", system));
|
||||
}
|
||||
messages.add(Map.of("role", "user", "content", prompt));
|
||||
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("model", model.getModelCode());
|
||||
requestBody.put("messages", messages);
|
||||
if (maxTokens != null) {
|
||||
requestBody.put("max_tokens", maxTokens);
|
||||
}
|
||||
if (thinkingDisabled) {
|
||||
requestBody.put("enable_thinking", false);
|
||||
}
|
||||
|
||||
try {
|
||||
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
|
||||
Map<String, Object> response = restTemplate.postForObject(url, entity, Map.class);
|
||||
|
||||
if (response == null) {
|
||||
log.error("百炼兼容模式API返回空响应");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Map<String, Object>> choices = (List<Map<String, Object>>) response.get("choices");
|
||||
if (choices == null || choices.isEmpty()) {
|
||||
log.error("百炼兼容模式API返回错误: {}", response.get("error"));
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, Object> message = (Map<String, Object>) choices.get(0).get("message");
|
||||
if (message == null) {
|
||||
log.error("百炼兼容模式API返回缺少message: {}", response);
|
||||
return null;
|
||||
}
|
||||
|
||||
return (String) message.get("content");
|
||||
} catch (Exception e) {
|
||||
log.error("百炼兼容模式API调用失败: {}", e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LlmModel getModel() {
|
||||
return model;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user