-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIPrompter.java
More file actions
57 lines (44 loc) · 2.1 KB
/
AIPrompter.java
File metadata and controls
57 lines (44 loc) · 2.1 KB
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
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class AIPrompter {
private static final String apiKey = System.getenv("OPENAI_API_KEY");
public static String chatGPT(String prompt) throws IOException {
String url = "https://api.openai.com/v1/chat/completions";
String model = "gpt-3.5-turbo";
try {
URL obj = new URL(url);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Bearer " + apiKey);
connection.setRequestProperty("Content-Type", "application/json");
// The request body
String body = "{\"model\": \"" + model + "\", \"messages\": [{\"role\": \"user\", \"content\": \"" + prompt + "\"}]}";
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(body);
writer.flush();
writer.close();
// Response from ChatGPT
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = br.readLine()) != null) {
response.append(line);
}
br.close();
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(response.toString());
System.out.println(node);
System.out.println(node.get("choices").get(0).get("message").get("content").asText());
return node.get("choices").get(0).get("message").get("content").asText();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}