Skip to content

Commit

Permalink
fix #1193 ModerationModel is not auto configured (#52)
Browse files Browse the repository at this point in the history
Fix issue
[langchain4j/langchain4j#1193](langchain4j/langchain4j#1193)

Added ModerationModel support to `AiService`,`AiServiceFactory` and
`AiServiceAutoConfig`
  • Loading branch information
N3cr0s1s authored Nov 11, 2024
1 parent 1ac0350 commit 017b205
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.moderation.ModerationModel;
import dev.langchain4j.rag.RetrievalAugmentor;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
Expand Down Expand Up @@ -91,9 +92,15 @@
*/
String retrievalAugmentor() default "";

/**
* When the {@link #wiringMode()} is set to {@link AiServiceWiringMode#EXPLICIT},
* this attribute specifies the name of a {@link ModerationModel} bean that should be used by this AI Service.
*/
String moderationModel() default "";

/**
* When the {@link #wiringMode()} is set to {@link AiServiceWiringMode#EXPLICIT},
* this attribute specifies the names of beans containing methods annotated with {@link Tool} that should be used by this AI Service.
*/
String[] tools() default {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.moderation.ModerationModel;
import dev.langchain4j.rag.RetrievalAugmentor;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
Expand All @@ -22,6 +23,7 @@ class AiServiceFactory implements FactoryBean<Object> {
private ChatMemoryProvider chatMemoryProvider;
private ContentRetriever contentRetriever;
private RetrievalAugmentor retrievalAugmentor;
private ModerationModel moderationModel;
private List<Object> tools;

public AiServiceFactory(Class<Object> aiServiceClass) {
Expand Down Expand Up @@ -52,6 +54,10 @@ public void setRetrievalAugmentor(RetrievalAugmentor retrievalAugmentor) {
this.retrievalAugmentor = retrievalAugmentor;
}

public void setModerationModel(ModerationModel moderationModel) {
this.moderationModel = moderationModel;
}

public void setTools(List<Object> tools) {
this.tools = tools;
}
Expand Down Expand Up @@ -83,6 +89,10 @@ public Object getObject() {
builder = builder.contentRetriever(contentRetriever);
}

if (moderationModel != null) {
builder = builder.moderationModel(moderationModel);
}

if (!isNullOrEmpty(tools)) {
builder = builder.tools(tools);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.moderation.ModerationModel;
import dev.langchain4j.rag.RetrievalAugmentor;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import org.springframework.beans.MutablePropertyValues;
Expand Down Expand Up @@ -43,6 +44,7 @@ BeanFactoryPostProcessor aiServicesRegisteringBeanFactoryPostProcessor() {
String[] chatMemoryProviders = beanFactory.getBeanNamesForType(ChatMemoryProvider.class);
String[] contentRetrievers = beanFactory.getBeanNamesForType(ContentRetriever.class);
String[] retrievalAugmentors = beanFactory.getBeanNamesForType(RetrievalAugmentor.class);
String[] moderationModels = beanFactory.getBeanNamesForType(ModerationModel.class);

Set<String> tools = new HashSet<>();
for (String beanName : beanFactory.getBeanDefinitionNames()) {
Expand Down Expand Up @@ -129,6 +131,16 @@ BeanFactoryPostProcessor aiServicesRegisteringBeanFactoryPostProcessor() {
propertyValues
);

addBeanReference(
ModerationModel.class,
aiServiceAnnotation,
aiServiceAnnotation.moderationModel(),
moderationModels,
"moderationModel",
"moderationModel",
propertyValues
);

if (aiServiceAnnotation.wiringMode() == EXPLICIT) {
propertyValues.add("tools", toManagedList(asList(aiServiceAnnotation.tools())));
} else if (aiServiceAnnotation.wiringMode() == AUTOMATIC) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.langchain4j.service.spring.mode.automatic.withModerationModel;

import dev.langchain4j.service.Moderate;
import dev.langchain4j.service.spring.AiService;

@AiService
interface AiServiceWithModerationModel {

@Moderate
String chat(String userMessage);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.langchain4j.service.spring.mode.automatic.withModerationModel;

import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.model.moderation.Moderation;
import dev.langchain4j.model.moderation.ModerationModel;
import dev.langchain4j.model.output.Response;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.List;

@SpringBootApplication
class AiServiceWithModerationModelApplication {

@Bean
ModerationModel moderationModel() {
return new ModerationModel() {
@Override
public Response<Moderation> moderate(String s) {
return Response.from(Moderation.flagged("Flagged"));
}

@Override
public Response<Moderation> moderate(List<ChatMessage> list) {
return Response.from(Moderation.flagged("Flagged"));
}
};
}

public static void main(String[] args) {
SpringApplication.run(AiServiceWithModerationModelApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.langchain4j.service.spring.mode.automatic.withModerationModel;

import dev.langchain4j.service.ModerationException;
import dev.langchain4j.service.spring.AiServicesAutoConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class AiServiceWithModerationModelIT {

ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));

@Test
void should_create_AI_service_with_moderation_model() {
contextRunner
.withPropertyValues(
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY,
"langchain4j.open-ai.chat-model.max-tokens=20",
"langchain4j.open-ai.chat-model.temperature=0.0"
)
.withUserConfiguration(AiServiceWithModerationModelApplication.class)
.run(context -> {

// given
AiServiceWithModerationModel aiService = context.getBean(AiServiceWithModerationModel.class);

// when & then
assertThatThrownBy(() -> aiService.chat("I'm violating content policy"))
.isInstanceOf(ModerationException.class)
.hasMessageContaining("Flagged");

});
}
}

0 comments on commit 017b205

Please sign in to comment.