Commit 12eca765 authored by zhouwei's avatar zhouwei

Merge branch 'dev' into 'master'

新增request id日志

See merge request !12
parents 6d186fb3 be361a3e
package com.nanyan.securitylink.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nanyan.securitylink.dto.AIRequestDTO;
import com.nanyan.securitylink.service.AIService;
import com.nanyan.securitylink.vo.AIResponse;
import com.nanyan.securitylink.vo.CodeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import com.nanyan.securitylink.vo.Response;
@Slf4j
@RestController
public class AIController {
@Autowired
AIService aiService;
@PostMapping("/translate")
public Response<AIResponse<String>> translate(@RequestBody AIRequestDTO AIRequestDTO) {
return Response.SUCCESS(aiService.translate(AIRequestDTO));
public Response<AIResponse<String>> translate(@RequestBody AIRequestDTO AIRequestDTO, @RequestHeader("request_id") String requestId) {
log.info("requestId:{}, body:{}", requestId, JSONObject.toJSONString(AIRequestDTO));
return Response.SUCCESS(aiService.translate(AIRequestDTO, requestId));
}
@PostMapping("/news/tag")
public Response<AIResponse<CodeVO>> newsTag(@RequestBody AIRequestDTO AIRequestDTO) {
......
......@@ -6,7 +6,7 @@ import com.nanyan.securitylink.vo.AIResponse;
import com.nanyan.securitylink.vo.CodeVO;
public interface AIService {
AIResponse<String> translate(AIRequestDTO AIRequestDTO);
AIResponse<String> translate(AIRequestDTO AIRequestDTO, String requestId);
AIResponse<CodeVO> newsTags(AIRequestDTO AIRequestDTO);
AIResponse<CodeVO> naturalDisasterTag(AIRequestDTO aiRequestDTO);
......
......@@ -149,12 +149,13 @@ public class AIServiceImpl implements AIService {
private final static String DEEP_SEEK_URL = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
@Override
public AIResponse<String> translate(AIRequestDTO AIRequestDTO) {
public AIResponse<String> translate(AIRequestDTO AIRequestDTO, String requestId) {
ChatInputData chatInputData = new ChatInputData();
buildTranslate(chatInputData, AIRequestDTO);
String apiKey = getModelApiKey();
ChatCompletionResponse chatCompletionResponse = aiRequest(chatInputData, apiKey);
log.info("requestId:{} translate response:{}",requestId, JSONObject.toJSONString(chatCompletionResponse));
if (CollectionUtils.isNotEmpty(chatCompletionResponse.getChoices())) {
AIResponse<String> aiResponse = new AIResponse<>();
......@@ -561,12 +562,7 @@ public class AIServiceImpl implements AIService {
HttpEntity<String> entity = new HttpEntity<>(writeValueAsString, header.getHeaders());
ResponseEntity<ChatCompletionResponse> response = restTemplate.postForEntity(DEEP_SEEK_URL, entity, ChatCompletionResponse.class);
if (response.getStatusCode().is2xxSuccessful()) {
if(response.getHeaders().containsKey(REQUEST_ID)){
log.info("request id:{},translate response:{}",response.getHeaders().get(REQUEST_ID), JSONObject.toJSONString(response.getBody()));
}else {
log.info("translate response:{}",JSONObject.toJSONString(response.getBody()));
}
log.info("ai response:{}",JSONObject.toJSONString(response.getBody()));
return response.getBody();
}
throw new BaseException(MsgCode.TRANSLATE_ERROR);
......
......@@ -8,6 +8,7 @@ public class AIResponse<T> {
private String task_id;
private String id;
private String message_id;
private String requestId;
private String mode;
private String answer;
private ResultVO<T> outputs;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment