|
| 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 | +} |
0 commit comments