Skip to content

Commit

Permalink
feature: AI+JOB 一期 #2995
Browse files Browse the repository at this point in the history
1.对话类接口响应更新为流式响应定义。
  • Loading branch information
jsonwan committed Aug 14, 2024
1 parent dc8fc95 commit 6686151
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.tencent.bk.job.analysis.model.web.req.AIAnalyzeErrorReq;
import com.tencent.bk.job.analysis.model.web.req.AICheckScriptReq;
import com.tencent.bk.job.analysis.model.web.req.AIGeneralChatReq;
import com.tencent.bk.job.analysis.model.web.resp.AIAnswer;
import com.tencent.bk.job.analysis.model.web.resp.AIChatRecord;
import com.tencent.bk.job.analysis.model.web.resp.ClearChatHistoryResp;
import com.tencent.bk.job.common.annotation.WebAPI;
Expand All @@ -48,6 +47,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.Min;
Expand Down Expand Up @@ -106,9 +106,12 @@ Response<List<AIChatRecord>> getLatestChatHistoryList(
Integer length
);

@ApiOperation(value = "通用聊天接口", produces = "application/json")
@ApiOperation(value = "通用聊天接口(流式接口),返回换行符分隔的多条JSON数据,可分块读取,单条JSON数据格式:{\"success\":true,\"code\":0," +
"\"errorMsg\":\"成功\",\"data\":{\"errorCode\":\"0\",\"errorMessage\":null,\"content\":\"hello world\"," +
"\"time\":\"2024-08-14 12:00:00\"},\"requestId\":\"fb991170da868b2a1eb5835bc426e992\",\"authResult\": null," +
"\"errorDetail\": null}", produces = "application/json")
@PostMapping("/general/chat")
Response<AIAnswer> generalChat(
StreamingResponseBody generalChat(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
String username,
Expand All @@ -126,9 +129,12 @@ Response<AIAnswer> generalChat(
@RequestBody AIGeneralChatReq req
);

@ApiOperation(value = "检查脚本", produces = "application/json")
@ApiOperation(value = "检查脚本(流式接口),返回换行符分隔的多条JSON数据,可分块读取,单条JSON数据格式:{\"success\":true,\"code\":0," +
"\"errorMsg\":\"成功\",\"data\":{\"errorCode\":\"0\",\"errorMessage\":null,\"content\":\"hello world\"," +
"\"time\":\"2024-08-14 12:00:00\"},\"requestId\":\"fb991170da868b2a1eb5835bc426e992\",\"authResult\": null," +
"\"errorDetail\": null}", produces = "application/json")
@PostMapping("/checkScript")
Response<AIAnswer> checkScript(
StreamingResponseBody checkScript(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
String username,
Expand All @@ -146,9 +152,12 @@ Response<AIAnswer> checkScript(
@RequestBody AICheckScriptReq req
);

@ApiOperation(value = "分析报错信息", produces = "application/json")
@ApiOperation(value = "分析报错信息(流式接口),返回换行符分隔的多条JSON数据,可分块读取,单条JSON数据格式:{\"success\":true,\"code\":0," +
"\"errorMsg\":\"成功\",\"data\":{\"errorCode\":\"0\",\"errorMessage\":null,\"content\":\"hello world\"," +
"\"time\":\"2024-08-14 12:00:00\"},\"requestId\":\"fb991170da868b2a1eb5835bc426e992\",\"authResult\": null," +
"\"errorDetail\": null}", produces = "application/json")
@PostMapping("/analyzeError")
Response<AIAnswer> analyzeError(
StreamingResponseBody analyzeError(
@ApiParam("用户名,网关自动传入")
@RequestHeader("username")
String username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@
import com.tencent.bk.job.common.model.dto.AppResourceScope;
import com.tencent.bk.job.common.model.error.ErrorType;
import com.tencent.bk.job.execute.common.constants.StepExecuteTypeEnum;
import com.tencent.bk.sdk.iam.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -127,34 +130,56 @@ private AIAnswer getGreetingAIAnswer() {
}

@Override
public Response<AIAnswer> generalChat(String username,
AppResourceScope appResourceScope,
String scopeType,
String scopeId,
AIGeneralChatReq req) {
public StreamingResponseBody generalChat(String username,
AppResourceScope appResourceScope,
String scopeType,
String scopeId,
AIGeneralChatReq req) {
AIAnswer aiAnswer = chatService.chatWithAI(username, req.getContent());
return Response.buildSuccessResp(aiAnswer);
Response<AIAnswer> response = Response.buildSuccessResp(aiAnswer);
return buildStreamingResponseBody(response);
}

@Override
public Response<AIAnswer> checkScript(String username,
AppResourceScope appResourceScope,
String scopeType,
String scopeId,
AICheckScriptReq req) {
public StreamingResponseBody checkScript(String username,
AppResourceScope appResourceScope,
String scopeType,
String scopeId,
AICheckScriptReq req) {
AIAnswer aiAnswer = aiCheckScriptService.check(username, req.getType(), req.getContent());
return Response.buildSuccessResp(aiAnswer);
Response<AIAnswer> response = Response.buildSuccessResp(aiAnswer);
return buildStreamingResponseBody(response);
}

@Override
public Response<AIAnswer> analyzeError(String username,
AppResourceScope appResourceScope,
String scopeType,
String scopeId,
AIAnalyzeErrorReq req) {
public StreamingResponseBody analyzeError(String username,
AppResourceScope appResourceScope,
String scopeType,
String scopeId,
AIAnalyzeErrorReq req) {
checkScriptLogContentLength(req);
AIAnswer aiAnswer = aiAnalyzeErrorService.analyze(username, appResourceScope.getAppId(), req);
return Response.buildSuccessResp(aiAnswer);
Response<AIAnswer> response = Response.buildSuccessResp(aiAnswer);
return buildStreamingResponseBody(response);
}

private StreamingResponseBody buildStreamingResponseBody(Object response) {
return outputStream -> {
try {
outputStream.write(JsonUtil.toJson(response).getBytes());
outputStream.flush();
} catch (IOException e) {
// 处理写入数据时的异常
log.error("Fail to write data", e);
} finally {
// 确保输出流被关闭
try {
outputStream.close();
} catch (IOException e) {
log.error("Fail to close output stream", e);
}
}
};
}

/**
Expand Down

0 comments on commit 6686151

Please sign in to comment.