| | |
| | | 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 { |
| | |
| | | } |
| | | 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()); |
| | | |
| | | // 创建请求(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(); |
| | | 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),这里使用的是字符串类型的请求数据 |
| | | |
| | | |
| | | |
| | | // 创建请求(Request) |
| | | if( parentId != null ) fastgpt_kl = fastgpt_kl+"?parentId="+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" ; |
| | | } |
| | | } |