Skip to content

Commit 2f5cd0a

Browse files
xchopinXavier Chopin
authored and
Xavier Chopin
committed
feat: case insensitive for "sensitive words" and polish
Signed-off-by: Xavier Chopin <[email protected]>
1 parent 442b2e9 commit 2f5cd0a

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

Diff for: spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/DefaultAroundAdvisorChain.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public Builder pushAll(List<? extends Advisor> advisors) {
161161
}
162162

163163
List<StreamAroundAdvisor> streamAroundAdvisorList = advisors.stream()
164-
.filter(a -> a instanceof StreamAroundAdvisor)
164+
.filter(StreamAroundAdvisor.class::isInstance)
165165
.map(a -> (StreamAroundAdvisor) a)
166166
.toList();
167167

Diff for: spring-ai-client-chat/src/main/java/org/springframework/ai/chat/client/advisor/SafeGuardAdvisor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ public String getName() {
7474

7575
@Override
7676
public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
77-
77+
String userTextLowerCase = advisedRequest.userText().toLowerCase();
7878
if (!CollectionUtils.isEmpty(this.sensitiveWords)
79-
&& this.sensitiveWords.stream().anyMatch(w -> advisedRequest.userText().contains(w))) {
79+
&& this.sensitiveWords.stream().anyMatch(w -> userTextLowerCase.contains(w.toLowerCase()))) {
8080

8181
return createFailureResponse(advisedRequest);
8282
}
@@ -86,9 +86,9 @@ public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvis
8686

8787
@Override
8888
public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
89-
89+
String userTextLowerCase = advisedRequest.userText().toLowerCase();
9090
if (!CollectionUtils.isEmpty(this.sensitiveWords)
91-
&& this.sensitiveWords.stream().anyMatch(w -> advisedRequest.userText().contains(w))) {
91+
&& this.sensitiveWords.stream().anyMatch(w -> userTextLowerCase.contains(w.toLowerCase()))) {
9292
return Flux.just(createFailureResponse(advisedRequest));
9393
}
9494

Diff for: spring-ai-client-chat/src/main/java/org/springframework/ai/chat/memory/InMemoryChatMemory.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.ai.chat.memory;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collections;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.concurrent.ConcurrentHashMap;
@@ -41,8 +42,9 @@ public class InMemoryChatMemory implements ChatMemory {
4142

4243
@Override
4344
public void add(String conversationId, List<Message> messages) {
44-
this.conversationHistory.putIfAbsent(conversationId, new ArrayList<>());
45-
this.conversationHistory.get(conversationId).addAll(messages);
45+
this.conversationHistory
46+
.computeIfAbsent(conversationId, v -> new ArrayList<>())
47+
.addAll(messages);
4648
}
4749

4850
@Override

Diff for: spring-ai-client-chat/src/main/java/org/springframework/ai/converter/BeanOutputConverter.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {
6363

6464
private final Logger logger = LoggerFactory.getLogger(BeanOutputConverter.class);
6565

66-
/**
67-
* The target class type reference to which the output will be converted.
68-
*/
66+
/** The target class type reference to which the output will be converted. */
6967
private final Type type;
7068

7169
/** The object mapper used for deserialization and other JSON operations. */
@@ -119,7 +117,7 @@ public BeanOutputConverter(ParameterizedTypeReference<T> typeRef, ObjectMapper o
119117
* @param objectMapper Custom object mapper for JSON operations. endings.
120118
*/
121119
private BeanOutputConverter(Type type, ObjectMapper objectMapper) {
122-
Objects.requireNonNull(type, "Type cannot be null;");
120+
Objects.requireNonNull(type, "Type cannot be null");
123121
this.type = type;
124122
this.objectMapper = objectMapper != null ? objectMapper : getObjectMapper();
125123
generateSchema();

0 commit comments

Comments
 (0)