29
29
import com .google .cloud .vertexai .api .GenerationConfig ;
30
30
import com .google .cloud .vertexai .api .SafetySetting ;
31
31
import com .google .cloud .vertexai .api .Tool ;
32
- import com .google .cloud .vertexai .api .ToolConfig ;
33
32
import com .google .common .collect .ImmutableList ;
34
33
import java .io .IOException ;
35
34
import java .util .ArrayList ;
@@ -41,8 +40,8 @@ public final class ChatSession {
41
40
private final GenerativeModel model ;
42
41
private final Optional <ChatSession > rootChatSession ;
43
42
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 ;
46
45
private Optional <ResponseStream <GenerateContentResponse >> currentResponseStream ;
47
46
private Optional <GenerateContentResponse > currentResponse ;
48
47
@@ -51,17 +50,14 @@ public final class ChatSession {
51
50
* GenerationConfig) inherits from the model.
52
51
*/
53
52
public ChatSession (GenerativeModel model ) {
54
- this (model , new ArrayList <>(), 0 , Optional .empty (), Optional .empty ());
53
+ this (model , Optional .empty (), Optional .empty ());
55
54
}
56
55
57
56
/**
58
57
* Creates a new chat session given a GenerativeModel instance and a root chat session.
59
58
* Configurations of the chat (e.g., GenerationConfig) inherits from the model.
60
59
*
61
60
* @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.
65
61
* @param rootChatSession a root {@link ChatSession} instance. All the chat history in the current
66
62
* chat session will be merged to the root chat session.
67
63
* @param automaticFunctionCallingResponder an {@link AutomaticFunctionCallingResponder} instance
@@ -70,14 +66,10 @@ public ChatSession(GenerativeModel model) {
70
66
*/
71
67
private ChatSession (
72
68
GenerativeModel model ,
73
- List <Content > history ,
74
- int previousHistorySize ,
75
69
Optional <ChatSession > rootChatSession ,
76
70
Optional <AutomaticFunctionCallingResponder > automaticFunctionCallingResponder ) {
77
71
checkNotNull (model , "model should not be null" );
78
72
this .model = model ;
79
- this .history = history ;
80
- this .previousHistorySize = previousHistorySize ;
81
73
this .rootChatSession = rootChatSession ;
82
74
this .automaticFunctionCallingResponder = automaticFunctionCallingResponder ;
83
75
currentResponseStream = Optional .empty ();
@@ -92,12 +84,15 @@ private ChatSession(
92
84
* @return a new {@link ChatSession} instance with the specified GenerationConfig.
93
85
*/
94
86
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 ;
101
96
}
102
97
103
98
/**
@@ -108,12 +103,15 @@ public ChatSession withGenerationConfig(GenerationConfig generationConfig) {
108
103
* @return a new {@link ChatSession} instance with the specified SafetySettings.
109
104
*/
110
105
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 ;
117
115
}
118
116
119
117
/**
@@ -124,44 +122,13 @@ public ChatSession withSafetySettings(List<SafetySetting> safetySettings) {
124
122
* @return a new {@link ChatSession} instance with the specified Tools.
125
123
*/
126
124
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 ;
165
132
}
166
133
167
134
/**
@@ -174,12 +141,13 @@ public ChatSession withSystemInstruction(Content systemInstruction) {
174
141
*/
175
142
public ChatSession withAutomaticFunctionCallingResponder (
176
143
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 ;
183
151
}
184
152
185
153
/**
0 commit comments