-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #1193 ModerationModel is not auto configured (#52)
Fix issue [langchain4j/langchain4j#1193](langchain4j/langchain4j#1193) Added ModerationModel support to `AiService`,`AiServiceFactory` and `AiServiceAutoConfig`
- Loading branch information
Showing
6 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ain4j/service/spring/mode/automatic/withModerationModel/AiServiceWithModerationModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...ce/spring/mode/automatic/withModerationModel/AiServiceWithModerationModelApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...n4j/service/spring/mode/automatic/withModerationModel/AiServiceWithModerationModelIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
||
}); | ||
} | ||
} |