赞
踩
我们在逛淘宝拼多多的时候,看到一个喜欢的商品,就会像客服问很多问题,这些问题有很多重复的,如果客服每次都回答,会浪费很多时间和人力,这个时候就可以整理成FAQ(Frequently Asked Questions,经常问到的问题),客户当然不会费劲去看这些FAQ,他们只关心自己提问的问题,这时就需要用到自然语言处理和知识库了。
MiniMax官网: MiniMax-与用户共创智能
MiniMax开发平台: MiniMax-与用户共创智能
账户信息中查看groupID
接口秘钥中新建秘钥
以下代码使用了Hutool工具类
第一步先创建知识库,保存返回的knowledgeId,后面会用到
-
- public void createKnowledge() {
- String url = buildMiniMaxUrl("embedding/create_knowledge_base");
- JSONObject data = new JSONObject();
- data.putOpt("operator_id", System.currentTimeMillis());
- data.putOpt("name", "我的知识库test");
- data.putOpt("embedding_model", "kbq-001");
- String result = buildMiniMaxHeader(HttpRequest.post(url))
- .body(data.toString())
- .execute().body();
- validMiniMaxResult(result);
- JSONObject resp = new JSONObject(result);
- long knowledgeBaseId = resp.getLong("knowledge_base_id");
- System.out.println(resp);
- }
-
- private String buildMiniMaxUrl(String url) {
- StringBuilder sb = new StringBuilder("https://api.minimax.chat/v1/");
- sb.append(url);
- sb.append(url.indexOf("?") == -1 ? "?" : "&");
- sb.append("GroupId=" + groupId);
- return sb.toString();
- }
-
- private HttpRequest buildMiniMaxHeader(HttpRequest request) {
- request.header("content-type", "application/json");
- request.header("Authorization", "Bearer " + secretKey);
- return request;
- }
-
- private void validMiniMaxResult(String result) {
- JSONObject json = new JSONObject(result);
- JSONObject baseResp = json.getJSONObject("base_resp");
- if (baseResp.getInt("status_code") != 0) {
- log.error("调用miniMax接口错误: {}", result);
- throw new ServiceFailException(baseResp.getStr("status_msg"));
- }
- }
上传文档保存返回的file_id
- public void filesUpload() {
- String url = buildMiniMaxUrl("files/upload");
- Map params = new HashMap();
- params.put("purpose", "retrieval");
- params.put("file", new File("d:/C中心.docx"));
- String result = buildMiniMaxHeader(HttpRequest.post(url))
- .form(params)
- .execute().body();
- validMiniMaxResult(result);
- JSONObject resp = new JSONObject(result);
- long fileId = resp.getJSONObject("file").getLong("file_id");
- System.out.println(result);
- }
- public void addDocument() {
- String url = buildMiniMaxUrl("embedding/add_document");
- JSONObject data = new JSONObject();
- data.putOpt("operator_id", System.currentTimeMillis());
- data.putOpt("knowledge_base_id", knowledge_base_id);
- data.putOpt("file_id", file_id);
- data.putOpt("doc_params", new JSONObject()
- .putOpt("chunk_size", 1000)
- .putOpt("chunk_overlap", 100));
- String result = buildMiniMaxHeader(HttpRequest.post(url))
- .body(data.toString())
- .execute().body();
- validMiniMaxResult(result);
- JSONObject resp = new JSONObject(result);
- System.out.println(result);
- }
由于检索知识库用到了流式返回,所以需要用到okhttp库
- <dependency>
- <groupId>com.squareup.okhttp3</groupId>
- <artifactId>okhttp</artifactId>
- <version>4.10.0</version>
- </dependency>
- <dependency>
- <groupId>com.squareup.okhttp3</groupId>
- <artifactId>okhttp-sse</artifactId>
- <version>4.10.0</version>
- </dependency>
- public void chatcompletion() throws IOException {
- StringBuilder sb = new StringBuilder();
- BufferedReader reader = null;
- String url = buildMiniMaxUrl("text/chatcompletion_pro");
- JSONObject data = new JSONObject()
- .putOpt("bot_setting", new JSONArray()
- .set(new JSONObject()
- .putOpt("bot_name", "MM智能助理")
- .putOpt("content", "MM智能助理是一款由MiniMax自研的,没有调用其他产品的接口的大型语言模型。MiniMax是一家中国科技公司,一直致力于进行大模型相关的研究。")))
- .putOpt("model", "abab6.5s-chat")
- .putOpt("mask_sensitive_info", false)
- .putOpt("stream", true)
- .putOpt("reply_constraints"
- , new JSONObject()
- .putOpt("sender_type", "BOT")
- .putOpt("sender_name", "MM智能助理"))
- .putOpt("messages", new JSONArray()
- .set(new JSONObject()
- .putOpt("sender_type", "USER")
- .putOpt("sender_name", "用户")
- .putOpt("text", "卖家直送物流目前可以支持配送到哪些地区")))
- .putOpt("knowledge_base_param", new JSONObject()
- .putOpt("knowledge_base_id", knowledge_base_id));
- //System.out.println(data.toString());
- Request okhttpRequest = new Request.Builder()
- .url(url)
- .post(RequestBody.create(data.toString(), MediaType.get(ContentType.JSON.getValue())))
- .addHeader("Authorization", "Bearer " + secretKey)
- .build();
- Call call = new OkHttpClient.Builder()
- .connectTimeout(10, TimeUnit.SECONDS)
- .writeTimeout(60, TimeUnit.SECONDS)
- .readTimeout(120, TimeUnit.SECONDS)
- .build()
- .newCall(okhttpRequest);
- Response okhttpResponse = call.execute();
- reader = new BufferedReader(okhttpResponse.body().charStream());
-
- String line;
- int totalToken = 0;
- while ((line = reader.readLine()) != null) {
- if (cn.hutool.core.util.StrUtil.isBlank(line)) {
- continue;
- }
- System.out.println(line);
- if (JSONUtil.isTypeJSON(line)) {
- JSONObject json = new JSONObject(line);
- JSONObject baseResp = json.getJSONObject("base_resp");
- if (baseResp.getInt("status_code") != 0) {
- throw new RuntimeException(baseResp.getStr("status_msg"));
- }
- }
- line = cn.hutool.core.util.StrUtil.replace(line, "data: ", cn.hutool.core.util.StrUtil.EMPTY);
- if (StrUtil.equals("[DONE]", line) || !JSONUtil.isTypeJSON(line)) {
- System.out.println(sb.toString());
- }
- JSONObject lineObj = JSONUtil.parseObj(line);
- if (lineObj != null && lineObj.getJSONObject("usage") != null) {
- totalToken = lineObj.getJSONObject("usage").getInt("total_tokens");
- }
- Optional.of(lineObj)
- .map(x -> x.getJSONArray("choices"))
- .filter(CollUtil::isNotEmpty)
- .map(x -> (JSONObject) x.get(0))
- .filter(object -> !"stop".equals(object.getStr("finish_reason")))
- .map(x -> x.getJSONArray("messages"))
- .filter(CollUtil::isNotEmpty)
- .map(x -> (JSONObject) x.get(0))
- .ifPresent(x -> {
- sb.append(x.getStr("text"));
- });
- }
- System.out.println(totalToken);
- System.out.println(sb.toString());
- }
以上是FAQ内容,我们看看知识库的问答
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。