Skip to content

Commit 25d00c7

Browse files
feat: [vertexai] add GenerateContentConfig to sendMessage method (googleapis#10436)
PiperOrigin-RevId: 610386082 Co-authored-by: Jaycee Li <[email protected]>
1 parent d3110ae commit 25d00c7

File tree

1 file changed

+70
-1
lines changed
  • java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai

1 file changed

+70
-1
lines changed

java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ChatSession.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ public ResponseStream<GenerateContentResponse> sendMessageStream(String text) th
5858
return sendMessageStream(text, null, null);
5959
}
6060

61+
/**
62+
* Sends a message to the model and returns a stream of responses.
63+
*
64+
* @param text the message to be sent.
65+
* @param config a {@link GenerateContentConfig} that contains all the configs for sending message
66+
* in a chat session.
67+
* @return an iterable in which each element is a GenerateContentResponse. Can be converted to
68+
* stream by stream() method.
69+
*/
70+
@BetaApi
71+
public ResponseStream<GenerateContentResponse> sendMessageStream(
72+
String text, GenerateContentConfig config) throws IOException {
73+
return sendMessageStream(ContentMaker.fromString(text), config);
74+
}
75+
6176
/**
6277
* Sends a message to the model and returns a stream of responses.
6378
*
@@ -123,6 +138,27 @@ public ResponseStream<GenerateContentResponse> sendMessageStream(Content content
123138
return sendMessageStream(content, null, null);
124139
}
125140

141+
/**
142+
* Sends a message to the model and returns a stream of responses.
143+
*
144+
* @param content the content to be sent.
145+
* @param config a {@link GenerateContentConfig} that contains all the configs for sending message
146+
* in a chat session.
147+
* @return an iterable in which each element is a GenerateContentResponse. Can be converted to
148+
* stream by stream() method.
149+
*/
150+
@BetaApi
151+
public ResponseStream<GenerateContentResponse> sendMessageStream(
152+
Content content, GenerateContentConfig config) throws IOException {
153+
checkLastResponseAndEditHistory();
154+
history.add(content);
155+
ResponseStream<GenerateContentResponse> respStream =
156+
model.generateContentStream(history, config);
157+
currentResponseStream = respStream;
158+
currentResponse = null;
159+
return respStream;
160+
}
161+
126162
/**
127163
* Sends a message to the model and returns a stream of responses.
128164
*
@@ -187,6 +223,20 @@ public GenerateContentResponse sendMessage(String text) throws IOException {
187223
return sendMessage(text, null, null);
188224
}
189225

226+
/**
227+
* Sends a message to the model and returns a response.
228+
*
229+
* @param text the message to be sent.
230+
* @param config a {@link GenerateContentConfig} that contains all the configs for sending message
231+
* in a chat session.
232+
* @return a response.
233+
*/
234+
@BetaApi
235+
public GenerateContentResponse sendMessage(String text, GenerateContentConfig config)
236+
throws IOException {
237+
return sendMessage(ContentMaker.fromString(text), config);
238+
}
239+
190240
/**
191241
* Sends a message to the model and returns a response.
192242
*
@@ -246,6 +296,25 @@ public GenerateContentResponse sendMessage(Content content) throws IOException {
246296
return sendMessage(content, null, null);
247297
}
248298

299+
/**
300+
* Sends a message to the model and returns a response.
301+
*
302+
* @param content the content to be sent.
303+
* @param config a {@link GenerateContentConfig} that contains all the configs for sending message
304+
* in a chat session.
305+
* @return a response.
306+
*/
307+
@BetaApi
308+
public GenerateContentResponse sendMessage(Content content, GenerateContentConfig config)
309+
throws IOException {
310+
checkLastResponseAndEditHistory();
311+
history.add(content);
312+
GenerateContentResponse response = model.generateContent(history, config);
313+
currentResponse = response;
314+
currentResponseStream = null;
315+
return response;
316+
}
317+
249318
/**
250319
* Sends a message to the model and returns a response.
251320
*
@@ -303,7 +372,7 @@ private void removeLastContent() {
303372
*
304373
* @throws IllegalStateException if the response stream is not finished.
305374
*/
306-
private void checkLastResponseAndEditHistory() throws IllegalStateException {
375+
private void checkLastResponseAndEditHistory() {
307376
if (currentResponseStream == null && currentResponse == null) {
308377
return;
309378
} else if (currentResponseStream != null && !currentResponseStream.isConsumed()) {

0 commit comments

Comments
 (0)