Skip to content

Commit 62b2c0d

Browse files
committed
create structure for examples
1 parent 2b534b4 commit 62b2c0d

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

gemini-tester/src/main/java/swiss/ameri/gemini/tester/GeminiTester.java

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import swiss.ameri.gemini.gson.GsonJsonParser;
55
import swiss.ameri.gemini.spi.JsonParser;
66

7+
import java.util.concurrent.ExecutionException;
78
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.TimeoutException;
810

911
/**
1012
* Example program to test the {@link GenAi} functionality.
@@ -29,15 +31,53 @@ public static void main(String[] args) throws Exception {
2931
apiKey,
3032
parser
3133
);
32-
genAi.listModels()
34+
35+
// each method represents an example usage
36+
listModels(genAi);
37+
getModel(genAi);
38+
generateContent(genAi);
39+
generateContentStream(genAi);
40+
multiChatTurn(genAi);
41+
}
42+
43+
private static void multiChatTurn(GenAi genAi) {
44+
System.out.println("----- multi turn chat");
45+
GenerativeModel chatModel = GenerativeModel.builder()
46+
.modelName(ModelVariant.GEMINI_1_0_PRO)
47+
.addContent(new Content.TextContent(
48+
Content.Role.USER.roleName(),
49+
"Write the first line of a story about a magic backpack."
50+
))
51+
.addContent(new Content.TextContent(
52+
Content.Role.MODEL.roleName(),
53+
"In the bustling city of Meadow brook, lived a young girl named Sophie. She was a bright and curious soul with an imaginative mind."
54+
))
55+
.addContent(new Content.TextContent(
56+
Content.Role.USER.roleName(),
57+
"Can you set it in a quiet village in 1600s France? Max 30 words"
58+
))
59+
.build();
60+
genAi.generateContentStream(chatModel)
3361
.forEach(System.out::println);
34-
System.out.println("----- Get Model");
35-
System.out.println(
36-
genAi.getModel(ModelVariant.GEMINI_1_0_PRO)
37-
);
62+
}
3863

64+
private static void generateContentStream(GenAi genAi) {
65+
System.out.println("----- Generate content (streaming)");
66+
var model = createStoryModel();
67+
genAi.generateContentStream(model)
68+
.forEach(System.out::println);
69+
}
70+
71+
private static void generateContent(GenAi genAi) throws InterruptedException, ExecutionException, TimeoutException {
72+
var model = createStoryModel();
3973
System.out.println("----- Generate content (blocking)");
40-
var model = GenerativeModel.builder()
74+
genAi.generateContent(model)
75+
.thenAccept(System.out::println)
76+
.get(20, TimeUnit.SECONDS);
77+
}
78+
79+
private static GenerativeModel createStoryModel() {
80+
return GenerativeModel.builder()
4181
.modelName(ModelVariant.GEMINI_1_0_PRO)
4282
.addContent(new Content.TextContent(
4383
Content.Role.USER.roleName(),
@@ -57,32 +97,18 @@ public static void main(String[] args) throws Exception {
5797
null
5898
))
5999
.build();
60-
genAi.generateContent(model)
61-
.thenAccept(System.out::println)
62-
.get(20, TimeUnit.SECONDS);
63-
64-
System.out.println("----- Generate content (streaming)");
65-
genAi.generateContentStream(model)
66-
.forEach(System.out::println);
100+
}
67101

102+
private static void getModel(GenAi genAi) {
103+
System.out.println("----- Get Model");
104+
System.out.println(
105+
genAi.getModel(ModelVariant.GEMINI_1_0_PRO)
106+
);
107+
}
68108

69-
System.out.println("----- multi turn chat");
70-
GenerativeModel chatModel = GenerativeModel.builder()
71-
.modelName(ModelVariant.GEMINI_1_0_PRO)
72-
.addContent(new Content.TextContent(
73-
Content.Role.USER.roleName(),
74-
"Write the first line of a story about a magic backpack."
75-
))
76-
.addContent(new Content.TextContent(
77-
Content.Role.MODEL.roleName(),
78-
"In the bustling city of Meadow brook, lived a young girl named Sophie. She was a bright and curious soul with an imaginative mind."
79-
))
80-
.addContent(new Content.TextContent(
81-
Content.Role.USER.roleName(),
82-
"Can you set it in a quiet village in 1600s France? Max 30 words"
83-
))
84-
.build();
85-
genAi.generateContentStream(chatModel)
109+
private static void listModels(GenAi genAi) {
110+
System.out.println("----- List models");
111+
genAi.listModels()
86112
.forEach(System.out::println);
87113
}
88114
}

0 commit comments

Comments
 (0)