• 使用Java拓展本地开源大模型的网络搜索问答能力


    在这里插入图片描述

    背景

    开源大模型通常不具备最新语料的问答能力。因此需要外部插件的拓展,目前主流的langChain框架已经集成了网络搜索的能力。但是作为一个倔强的Java程序员,还是想要用Java去实现。

    注册SerpAPI

    Serpapi 提供了多种搜索引擎的搜索API接口。
    访问 Serpapi 官网上注册一个用户:

    https://serpapi.com/

    在这里插入图片描述

    可以选择Free Plan,提供每月100次的免费使用。接下来就是使用自己的邮箱和手机号进行注册。
    在这里插入图片描述
    注册成功登录:
    在这里插入图片描述

    创建SerpApiHttp对象

    public class SerpApiHttp {
    
        private int httpConnectionTimeout;
        private int httpReadTimeout;
    
        /**
         * 后端服务地址
         */
        private static final String BACK_END = "https://serpapi.com";
    
        /**
         * 初始化Gson对象
         */
        private static Gson gson = new Gson();
    
        /**
         * 当前后端HTTP路径
         */
        public String path;
    
        /***
         * 构造函数
         * @param path HTTP url路径
         */
        public SerpApiHttp(String path) {
            this.path = path;
        }
    
        /***
         * 建立Socket连接
         *
         * @param path URL端点
         * @param parameter 客户端参数,如: { "q": "coffee", "location": "Austin, TX"}
         * @return HttpURLConnection 连接对象
         * @throws SerpApiException 包装错误信息
         */
        protected HttpURLConnection connect(String path, Map<String, String> parameter) throws SerpApiException {
            HttpURLConnection con;
            try {
                //allowHTTPS(); // 允许HTTPS支持
                String query = ParameterStringBuilder.getParamsString(parameter);
                URL url = new URL(BACK_END + path + "?" + query);
                con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
            } catch (IOException e) {
                throw new SerpApiException(e);
            } catch (Exception e) {
                e.printStackTrace();
                throw new SerpApiException(e);
            }
    
            String outputFormat = parameter.get("output");
            if (outputFormat == null) {
                throw new SerpApiException("output format must be defined: " + path);
            } else if (outputFormat.startsWith("json")) {
                con.setRequestProperty("Content-Type", "application/json");
            }
            con.setConnectTimeout(getHttpConnectionTimeout());
            con.setReadTimeout(getHttpReadTimeout());
    
            con.setDoOutput(true);
            return con;
        }
    
        /***
         * 返回HTTP响应内容的原始字符串
         *
         * @param parameter 用户客户端参数
         * @return HTTP响应体
         * @throws SerpApiException 包装错误信息
         */
        public String get(Map<String, String> parameter) throws SerpApiException {
            HttpURLConnection con = connect(this.path, parameter);
    
            // 获取HTTP状态码
            int statusCode = -1;
            // 保存响应流
            InputStream is = null;
            // 读取缓冲区
            BufferedReader in = null;
            try {
                statusCode = con.getResponseCode();
    
                if (statusCode == 200) {
                    is = con.getInputStream();
                } else {
                    is = con.getErrorStream();
                }
    
                Reader reader = new InputStreamReader(is);
                in = new BufferedReader(reader);
            } catch (IOException e) {
                throw new SerpApiException(e);
            }
    
            String inputLine;
            StringBuilder content = new StringBuilder();
            try {
                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }
                in.close();
            } catch (IOException e) {
                throw new SerpApiException(e);
            }
    
            // 断开连接
            con.disconnect();
    
            if (statusCode != 200) {
                triggerSerpApiException(content.toString());
            }
            return content.toString();
        }
    
        /**
         * 在错误情况下触发异常
         *
         * @param content 从serpapi.com返回的原始JSON响应
         * @throws SerpApiException 包装错误信息
         */
        protected void triggerSerpApiException(String content) throws SerpApiException {
            String errorMessage;
            try {
                JsonObject element = gson.fromJson(content, JsonObject.class);
                errorMessage = element.get("error").getAsString();
            } catch (Exception e) {
                throw new AssertionError("invalid response format: " + content);
            }
            throw new SerpApiException(errorMessage);
        }
    
        /**
         * @return 当前HTTP连接超时时间
         */
        public int getHttpConnectionTimeout() {
            return httpConnectionTimeout;
        }
    
        /**
         * @param httpConnectionTimeout 设置HTTP连接超时时间
         */
        public void setHttpConnectionTimeout(int httpConnectionTimeout) {
            this.httpConnectionTimeout = httpConnectionTimeout;
        }
    
        /**
         * @return 当前HTTP读取超时时间
         */
        public int getHttpReadTimeout() {
            return httpReadTimeout;
        }
    
        /**
         * @param httpReadTimeout 设置HTTP读取超时时间
         */
        public void setHttpReadTimeout(int httpReadTimeout) {
            this.httpReadTimeout = httpReadTimeout;
        }
    }
    
    • 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

    创建SerpApi对象

    public class SerpApi extends Exception {
        /**
         * 客户端参数
         */
        private final Map<String, String> parameter;
    
        /**
         * 初始化 gson
         */
        private static final Gson gson = new Gson();
    
        /**
         * Java 7+ 的 https 客户端实现
         */
        private final SerpApiHttp client;
    
        /**
         * 默认 HTTP 客户端超时时间
         */
        private static final Integer TIME_OUT = 60000;
    
        /**
         * 搜索路径
         */
        private static final String SEARCH_PATH = "/search";
    
        /***
         * 构造函数
         *
         * @param parameter 默认搜索参数,应包括 {"api_key": "secret_api_key", "engine": "google" }
         */
        public SerpApi(Map<String, String> parameter) {
            this.parameter = parameter;
            this.client = new SerpApiHttp(SEARCH_PATH);
            this.client.setHttpConnectionTimeout(TIME_OUT);
        }
    
        /***
         * 返回原始HTML搜索结果
         *
         * @param parameter HTML搜索参数
         * @return 从客户端引擎获取的原始HTML响应,用于自定义解析
         * @throws SerpApiException 封装后端错误消息
         */
        public String html(Map<String, String> parameter) throws SerpApiException {
            return get("/client", "html", parameter);
        }
    
        /***
         * 返回JSON格式的搜索结果
         *
         * @param parameter 自定义搜索参数,可覆盖构造函数中提供的默认参数
         * @return JSON对象,包含搜索结果的顶层节点
         * @throws SerpApiException 封装后端错误消息
         */
        public JsonObject search(Map<String, String> parameter) throws SerpApiException {
            return json(SEARCH_PATH, parameter);
        }
    
        /***
         * 使用Location API返回位置信息
         *
         * @param parameter 必须包括 {q: "city", limit: 3}
         * @return JSON数组,使用Location API返回的位置信息
         * @throws SerpApiException 封装后端错误消息
         */
        public JsonArray location(Map<String, String> parameter) throws SerpApiException {
            String content = get("/locations.json", "json", parameter);
            JsonElement element = gson.fromJson(content, JsonElement.class);
            return element.getAsJsonArray();
        }
    
        /***
         * 通过Search Archive API检索搜索结果
         *
         * @param id 搜索的唯一标识符
         * @return 客户端结果的JSON对象
         * @throws SerpApiException 封装后端错误消息
         */
        public JsonObject searchArchive(String id) throws SerpApiException {
            return json("/searches/" + id + ".json", null);
        }
    
        /***
         * 使用Account API获取账户信息
         *
         * @param parameter 包含api_key的Map,如果未在默认客户端参数中设置
         * @return JSON对象,账户信息
         * @throws SerpApiException 封装后端错误消息
         */
        public JsonObject account(Map<String, String> parameter) throws SerpApiException {
            return json("/account.json", parameter);
        }
    
        /***
         * 使用Account API获取账户信息
         *
         * @return JSON对象,账户信息
         * @throws SerpApiException 封装后端错误消息
         */
        public JsonObject account() throws SerpApiException {
            return json("/account.json", null);
        }
    
        /***
         * 将HTTP内容转换为JsonValue
         *
         * @param endpoint 原始JSON HTTP响应
         * @return 通过gson解析器创建的JSON对象
         */
        private JsonObject json(String endpoint, Map<String, String> parameter) throws SerpApiException {
            String content = get(endpoint, "json", parameter);
            JsonElement element = gson.fromJson(content, JsonElement.class);
            return element.getAsJsonObject();
        }
    
        /***
         * 获取HTTP客户端
         *
         * @return 客户端实例
         */
        public SerpApiHttp getClient() {
            return this.client;
        }
    
        /***
         * 扩展现有参数构建Serp API查询
         *
         * @param path 后端HTTP路径
         * @param output 输出类型(json, html, json_with_images)
         * @param parameter 自定义搜索参数,可覆盖默认参数
         * @return 格式化参数HashMap
         * @throws SerpApiException 封装后端错误消息
         */
        public String get(String path, String output, Map<String, String> parameter) throws SerpApiException {
            // 更新客户端路径
            this.client.path = path;
    
            // 创建HTTP查询
            Map<String, String> query = new HashMap(16);
    
            if (path.startsWith("/searches")) {
                // 仅保留API_KEY
                query.put("api_key", this.parameter.get("api_key"));
            } else {
                // 合并默认参数
                query.putAll(this.parameter);
            }
    
            // 用自定义参数覆盖默认参数
            if (parameter != null) {
                query.putAll(parameter);
            }
    
            // 设置当前编程语言
            query.put("source", "java");
    
            // 设置输出格式
            query.put("output", output);
    
            return this.client.get(query);
        }
    }
    
    • 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

    构建WebSearchChain

    public class WebSearchChain {
    
        /**
         * apiKey
         */
        private String apiKey;
    
        /**
         * 构造函数
         * @param apiKey
         */
        public WebSearchChain(String apiKey){
            this.apiKey = apiKey;
        }
    
        /**
         * 初始化
         * @param apiKey
         * @return
         */
        public static WebSearchChain fromLlm(String apiKey){
            return new WebSearchChain(apiKey);
        }
    
        /**
         * 搜索
         * @param question
         * @return
         */
        public String search(String question){
            Map<String, String> parameter = new HashMap<>();
            parameter.put("api_key", apiKey);
            parameter.put("q", question);
            parameter.put("hl", "zh-cn");
            parameter.put("gl", "cn");
            parameter.put("google_domain", "google.com");
            parameter.put("safe", "active");
            parameter.put("start", "10");
            parameter.put("num", "10");
            parameter.put("device", "desktop");
    
            SerpApi serpapi = new SerpApi(parameter);
            JsonObject results = null;
            StringBuilder stringBuilder = new StringBuilder();
            try {
                results = serpapi.search(parameter);
                results.getAsJsonArray("organic_results").forEach(organicResult->{
                    JsonObject result = organicResult.getAsJsonObject();
                    String title = result.getAsJsonPrimitive("title").getAsString();
                    String snippet = result.getAsJsonPrimitive("snippet").getAsString();
                    stringBuilder.append(title).append("。").append(snippet).append("。");
                });
            } catch (SerpApiException e) {
                e.printStackTrace();
            }
            return stringBuilder.toString();
        }
    }
    
    • 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

    使用

    博主之前借鉴langChain的思路封装一个Java版的框架,可参考:https://blog.csdn.net/weixin_44455388/article/details/137098743?spm=1001.2014.3001.5501

    因此,直接调用即可:

    public static void test7() {
        String prompt = "吴亦凡犯了什么事";
        OpenAIChat openAIChat = OpenAIChat.builder().endpointUrl("http://192.168.0.84:9997/v1").model("Qwen1.5-14B-Chat").build().init();
        WebSearchChain webSearchChain = WebSearchChain.fromLlm("48d1bd8f7419xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        String searchResult = webSearchChain.search(prompt);
        Flux<String> stringFlux = openAIChat.streamChatWithChain("112233", "你是一个AI助手", searchResult, prompt);
        stringFlux.subscribe();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

  • 相关阅读:
    编程每日一练(多语言实现)基础篇:求100~200之间的素数
    遭灰熊做空,蔚来汽车在股价跳水后选择了回应
    termux使用
    基础算法相关笔记
    Openlayers 自定义气泡框以及定位到气泡框
    C Primer Plus(6) 中文版 第3章 数据和C 3.6 参数和陷阱
    C++:关联式容器map的使用
    分割数组的最大值问题
    Kibana生产上的常用功能总结
    工业 web4.0 的 UI 卓越非凡
  • 原文地址:https://blog.csdn.net/weixin_44455388/article/details/137340000