[ 
https://issues.apache.org/jira/browse/CAMEL-27912?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Jiří Ondrušek updated CAMEL-27912:
----------------------------------
    Description: 
h3. Problem
LangChain4j guardrails are Java classes. In Camel they can only be attached by 
class ({{AgentConfiguration.withInputGuardrailClasses}} / 
{{withOutputGuardrailClasses}}); there is no way to write a guardrail as a 
route, in the Java DSL or in YAML.

h3. Proposal
A new consumer component 
{{ai-guardrail:<name>?type=input|output&tags=..&order=..&timeout=..}}: the 
route is the guardrail, selected by tag like {{ai-tool}}. Adapters 
{{CamelRouteInputGuardrail}} / {{CamelRouteOutputGuardrail}} in 
{{camel-langchain4j-core}} run the routes of given tags from any LangChain4j AI 
service; a {{guardrailTags}} option on {{langchain4j-agent}} attaches them to 
an agent.
* The body is the text to check; type, name, memory id, user message and 
invocation parameters are exchange properties.
* Outcome header {{CamelAiGuardrailOutcome}}: {{success}}, {{rewrite}} (body = 
new text), {{failure}} (message in {{CamelAiGuardrailMessage}}, the next 
guardrails still run), {{fatal}} (stops at once). A missing outcome is fatal 
unless {{implicitSuccess=true}}.
* Fails closed: a missing, blank or unknown outcome, a route exception (also 
one an error handler handled), rollback, timeout, rejection, a route that is 
not running, a tag without a route of the call's type.

How a chain of guardrails runs (the routes of a tag, by {{order}}, then name):
{noformat}
text T
  |
  v
next route (by order, name) --none left--> any failure collected?
  |                                          yes            -> Failure (all 
messages joined)
  v                                          no, rewritten  -> Rewrite (last 
text)
run the route on the current text            no             -> Success
  |
  +- success   -> next route
  +- rewrite   -> current text = new text, next route
  +- failure   -> collect the message, keep the text, next route
  +- fatal     -> stop at once, return Fatal
{noformat}

h3. Delivery
Because of the size, in 3 PRs:
# {{camel-ai-guardrail}} component + the adapters in 
{{camel-langchain4j-core}}; docs with Java and YAML examples and pitfalls.
# {{langchain4j-agent}} option {{guardrailTags}}: attaches only the guardrail 
types a tag has routes for; a tag with no route fails startup; one log line per 
tag.
# {{AiGuardrailEvent}}, JMX counters and a shared {{configuration}} bean, like 
{{ai-tool}}.

h3. Follow-ups
Retry and reprompt outcomes (output only, own Jira), an {{ai-guardrail-source}} 
Kamelet in camel-kamelets, and a Camel Quarkus extension with an 
{{@CamelGuardrails}} bridge for {{@RegisterAiService}}. Tool-level guardrails 
are out of scope.


  was:
h3. Problem
LangChain4j guardrails are Java classes. In Camel they can only be attached by 
class (\{{AgentConfiguration.withInputGuardrailClasses}} / 
\{{withOutputGuardrailClasses}}); there is no way to write a guardrail as a 
route, in the Java DSL or in YAML.

h3. Proposal
A new consumer component 
\{{ai-guardrail:<name>?type=input|output&tags=..&order=..&timeout=..}}: the 
route is the guardrail, selected by tag like \{{ai-tool}}. Adapters 
\{{CamelRouteInputGuardrail}} / \{{CamelRouteOutputGuardrail}} in 
\{{camel-langchain4j-core}} run the routes of given tags from any LangChain4j 
AI service; a \{{guardrailTags}} option on \{{langchain4j-agent}} attaches them 
to an agent.
* The body is the text to check; type, name, memory id, user message and 
invocation parameters are exchange properties.
* Outcome header \{{CamelAiGuardrailOutcome}}: \{{success}}, \{{rewrite}} (body 
= new text), \{{failure}} (message in \{{CamelAiGuardrailMessage}}, the next 
guardrails still run), \{{fatal}} (stops at once). A missing outcome is fatal 
unless \{{implicitSuccess=true}}.
* Fails closed: a missing, blank or unknown outcome, a route exception (also 
one an error handler handled), rollback, timeout, rejection, a route that is 
not running, a tag without a route of the call's type.

How a chain of guardrails runs (the routes of a tag, by \{{order}}, then name):
{noformat}
text T
  |
  v
next route (by order, name) --none left--> any failure collected?
  |                                          yes            -> Failure (all 
messages joined)
  v                                          no, rewritten  -> Rewrite (last 
text)
run the route on the current text            no             -> Success
  |
  +- success   -> next route
  +- rewrite   -> current text = new text, next route
  +- failure   -> collect the message, keep the text, next route
  +- fatal     -> stop at once, return Fatal
{noformat}

h3. Delivery
Because of the size, in 3 PRs:
# \{{camel-ai-guardrail}} component + the adapters in 
\{{camel-langchain4j-core}}; docs with Java and YAML examples and pitfalls.
# \{{langchain4j-agent}} option \{{guardrailTags}}: attaches only the guardrail 
types a tag has routes for; a tag with no route fails startup; one log line per 
tag.
# \{{AiGuardrailEvent}}, JMX counters and a shared \{{configuration}} bean, 
like \{{ai-tool}}.

h3. Follow-ups
Retry and reprompt outcomes (output only, own Jira), an 
\{{ai-guardrail-source}} Kamelet in camel-kamelets, and a Camel Quarkus 
extension with an \{{@CamelGuardrails}} bridge for \{{@RegisterAiService}}. 
Tool-level guardrails are out of scope.


> camel-ai-guardrail: use Camel routes as LangChain4j guardrails
> --------------------------------------------------------------
>
>                 Key: CAMEL-27912
>                 URL: https://issues.apache.org/jira/browse/CAMEL-27912
>             Project: Camel
>          Issue Type: New Feature
>          Components: camel-ai
>            Reporter: Jiří Ondrušek
>            Assignee: Jiří Ondrušek
>            Priority: Major
>
> h3. Problem
> LangChain4j guardrails are Java classes. In Camel they can only be attached 
> by class ({{AgentConfiguration.withInputGuardrailClasses}} / 
> {{withOutputGuardrailClasses}}); there is no way to write a guardrail as a 
> route, in the Java DSL or in YAML.
> h3. Proposal
> A new consumer component 
> {{ai-guardrail:<name>?type=input|output&tags=..&order=..&timeout=..}}: the 
> route is the guardrail, selected by tag like {{ai-tool}}. Adapters 
> {{CamelRouteInputGuardrail}} / {{CamelRouteOutputGuardrail}} in 
> {{camel-langchain4j-core}} run the routes of given tags from any LangChain4j 
> AI service; a {{guardrailTags}} option on {{langchain4j-agent}} attaches them 
> to an agent.
> * The body is the text to check; type, name, memory id, user message and 
> invocation parameters are exchange properties.
> * Outcome header {{CamelAiGuardrailOutcome}}: {{success}}, {{rewrite}} (body 
> = new text), {{failure}} (message in {{CamelAiGuardrailMessage}}, the next 
> guardrails still run), {{fatal}} (stops at once). A missing outcome is fatal 
> unless {{implicitSuccess=true}}.
> * Fails closed: a missing, blank or unknown outcome, a route exception (also 
> one an error handler handled), rollback, timeout, rejection, a route that is 
> not running, a tag without a route of the call's type.
> How a chain of guardrails runs (the routes of a tag, by {{order}}, then name):
> {noformat}
> text T
>   |
>   v
> next route (by order, name) --none left--> any failure collected?
>   |                                          yes            -> Failure (all 
> messages joined)
>   v                                          no, rewritten  -> Rewrite (last 
> text)
> run the route on the current text            no             -> Success
>   |
>   +- success   -> next route
>   +- rewrite   -> current text = new text, next route
>   +- failure   -> collect the message, keep the text, next route
>   +- fatal     -> stop at once, return Fatal
> {noformat}
> h3. Delivery
> Because of the size, in 3 PRs:
> # {{camel-ai-guardrail}} component + the adapters in 
> {{camel-langchain4j-core}}; docs with Java and YAML examples and pitfalls.
> # {{langchain4j-agent}} option {{guardrailTags}}: attaches only the guardrail 
> types a tag has routes for; a tag with no route fails startup; one log line 
> per tag.
> # {{AiGuardrailEvent}}, JMX counters and a shared {{configuration}} bean, 
> like {{ai-tool}}.
> h3. Follow-ups
> Retry and reprompt outcomes (output only, own Jira), an 
> {{ai-guardrail-source}} Kamelet in camel-kamelets, and a Camel Quarkus 
> extension with an {{@CamelGuardrails}} bridge for {{@RegisterAiService}}. 
> Tool-level guardrails are out of scope.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to