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();
|
}
|
}
|
}
|