package com.yb.util; import com.alibaba.fastjson.JSON; import okhttp3.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; 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; import java.util.concurrent.TimeUnit; @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 { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(5, TimeUnit.SECONDS) .readTimeout(5, TimeUnit.SECONDS) .writeTimeout(5, TimeUnit.SECONDS) .build(); Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); return response.body().string(); }catch (IOException e){ System.out.println(e.getMessage()); } return null ; } // post请求 public String postMessage(String url, String params) { try { OkHttpClient client = new OkHttpClient(); RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params); Request request = new Request.Builder() .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 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 ; } @Async 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 hashMap = new HashMap<>() ; hashMap.put("chatId","1111"); hashMap.put("stream","false"); hashMap.put("detail","false"); List> list = new ArrayList<>(); HashMap 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()); // 创建请求(Request) 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 is ok...."); 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),这里使用的是字符串类型的请求数据 // 创建请求(Request) 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" ; } }