Skip to content

feat: case insensitive for "sensitive words" and polish #2693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -161,7 +161,7 @@ public Builder pushAll(List<? extends Advisor> advisors) {
}

List<StreamAroundAdvisor> streamAroundAdvisorList = advisors.stream()
.filter(a -> a instanceof StreamAroundAdvisor)
.filter(StreamAroundAdvisor.class::isInstance)
.map(a -> (StreamAroundAdvisor) a)
.toList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public String getName() {

@Override
public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {

String userTextLowerCase = advisedRequest.userText().toLowerCase();
if (!CollectionUtils.isEmpty(this.sensitiveWords)
&& this.sensitiveWords.stream().anyMatch(w -> advisedRequest.userText().contains(w))) {
&& this.sensitiveWords.stream().anyMatch(w -> userTextLowerCase.contains(w.toLowerCase()))) {

return createFailureResponse(advisedRequest);
}
Expand All @@ -86,9 +86,9 @@ public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvis

@Override
public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {

String userTextLowerCase = advisedRequest.userText().toLowerCase();
if (!CollectionUtils.isEmpty(this.sensitiveWords)
&& this.sensitiveWords.stream().anyMatch(w -> advisedRequest.userText().contains(w))) {
&& this.sensitiveWords.stream().anyMatch(w -> userTextLowerCase.contains(w.toLowerCase()))) {
return Flux.just(createFailureResponse(advisedRequest));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.ai.chat.memory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -41,8 +42,9 @@ public class InMemoryChatMemory implements ChatMemory {

@Override
public void add(String conversationId, List<Message> messages) {
this.conversationHistory.putIfAbsent(conversationId, new ArrayList<>());
this.conversationHistory.get(conversationId).addAll(messages);
this.conversationHistory
.computeIfAbsent(conversationId, v -> new ArrayList<>())
.addAll(messages);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {

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

/**
* The target class type reference to which the output will be converted.
*/
/** The target class type reference to which the output will be converted. */
Copy link
Author

@xchopin xchopin Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every attribute comment was on one line except for this one

private final Type type;

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