|
|
@@ -13,6 +13,7 @@ import com.telerobot.fs.robot.AbstractChatRobot;
|
|
|
import com.telerobot.fs.service.SysService;
|
|
|
import com.telerobot.fs.utils.CommonUtils;
|
|
|
import okhttp3.MediaType;
|
|
|
+import okhttp3.Headers;
|
|
|
import okhttp3.Request;
|
|
|
import okhttp3.RequestBody;
|
|
|
import okhttp3.Response;
|
|
|
@@ -28,6 +29,23 @@ import java.util.List;
|
|
|
*/
|
|
|
public class TencentChat extends AbstractChatRobot {
|
|
|
|
|
|
+ private String findLlmRequestId(Headers headers) {
|
|
|
+ if (headers == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String requestId = headers.get("x-request-id");
|
|
|
+ if (StringUtils.isBlank(requestId)) {
|
|
|
+ requestId = headers.get("request-id");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(requestId)) {
|
|
|
+ requestId = headers.get("x-requestid");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(requestId)) {
|
|
|
+ requestId = headers.get("requestid");
|
|
|
+ }
|
|
|
+ return StringUtils.defaultString(requestId);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public LlmAiphoneRes talkWithAiAgent(String question, Boolean... withKbResponse) {
|
|
|
@@ -133,11 +151,21 @@ public class TencentChat extends AbstractChatRobot {
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
|
|
try (Response response = CLIENT.newCall(request).execute()) {
|
|
|
+ String headerRequestId = findLlmRequestId(response.headers());
|
|
|
+ if (StringUtils.isNotBlank(headerRequestId)) {
|
|
|
+ logger.info("{} llm response header requestId={}", getTraceId(), headerRequestId);
|
|
|
+ }
|
|
|
if (!response.isSuccessful()) {
|
|
|
- logger.error("Model api error: http-code={}, msg={}, url={}",
|
|
|
+ String errorBody = "";
|
|
|
+ if (response.body() != null) {
|
|
|
+ errorBody = response.body().string();
|
|
|
+ }
|
|
|
+ logger.error("Model api error: http-code={}, msg={}, url={}, requestId={}, body={}",
|
|
|
response.code(),
|
|
|
response.message(),
|
|
|
- getAccount().serverUrl
|
|
|
+ getAccount().serverUrl,
|
|
|
+ headerRequestId,
|
|
|
+ errorBody
|
|
|
);
|
|
|
if(response.code() == HttpStatus.SC_UNAUTHORIZED) {
|
|
|
CommonUtils.setHangupCauseDetail(
|
|
|
@@ -167,6 +195,7 @@ public class TencentChat extends AbstractChatRobot {
|
|
|
|
|
|
Integer completionTokens = 0; // 模型生成回复转换为 Token 后的长度。
|
|
|
Integer promptTokens = 0; // 用户的输入转换成 Token 后的长度。
|
|
|
+ String bodyRequestId = "";
|
|
|
|
|
|
while (!source.exhausted()) {
|
|
|
String line = source.readUtf8Line();
|
|
|
@@ -177,6 +206,12 @@ public class TencentChat extends AbstractChatRobot {
|
|
|
}
|
|
|
|
|
|
JSONObject jsonResponse = JSON.parseObject(jsonData);
|
|
|
+ if (StringUtils.isBlank(bodyRequestId)) {
|
|
|
+ bodyRequestId = jsonResponse.getString("id");
|
|
|
+ if (StringUtils.isNotBlank(bodyRequestId)) {
|
|
|
+ logger.info("{} llm stream requestId={}", getTraceId(), bodyRequestId);
|
|
|
+ }
|
|
|
+ }
|
|
|
JSONObject message = jsonResponse.getJSONArray("choices")
|
|
|
.getJSONObject(0)
|
|
|
.getJSONObject("delta"); // 注意:流式响应中消息在 "delta" 字段中
|
|
|
@@ -228,7 +263,8 @@ public class TencentChat extends AbstractChatRobot {
|
|
|
}
|
|
|
|
|
|
String answer = responseBuilder.toString();
|
|
|
- logger.info("{} recv llm response end flag. answer={}", this.uuid, answer);
|
|
|
+ logger.info("{} recv llm response end flag. requestId={}, headerRequestId={}, answer={}",
|
|
|
+ this.uuid, bodyRequestId, headerRequestId, answer);
|
|
|
if(ttsTextLength > 0){
|
|
|
sendToTts();
|
|
|
}
|