From 28fc8b028099f0fd4719b0a35d4fb98a728d73cd Mon Sep 17 00:00:00 2001 From: leutu <leutu@qq.com> Date: 星期三, 19 六月 2024 14:20:57 +0800 Subject: [PATCH] 新增对fastgpt api请求知识库接口 --- src/main/java/com/yb/util/OkHttpUtil.java | 143 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 143 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/yb/util/OkHttpUtil.java b/src/main/java/com/yb/util/OkHttpUtil.java index c7a9807..94d050e 100644 --- a/src/main/java/com/yb/util/OkHttpUtil.java +++ b/src/main/java/com/yb/util/OkHttpUtil.java @@ -1,11 +1,28 @@ package com.yb.util; +import com.alibaba.fastjson.JSON; import okhttp3.*; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RestController; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + @Component +@RestController public class OkHttpUtil { + @Value("${spring.fastgpt.apikey}") + String apikey ; + + @Value("${spring.fastgpt.url}") + String url ; + + + @Value("${spring.fastgpt.kl}") + String fastgpt_kl ; // get璇锋眰 public String getMessage(String url) { try { @@ -40,4 +57,130 @@ } return null ; } + + public String postFastgpt( String params) { + try { + OkHttpClient client = new OkHttpClient(); + RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params); + Request request = new Request.Builder() + .addHeader("Authorization"," Bearer "+apikey) + .addHeader("Content-Type","application/json") + .url(url) + .post(body) + .build(); + + Response response = client.newCall(request).execute(); + return response.body().string(); + + } catch (IOException e) { + System.out.println(e.getMessage()); + } + return null ; + } + + public String requesFast(String message){ + OkHttpClient client = new OkHttpClient(); + Response response = null ; + // 鍒涘缓璇锋眰澶达紙Headers锛� + okhttp3.Headers headers = new okhttp3.Headers.Builder() + .add("Authorization", "Bearer fastgpt-rML09d7nyx8Bn2Al3UipZia3Q8FNuXdekU1nyMlgCfHNwS8NN1giSPNZ1y6Wmt") + .build(); + + /* + curl --location --request POST 'https://api.fastgpt.in/api/v1/chat/completions' \ + --header 'Authorization: Bearer fastgpt-xxxxxx' \ + --header 'Content-Type: application/json' \ + --data-raw '{ + "chatId": "111", + "stream": false, + "detail": false, + "messages": [ + { + "content": "瀵兼紨鏄皝", + "role": "user" + } + ] + }' + */ + // 鏋勫缓璇锋眰浣擄紙RequestBody锛夛紝杩欓噷浣跨敤鐨勬槸瀛楃涓茬被鍨嬬殑璇锋眰鏁版嵁 + + HashMap<String,Object> hashMap = new HashMap<>() ; + hashMap.put("chatId",""+Math.random()*100000); + hashMap.put("stream","false"); + hashMap.put("detail","false"); + List<HashMap<String,String>> list = new ArrayList<>(); + HashMap<String,String> messageMap = new HashMap<>(); + messageMap.put("content",message); + messageMap.put("role","user"); + list.add(messageMap); + hashMap.put("messages",list); + MediaType contentType = MediaType.parse("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(contentType, JSON.toJSON(hashMap).toString()); + + // 鍒涘缓璇锋眰锛圧equest锛� + okhttp3.Request.Builder builder = new okhttp3.Request.Builder() + .url(url) + .headers(headers) + .post(body); + okhttp3.Request request = builder.build(); + + // 鍙戦�佽姹傚苟鑾峰彇鍝嶅簲 + try { + response = client.newCall(request).execute(); + if (response.isSuccessful()) { + //System.out.println("Success: " + response.body().string()); + String rep = response.body().string(); + return rep ; + } else { + System.out.println("Error: " + response.code() + " " + response.message()); + } + } catch (IOException e) { + e.printStackTrace(); + } + + + return response != null?response.body().toString():"null" ; + } + + public String requesFast_kl(String parentId){ + OkHttpClient client = new OkHttpClient(); + Response response = null ; + // 鍒涘缓璇锋眰澶达紙Headers锛� + okhttp3.Headers headers = new okhttp3.Headers.Builder() + .add("Authorization", "Bearer fastgpt-rML09d7nyx8Bn2Al3UipZia3Q8FNuXdekU1nyMlgCfHNwS8NN1giSPNZ1y6Wmt") + .build(); + + /* + curl --location --request GET 'http://localhost:3000/api/core/dataset/list?parentId=' \ +--header 'Authorization: Bearer {{authorization}}' \ + */ + // 鏋勫缓璇锋眰浣擄紙RequestBody锛夛紝杩欓噷浣跨敤鐨勬槸瀛楃涓茬被鍨嬬殑璇锋眰鏁版嵁 + + + + // 鍒涘缓璇锋眰锛圧equest锛� + if( parentId != null ) fastgpt_kl = fastgpt_kl+"锛焢arentId="+parentId; + okhttp3.Request.Builder builder = new okhttp3.Request.Builder() + .url(fastgpt_kl) + .headers(headers) ; + + okhttp3.Request request = builder.build(); + + // 鍙戦�佽姹傚苟鑾峰彇鍝嶅簲 + try { + response = client.newCall(request).execute(); + if (response.isSuccessful()) { + //System.out.println("Success: " + response.body().string()); + String rep = response.body().string(); + return rep ; + } else { + System.out.println("Error: " + response.code() + " " + response.message()); + } + } catch (IOException e) { + e.printStackTrace(); + } + + + return response != null?response.body().toString():"null" ; + } } -- Gitblit v1.9.3