leutu
2024-08-27 55e607b91e8058a7fe713fa07cd4c829b5593aa5
src/main/java/com/yb/controller/AiController.java
@@ -3,23 +3,19 @@
import com.alibaba.fastjson.JSON;
import com.yb.util.OkHttpUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import net.sf.json.JSONObject;
import okhttp3.Response;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
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.bind.annotation.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@Tag(name="terra openai")
@@ -35,20 +31,83 @@
    @Value("${spring.intent}")
    String intent ;
    @Autowired
    WebSocketController webSocketController;
    @GetMapping("/chat")
    @Operation(summary = "大模型对话")
    public String chat(@RequestParam(value = "message",defaultValue = "Hi") String message){
        return chatModel.call("请用中文回答如下问题,如果有地名出现,回到内容包括经纬度并使用json返回,"+message);
        return chatModel.call(message);
    }
    @GetMapping("/ai")
    @Operation(summary = "大模型对话")
    public String ai(@RequestParam(value = "message",defaultValue = "Hi") String message){
        System.out.println("rec message:"+message);
        HashMap<String,String> hash = new HashMap<>();
        hash.put("func","flyTo()");
        hash.put("content","x:120.9671,y:13.5748");
        message = JSON.toJSONString(hash);
        return webSocketController.sendAllMessage(message);
    }
    @GetMapping("/ai/{place}/{funcName}/showPlaceMap")
    @Operation(summary = "大模型对话")
    public String intentShowMap(@PathVariable("place")String place,@PathVariable("funcName")String funcName){
        System.out.println("rec message:"+place);
        HashMap<String,Object> hash = new HashMap<>();
        hash.put("func","flyTo()");
        HashMap<String,String> xy = new HashMap<>();
        xy.put("x","120.9671");
        xy.put("y","13.5748");
        hash.put("content",xy);
        webSocketController.sendAllMessage(JSON.toJSONString(hash));
        HashMap<String,Object> result = new HashMap<>();
        result.put("code","200");
        result.put("message","成功");
        result.put("data","");
        return JSON.toJSONString(result);
    }
    @GetMapping("/intent")
    @Operation(summary = "意图请求")
    public String intent(@RequestParam(value = "message",defaultValue = "Hi") String message){
        String json = okHttpUtil.getMessage(intent+"="+message);
        if( message.toLowerCase().indexOf("x") == 0){
            return getString(message.substring(1));
        }
        String r = intent+"=\""+message+"\"";
        System.out.println(r);
        String json = okHttpUtil.getMessage(r);
        System.out.println(json);
        Map<String,String> hashMap = new HashMap<>() ;
        if( json == null){
            hashMap.put("status","500");
            //String llm = chatModel.call(message);
            //hashMap.put("content","识别异常,"+llm);
            if( hashMap.get("name") == null ){
                String json2 = okHttpUtil.requesFast(message);
                return  json2 ;
            }
            return  JSON.toJSON(hashMap).toString() ;
        }
        iteraJson(json,hashMap);
        if( hashMap.get("name") == null ){
            String json2 = okHttpUtil.requesFast(message);
            return  json2 ;
        }
        intentSwitch(hashMap,message);
        return  JSON.toJSON(hashMap).toString() ;
    }
    private String getString(String message) {
        String json = okHttpUtil.requesFast(message.substring(1));
        return json;
    }
    public  boolean iteraJson(String str, Map res){
        if(str.toString().indexOf(":") == -1){
            return true;
@@ -67,22 +126,28 @@
    }
    @GetMapping("/mix")
    @Operation(summary = "意图与大模型请求")
    public String mix(@RequestParam(value = "message",defaultValue = "Hi") String message){
        String json = okHttpUtil.postFastgpt(message);
        String json = okHttpUtil.getMessage(intent+"="+message);
        System.out.println(message);
//        JSONObject jsonObject = JSONObject.parseObject(json);
        Map<String,String> hashMap = new HashMap<>() ;
        iteraJson(json,hashMap);
        String place = null ;
        if( json != null) {
        String place = hashMap.get("place");
            iteraJson(json, hashMap);
            intentSwitch(hashMap,message);
            place = hashMap.get("place");
        }
        if( place != null ){
            String llm = chatModel.call("请用中文回答如下问题,如果有地名出现,回到内容包括经纬度并使用json返回,"+place);
            String llm = chatModel.call(place);
           hashMap.put("content",llm);
          json =   JSON.toJSON(hashMap).toString() ;
        }else{
            json = hashMap.put("content", chatModel.call("请用中文回答如下问题,如果有地名出现,回到内容包括经纬度并使用json返回,"+message));
            json = hashMap.put("content", chatModel.call(message));
        }
        hashMap.put("code","200");
        System.out.println(JSON.toJSON(hashMap).toString());
        System.out.println("====================================");
        return JSON.toJSON(hashMap).toString() ;
@@ -91,20 +156,97 @@
    }
    @GetMapping("/fastgpt")
    public String fastgpt(@RequestParam(value = "message",defaultValue = "Hi") String message){
    @Operation(summary = "知识库查询")
    public String fastgpt(@RequestParam(value = "message",defaultValue = "055") String message){
        okHttpUtil.requesFast(message);
        //okHttpUtil.requesFast(message);
        String json = okHttpUtil.requesFast(message);
        return  json ;
    }
    @GetMapping("/fastgpt_list")
    public String fastgpt_list(String parentId){
    @Operation(summary = "知识库列表")
    public String fastgpt_list(){
        String json = okHttpUtil.requesFast_kl(parentId);
        String json = okHttpUtil.requesFast_kl();
        return  json ;
    }
    /*
  def anomalyCheck(time,place):    【映射到意图1】
  """异常情况查询。查询在某个时间某个地点的异常情况,与往日有何不同。查看某个地点的卫星图片。"""
  scanWeapon();
  =======================================
  def weaponParameters(weapons):   【映射到意图2】
  """武器详细参数查询。查询武器的详细信息。查询武器的具体参数。"""
  raise ToolException(f"没有名称为{weapons}的武器。")
  weaponParameters();
  =========================================
  def searchTarget(place, entity):    【映射到意图3,4,5,6
【1. 输入参数为:政治目标,社会目标,军事目标,民宅,医院对应意图3.4
searchTarget();
2.输入参数为:可打击目标/不可打击目标对应意图5
searchStrikeableTarget();
3.输入参数为:美国空军指挥中心,对应意图6】
  """查询某地的政治目标/社会目标/经济目标/军事目标/民宅/医院/可打击目标/不可打击目标等建筑实体的情况"""
  USAFCommandCenterQuery();
def attackanddefenceCenter(weapons: str, place: str):  #【映射到意图7.8。
    这里让你判断是杀伤性武器还是防御性武器,杀伤性武器的就对对应意图7杀伤半径,
    防御性武器就对应意图8防御半径。】
  """该工具的输入必须为武器名称和地点。武器模拟仿真,给出某种场景。武器分析,地图仿真指令。或者查询武器的杀伤半径/防空半径/杀伤范围/防空范围。"""
attackUSAFCommandCenter();
defenceRangeTyphoonSystem();
   */
    private void intentSwitch(Map<String,String>hash,String message){
        if( hash.get("name") == null ) return ;
//        if( hash.get("name").equals("anomalyCheck")) hash.put("name","scanWeapon");
//        if( hash.get("name").equals("weaponParameters")) hash.put("name","weaponParameters");
        String ws = " 堤丰武器系统 堤丰系统" +
                "圣安东尼奥级" +
                " C130 C130飞机  C130运输机  C130炮艇" +
                "何塞黎刹级系统" +
                " 鱼鹰武器系统 鱼鹰系统" +
                "丹鹿级武器系统 丹鹿级系统" +
                "先锋级武器系统 先锋级系统" +
                "支奴干武器系统 支奴干直升机 支奴干系统 支奴干运输机" +
                "黑鹰武器系统 黑鹰直升机 黑鹰系统 黑鹰武装直升机";
        if( hash.get("name").toLowerCase().equals("weaponsystemparametersinput")
                || hash.get("name").toLowerCase().equals("weaponsystemparameters")){
            if(ws.indexOf(hash.get("weapons")) > -1 )hash.put("name","weaponSystemParameters");
            else {
                hash.put("name", "weaponSystemParameters");
                hash.put("weapons", "");
                String llm = chatModel.call(message);
                System.out.println("llm:" + llm);
                hash.put("content", llm);
            }
        }
        if( hash.get("name").equals("searchTarget")) {
            String entity = hash.get("entity");
            if( entity.indexOf("政治")>=0 || entity.indexOf("社会")>=0||entity.indexOf("军事")>=0 ||
                    entity.indexOf("民宅")>=0 || entity.indexOf("医院")>=0 || entity.indexOf("其它")>=0)
                hash.put("name","searchTarget");
            if( entity.indexOf("打击")>=0 )
                hash.put("name","searchStrikeableTarget");
            if( entity.indexOf("美国")>=0 || entity.indexOf("空军")>=0||entity.indexOf("指挥")>=0
                ||entity.indexOf("中心")>=0)
                hash.put("name","USAFCommandCenterQuery");
        }
        if( hash.get("name").equals("attackanddefenceCenter")) {
            if( message.indexOf("打击")>=0 || message.indexOf("杀伤")>=0 || message.indexOf("摧毁")>=0 ||
                    message.indexOf("破坏")>=0)
                hash.put("name","attackUSAFCommandCenter");
            else
                hash.put("name","defenceRangeTyphoonSystem");
        }
    }
}