Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.reactivex.rxjava3.core.Single;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -109,8 +110,8 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
continue;
}

Map<String, ToolConfirmation> toolsToResumeWithConfirmation = new HashMap<>();
Map<String, FunctionCall> toolsToResumeWithArgs = new HashMap<>();
final Map<String, ToolConfirmation> toolsToResumeWithConfirmation = new HashMap<>();
final Map<String, FunctionCall> toolsToResumeWithArgs = new HashMap<>();

event.functionCalls().stream()
.filter(
Expand Down Expand Up @@ -163,6 +164,16 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
// Create an updated LlmRequest including the new event's content
ImmutableList.Builder<Content> updatedContentsBuilder =
ImmutableList.<Content>builder().addAll(llmRequest.contents());

final List<Part> functionCalls =
toolsToResumeWithArgs.values().stream()
.map(functionCall -> Part.builder().functionCall(functionCall).build())
.collect(toImmutableList());
Content functionCallContent =
Content.builder().role("model").parts(functionCalls).build();
// add function call
updatedContentsBuilder.add(functionCallContent);
// add function response
assembledEvent.content().ifPresent(updatedContentsBuilder::add);

LlmRequest updatedLlmRequest =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,48 @@ public class RequestConfirmationLlmRequestProcessorTest {
.build()))
.build();

private static final Event FUNCTION_CALL_EVENT =
Event.builder()
.author("model")
.content(
Content.builder()
.role("model")
.parts(
Part.builder()
.functionCall(
FunctionCall.builder()
.id(ORIGINAL_FUNCTION_CALL_ID)
.name(ECHO_TOOL_NAME)
.args(ImmutableMap.of("say", "hello"))
.build())
.build())
.build())
.build();

private static final Event FUNCTION_RESPONSE_EVENT =
Event.builder()
.author("user")
.content(
Content.builder()
.role("user")
.parts(
Part.builder()
.functionResponse(
FunctionResponse.builder()
.id(ORIGINAL_FUNCTION_CALL_ID)
.name(ECHO_TOOL_NAME)
.response(
ImmutableMap.of("result", ImmutableMap.of("say", "hello")))
.build())
.build())
.build())
.build();

private static final RequestConfirmationLlmRequestProcessor processor =
new RequestConfirmationLlmRequestProcessor();

@Test
public void runAsync_withConfirmation_callsOriginalFunction() {
public void runAsync_withConfirmation_callsOriginalFunctionAndAppendsToUpdatedRequest() {
LlmAgent agent = createAgentWithEchoTool();
Session session =
Session.builder("session_id")
Expand All @@ -126,6 +163,14 @@ public void runAsync_withConfirmation_callsOriginalFunction() {
assertThat(fr.id()).hasValue(ORIGINAL_FUNCTION_CALL_ID);
assertThat(fr.name()).hasValue(ECHO_TOOL_NAME);
assertThat(fr.response()).hasValue(ImmutableMap.of("result", ImmutableMap.of("say", "hello")));
assertThat(result.updatedRequest())
.isEqualTo(
LlmRequest.builder()
.contents(
ImmutableList.of(
FUNCTION_CALL_EVENT.content().get(),
FUNCTION_RESPONSE_EVENT.content().get()))
.build());
}

@Test
Expand Down