3535/** Request / Response utilities for {@link Gemini}. */
3636public final class GeminiUtil {
3737
38+ /**
39+ * Text of the user turn appended when a request has no contents, so that the model acts on the
40+ * system instruction.
41+ */
42+ public static final String HANDLE_SYSTEM_INSTRUCTION_MESSAGE =
43+ "Handle the requests as specified in the System Instruction." ;
44+
45+ /**
46+ * Text of the user turn appended when the last content is not from the user, so that the model
47+ * keeps producing output.
48+ */
3849 public static final String CONTINUE_OUTPUT_MESSAGE =
39- "Continue output. DO NOT look at this line. ONLY look at the content before this line and "
40- + " system instruction ." ;
50+ "Continue processing previous requests as instructed. Exit or provide a summary if no more "
51+ + " outputs are needed ." ;
4152
4253 private GeminiUtil () {}
4354
@@ -193,9 +204,10 @@ private static Part removeClientFunctionCallIdFromPart(Part part) {
193204 * Ensures that the content is conducive to prompting a model response by ensuring the last
194205 * content part is from the user.
195206 *
196- * <p>If the list is empty or the last message is not from the user, a new "user" content part
197- * with a {@link #CONTINUE_OUTPUT_MESSAGE} is appended to the list. This is necessary to prompt
198- * the model to generate a response.
207+ * <p>If the list is empty, a new "user" content part with {@link
208+ * #HANDLE_SYSTEM_INSTRUCTION_MESSAGE} is appended. If the last message is not from the user, a
209+ * new "user" content part with {@link #CONTINUE_OUTPUT_MESSAGE} is appended. This is necessary to
210+ * prompt the model to generate a response.
199211 *
200212 * @param contents The original list of {@link Content}.
201213 * @return A list of {@link Content} where the last element is guaranteed to be from the "user".
@@ -204,11 +216,10 @@ static List<Content> ensureModelResponse(List<Content> contents) {
204216 // Last content must be from the user, otherwise the model won't respond.
205217 if (contents .isEmpty ()
206218 || !Ascii .equalsIgnoreCase (Iterables .getLast (contents ).role ().orElse ("" ), Role .USER )) {
219+ String text =
220+ contents .isEmpty () ? HANDLE_SYSTEM_INSTRUCTION_MESSAGE : CONTINUE_OUTPUT_MESSAGE ;
207221 Content userContent =
208- Content .builder ()
209- .parts (ImmutableList .of (Part .fromText (CONTINUE_OUTPUT_MESSAGE )))
210- .role (Role .USER )
211- .build ();
222+ Content .builder ().parts (ImmutableList .of (Part .fromText (text ))).role (Role .USER ).build ();
212223 return Stream .concat (contents .stream (), Stream .of (userContent )).collect (toImmutableList ());
213224 }
214225 return contents ;
0 commit comments