-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simple.java
34 lines (30 loc) · 1.09 KB
/
Simple.java
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
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Simple {
public static void main(String[] args) throws IOException, InterruptedException {
var apiKey = System.getenv("OPENAI_API_KEY");
var body = """
{
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "Tell me a good dad joke about cats"
}
]
}
""";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.openai.com/v1/chat/completions"))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
var client = HttpClient.newHttpClient();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}