Federico Mariani created CAMEL-23954:
----------------------------------------

             Summary: camel-langchain4j-agent: moderation support 
(ModerationModel + @Moderate)
                 Key: CAMEL-23954
                 URL: https://issues.apache.org/jira/browse/CAMEL-23954
             Project: Camel
          Issue Type: New Feature
          Components: camel-langchain4j-agent
            Reporter: Federico Mariani


LangChain4j supports content moderation on AI services via 
{{AiServices.moderationModel(...)}} combined with the {{@Moderate}} annotation 
on the service interface methods: the moderation call runs concurrently with 
the chat call, and flagged input raises {{ModerationException}} before the 
answer is returned.

The agent component exposes neither. Unlike most builder options this is not a 
pure {{AgentConfiguration}} addition: {{@Moderate}} must be present on the 
internal service interfaces ({{AiAgentWithMemoryService}} / 
{{AiAgentWithoutMemoryService}}), so it needs either annotated method variants 
or separate moderated service interfaces, selected when a {{moderationModel}} 
is configured.

*Use case*: a public-facing support chatbot exposed via {{platform-http}}. 
Untrusted end-user text goes straight into an agent with tool access; the 
operator must reject policy-violating input (harassment, self-harm, 
prompt-injection bait wrapped in abusive content) *before* the agent spends 
tokens and invokes tools, and must return a controlled refusal instead of a 
stack trace:

{code:java}
// AgentConfiguration bean
new AgentConfiguration()
    .withChatModel(chatModel)
    
.withModerationModel(OpenAiModerationModel.builder().apiKey("{{openai.key}}").build());

// Route
onException(ModerationException.class)
    .handled(true)
    .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(422))
    .setBody(constant("Sorry, your message violates our usage policy."));

from("platform-http:/support/chat")
    
.to("langchain4j-agent:support?agentConfiguration=#supportAgentConfig&tags=orders,billing");
{code}

*Proposal*: {{AgentConfiguration.withModerationModel(ModerationModel)}} + 
moderated service interface variants + {{ModerationException}} surfaced as the 
Camel exchange exception with a dedicated header carrying the flagged content 
details.

_This issue was drafted by Claude Code on behalf of Federico Mariani._



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to