Zineb Bendhiba created CAMEL-23344:
--------------------------------------
Summary: Title: LangChain4j Agent: Support Camel route-based
output/input guardrails via CamelContextAware
Key: CAMEL-23344
URL: https://issues.apache.org/jira/browse/CAMEL-23344
Project: Camel
Issue Type: New Feature
Components: langchain4j-agent
Reporter: Zineb Bendhiba
Assignee: Zineb Bendhiba
Problem
The current AgentConfiguration only supports guardrails by class
(withOutputGuardrailClasses), which requires guardrails to have a no-arg
constructor. Guardrails that need parameters at construction time (like
JsonSchemaFormatGuardrail, which requires a schema string) cannot be configured
this way.
Additionally, loading resources (e.g., a JSON schema file from classpath:)
requires CamelContext, which is not available during agent configuration and
creation.
*Proposed Solution*
Introduce a Camel route-based guardrail pattern using CamelContextAware:
1. Add guardrail instance support to AgentConfiguration: new
withOutputGuardrails(List<OutputGuardrail>) and
withInputGuardrails(List<InputGuardrail>) methods alongside the existing
class-based ones.
2. Create CamelRouteOutputGuardrail: a guardrail that implements both
OutputGuardrail and CamelContextAware. It delegates validation to a Camel route
endpoint, allowing users to leverage any Camel component for validation (e.g.,
json-validator, custom beans).
3. Inject CamelContext in the Producer: LangChain4jAgentProducer iterates over
guardrail instances and injects CamelContext into any that implement
CamelContextAware.
4. Wire instances in AbstractAgent.configureBuilder(): call
builder.outputGuardrails(list) / builder.inputGuardrails(list) when instances
are present (LangChain4j 1.13.0 supports this).
+*Usage Example*+
{code:java}
// Define a validation route using existing Camel components
from("direct:validate-person")
.to("json-validator:classpath:person-schema.json");
// Create a route-based guardrail
CamelRouteOutputGuardrail guardrail
= new CamelRouteOutputGuardrail("direct:validate-person");
// Configure the agent with the guardrail instance
AgentConfiguration config = new AgentConfiguration()
.withChatModel(chatModel)
.withOutputGuardrails(List.of(guardrail));
Agent agent = new AgentWithoutMemory(config);
{code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)