leutu
2024-06-07 25bb09bc4e19103bdd55a8159c70a9642803b7b8
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
package com.yb.controller;
 
 
import com.alibaba.fastjson.JSON;
import com.yb.util.OkHttpUtil;
import jakarta.annotation.Resource;
 
import net.sf.json.JSONObject;
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;
 
 
@RestController
@RequestMapping("/api/v1")
public class AiController {
    @Resource
    private OllamaChatModel chatModel;
 
    @Autowired
    private OkHttpUtil okHttpUtil ;
 
    @Value("${spring.intent}")
    String intent ;
 
    @GetMapping("/chat")
    public String chat(@RequestParam(value = "message",defaultValue = "Hi") String message){
        return chatModel.call("请用中文回答如下问题,如果有地名出现,回到内容包括经纬度并使用json返回,"+message);
    }
 
    @GetMapping("/intent")
    public String intent(@RequestParam(value = "message",defaultValue = "Hi") String message){
        String json = okHttpUtil.getMessage(intent+"="+message);
 
        return json ;
    }
    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")
    public String mix(@RequestParam(value = "message",defaultValue = "Hi") String message){
        String json = okHttpUtil.getMessage(intent+"="+message);
//        JSONObject jsonObject = JSONObject.parseObject(json);
        Map<String,String> hashMap = new HashMap<>() ;
        iteraJson(json,hashMap);
 
        String place = hashMap.get("place");
        if( place != null ){
            String llm = chatModel.call("请用中文回答如下问题,如果有地名出现,回到内容包括经纬度并使用json返回,"+place);
           hashMap.put("content",llm);
 
          json =   JSON.toJSON(hashMap).toString() ;
        }else{
            json = hashMap.put("content", chatModel.call("请用中文回答如下问题,如果有地名出现,回到内容包括经纬度并使用json返回,"+message));
        }
        return json ;
 
 
    }
}