From 3f1ea1e3240f045412189619d7e4021d5f26a4f2 Mon Sep 17 00:00:00 2001 From: leutu <leutu@qq.com> Date: 星期一, 24 六月 2024 14:14:17 +0800 Subject: [PATCH] apikey更新 --- src/main/java/com/yb/util/OkHttpUtil.java | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 155 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..fcdf3ee 100644 --- a/src/main/java/com/yb/util/OkHttpUtil.java +++ b/src/main/java/com/yb/util/OkHttpUtil.java @@ -1,11 +1,30 @@ package com.yb.util; +import com.alibaba.fastjson.JSON; +import com.google.gson.Gson; +import com.yb.entity.ResponseEntity; 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 +59,140 @@ } 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){ + System.out.println("fastgpt:"+message) ; + OkHttpClient client = new OkHttpClient(); + Response response = null ; + // 鍒涘缓璇锋眰澶达紙Headers锛� + okhttp3.Headers headers = new okhttp3.Headers.Builder() + .add("Authorization", "Bearer "+apikey) + .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","1111"); + 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(); + if( rep.indexOf("flowResponses") > 0) { + int index = rep.indexOf("flowResponses"); + int index_s = rep.indexOf("data", index); + String data = rep.substring(index_s + 5); + System.out.println(data); + return data; + }else{ + return rep ; + } + } else { + System.out.println("Error: " + response.code() + " " + response.message()); + } + } catch (IOException e) { + e.printStackTrace(); + } + + + return null ; + } + + public String requesFast_kl(){ + OkHttpClient client = new OkHttpClient(); + Response response = null ; + // 鍒涘缓璇锋眰澶达紙Headers锛� + okhttp3.Headers headers = new okhttp3.Headers.Builder() + .add("Authorization", "Bearer "+apikey) + .build(); + + /* + curl --location --request GET 'http://localhost:3000/api/core/dataset/list?parentId=' \ +--header 'Authorization: Bearer {{authorization}}' \ + */ + // 鏋勫缓璇锋眰浣擄紙RequestBody锛夛紝杩欓噷浣跨敤鐨勬槸瀛楃涓茬被鍨嬬殑璇锋眰鏁版嵁 + + + + // 鍒涘缓璇锋眰锛圧equest锛� + + 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().toString(); + + 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