-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathExampleListModels.java
More file actions
58 lines (49 loc) · 1.85 KB
/
ExampleListModels.java
File metadata and controls
58 lines (49 loc) · 1.85 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
58
package examples;
import io.github.jetkai.openai.api.ListModels;
import io.github.jetkai.openai.api.data.model.ModelData;
import io.github.jetkai.openai.openai.OpenAI;
import java.util.List;
import java.util.Optional;
/**
* ExampleListModels
*
* @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 ExampleListModels {
public static void main(String[] args) {
//Initialize ExampleListModels class
ExampleListModels getModels = new ExampleListModels();
//Send request to API and deserialize response as List<ModalData>
List<ModelData> models = getModels.communicate();
for(ModelData model : models) {
Optional<String> id = model.id();
if(id.isEmpty()) {
continue;
}
//Print out the names of all the available models, to the console
System.out.println(id.get());
}
//models.stream().map(ModelData::id).forEach(System.out::println);
}
private List<ModelData> communicate() {
OpenAI openAI = OpenAI.builder()
.setApiKey(System.getenv("OPEN_AI_API_KEY"))
.listModels()
.build()
//Sends the request to OpenAI's endpoint & parses the response data
.sendRequest();
//Call the ListModels API from OpenAI & create instance
Optional<ListModels> getModels = openAI.models();
//Return models as a data structure list, so that we can get the name from each model
return getModels.map(ListModels::asDataList).orElse(null);
}
}