leutu
2024-06-24 3f1ea1e3240f045412189619d7e4021d5f26a4f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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 {
            OkHttpClient client = new OkHttpClient();
            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 ;
    }
 
    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());
 
        // 创建请求(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);
                    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" ;
    }
}