Skip to content
Merged
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
27 changes: 15 additions & 12 deletions core/src/main/java/com/google/adk/flows/llmflows/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import io.opentelemetry.context.Scope;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Function;
Expand Down Expand Up @@ -152,15 +153,16 @@ public static Maybe<Event> handleFunctionCalls(
Function<FunctionCall, Maybe<Event>> functionCallMapper =
getFunctionCallMapper(invocationContext, tools, toolConfirmations, false);

Flowable<Event> functionResponseEventsFlowable;
Observable<Event> functionResponseEventsObservable;
if (invocationContext.runConfig().toolExecutionMode() == ToolExecutionMode.SEQUENTIAL) {
functionResponseEventsFlowable =
Flowable.fromIterable(functionCalls).concatMapMaybe(functionCallMapper);
functionResponseEventsObservable =
Observable.fromIterable(functionCalls).concatMapMaybe(functionCallMapper);
} else {
functionResponseEventsFlowable =
Flowable.fromIterable(functionCalls).flatMapMaybe(functionCallMapper);
functionResponseEventsObservable =
Observable.fromIterable(functionCalls)
.concatMapEager(call -> functionCallMapper.apply(call).toObservable());
}
return functionResponseEventsFlowable
return functionResponseEventsObservable
.toList()
.flatMapMaybe(
events -> {
Expand Down Expand Up @@ -217,16 +219,17 @@ public static Maybe<Event> handleFunctionCallsLive(
Function<FunctionCall, Maybe<Event>> functionCallMapper =
getFunctionCallMapper(invocationContext, tools, toolConfirmations, true);

Flowable<Event> responseEventsFlowable;
Observable<Event> responseEventsObservable;
if (invocationContext.runConfig().toolExecutionMode() == ToolExecutionMode.SEQUENTIAL) {
responseEventsFlowable =
Flowable.fromIterable(functionCalls).concatMapMaybe(functionCallMapper);
responseEventsObservable =
Observable.fromIterable(functionCalls).concatMapMaybe(functionCallMapper);
} else {
responseEventsFlowable =
Flowable.fromIterable(functionCalls).flatMapMaybe(functionCallMapper);
responseEventsObservable =
Observable.fromIterable(functionCalls)
.concatMapEager(call -> functionCallMapper.apply(call).toObservable());
}

return responseEventsFlowable
return responseEventsObservable
.toList()
.flatMapMaybe(
events -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.junit.Assert.assertThrows;

import com.google.adk.agents.InvocationContext;
import com.google.adk.agents.RunConfig;
import com.google.adk.agents.RunConfig.ToolExecutionMode;
import com.google.adk.events.Event;
import com.google.adk.testing.TestUtils;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -151,8 +153,11 @@ public void handleFunctionCalls_singleFunctionCall() {
}

@Test
public void handleFunctionCalls_multipleFunctionCalls() {
InvocationContext invocationContext = createInvocationContext(createRootAgent());
public void handleFunctionCalls_multipleFunctionCalls_parallel() {
InvocationContext invocationContext =
createInvocationContext(
createRootAgent(),
RunConfig.builder().setToolExecutionMode(ToolExecutionMode.PARALLEL).build());
ImmutableMap<String, Object> args1 = ImmutableMap.<String, Object>of("key1", "value2");
ImmutableMap<String, Object> args2 = ImmutableMap.<String, Object>of("key2", "value2");
Event event =
Expand Down Expand Up @@ -201,7 +206,66 @@ public void handleFunctionCalls_multipleFunctionCalls() {
.name("echo_tool")
.response(ImmutableMap.of("result", args2))
.build())
.build());
.build())
.inOrder();
}

@Test
public void handleFunctionCalls_multipleFunctionCalls_sequential() {
InvocationContext invocationContext =
createInvocationContext(
createRootAgent(),
RunConfig.builder().setToolExecutionMode(ToolExecutionMode.SEQUENTIAL).build());
ImmutableMap<String, Object> args1 = ImmutableMap.<String, Object>of("key1", "value2");
ImmutableMap<String, Object> args2 = ImmutableMap.<String, Object>of("key2", "value2");
Event event =
createEvent("event").toBuilder()
.content(
Content.fromParts(
Part.fromText("..."),
Part.builder()
.functionCall(
FunctionCall.builder()
.id("function_call_id1")
.name("echo_tool")
.args(args1)
.build())
.build(),
Part.builder()
.functionCall(
FunctionCall.builder()
.id("function_call_id2")
.name("echo_tool")
.args(args2)
.build())
.build()))
.build();

Event functionResponseEvent =
Functions.handleFunctionCalls(
invocationContext, event, ImmutableMap.of("echo_tool", new TestUtils.EchoTool()))
.blockingGet();

assertThat(functionResponseEvent).isNotNull();
assertThat(functionResponseEvent.content().get().parts().get())
.containsExactly(
Part.builder()
.functionResponse(
FunctionResponse.builder()
.id("function_call_id1")
.name("echo_tool")
.response(ImmutableMap.of("result", args1))
.build())
.build(),
Part.builder()
.functionResponse(
FunctionResponse.builder()
.id("function_call_id2")
.name("echo_tool")
.response(ImmutableMap.of("result", args2))
.build())
.build())
.inOrder();
}

@Test
Expand Down