Skip to content

Commit 43641f5

Browse files
committed
Fixing issues in BedrockTitanEmbeddingModel.java
Signed-off-by: PSriVarshan <[email protected]>
1 parent 945aabb commit 43641f5

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

auto-configurations/common/spring-ai-autoconfigure-retry/src/main/java/org/springframework/ai/retry/autoconfigure/SpringAiRetryAutoConfiguration.java

+35-26
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
import org.springframework.web.client.ResponseErrorHandler;
4242

4343
/**
44-
* {@link AutoConfiguration Auto-configuration} for AI Retry.
44+
* {@link AutoConfiguration Auto-configuration} for AI Retry. Provides beans for retry
45+
* template and response error handling. Handles transient and non-transient exceptions
46+
* based on HTTP status codes.
4547
*
46-
* @author Christian Tzolov
48+
* Author: Christian Tzolov
4749
*/
4850
@AutoConfiguration
4951
@ConditionalOnClass(RetryUtils.class)
@@ -63,9 +65,10 @@ public RetryTemplate retryTemplate(SpringAiRetryProperties properties) {
6365
.withListener(new RetryListener() {
6466

6567
@Override
66-
public <T extends Object, E extends Throwable> void onError(RetryContext context,
67-
RetryCallback<T, E> callback, Throwable throwable) {
68-
logger.warn("Retry error. Retry count:" + context.getRetryCount(), throwable);
68+
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback,
69+
Throwable throwable) {
70+
logger.warn("Retry error. Retry count: {}, Exception: {}", context.getRetryCount(),
71+
throwable.getMessage(), throwable);
6972
}
7073
})
7174
.build();
@@ -84,29 +87,35 @@ public boolean hasError(@NonNull ClientHttpResponse response) throws IOException
8487

8588
@Override
8689
public void handleError(@NonNull ClientHttpResponse response) throws IOException {
87-
if (response.getStatusCode().isError()) {
88-
String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
89-
String message = String.format("%s - %s", response.getStatusCode().value(), error);
90-
91-
// Explicitly configured transient codes
92-
if (properties.getOnHttpCodes().contains(response.getStatusCode().value())) {
93-
throw new TransientAiException(message);
94-
}
95-
96-
// onClientErrors - If true, do not throw a NonTransientAiException,
97-
// and do not attempt retry for 4xx client error codes, false by
98-
// default.
99-
if (!properties.isOnClientErrors() && response.getStatusCode().is4xxClientError()) {
100-
throw new NonTransientAiException(message);
101-
}
102-
103-
// Explicitly configured non-transient codes
104-
if (!CollectionUtils.isEmpty(properties.getExcludeOnHttpCodes())
105-
&& properties.getExcludeOnHttpCodes().contains(response.getStatusCode().value())) {
106-
throw new NonTransientAiException(message);
107-
}
90+
if (!response.getStatusCode().isError()) {
91+
return;
92+
}
93+
94+
String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
95+
if (error == null || error.isEmpty()) {
96+
error = "No response body available";
97+
}
98+
99+
String message = String.format("HTTP %s - %s", response.getStatusCode().value(), error);
100+
101+
// Explicitly configured transient codes
102+
if (properties.getOnHttpCodes().contains(response.getStatusCode().value())) {
108103
throw new TransientAiException(message);
109104
}
105+
106+
// Handle client errors (4xx)
107+
if (!properties.isOnClientErrors() && response.getStatusCode().is4xxClientError()) {
108+
throw new NonTransientAiException(message);
109+
}
110+
111+
// Explicitly configured non-transient codes
112+
if (!CollectionUtils.isEmpty(properties.getExcludeOnHttpCodes())
113+
&& properties.getExcludeOnHttpCodes().contains(response.getStatusCode().value())) {
114+
throw new NonTransientAiException(message);
115+
}
116+
117+
// Default to transient exception
118+
throw new TransientAiException(message);
110119
}
111120
};
112121
}

auto-configurations/models/spring-ai-autoconfigure-model-bedrock-ai/pom.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@
110110
<artifactId>mockito-core</artifactId>
111111
<scope>test</scope>
112112
</dependency>
113-
</dependencies>
113+
114+
<dependency>
115+
<groupId>io.micrometer</groupId>
116+
<artifactId>micrometer-observation</artifactId>
117+
<version>1.15.0-RC1</version>
118+
</dependency>
119+
</dependencies>
114120

115121
</project>

auto-configurations/models/spring-ai-autoconfigure-model-bedrock-ai/src/main/java/org/springframework/ai/model/bedrock/titan/autoconfigure/BedrockTitanEmbeddingAutoConfiguration.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.ai.model.bedrock.titan.autoconfigure;
1818

1919
import com.fasterxml.jackson.databind.ObjectMapper;
20+
21+
import io.micrometer.observation.ObservationRegistry;
2022
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
2123
import software.amazon.awssdk.regions.providers.AwsRegionProvider;
2224

@@ -26,6 +28,7 @@
2628
import org.springframework.ai.model.SpringAIModels;
2729
import org.springframework.ai.model.bedrock.autoconfigure.BedrockAwsConnectionConfiguration;
2830
import org.springframework.ai.model.bedrock.autoconfigure.BedrockAwsConnectionProperties;
31+
import org.springframework.beans.factory.annotation.Autowired;
2932
import org.springframework.boot.autoconfigure.AutoConfiguration;
3033
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3134
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -50,6 +53,9 @@
5053
@Import(BedrockAwsConnectionConfiguration.class)
5154
public class BedrockTitanEmbeddingAutoConfiguration {
5255

56+
@Autowired
57+
private ObservationRegistry observationRegistry;
58+
5359
@Bean
5460
@ConditionalOnMissingBean
5561
@ConditionalOnBean({ AwsCredentialsProvider.class, AwsRegionProvider.class })
@@ -65,7 +71,8 @@ public TitanEmbeddingBedrockApi titanEmbeddingBedrockApi(AwsCredentialsProvider
6571
@ConditionalOnBean(TitanEmbeddingBedrockApi.class)
6672
public BedrockTitanEmbeddingModel titanEmbeddingModel(TitanEmbeddingBedrockApi titanEmbeddingApi,
6773
BedrockTitanEmbeddingProperties properties) {
68-
return new BedrockTitanEmbeddingModel(titanEmbeddingApi).withInputType(properties.getInputType());
74+
return new BedrockTitanEmbeddingModel(titanEmbeddingApi, observationRegistry)
75+
.withInputType(properties.getInputType());
6976
}
7077

7178
}

0 commit comments

Comments
 (0)