|
@@ -138,12 +138,17 @@ public class GRPCController extends BaseController {
|
|
|
.buildPartial();
|
|
|
outputStream = response.getOutputStream();
|
|
|
Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
|
|
|
+ outputStream.write("begin".getBytes());
|
|
|
+ outputStream.flush();
|
|
|
while (predictions.hasNext()) {
|
|
|
String responseStr = predictions.next().getPrediction().toStringUtf8();
|
|
|
log.info("决策流式返回的结果是{}", responseStr);
|
|
|
responseStr = JSON.parseObject(responseStr).getString("message");
|
|
|
if("complete".equals(responseStr)){
|
|
|
System.out.println("结尾语句并且是非JSON,无需处理");
|
|
|
+ //结束语句也流式输出,但是并不记录下来
|
|
|
+ outputStream.write(responseStr.getBytes());
|
|
|
+ outputStream.flush();
|
|
|
}else{
|
|
|
// sb.append(responseStr);
|
|
|
resultData.add(responseStr);
|
|
@@ -250,6 +255,9 @@ public class GRPCController extends BaseController {
|
|
|
responseStr = JSON.parseObject(responseStr).getString("message");
|
|
|
if("complete".equals(responseStr)){
|
|
|
System.out.println("结尾语句并且是非JSON,无需处理");
|
|
|
+ //结束语句也流式输出,但是并不记录下来
|
|
|
+ outputStream.write(responseStr.getBytes());
|
|
|
+ outputStream.flush();
|
|
|
}else{
|
|
|
sb.append(responseStr);
|
|
|
outputStream.write(responseStr.getBytes());
|
|
@@ -437,4 +445,79 @@ public class GRPCController extends BaseController {
|
|
|
}
|
|
|
// return AjaxResult.success("ok");
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调⽤通过输入数据获取大模型决策接口
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/myData2DecisionStream")
|
|
|
+ public void myData2DecisionStream(HttpServletResponse response, @RequestBody ChatReq chatReq)
|
|
|
+// public void decisionStream(HttpServletResponse response, ChatReq chatReq)
|
|
|
+ {
|
|
|
+ log.info("进入了调⽤大模型决策接口");
|
|
|
+// StringBuilder sb = new StringBuilder();
|
|
|
+ //大模型结果 放入一个结合中
|
|
|
+ List<String> resultData = new ArrayList<>();
|
|
|
+ //决策和问答不一样 没有历史的概念 所以sessionId都是新的 次数都是1
|
|
|
+ String sessionId = IdUtils.simpleUUID();
|
|
|
+ String feedback = chatReq.getFeedback();
|
|
|
+ String rows = chatReq.getRows();
|
|
|
+ String category = chatReq.getCategory();
|
|
|
+ // 获取输出流
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ ManagedChannel channel = null;
|
|
|
+ response.setContentType("text/plain");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ String dataJson = "";
|
|
|
+ try {
|
|
|
+ channel = ManagedChannelBuilder.forAddress("10.0.0.24", 17070)
|
|
|
+ .usePlaintext()
|
|
|
+ .build();
|
|
|
+ InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
|
|
|
+ dataJson = "{\"bot_id\":\"b00001\",\"exp_id\":\"721\",\"norm\":\"" + category + "\",\"feedback\":" + feedback + ",\"session_id\":" + "\"" + sessionId + "\"" + ",\"generate_args\":{\"max_new_tokens\":1024,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{\"rows\":" + rows + "}}";
|
|
|
+ log.info("请求大模型的决策的参数为{}", dataJson);
|
|
|
+ PredictionsRequest request = PredictionsRequest.newBuilder()
|
|
|
+ .setModelName("slibra_bot")
|
|
|
+ .putInput("method", ByteString.copyFrom("decision_stream", "utf-8"))//推理
|
|
|
+ .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
|
|
|
+ .buildPartial();
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
|
|
|
+ outputStream.write("begin".getBytes());
|
|
|
+ outputStream.flush();
|
|
|
+ while (predictions.hasNext()) {
|
|
|
+ String responseStr = predictions.next().getPrediction().toStringUtf8();
|
|
|
+ log.info("决策流式返回的结果是{}", responseStr);
|
|
|
+ responseStr = JSON.parseObject(responseStr).getString("message");
|
|
|
+ if("complete".equals(responseStr)){
|
|
|
+ System.out.println("结尾语句并且是非JSON,无需处理");
|
|
|
+ //结束语句也流式输出,但是并不记录下来
|
|
|
+ outputStream.write(responseStr.getBytes());
|
|
|
+ outputStream.flush();
|
|
|
+ }else{
|
|
|
+// sb.append(responseStr);
|
|
|
+ resultData.add(responseStr);
|
|
|
+ outputStream.write(responseStr.getBytes());
|
|
|
+ outputStream.flush();
|
|
|
+ }
|
|
|
+ // todo 类型判断处理
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+ log.error("处理大模型推理异常,异常信息为{}", JSON.toJSONString(e));
|
|
|
+ } finally {
|
|
|
+ System.out.println(JSON.toJSONString(resultData));
|
|
|
+ // 关闭输出流
|
|
|
+ try {
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ channel.shutdown();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|