Skip to content

Commit 23dc1a0

Browse files
committed
Added java sample for chat
1 parent 8a383b5 commit 23dc1a0

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.gemini;
18+
19+
import com.google.genai.Chat;
20+
import com.google.genai.Client;
21+
import com.google.genai.types.Content;
22+
import com.google.genai.types.GenerateContentConfig;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.Part;
25+
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
public class ChatSession {
30+
public static List<GenerateContentResponse> chat() {
31+
// [START chat]
32+
Client client = new Client();
33+
34+
Content userContent = Content.fromParts(Part.fromText("Hello"));
35+
Content modelContent =
36+
Content.builder()
37+
.role("model")
38+
.parts(
39+
Collections.singletonList(
40+
Part.fromText("Great to meet you. What would you like to know?")
41+
)
42+
).build();
43+
44+
Chat chat = client.chats.create(
45+
"gemini-2.0-flash",
46+
GenerateContentConfig.builder()
47+
.systemInstruction(userContent)
48+
.systemInstruction(modelContent)
49+
.build()
50+
);
51+
52+
GenerateContentResponse response1 = chat.sendMessage("I have 2 dogs in my house.");
53+
System.out.println(response1.text());
54+
55+
GenerateContentResponse response2 = chat.sendMessage("How many paws are in my house?");
56+
System.out.println(response2.text());
57+
58+
// [END chat]
59+
return List.of(response1, response2);
60+
}
61+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.gemini;
18+
19+
import com.google.genai.types.GenerateContentResponse;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.List;
23+
24+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25+
import static org.junit.jupiter.api.Assertions.assertFalse;
26+
import static org.junit.jupiter.api.Assertions.assertNotNull;
27+
28+
public class ChatSessionTest {
29+
@Test
30+
public void test_chat() {
31+
List<GenerateContentResponse> chatResponses = assertDoesNotThrow(ChatSession::chat,
32+
"chat returned an error");
33+
34+
for (GenerateContentResponse response : chatResponses) {
35+
assertNotNull(response, "Response should not be null");
36+
assertNotNull(response.text(), "Response text should not be null");
37+
assertFalse(response.text().trim().isEmpty(), "Response text should not be empty");
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)