leutu
2024-06-27 b8b0b633bbe96658ba72ff78744591e5e1bdafd4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.yb.controller;
 
 
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 java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
 
@Tag(name="terra openai")
@RestController
@RequestMapping("/api/v1")
public class AiController {
    @Resource
    private OllamaChatModel chatModel;
 
    @Autowired
    private OkHttpUtil okHttpUtil ;
 
    @Value("${spring.intent}")
    String intent ;
 
 
 
    @GetMapping("/chat")
    @Operation(summary = "大模型对话")
    public String chat(@RequestParam(value = "message",defaultValue = "Hi") String message){
        return chatModel.call(message);
    }
 
 
    @GetMapping("/intent")
    @Operation(summary = "意图请求")
    public String intent(@RequestParam(value = "message",defaultValue = "Hi") String message){
        String json = okHttpUtil.getMessage(intent+"="+message);
        Map<String,String> hashMap = new HashMap<>() ;
        iteraJson(json,hashMap);
        intentSwitch(hashMap,message);
        return  JSON.toJSON(hashMap).toString() ;
    }
    public  boolean iteraJson(String str, Map res){
        if(str.toString().indexOf(":") == -1){
            return true;
        }
        JSONObject fromObject = JSONObject.fromObject(str);
        Iterator keys = fromObject.keys();
        while(keys.hasNext()){
            String key = keys.next().toString();
            Object value = fromObject.get(key);
            if(iteraJson(value.toString(),res)){
                res.put(key, value);
            }
        }
        return false;
 
    }
 
    @GetMapping("/mix")
    @Operation(summary = "意图与大模型请求")
    public String mix(@RequestParam(value = "message",defaultValue = "Hi") String message){
        String json = okHttpUtil.getMessage(intent+"="+message);
        System.out.println(message);
//        JSONObject jsonObject = JSONObject.parseObject(json);
        Map<String,String> hashMap = new HashMap<>() ;
        String place = null ;
        if( json != null) {
 
            iteraJson(json, hashMap);
            intentSwitch(hashMap,message);
            place = hashMap.get("place");
        }
        if( place != null ){
            String llm = chatModel.call(place);
           hashMap.put("content",llm);
 
          json =   JSON.toJSON(hashMap).toString() ;
        }else{
            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() ;
 
 
    }
 
    @GetMapping("/fastgpt")
    @Operation(summary = "知识库查询")
    public String fastgpt(@RequestParam(value = "message",defaultValue = "三维实景") String message){
 
        //okHttpUtil.requesFast(message);
        String json = okHttpUtil.requesFast(message);
 
        return  json ;
    }
 
    @GetMapping("/fastgpt_list")
    @Operation(summary = "知识库列表")
    public String fastgpt_list(){
 
 
        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");
        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");
        }
    }
}