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 ;
3332import com .google .common .collect .ImmutableList ;
3433import java .io .IOException ;
3534import java .util .ArrayList ;
@@ -41,8 +40,8 @@ public final class ChatSession {
4140 private final GenerativeModel model ;
4241 private final Optional <ChatSession > rootChatSession ;
4342 private final Optional <AutomaticFunctionCallingResponder > automaticFunctionCallingResponder ;
44- private List <Content > history ;
45- private int previousHistorySize ;
43+ private List <Content > history = new ArrayList <>() ;
44+ private int previousHistorySize = 0 ;
4645 private Optional <ResponseStream <GenerateContentResponse >> currentResponseStream ;
4746 private Optional <GenerateContentResponse > currentResponse ;
4847
@@ -51,17 +50,14 @@ public final class ChatSession {
5150 * GenerationConfig) inherits from the model.
5251 */
5352 public ChatSession (GenerativeModel model ) {
54- this (model , new ArrayList <>(), 0 , Optional .empty (), Optional .empty ());
53+ this (model , Optional .empty (), Optional .empty ());
5554 }
5655
5756 /**
5857 * Creates a new chat session given a GenerativeModel instance and a root chat session.
5958 * Configurations of the chat (e.g., GenerationConfig) inherits from the model.
6059 *
6160 * @param model a {@link GenerativeModel} instance that generates contents in the chat.
62- * @param history a list of {@link Content} containing interleaving conversation between "user"
63- * and "model".
64- * @param previousHistorySize the size of the previous history.
6561 * @param rootChatSession a root {@link ChatSession} instance. All the chat history in the current
6662 * chat session will be merged to the root chat session.
6763 * @param automaticFunctionCallingResponder an {@link AutomaticFunctionCallingResponder} instance
@@ -70,14 +66,10 @@ public ChatSession(GenerativeModel model) {
7066 */
7167 private ChatSession (
7268 GenerativeModel model ,
73- List <Content > history ,
74- int previousHistorySize ,
7569 Optional <ChatSession > rootChatSession ,
7670 Optional <AutomaticFunctionCallingResponder > automaticFunctionCallingResponder ) {
7771 checkNotNull (model , "model should not be null" );
7872 this .model = model ;
79- this .history = history ;
80- this .previousHistorySize = previousHistorySize ;
8173 this .rootChatSession = rootChatSession ;
8274 this .automaticFunctionCallingResponder = automaticFunctionCallingResponder ;
8375 currentResponseStream = Optional .empty ();
@@ -92,12 +84,15 @@ private ChatSession(
9284 * @return a new {@link ChatSession} instance with the specified GenerationConfig.
9385 */
9486 public ChatSession withGenerationConfig (GenerationConfig generationConfig ) {
95- return new ChatSession (
96- model .withGenerationConfig (generationConfig ),
97- history ,
98- previousHistorySize ,
99- Optional .of (rootChatSession .orElse (this )),
100- automaticFunctionCallingResponder );
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 ;
10196 }
10297
10398 /**
@@ -108,12 +103,15 @@ public ChatSession withGenerationConfig(GenerationConfig generationConfig) {
108103 * @return a new {@link ChatSession} instance with the specified SafetySettings.
109104 */
110105 public ChatSession withSafetySettings (List <SafetySetting > safetySettings ) {
111- return new ChatSession (
112- model .withSafetySettings (safetySettings ),
113- history ,
114- previousHistorySize ,
115- Optional .of (rootChatSession .orElse (this )),
116- automaticFunctionCallingResponder );
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 ;
117115 }
118116
119117 /**
@@ -124,44 +122,13 @@ public ChatSession withSafetySettings(List<SafetySetting> safetySettings) {
124122 * @return a new {@link ChatSession} instance with the specified Tools.
125123 */
126124 public ChatSession withTools (List <Tool > tools ) {
127- return new ChatSession (
128- model .withTools (tools ),
129- history ,
130- previousHistorySize ,
131- Optional .of (rootChatSession .orElse (this )),
132- automaticFunctionCallingResponder );
133- }
134-
135- /**
136- * Creates a copy of the current ChatSession with updated ToolConfig.
137- *
138- * @param toolConfig a {@link com.google.cloud.vertexai.api.ToolConfig} that will be used in the
139- * new ChatSession.
140- * @return a new {@link ChatSession} instance with the specified ToolConfigs.
141- */
142- public ChatSession withToolConfig (ToolConfig toolConfig ) {
143- return new ChatSession (
144- model .withToolConfig (toolConfig ),
145- history ,
146- previousHistorySize ,
147- Optional .of (rootChatSession .orElse (this )),
148- automaticFunctionCallingResponder );
149- }
150-
151- /**
152- * Creates a copy of the current ChatSession with updated SystemInstruction.
153- *
154- * @param systemInstruction a {@link com.google.cloud.vertexai.api.Content} containing system
155- * instructions.
156- * @return a new {@link ChatSession} instance with the specified ToolConfigs.
157- */
158- public ChatSession withSystemInstruction (Content systemInstruction ) {
159- return new ChatSession (
160- model .withSystemInstruction (systemInstruction ),
161- history ,
162- previousHistorySize ,
163- Optional .of (rootChatSession .orElse (this )),
164- automaticFunctionCallingResponder );
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 ;
165132 }
166133
167134 /**
@@ -174,12 +141,13 @@ public ChatSession withSystemInstruction(Content systemInstruction) {
174141 */
175142 public ChatSession withAutomaticFunctionCallingResponder (
176143 AutomaticFunctionCallingResponder automaticFunctionCallingResponder ) {
177- return new ChatSession (
178- model ,
179- history ,
180- previousHistorySize ,
181- Optional .of (rootChatSession .orElse (this )),
182- Optional .of (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 ;
183151 }
184152
185153 /**
0 commit comments