Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions core/src/main/java/com/google/adk/models/LlmRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.adk.models;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;

Expand Down Expand Up @@ -172,29 +171,33 @@ public final Builder appendInstructions(List<String> instructions) {
return liveConnectConfig(liveCfg.toBuilder().systemInstruction(newLiveSi).build());
}

// In this particular case we can keep the Optional as a type of a
// parameter, since the function is private and used in only one place while
// the Optional type plays nicely with flatMaps in the code (if we had a
// nullable here, we'd wrap it in the Optional anyway)
private Content addInstructions(
Optional<Content> currentSystemInstruction, List<String> additionalInstructions) {
@SuppressWarnings("checkstyle:IllegalType") Optional<Content> currentSystemInstruction,
List<String> additionalInstructions) {
checkArgument(
currentSystemInstruction.isEmpty()
|| currentSystemInstruction.get().parts().map(parts -> parts.size()).orElse(0) <= 1,
currentSystemInstruction.flatMap(Content::parts).map(parts -> parts.size()).orElse(0)
<= 1,
"At most one instruction is supported.");

// Either append to the existing instruction, or create a new one.
String instructions = String.join("\n\n", additionalInstructions);

Optional<Part> part =
currentSystemInstruction
.flatMap(Content::parts)
.flatMap(parts -> parts.stream().findFirst());
if (part.isEmpty() || part.get().text().isEmpty()) {
part = Optional.of(Part.fromText(instructions));
} else {
part = Optional.of(Part.fromText(part.get().text().get() + "\n\n" + instructions));
}
checkState(part.isPresent(), "Failed to create instruction.");
Part part =
Part.fromText(
currentSystemInstruction
.flatMap(Content::parts)
.flatMap(parts -> parts.stream().findFirst())
.flatMap(Part::text)
.map(text -> text + "\n\n" + instructions)
.orElse(instructions));

String role = currentSystemInstruction.flatMap(Content::role).orElse("user");
return Content.builder().parts(part.get()).role(role).build();

return Content.builder().parts(part).role(role).build();
}

@CanIgnoreReturnValue
Expand Down