-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExampleListModel.java
67 lines (51 loc) · 1.87 KB
/
ExampleListModel.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
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
60
61
62
63
64
65
66
67
package examples;
import io.github.jetkai.openai.api.ListModel;
import io.github.jetkai.openai.openai.OpenAI;
import java.util.Optional;
/**
* ExampleListModel
*
* @author <a href="https://github.com/jetkai">Kai</a>
* @version 1.1.0
* {@code - 07/03/2023}
* @since 1.0.0
* {@code - 05/03/2023}
*
* <p>
* Note - This is just a test class, it is recommended to import this project as a library
* <p>
* You can get a free API key from <a href="https://platform.openai.com/account/api-keys">here</a>
*/
final class ExampleListModel {
public static void main(String[] args) {
//Initialize ExampleListModel class
ExampleListModel getModel = new ExampleListModel();
//Send request to API - response as JSON string
String modelJson = getModel.communicate();
//Print out the model information in JSON
System.out.println(modelJson);
}
private String communicate() {
String modelName = "davinci";
OpenAI openAI = OpenAI.builder()
.setApiKey(System.getenv("OPEN_AI_API_KEY"))
.listModel(modelName)
.build()
//Sends the request to OpenAI's endpoint & parses the response data
.sendRequest();
//Call the ListModels API from OpenAI & create instance
Optional<ListModel> getModels = openAI.model();
//Return model as a JSON string
return getModels.map(ListModel::asJson).orElse(null);
}
private String example2() {
String modelName = "davinci";
OpenAI openAI = OpenAI.builder()
.setApiKey(System.getenv("OPEN_AI_API_KEY"))
.build();
//Call the ListModels API from OpenAI & create instance
ListModel listModel = openAI.createInstance(ListModel.class, modelName);
//Return model as a JSON string
return listModel.asJson();
}
}