Skip to content

Commit 198339a

Browse files
committed
javadoc
1 parent 5355fa4 commit 198339a

File tree

8 files changed

+124
-7
lines changed

8 files changed

+124
-7
lines changed

gemini-api/src/main/java/swiss/ameri/gemini/api/Content.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,43 @@
33
import java.util.List;
44
import java.util.Locale;
55

6+
/**
7+
* Content which can be sent to gemini API.
8+
*/
69
public sealed interface Content {
710

811
// todo add builder (with support for role enum)
912

13+
/**
14+
* A part of a conversation that contains text.
15+
*
16+
* @param role belonging to this turn in the conversation. see {@link Role}
17+
* @param text by the role
18+
*/
1019
record TextContent(
1120
String role,
1221
String text
1322
) implements Content {
1423
}
1524

25+
/**
26+
* A part of a conversation that contains media.
27+
*
28+
* @param role belonging to this turn in the conversation. see {@link Role}
29+
* @param media data
30+
*/
1631
record MediaContent(
1732
String role,
1833
MediaData media
1934
) implements Content {
2035
// todo test
2136
}
2237

38+
/**
39+
* A part of a conversation that contains text and media.
40+
*
41+
* @param role belonging to this turn in the conversation. see {@link Role}
42+
*/
2343
record TextAndMediaContent(
2444
String role,
2545
String text,
@@ -28,6 +48,12 @@ record TextAndMediaContent(
2848
// todo test
2949
}
3050

51+
/**
52+
* Media used during a conversion.
53+
*
54+
* @param mimeType e.g. image/jpeg
55+
* @param mediaBase64 the media, base64 encoded
56+
*/
3157
record MediaData(
3258
String mimeType,
3359
String mediaBase64

gemini-api/src/main/java/swiss/ameri/gemini/api/GenAi.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
import java.util.concurrent.CompletableFuture;
1313
import java.util.stream.Stream;
1414

15+
// todo decide if thread-safe or not, once responses are stored
16+
17+
/**
18+
* Entry point for all interactions with Gemini API.
19+
*/
1520
public class GenAi {
1621

1722
private static final String STREAM_LINE_PREFIX = "data: ";
@@ -44,6 +49,9 @@ public GenAi(
4449
this.client = client;
4550
}
4651

52+
/**
53+
* List models that are currently available.
54+
*/
4755
public List<Model> listModels() {
4856
return execute(() -> {
4957
HttpResponse<String> response = client.send(
@@ -59,8 +67,13 @@ public List<Model> listModels() {
5967
});
6068
}
6169

70+
/***
71+
* Get information of a model. Can be used to create a {@link GenerativeModel}.
72+
* @param model of which the information is wanted.
73+
* @see #listModels()
74+
*/
6275
public Model getModel(ModelVariant model) {
63-
// todo return GenerativeModelBuilder
76+
// todo return GenerativeModelBuilder. or add "toGenerativeModelBuilder" method. or add separate method...
6477
return getModel(model.variant());
6578
}
6679

@@ -82,7 +95,14 @@ public Model getModel(String model) {
8295
});
8396
}
8497

85-
98+
/**
99+
* Generates a response from Gemini API based on the given {@code model}. The response is streamed in chunks of text. The
100+
* stream items are delivered as they arrive.
101+
*
102+
* @param model with the necessary information for Gemini API to generate content
103+
* @return A live stream of the response, as it arrives
104+
* @see #generateContent(GenerativeModel) which returns the whole response at once (asynchronously)
105+
*/
86106
public Stream<GeneratedContent> generateContentStream(GenerativeModel model) {
87107
// todo, keep responses in the state.
88108
// add up the usageMetadata
@@ -112,6 +132,14 @@ public Stream<GeneratedContent> generateContentStream(GenerativeModel model) {
112132
});
113133
}
114134

135+
/**
136+
* Generates a response from Gemini API based on the given {@code model}.
137+
*
138+
* @param model with the necessary information for Gemini API to generate content
139+
* @return a {@link CompletableFuture} which completes once the response from Gemini API has arrived. The {@link CompletableFuture}
140+
* fails, if an unexpected response returns (e.g. invalid token or parameters are used)
141+
* @see #generateContentStream(GenerativeModel) to stream the response in chunks, instead of receiving all at once
142+
*/
115143
public CompletableFuture<GeneratedContent> generateContent(GenerativeModel model) {
116144
return execute(() -> {
117145
CompletableFuture<HttpResponse<String>> response = client.sendAsync(
@@ -201,6 +229,9 @@ private <T> T execute(ThrowingSupplier<T> supplier) {
201229
}
202230
}
203231

232+
/**
233+
* Content generated by Gemini API.
234+
*/
204235
public record GeneratedContent(
205236
String text
206237
) {
@@ -240,7 +271,6 @@ private record GenerateContentRequest(
240271
) {
241272
}
242273

243-
244274
private record GenerationContent(
245275
String role,
246276
List<GenerationPart> parts
@@ -254,7 +284,6 @@ private record GenerationPart(
254284
) {
255285
}
256286

257-
258287
private record ModelResponse(List<Model> models) {
259288
}
260289

gemini-api/src/main/java/swiss/ameri/gemini/api/GenerationConfig.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,34 @@
33
import java.util.List;
44

55
/**
6-
* According to <a href="https://ai.google.dev/api/rest/v1beta/GenerationConfig">GenerationConfig</a>
6+
* Generation configuration.
7+
*
8+
* @param stopSequences Optional. The set of character sequences (up to 5) that will stop output generation.
9+
* If specified, the API will stop at the first appearance of a stop sequence.
10+
* The stop sequence will not be included as part of the response.
11+
* @param responseMimeType Optional. Output response mimetype of the generated candidate text.
12+
* Supported mimetype: text/plain: (default) Text output.
13+
* application/json: JSON response in the candidates.
14+
* @param responseSchema Optional. Output response schema of the generated candidate text when response mime type can have schema.
15+
* Schema can be objects, primitives or arrays and is a subset of OpenAPI schema.
16+
* If set, a compatible responseMimeType must also be set. Compatible mimetypes: application/json: Schema for JSON response.
17+
* @param maxOutputTokens Optional. The maximum number of tokens to include in a candidate.
18+
* Note: The default value varies by model, see the Model.output_token_limit attribute of the Model returned from the getModel function.
19+
* @param temperature Optional. Controls the randomness of the output.
20+
* Note: The default value varies by model, see the Model. temperature attribute of the Model returned from the getModel function.
21+
* Values can range from [0.0, 2.0].
22+
* @param topP Optional. The maximum cumulative probability of tokens to consider when sampling.
23+
* The model uses combined Top-k and nucleus sampling.
24+
* Tokens are sorted based on their assigned probabilities so that only the most likely tokens are considered.
25+
* Top-k sampling directly limits the maximum number of tokens to consider, while Nucleus sampling limits number of tokens based on the cumulative probability.
26+
* Note: The default value varies by model, see the Model.top_p attribute of the Model returned from the getModel function.
27+
* @param topK Optional. The maximum number of tokens to consider when sampling.
28+
* Models use nucleus sampling or combined Top-k and nucleus sampling.
29+
* Top-k sampling considers the set of topK most probable tokens.
30+
* Models running with nucleus sampling don't allow topK setting.
31+
* Note: The default value varies by model, see the Model.top_k attribute of the Model returned from the getModel function.
32+
* Empty topK field in Model indicates the model doesn't apply top-k sampling and doesn't allow setting topK on requests.
33+
* @see <a href="https://ai.google.dev/api/rest/v1beta/GenerationConfig">GenerationConfig</a>
734
*/
835
public record GenerationConfig(
936
List<String> stopSequences,

gemini-api/src/main/java/swiss/ameri/gemini/api/GenerativeModel.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
import java.util.List;
44

5+
/**
6+
* Contains all the information needed for Gemini API to generate new content.
7+
*
8+
* @param modelName to be used. see {@link ModelVariant}. Must start with "models/"
9+
* @param contents given as input to Gemini API
10+
* @param safetySettings optional, to adjust safety settings
11+
* @param generationConfig optional, to configure the prompt
12+
*/
513
public record GenerativeModel(
614
String modelName,
715
List<Content> contents,

gemini-api/src/main/java/swiss/ameri/gemini/api/ModelVariant.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package swiss.ameri.gemini.api;
22

33
/**
4+
* (Potentially non-exhaustive) list of supported models.
5+
*
46
* @see <a href="https://ai.google.dev/gemini-api/docs/models/gemini">Gemini Models</a>
57
*/
68
public enum ModelVariant {
@@ -9,7 +11,6 @@ public enum ModelVariant {
911
GEMINI_1_0_PRO("gemini-1.0-pro"),
1012
GEMINI_1_0_PRO_VISION("gemini-pro-vision"),
1113
TEXT_EMBEDDING_004("text-embedding-004"),
12-
1314
;
1415

1516
private final String variant;

gemini-api/src/main/java/swiss/ameri/gemini/api/SafetySetting.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package swiss.ameri.gemini.api;
22

3+
/**
4+
* Safety settings according to <a href="https://ai.google.dev/api/rest/v1beta/SafetySetting#harmblockthreshold">SafetySetting</a>.
5+
*/
36
public record SafetySetting(
47
String category,
58
String threshold
69
) {
710

11+
/**
12+
* Create a SafetySetting by using the provided enums. Use the constructor for custom string values that might
13+
* be missing in the enums.
14+
*/
815
public static SafetySetting of(
916
HarmCategory category,
1017
HarmBlockThreshold threshold
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
package swiss.ameri.gemini.spi;
22

3+
/**
4+
* Used to (un-) marshal java objects (mainly {@code record}s) to JSON Strings.
5+
* To keep this library dependency free, no implementation is provided directly.
6+
* {@code swiss.ameri:gemini-gson} provides an example implementation using a gson dependency.
7+
*/
38
public interface JsonParser {
49

10+
/**
11+
* This method serializes the specified object into its equivalent JSON representation.
12+
*/
513
String toJson(Object object);
614

15+
/**
16+
* This method deserializes the specified JSON into an object of the specified class.
17+
*/
718
<T> T fromJson(String json, Class<T> clazz);
819

920
}

gemini-gson/src/main/java/swiss/ameri/gemini/gson/GsonJsonParser.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33
import com.google.gson.Gson;
44
import swiss.ameri.gemini.spi.JsonParser;
55

6+
/**
7+
* Reference implementation of {@link JsonParser} using {@link Gson} dependency.
8+
*/
69
public class GsonJsonParser implements JsonParser {
710

811
private final Gson gson;
912

13+
/**
14+
* Create a {@link JsonParser} with a custom {@link Gson}.
15+
*/
1016
public GsonJsonParser(Gson gson) {
1117
this.gson = gson;
1218
}
1319

20+
/**
21+
* Create a default {@link JsonParser} instance.
22+
*/
1423
public GsonJsonParser() {
1524
this(new Gson());
1625
}
1726

18-
1927
@Override
2028
public String toJson(Object object) {
2129
return gson.toJson(object);

0 commit comments

Comments
 (0)