@@ -6,6 +6,8 @@ The development of this JAVA-based AI SDK is primarily aimed at addressing the l
6
6
7
7
The project mainly involves encapsulating the REST API provided by model manufacturers, making it convenient for JAVA developers to use. It also introduces only a minimal number of dependency packages to avoid conflicts.
8
8
9
+ The SDK currently supports historical context. Simply pass in the corresponding historical data when calling the interface. For specific examples, please refer to the multi-turn dialogue in the example below.
10
+
9
11
## Importing
10
12
11
13
### Maven
@@ -46,73 +48,104 @@ public GeminiClient(String modelName, GeminiAccount geminiAccount)
46
48
Chat
47
49
48
50
``` java
49
- @Test
50
- public void chatTest() throws IOException {
51
+ @Test
52
+ public void chatTest() throws IOException {
51
53
52
- GeminiAccount account = GeminiAccount . builder(). apiKey(apiKey). baseUrl(baseUrl). build();
54
+ GeminiAccount account = GeminiAccount . builder(). apiKey(apiKey). baseUrl(baseUrl). build();
53
55
54
- GeminiGenerationConfig generationConfig = GeminiGenerationConfig . builder(). temperature(0.3 ). build();
56
+ GeminiGenerationConfig geminiGenerationConfig = GeminiGenerationConfig . builder(). temperature(0.3 ). build();
55
57
56
- GeminiClient client = new GeminiClient (account);
57
- GeminiTextResponse chatResponse1 = client. chat(" Do you know something about Yao Ming " , generationConfig );
58
- System . out. println(chatResponse1 );
59
- }
58
+ GeminiClient client = new GeminiClient (account);
59
+ GeminiTextResponse chatResponse = client. chat(" who are you " , geminiGenerationConfig );
60
+ System . out. println(chatResponse );
61
+ }
60
62
61
63
```
62
64
63
65
Multi-turn Chat
64
66
65
67
``` java
66
- @Test
67
- public void chatTest () throws IOException {
68
+ @Test
69
+ public void multiTurnChatTest () throws IOException {
68
70
69
- GeminiAccount account = GeminiAccount . builder(). apiKey(apiKey). baseUrl(baseUrl). build();
71
+ GeminiAccount account = GeminiAccount . builder(). apiKey(apiKey). baseUrl(baseUrl). build();
70
72
71
- GeminiGenerationConfig generationConfig = GeminiGenerationConfig . builder(). temperature(0.3 ). build();
73
+ GeminiGenerationConfig geminiGenerationConfig = GeminiGenerationConfig . builder(). temperature(0.3 ). build();
72
74
73
- GeminiClient client = new GeminiClient (account);
74
- GeminiTextResponse chatResponse1 = client. chat(" Do you know something about Yao Ming" , generationConfig );
75
- System . out. println(chatResponse1);
75
+ GeminiClient client = new GeminiClient (account);
76
+ GeminiTextResponse chatResponse1 = client. chat(" Do you know something about Yao Ming" , geminiGenerationConfig );
77
+ System . out. println(chatResponse1);
76
78
77
- GeminiTextResponse chatResponse2 = client . chat( " who is his wife " );
78
- System . out . println(chatResponse2 );
79
+ // round one history data
80
+ List< ChatHistory > history1 = chatResponse1 . getHistory( );
79
81
80
- GeminiTextResponse chatResponse3 = client. chat(" who is his daughter" , generationConfig);
81
- System . out. println(chatResponse3);
82
- }
82
+ GeminiTextResponse chatResponse2 = client. chat(" who is his wife" , geminiGenerationConfig, history1);
83
+ System . out. println(chatResponse2);
84
+
85
+ // round two history data
86
+ List<ChatHistory > history2 = chatResponse2. getHistory();
87
+
88
+ GeminiTextResponse chatResponse3 = client. chat(" who is his daughter" , geminiGenerationConfig, history2);
89
+ System . out. println(chatResponse3);
90
+ }
83
91
```
84
92
85
93
Multimodal (with context)
86
94
87
95
``` java
88
96
@Test
89
- public void chatMultiModalTest() throws IOException {
97
+ public void chatMultiModalTest() throws IOException {
90
98
91
- GeminiAccount account = GeminiAccount . builder(). apiKey(apiKey). baseUrl(baseUrl). build();
99
+ GeminiAccount account = GeminiAccount . builder(). apiKey(apiKey). baseUrl(baseUrl). build();
92
100
93
- GeminiGenerationConfig generationConfig = GeminiGenerationConfig . builder(). temperature(0.3 ). build();
101
+ GeminiGenerationConfig geminiGenerationConfig = GeminiGenerationConfig . builder(). temperature(0.3 ). build();
94
102
95
- GeminiClient client = new GeminiClient (GeminiModelEnum . GEMINI_PRO. getName(), account);
103
+ GeminiClient client = new GeminiClient (GeminiModelEnum . GEMINI_PRO. getName(), account);
96
104
97
- // local image
98
- // Path img = Paths.get("/path/abc.jpg");
99
- // String base64Image = Base64.getEncoder().encodeToString(Files.readAllBytes(img));
105
+ // image url
106
+ String imageUrl = " https://pic.qqtn.com/uploadfiles/2009-6/2009614181816.jpg" ;
107
+ // convert the image to base64
108
+ String base64 = Base64Util . imageUrlToBase64(imageUrl);
100
109
101
- // image url
102
- String imageUrl = " https://storage.googleapis.com/generativeai-downloads/images/scones.jpg" ;
110
+ MultiPartInlineData inlineData = MultiPartInlineData . builder(). mimeType(" image/jpeg" ). data(base64). build();
103
111
104
- String base64 = Base64Util . imageUrlToBase64(imageUrl) ;
112
+ String message = " What is this picture " ;
105
113
106
- MultiPartInlineData inlineData = MultiPartInlineData . builder(). mimeType(" image/jpeg" ). data(base64). build();
114
+ GeminiTextResponse chatResponse1 = client. chat(message, inlineData, geminiGenerationConfig, null );
115
+ System . out. println(chatResponse1);
107
116
108
- String message = " What is this picture" ;
117
+ // history data
118
+ List<ChatHistory > history = chatResponse1. getHistory();
109
119
110
- GeminiTextResponse chatResponse1 = client. chat(message, inlineData, generationConfig);
111
- System . out. println(chatResponse1);
120
+ GeminiTextResponse chatResponse2 = client. chat(" How many dog are there" , geminiGenerationConfig, history);
121
+ System . out. println(chatResponse2);
122
+ }
123
+ ```
112
124
113
- GeminiTextResponse chatResponse2 = client. chat(" How many flowers are there" , generationConfig);
114
- System . out. println(chatResponse2);
125
+ Stream Chat
126
+
127
+ ``` java
128
+ GeminiAccount account = GeminiAccount . builder(). apiKey(apiKey). baseUrl(baseUrl). build();
129
+
130
+ GeminiGenerationConfig geminiGenerationConfig = GeminiGenerationConfig . builder(). temperature(0.3 ). build();
131
+
132
+ GeminiClient client = new GeminiClient (account);
133
+
134
+ // image url
135
+ String imageUrl = " https://pic.qqtn.com/uploadfiles/2009-6/2009614181816.jpg" ;
136
+ // convert the image to base64
137
+ String base64 = Base64Util . imageUrlToBase64(imageUrl);
138
+
139
+ MultiPartInlineData inlineData = MultiPartInlineData . builder(). mimeType(" image/jpeg" ). data(base64). build();
140
+
141
+ client. stream(" What is this picture" , inlineData, geminiGenerationConfig, null , new GeminiStreamResponseListener () {
142
+
143
+ @Override
144
+ public void accept (Content content ) {
145
+ System . out. println(" accept3:" + content);
115
146
}
147
+
148
+ });
116
149
```
117
150
118
151
In the current full conversation methods, the function of automatically implementing historical records has been realized. The caller can obtain the ` history ` through the returned ` GeminiTextResponse ` object.
0 commit comments