2929import com .google .cloud .vertexai .api .GenerationConfig ;
3030import com .google .cloud .vertexai .api .SafetySetting ;
3131import com .google .cloud .vertexai .api .Tool ;
32+ import com .google .cloud .vertexai .api .ToolConfig ;
3233import com .google .common .collect .ImmutableList ;
3334import java .io .IOException ;
3435import java .util .ArrayList ;
3536import java .util .List ;
3637import java .util .Optional ;
3738
38- /** Represents a conversation between the user and the model */
39+ /**
40+ * Represents a conversation between the user and the model.
41+ *
42+ * <p>Note: this class is NOT thread-safe.
43+ */
3944public final class ChatSession {
4045 private final GenerativeModel model ;
4146 private final Optional <ChatSession > rootChatSession ;
4247 private final Optional <AutomaticFunctionCallingResponder > automaticFunctionCallingResponder ;
43- private List <Content > history = new ArrayList <>() ;
44- private int previousHistorySize = 0 ;
48+ private List <Content > history ;
49+ private int previousHistorySize ;
4550 private Optional <ResponseStream <GenerateContentResponse >> currentResponseStream ;
4651 private Optional <GenerateContentResponse > currentResponse ;
4752
@@ -50,14 +55,17 @@ public final class ChatSession {
5055 * GenerationConfig) inherits from the model.
5156 */
5257 public ChatSession (GenerativeModel model ) {
53- this (model , Optional .empty (), Optional .empty ());
58+ this (model , new ArrayList <>(), 0 , Optional .empty (), Optional .empty ());
5459 }
5560
5661 /**
5762 * Creates a new chat session given a GenerativeModel instance and a root chat session.
5863 * Configurations of the chat (e.g., GenerationConfig) inherits from the model.
5964 *
6065 * @param model a {@link GenerativeModel} instance that generates contents in the chat.
66+ * @param history a list of {@link Content} containing interleaving conversation between "user"
67+ * and "model".
68+ * @param previousHistorySize the size of the previous history.
6169 * @param rootChatSession a root {@link ChatSession} instance. All the chat history in the current
6270 * chat session will be merged to the root chat session.
6371 * @param automaticFunctionCallingResponder an {@link AutomaticFunctionCallingResponder} instance
@@ -66,10 +74,14 @@ public ChatSession(GenerativeModel model) {
6674 */
6775 private ChatSession (
6876 GenerativeModel model ,
77+ List <Content > history ,
78+ int previousHistorySize ,
6979 Optional <ChatSession > rootChatSession ,
7080 Optional <AutomaticFunctionCallingResponder > automaticFunctionCallingResponder ) {
7181 checkNotNull (model , "model should not be null" );
7282 this .model = model ;
83+ this .history = history ;
84+ this .previousHistorySize = previousHistorySize ;
7385 this .rootChatSession = rootChatSession ;
7486 this .automaticFunctionCallingResponder = automaticFunctionCallingResponder ;
7587 currentResponseStream = Optional .empty ();
@@ -84,15 +96,12 @@ private ChatSession(
8496 * @return a new {@link ChatSession} instance with the specified GenerationConfig.
8597 */
8698 public ChatSession withGenerationConfig (GenerationConfig generationConfig ) {
87- ChatSession rootChat = rootChatSession .orElse (this );
88- ChatSession newChatSession =
89- new ChatSession (
90- model .withGenerationConfig (generationConfig ),
91- Optional .of (rootChat ),
92- automaticFunctionCallingResponder );
93- newChatSession .history = history ;
94- newChatSession .previousHistorySize = previousHistorySize ;
95- return newChatSession ;
99+ return new ChatSession (
100+ model .withGenerationConfig (generationConfig ),
101+ history ,
102+ previousHistorySize ,
103+ Optional .of (rootChatSession .orElse (this )),
104+ automaticFunctionCallingResponder );
96105 }
97106
98107 /**
@@ -103,15 +112,12 @@ public ChatSession withGenerationConfig(GenerationConfig generationConfig) {
103112 * @return a new {@link ChatSession} instance with the specified SafetySettings.
104113 */
105114 public ChatSession withSafetySettings (List <SafetySetting > safetySettings ) {
106- ChatSession rootChat = rootChatSession .orElse (this );
107- ChatSession newChatSession =
108- new ChatSession (
109- model .withSafetySettings (safetySettings ),
110- Optional .of (rootChat ),
111- automaticFunctionCallingResponder );
112- newChatSession .history = history ;
113- newChatSession .previousHistorySize = previousHistorySize ;
114- return newChatSession ;
115+ return new ChatSession (
116+ model .withSafetySettings (safetySettings ),
117+ history ,
118+ previousHistorySize ,
119+ Optional .of (rootChatSession .orElse (this )),
120+ automaticFunctionCallingResponder );
115121 }
116122
117123 /**
@@ -122,13 +128,44 @@ public ChatSession withSafetySettings(List<SafetySetting> safetySettings) {
122128 * @return a new {@link ChatSession} instance with the specified Tools.
123129 */
124130 public ChatSession withTools (List <Tool > tools ) {
125- ChatSession rootChat = rootChatSession .orElse (this );
126- ChatSession newChatSession =
127- new ChatSession (
128- model .withTools (tools ), Optional .of (rootChat ), automaticFunctionCallingResponder );
129- newChatSession .history = history ;
130- newChatSession .previousHistorySize = previousHistorySize ;
131- return newChatSession ;
131+ return new ChatSession (
132+ model .withTools (tools ),
133+ history ,
134+ previousHistorySize ,
135+ Optional .of (rootChatSession .orElse (this )),
136+ automaticFunctionCallingResponder );
137+ }
138+
139+ /**
140+ * Creates a copy of the current ChatSession with updated ToolConfig.
141+ *
142+ * @param toolConfig a {@link com.google.cloud.vertexai.api.ToolConfig} that will be used in the
143+ * new ChatSession.
144+ * @return a new {@link ChatSession} instance with the specified ToolConfigs.
145+ */
146+ public ChatSession withToolConfig (ToolConfig toolConfig ) {
147+ return new ChatSession (
148+ model .withToolConfig (toolConfig ),
149+ history ,
150+ previousHistorySize ,
151+ Optional .of (rootChatSession .orElse (this )),
152+ automaticFunctionCallingResponder );
153+ }
154+
155+ /**
156+ * Creates a copy of the current ChatSession with updated SystemInstruction.
157+ *
158+ * @param systemInstruction a {@link com.google.cloud.vertexai.api.Content} containing system
159+ * instructions.
160+ * @return a new {@link ChatSession} instance with the specified ToolConfigs.
161+ */
162+ public ChatSession withSystemInstruction (Content systemInstruction ) {
163+ return new ChatSession (
164+ model .withSystemInstruction (systemInstruction ),
165+ history ,
166+ previousHistorySize ,
167+ Optional .of (rootChatSession .orElse (this )),
168+ automaticFunctionCallingResponder );
132169 }
133170
134171 /**
@@ -141,13 +178,12 @@ public ChatSession withTools(List<Tool> tools) {
141178 */
142179 public ChatSession withAutomaticFunctionCallingResponder (
143180 AutomaticFunctionCallingResponder automaticFunctionCallingResponder ) {
144- ChatSession rootChat = rootChatSession .orElse (this );
145- ChatSession newChatSession =
146- new ChatSession (
147- model , Optional .of (rootChat ), Optional .of (automaticFunctionCallingResponder ));
148- newChatSession .history = history ;
149- newChatSession .previousHistorySize = previousHistorySize ;
150- return newChatSession ;
181+ return new ChatSession (
182+ model ,
183+ history ,
184+ previousHistorySize ,
185+ Optional .of (rootChatSession .orElse (this )),
186+ Optional .of (automaticFunctionCallingResponder ));
151187 }
152188
153189 /**
0 commit comments