[ 
https://issues.apache.org/jira/browse/CAMEL-23965?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18103067#comment-18103067
 ] 

Karol Krawczyk commented on CAMEL-23965:
----------------------------------------

PR: https://github.com/apache/camel/pull/25410

Implemented as proposed, with the pass-through body plus verdict headers:

- {{CamelOpenAIModerationFlagged}} is always a Boolean (for a batch: true when 
at least one input was flagged), so the same content-based routing works for a 
single input and for a list.
- {{CamelOpenAIModerationCategories}} / {{CamelOpenAIModerationCategoryScores}} 
are a Map for a single input and a List of maps for a batch, mirroring 
{{CamelOpenAIOriginalText}} in the embeddings operation.
- New {{moderationModel}} option, default {{omni-moderation-latest}}, 
overridable per message with {{CamelOpenAIModerationModel}}.
- {{storeFullResponse=true}} stores the SDK response in a dedicated 
{{CamelOpenAIModerationResponse}} property rather than reusing 
{{CamelOpenAIResponse}}, whose declared javaType is ChatCompletion.

Two notes worth flagging:

- The issue was written against openai-java 4.41.0, but the project is now on 
4.49.0. The API used here was verified against 4.49.0.
- {{Moderation.Categories.illicit()}} / {{illicitViolent()}} are Optional, 
because only the omni-moderation-* models return them, while the same 
categories are plain doubles in CategoryScores. The category map therefore 
omits them for legacy models; documented and covered by a test.

There is no integration test: the ITs in camel-openai run against Ollama via 
testcontainers, and Ollama does not expose /v1/moderations. Instead, 
camel-test-infra-openai-mock was extended with the /moderations endpoint, and 
the operation is covered by 10 mock-based test cases.

[~croway] could you review it, since you reported this one? I cannot request 
reviewers on GitHub myself.

_Reported by Claude Code on behalf of Karol Krawczyk_

> camel-openai: add moderation operation
> --------------------------------------
>
>                 Key: CAMEL-23965
>                 URL: https://issues.apache.org/jira/browse/CAMEL-23965
>             Project: Camel
>          Issue Type: New Feature
>          Components: camel-openai
>    Affects Versions: 4.21.0
>            Reporter: Federico Mariani
>            Priority: Major
>              Labels: ai
>
> The pinned openai-java SDK (4.41.0) exposes 
> {{moderations().create(ModerationCreateParams)}}, but camel-openai has no 
> moderation operation. Moderation is the canonical pre-filter for untrusted 
> input on public-facing AI routes — rejecting policy-violating content 
> *before* spending chat tokens or triggering tool calls — and fits Camel's 
> routing model perfectly (content-based routing on the moderation verdict).
> Proposal: new operation {{openai:moderation}}:
> * body: String (or List of Strings for batch);
> * options/headers: {{moderationModel}} (e.g. {{omni-moderation-latest}});
> * output: body unchanged (pass-through style) with headers 
> {{CamelOpenAIModerationFlagged}} (Boolean), 
> {{CamelOpenAIModerationCategories}} (Map or JSON), 
> {{CamelOpenAIModerationCategoryScores}}; full response via 
> {{storeFullResponse}}.
> Pass-through body + verdict headers lets users do content-based routing on 
> the verdict without content juggling:
> {code:java}
> // guard a public-facing chat route: reject flagged input before spending 
> chat tokens
> from("platform-http:/chat")
>     .to("openai:moderation?moderationModel=omni-moderation-latest")
>     .choice()
>         .when(header(OpenAIConstants.MODERATION_FLAGGED).isEqualTo(true))
>             .setBody(constant("Your message violates our usage policy."))
>         .otherwise()
>             .to("openai:chat-completion?model=gpt-5")
>     .end();
> {code}
> Cross-reference: CAMEL-23954 proposes moderation for camel-langchain4j-agent 
> via LangChain4j {{@Moderate}}; a native operation in camel-openai is simpler 
> (no annotated-interface machinery) and would give the OpenAI stack parity.
> _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