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 @@ -31,6 +31,7 @@
import io.agentscope.core.model.GenerateOptions;
import io.agentscope.core.model.ModelContextWindows;
import io.agentscope.core.model.ModelException;
import io.agentscope.core.model.ModelUtils;
import io.agentscope.core.model.ToolSchema;
import io.agentscope.core.model.transport.ProxyConfig;
import io.agentscope.extensions.model.gemini.formatter.GeminiChatFormatter;
Expand Down Expand Up @@ -232,6 +233,17 @@ private static HttpOptions resolveHttpOptions(String baseUrl, HttpOptions httpOp
@Override
protected Flux<ChatResponse> doStream(
List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) {
return applyExecutionConfig(doStream0(messages, tools, options), options);
}

private Flux<ChatResponse> applyExecutionConfig(
Flux<ChatResponse> responseFlux, GenerateOptions options) {
return ModelUtils.applyTimeoutAndRetry(
responseFlux, options, defaultOptions, modelName, "gemini");
}

protected Flux<ChatResponse> doStream0(
List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) {
Instant startTime = Instant.now();
log.debug(
"Gemini stream: model={}, messages={}, tools_present={}, streaming={}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,32 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.genai.types.ClientOptions;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.ProxyOptions;
import io.agentscope.core.message.Msg;
import io.agentscope.core.model.ChatResponse;
import io.agentscope.core.model.ExecutionConfig;
import io.agentscope.core.model.GenerateOptions;
import io.agentscope.core.model.ModelException;
import io.agentscope.core.model.ToolChoice;
import io.agentscope.core.model.ToolSchema;
import io.agentscope.core.model.test.ModelTestUtils;
import io.agentscope.core.model.transport.ProxyConfig;
import io.agentscope.extensions.model.gemini.formatter.GeminiChatFormatter;
import io.agentscope.extensions.model.gemini.formatter.GeminiMultiAgentFormatter;
import java.lang.reflect.Field;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;

/**
* Unit tests for GeminiChatModel.
Expand All @@ -58,6 +67,59 @@ void setUp() {
mockApiKey = ModelTestUtils.createMockApiKey();
}

@Test
@DisplayName("Should apply request timeout to Gemini response streams")
void testAppliesRequestTimeout() {
GenerateOptions options =
GenerateOptions.builder()
.executionConfig(
ExecutionConfig.builder()
.timeout(Duration.ofMillis(10))
.maxAttempts(1)
.build())
.build();

ModelException error =
assertThrows(
ModelException.class,
() ->
new TestGeminiChatModel(Flux.never())
.stream(List.of(), List.of(), options)
.blockLast(Duration.ofSeconds(1)));

assertTrue(error.getMessage().contains("timeout"));
}

@Test
@DisplayName("Should retry Gemini response streams according to execution config")
void testRetriesRequestThroughStreamEntryPoint() {
AtomicInteger attempts = new AtomicInteger();
GenerateOptions options =
GenerateOptions.builder()
.executionConfig(
ExecutionConfig.builder()
.maxAttempts(2)
.initialBackoff(Duration.ofMillis(1))
.maxBackoff(Duration.ofMillis(1))
.retryOn(error -> true)
.build())
.build();

assertThrows(
RuntimeException.class,
() ->
new TestGeminiChatModel(
Flux.defer(
() -> {
attempts.incrementAndGet();
return Flux.error(
new IllegalStateException("failed"));
}))
.stream(List.of(), List.of(), options).blockLast());

assertEquals(2, attempts.get());
}

@Test
@DisplayName("Should create model with valid configuration")
void testBasicModelCreation() {
Expand Down Expand Up @@ -629,4 +691,20 @@ private static HttpOptions getHttpOptions(GeminiChatModel model) throws Exceptio
httpOptionsField.setAccessible(true);
return (HttpOptions) httpOptionsField.get(model);
}

private static final class TestGeminiChatModel extends GeminiChatModel {

private final Flux<ChatResponse> response;

private TestGeminiChatModel(Flux<ChatResponse> response) {
super("test-key", "gemini-test", true, null, null, null, null, null, null, null, null);
this.response = response;
}

@Override
protected Flux<ChatResponse> doStream0(
List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) {
return response;
}
}
}
Loading