办学质量监测教学评价系统
ageer
2024-01-16 1f7f97e86a79ee4f1515d753e84a1e20a3ec41fb
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
package com.xmzs.common.chat.openai;
 
import okhttp3.*;
 
import java.io.IOException;
 
public class TestOpenAIAPI {
 
    private final OkHttpClient client = new OkHttpClient();
    private static final String API_KEY = "sk-Waea254YSRYVg4FZVCz2CDz73B22xRpmKpJ41kbczVgpPxvg";
    private static final String URL = "https://api.gptgod.online/v1/chat/completions";
 
    public void getChatGptResponse(String prompt) throws IOException {
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"),
            "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"" + prompt + "\"}]}");
 
        Request request = new Request.Builder()
            .url(URL)
            .post(body)
            .addHeader("Authorization", "Bearer " + API_KEY)
            .build();
 
        try (Response response = client.newCall(request).execute()) {
            System.out.println(response.body().string());
        }
    }
 
    public static void main(String[] args) throws IOException {
        TestOpenAIAPI api = new TestOpenAIAPI();
        api.getChatGptResponse("Hello, how are you?");
    }
}