This commit is contained in:
ran.shang
2025-06-11 09:42:13 +08:00
commit 7e38b8d971
1935 changed files with 132836 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
package com.newyetai.sdk.xf.service;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.newyetai.common.AlitaResult;
import com.newyetai.common.util.JsonUtils;
import com.newyetai.sdk.common.constant.SdkConstants;
import com.newyetai.sdk.common.dto.DefaultSdkInvokeLogDTO;
import com.newyetai.sdk.common.util.HttpUtils;
import com.newyetai.sdk.xf.config.XfProperties;
import com.newyetai.sdk.xf.model.ChatReq;
import com.newyetai.sdk.xf.model.ChatRes;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class XfSparkProvider {
private static XfProperties xfProperties;
private XfSparkProvider(XfProperties xfProperties) {
XfSparkProvider.xfProperties = xfProperties;
}
public static AlitaResult<String> httpChat(ChatReq req) {
return httpChat(req, xfProperties.getHttpAPIPassword());
}
public static AlitaResult<String> httpChat(ChatReq req, String httpAPIPassword) {
ResponseEntity<ChatRes> response = null;
try {
try {
HttpHeaders header = new HttpHeaders();
header.add("Authorization", "Bearer " + httpAPIPassword);
header.add("Content-Type", "application/json");
// 发送请求
HttpEntity requestEntity = new HttpEntity(req, header);
response =
HttpUtils.restTemplate.exchange("https://spark-api-open.xf-yun.com/v1/chat/completions",
HttpMethod.POST, requestEntity, ChatRes.class);
} catch (Exception e) {
log.error("xfSpark http error:{}", e.getMessage());
return AlitaResult.error(SdkConstants.SDK_HTTP_INVOKE_ERROR_CODE, e.getMessage());
}
try {
String content = response.getBody().getChoices().get(0).getMessage().getContent();
content = dealJson(content);
return AlitaResult.success(content);
} catch (Exception e) {
return AlitaResult.error(SdkConstants.SDK_RESPONSE_CONVERT_ERROR_CODE, "xf返回结果获取content失败");
}
} finally {
log.info("xfSpark invoke:{}",
JsonUtils
.toJsonString(
DefaultSdkInvokeLogDTO.builder().requestData(req).responseData(response)
.build()));
}
}
public static String dealJson(String content) {
return content.replaceAll("(```json\n)|(\n```)|(\n)", "");
}
}