zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
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
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jeecg.JeecgLimsCloudApplication;
import org.jeecg.modules.lims.service.IWxService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.io.IOException;
 
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = JeecgLimsCloudApplication.class)
public class Test {
    @Autowired
    private IWxService wxService;
    @org.junit.Test
    public void test(){
        wxService.getWxAccessToken();
    }
 
 
    private static final String API_KEY = "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";
    private static final String API_URL = "https://api.openai.com/v1/chat/completions";
 
    public static String chatWithGPT(String prompt) throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
 
        HttpPost httpPost = new HttpPost(API_URL);
        httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + API_KEY);
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        String mode = "gpt-3.5-turbo";
        StringEntity requestBody = new StringEntity("{\"prompt\": \"" + prompt + "\", \"max_tokens\": 100, \"model\": \"" + mode + "\" }");
        httpPost.setEntity(requestBody);
 
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String responseBody = EntityUtils.toString(entity);
 
        return responseBody;
    }
 
    @org.junit.Test
    public void t(){
        try {
            String userPrompt = "Hello, ChatGPT!";
            String response = chatWithGPT(userPrompt);
            System.out.println("ChatGPT response: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}