package com.yb.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.yb.config.R; import com.yb.config.XzConfig; import com.yb.service.AgentService; import com.yb.service.IntentionService; import com.yb.service.XzService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; @Tag(name = "Agent意图") @RestController @RequestMapping("/api/agent") @Api(tags = "Agent意图") @Component public class agentController { @Autowired private XzConfig xzConfig; @Autowired private AgentService agentService; @Autowired private IntentionService intentionService; @ApiOperation("意图识别") @PostMapping("/intention") @Operation(summary = "意图识别") public R getIntention(@RequestParam Map allParams) throws Exception { System.out.println("rec getMessage:" + allParams); String msg = allParams.get("message"); // 判断msg是否为空 if (StringUtils.isEmpty(msg)) { String info = agentService.getAgentIntention(xzConfig.agentUrl, xzConfig.agentIntent, "''"); JSONObject obj = JSONObject.parseObject(info); String cont = obj.getString("content"); System.out.println("rec isEmpty:" + cont); return R.error(cont); // } // 判断是否调用地图 boolean isMap = msg.matches("(?i).*@map.*"); HashMap hashMap = new HashMap(); if (isMap) { hashMap.put("type", "Map"); String funcInfo = agentService.getAgentIntention(xzConfig.agentUrl, xzConfig.agentIntent, msg); JSONObject jsonObject = JSON.parseObject(funcInfo); String orderString = jsonObject.getString("order"); if (orderString == null) { hashMap.put("type", "text"); JSONObject errInfo = JSON.parseObject(funcInfo); String erroContent = errInfo.getString("content"); hashMap.put("content", erroContent); return R.error(erroContent); } // 意图函数匹配 HashMap mapObj = intentionService.getFunctionMatch(orderString); hashMap.put("content", mapObj); return R.ok(hashMap); } else { String ragInfo = agentService.getAgentIntention(xzConfig.agentUrl, xzConfig.agentChat, msg); JSONObject jsonObject = JSON.parseObject(ragInfo); String answer = jsonObject.getString("content"); System.out.println("Answer: " + answer); hashMap.put("content", answer); hashMap.put("type", "Rag"); return R.ok(hashMap); } } }