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 ;
32
33
import com .google .common .collect .ImmutableList ;
33
34
import java .io .IOException ;
34
35
import java .util .ArrayList ;
35
36
import java .util .List ;
36
37
import java .util .Optional ;
37
38
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
+ */
39
44
public final class ChatSession {
40
45
private final GenerativeModel model ;
41
46
private final Optional <ChatSession > rootChatSession ;
42
47
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 ;
45
50
private Optional <ResponseStream <GenerateContentResponse >> currentResponseStream ;
46
51
private Optional <GenerateContentResponse > currentResponse ;
47
52
@@ -50,14 +55,17 @@ public final class ChatSession {
50
55
* GenerationConfig) inherits from the model.
51
56
*/
52
57
public ChatSession (GenerativeModel model ) {
53
- this (model , Optional .empty (), Optional .empty ());
58
+ this (model , new ArrayList <>(), 0 , Optional .empty (), Optional .empty ());
54
59
}
55
60
56
61
/**
57
62
* Creates a new chat session given a GenerativeModel instance and a root chat session.
58
63
* Configurations of the chat (e.g., GenerationConfig) inherits from the model.
59
64
*
60
65
* @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.
61
69
* @param rootChatSession a root {@link ChatSession} instance. All the chat history in the current
62
70
* chat session will be merged to the root chat session.
63
71
* @param automaticFunctionCallingResponder an {@link AutomaticFunctionCallingResponder} instance
@@ -66,10 +74,14 @@ public ChatSession(GenerativeModel model) {
66
74
*/
67
75
private ChatSession (
68
76
GenerativeModel model ,
77
+ List <Content > history ,
78
+ int previousHistorySize ,
69
79
Optional <ChatSession > rootChatSession ,
70
80
Optional <AutomaticFunctionCallingResponder > automaticFunctionCallingResponder ) {
71
81
checkNotNull (model , "model should not be null" );
72
82
this .model = model ;
83
+ this .history = history ;
84
+ this .previousHistorySize = previousHistorySize ;
73
85
this .rootChatSession = rootChatSession ;
74
86
this .automaticFunctionCallingResponder = automaticFunctionCallingResponder ;
75
87
currentResponseStream = Optional .empty ();
@@ -84,15 +96,12 @@ private ChatSession(
84
96
* @return a new {@link ChatSession} instance with the specified GenerationConfig.
85
97
*/
86
98
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 );
96
105
}
97
106
98
107
/**
@@ -103,15 +112,12 @@ public ChatSession withGenerationConfig(GenerationConfig generationConfig) {
103
112
* @return a new {@link ChatSession} instance with the specified SafetySettings.
104
113
*/
105
114
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 );
115
121
}
116
122
117
123
/**
@@ -122,13 +128,44 @@ public ChatSession withSafetySettings(List<SafetySetting> safetySettings) {
122
128
* @return a new {@link ChatSession} instance with the specified Tools.
123
129
*/
124
130
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 );
132
169
}
133
170
134
171
/**
@@ -141,13 +178,12 @@ public ChatSession withTools(List<Tool> tools) {
141
178
*/
142
179
public ChatSession withAutomaticFunctionCallingResponder (
143
180
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 ));
151
187
}
152
188
153
189
/**
0 commit comments