Skip to content

Commit b7f45e2

Browse files
Merge pull request #1629 from innoprej:fix/gemini-placeholder-parity
PiperOrigin-RevId: 1008931998
2 parents 8b560fc + 234d7dc commit b7f45e2

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

‎core/src/main/java/com/google/adk/models/GeminiUtil.java‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,20 @@
3535
/** Request / Response utilities for {@link Gemini}. */
3636
public 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;

‎core/src/test/java/com/google/adk/models/GeminiUtilTest.java‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@
3737
@RunWith(JUnit4.class)
3838
public final class GeminiUtilTest {
3939

40+
private static final Content SYSTEM_INSTRUCTION_CONTENT =
41+
Content.fromParts(
42+
Part.fromText("Handle the requests as specified in the System Instruction."));
4043
private static final Content CONTINUE_CONTENT =
41-
Content.fromParts(Part.fromText(GeminiUtil.CONTINUE_OUTPUT_MESSAGE));
44+
Content.fromParts(
45+
Part.fromText(
46+
"Continue processing previous requests as instructed. Exit or provide a summary if"
47+
+ " no more outputs are needed."));
4248

4349
@Test
4450
public void getPart0FromLlmResponse_noContent_returnsEmpty() {
@@ -334,12 +340,12 @@ public void sanitizeRequestForGeminiApi_multipleContents_sanitizesAll() {
334340
}
335341

336342
@Test
337-
public void ensureModelResponse_emptyList_appendsContinueMessage() {
343+
public void ensureModelResponse_emptyList_appendsSystemInstructionMessage() {
338344
ImmutableList<Content> contents = ImmutableList.of();
339345

340346
List<Content> result = GeminiUtil.ensureModelResponse(contents);
341347

342-
assertThat(result).containsExactly(CONTINUE_CONTENT);
348+
assertThat(result).containsExactly(SYSTEM_INSTRUCTION_CONTENT);
343349
}
344350

345351
@Test
@@ -405,12 +411,13 @@ public void ensureModelResponse_lastContentIsNotUser_appendsContinueMessage() {
405411
}
406412

407413
@Test
408-
public void prepareGenenerateContentRequest_emptyRequest_returnsRequestWithContinueContent() {
414+
public void
415+
prepareGenenerateContentRequest_emptyRequest_returnsRequestWithSystemInstructionContent() {
409416
LlmRequest request = LlmRequest.builder().build();
410417

411418
LlmRequest result = GeminiUtil.prepareGenenerateContentRequest(request, true);
412419

413-
assertThat(result.contents()).containsExactly(CONTINUE_CONTENT);
420+
assertThat(result.contents()).containsExactly(SYSTEM_INSTRUCTION_CONTENT);
414421
assertThat(result.config()).isEmpty();
415422
}
416423

0 commit comments

Comments
 (0)