This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27177-bedrock-converse-text-body in repository https://gitbox.apache.org/repos/asf/camel.git
commit 56eb5431f25505d9e2b2de47ed5bb600de880d27 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Nov 19 18:13:38 2026 +0100 CAMEL-27177: camel-aws-bedrock - converse, converseStream and applyGuardrail take a text body converse and converseStream read the conversation only from the CamelAwsBedrockConverseMessages header (a List of SDK Message objects), so a route without Java code could not call them, nor use the guardrail options. When the header is not set, the body as text is now sent as one user message. applyGuardrail likewise checks the body as text when the CamelAwsBedrockGuardrailContent header is not set. The headers keep precedence, so existing routes are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../camel/catalog/components/aws-bedrock.json | 6 +- .../camel/catalog/docs/aws-bedrock-component.adoc | 21 ++- .../aws2/bedrock/runtime/aws-bedrock.json | 6 +- .../src/main/docs/aws-bedrock-component.adoc | 21 ++- .../aws2/bedrock/runtime/BedrockConstants.java | 5 +- .../aws2/bedrock/runtime/BedrockEndpoint.java | 13 +- .../aws2/bedrock/runtime/BedrockProducer.java | 57 +++++--- .../bedrock/runtime/BedrockApplyGuardrailTest.java | 34 +++++ .../runtime/BedrockConverseTextBodyTest.java | 162 +++++++++++++++++++++ .../dsl/BedrockEndpointBuilderFactory.java | 6 +- 10 files changed, 290 insertions(+), 41 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws-bedrock.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws-bedrock.json index f455c8e72be5..e13ca2788429 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws-bedrock.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/aws-bedrock.json @@ -62,7 +62,7 @@ "CamelAwsBedrockCompletionReason": { "index": 4, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The completion reason for streaming response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#STREAMING_COMPLETION_REASON" }, "CamelAwsBedrockTokenCount": { "index": 5, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "Integer", "deprecated": false, "autowired": false, "secret": false, "description": "The number of tokens generated in streaming response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#STREAMING_TOKEN_COUNT" }, "CamelAwsBedrockChunkCount": { "index": 6, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "Integer", "deprecated": false, "autowired": false, "secret": false, "description": "The number of chunks received in streaming response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#STREAMING_CHUNK_COUNT" }, - "CamelAwsBedrockConverseMessages": { "index": 7, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.Message>", "deprecated": false, "autowired": false, "secret": false, "description": "The conversation messages for Converse API", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_MESSAGES" }, + "CamelAwsBedrockConverseMessages": { "index": 7, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.Message>", "deprecated": false, "autowired": false, "secret": false, "description": "The conversation messages for Converse API. When not set, the body as text is sent as one user message.", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVE [...] "CamelAwsBedrockConverseSystem": { "index": 8, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.SystemContentBlock>", "deprecated": false, "autowired": false, "secret": false, "description": "The system prompts for Converse API", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_SYSTEM" }, "CamelAwsBedrockConverseInferenceConfig": { "index": 9, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.InferenceConfiguration", "deprecated": false, "autowired": false, "secret": false, "description": "The inference configuration for Converse API", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_INFERENCE_CONFIG" }, "CamelAwsBedrockConverseToolConfig": { "index": 10, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.ToolConfiguration", "deprecated": false, "autowired": false, "secret": false, "description": "The tool configuration for Converse API", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_TOOL_CONFIG" }, @@ -72,7 +72,7 @@ "CamelAwsBedrockConverseOutputMessage": { "index": 14, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.Message", "deprecated": false, "autowired": false, "secret": false, "description": "The output message from Converse API response (converse only)", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_OUTPUT_MESSAGE" }, "CamelAwsBedrockGuardrailConfig": { "index": 15, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.GuardrailConfiguration", "deprecated": false, "autowired": false, "secret": false, "description": "The guardrail configuration to apply to the request. converseStream also takes a GuardrailStreamConfiguration", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.Bedrock [...] "CamelAwsBedrockGuardrailIdentifier": { "index": 16, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The guardrail identifier to use for the ApplyGuardrail operation", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_IDENTIFIER" }, - "CamelAwsBedrockGuardrailContent": { "index": 17, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.GuardrailContentBlock>", "deprecated": false, "autowired": false, "secret": false, "description": "The content blocks for ApplyGuardrail operation", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_CONTENT" }, + "CamelAwsBedrockGuardrailContent": { "index": 17, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.GuardrailContentBlock>", "deprecated": false, "autowired": false, "secret": false, "description": "The content blocks for ApplyGuardrail operation. When not set, the body as text is checked.", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GU [...] "CamelAwsBedrockGuardrailSource": { "index": 18, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The source type for ApplyGuardrail operation (INPUT or OUTPUT)", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_SOURCE" }, "CamelAwsBedrockGuardrailOutput": { "index": 19, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.GuardrailOutputContent>", "deprecated": false, "autowired": false, "secret": false, "description": "The output content of the ApplyGuardrail response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_OUTPUT" }, "CamelAwsBedrockGuardrailTrace": { "index": 20, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.GuardrailTraceAssessment", "deprecated": false, "autowired": false, "secret": false, "description": "The trace information from guardrail evaluation", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_TRACE" }, @@ -80,7 +80,7 @@ "CamelAwsBedrockGuardrailUsage": { "index": 22, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.GuardrailUsage", "deprecated": false, "autowired": false, "secret": false, "description": "The guardrail usage metrics from ApplyGuardrail response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_USAGE" } }, "bodyTypes": { - "producerIn": { "carriers": [ "text", "bytes", "stream", "file", "map" ], "byOption": [ { "option": "operation", "value": "converse", "carriers": [ "any" ] }, { "option": "operation", "value": "converseStream", "carriers": [ "any" ] }, { "option": "operation", "value": "applyGuardrail", "carriers": [ "any" ] } ], "description": "The request of the model as JSON text for the invoke operations, which also need the CamelAwsBedrockContentType and CamelAwsBedrockAcceptContentType headers. [...] + "producerIn": { "carriers": [ "text", "bytes", "stream", "file", "map" ], "byOption": [ { "option": "operation", "value": "converse", "carriers": [ "text" ] }, { "option": "operation", "value": "converseStream", "carriers": [ "text" ] }, { "option": "operation", "value": "applyGuardrail", "carriers": [ "text" ] } ], "description": "The request of the model as JSON text for the invoke operations, which also need the CamelAwsBedrockContentType and CamelAwsBedrockAcceptContentType heade [...] "producerOut": { "carriers": [ "text", "list" ], "byOption": [ { "option": "operation", "value": "invokeTextModel", "carriers": [ "text", "pojo" ] }, { "option": "operation", "value": "invokeImageModel", "carriers": [ "pojo" ] }, { "option": "operation", "value": "invokeEmbeddingsModel", "carriers": [ "pojo" ], "javaType": "software.amazon.awssdk.services.bedrockruntime.model.InvokeModelResponse" }, { "option": "operation", "value": "converse", "carriers": [ "text" ], "javaType": "ja [...] }, "properties": { diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws-bedrock-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws-bedrock-component.adoc index dfc5e17b4020..d83ada2e0a22 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws-bedrock-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/aws-bedrock-component.adoc @@ -178,7 +178,9 @@ aws-bedrock://label?operation=converseStream&modelId=anthropic.claude-3-sonnet-2 Converse API uses message headers for configuration: -- `CamelAwsBedrockConverseMessages` (required): List of Message objects representing the conversation +- `CamelAwsBedrockConverseMessages`: List of Message objects representing the conversation. When the header is not + set, the message body as text is sent as one user message, so a route in YAML or XML (or a Kamelet) can call + `converse` with a text prompt. Without the header and without a body, the exchange fails. - `CamelAwsBedrockConverseSystem`: List of SystemContentBlock for system-level instructions - `CamelAwsBedrockConverseInferenceConfig`: InferenceConfiguration for temperature, maxTokens, etc. - `CamelAwsBedrockConverseToolConfig`: ToolConfiguration for function calling support @@ -187,6 +189,20 @@ Converse API uses message headers for configuration: For streaming operations, you can also use: - `CamelAwsBedrockStreamOutputMode`: Set to "complete" (default) or "chunks" +For example, the same prompt for every model, with a guardrail, from a text body: + +[source,yaml] +---- +- route: + from: + uri: timer:t?repeatCount=1 + steps: + - setBody: + constant: "What is Apache Camel?" + - to: aws-bedrock:label?operation=converse&modelId=anthropic.claude-3-haiku-20240307-v1:0®ion=us-east-1&guardrailIdentifier=abc123&guardrailVersion=1 + - log: "${body}" +---- + ==== Converse Response Headers When a conversation completes, the following headers are set: @@ -254,7 +270,8 @@ aws-bedrock://label?operation=converseStream&modelId=anthropic.claude-3-sonnet-2 &guardrailIdentifier=abc123xyz&guardrailVersion=DRAFT ---- -*applyGuardrail*: Standalone operation to validate content against a guardrail without invoking a model +*applyGuardrail*: Standalone operation to validate content against a guardrail without invoking a model. The content +is the `CamelAwsBedrockGuardrailContent` header, or else the message body as text. [source] ---- diff --git a/components/camel-aws/camel-aws-bedrock/src/generated/resources/META-INF/org/apache/camel/component/aws2/bedrock/runtime/aws-bedrock.json b/components/camel-aws/camel-aws-bedrock/src/generated/resources/META-INF/org/apache/camel/component/aws2/bedrock/runtime/aws-bedrock.json index f455c8e72be5..e13ca2788429 100644 --- a/components/camel-aws/camel-aws-bedrock/src/generated/resources/META-INF/org/apache/camel/component/aws2/bedrock/runtime/aws-bedrock.json +++ b/components/camel-aws/camel-aws-bedrock/src/generated/resources/META-INF/org/apache/camel/component/aws2/bedrock/runtime/aws-bedrock.json @@ -62,7 +62,7 @@ "CamelAwsBedrockCompletionReason": { "index": 4, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The completion reason for streaming response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#STREAMING_COMPLETION_REASON" }, "CamelAwsBedrockTokenCount": { "index": 5, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "Integer", "deprecated": false, "autowired": false, "secret": false, "description": "The number of tokens generated in streaming response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#STREAMING_TOKEN_COUNT" }, "CamelAwsBedrockChunkCount": { "index": 6, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "Integer", "deprecated": false, "autowired": false, "secret": false, "description": "The number of chunks received in streaming response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#STREAMING_CHUNK_COUNT" }, - "CamelAwsBedrockConverseMessages": { "index": 7, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.Message>", "deprecated": false, "autowired": false, "secret": false, "description": "The conversation messages for Converse API", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_MESSAGES" }, + "CamelAwsBedrockConverseMessages": { "index": 7, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.Message>", "deprecated": false, "autowired": false, "secret": false, "description": "The conversation messages for Converse API. When not set, the body as text is sent as one user message.", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVE [...] "CamelAwsBedrockConverseSystem": { "index": 8, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.SystemContentBlock>", "deprecated": false, "autowired": false, "secret": false, "description": "The system prompts for Converse API", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_SYSTEM" }, "CamelAwsBedrockConverseInferenceConfig": { "index": 9, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.InferenceConfiguration", "deprecated": false, "autowired": false, "secret": false, "description": "The inference configuration for Converse API", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_INFERENCE_CONFIG" }, "CamelAwsBedrockConverseToolConfig": { "index": 10, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.ToolConfiguration", "deprecated": false, "autowired": false, "secret": false, "description": "The tool configuration for Converse API", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_TOOL_CONFIG" }, @@ -72,7 +72,7 @@ "CamelAwsBedrockConverseOutputMessage": { "index": 14, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.Message", "deprecated": false, "autowired": false, "secret": false, "description": "The output message from Converse API response (converse only)", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#CONVERSE_OUTPUT_MESSAGE" }, "CamelAwsBedrockGuardrailConfig": { "index": 15, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.GuardrailConfiguration", "deprecated": false, "autowired": false, "secret": false, "description": "The guardrail configuration to apply to the request. converseStream also takes a GuardrailStreamConfiguration", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.Bedrock [...] "CamelAwsBedrockGuardrailIdentifier": { "index": 16, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The guardrail identifier to use for the ApplyGuardrail operation", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_IDENTIFIER" }, - "CamelAwsBedrockGuardrailContent": { "index": 17, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.GuardrailContentBlock>", "deprecated": false, "autowired": false, "secret": false, "description": "The content blocks for ApplyGuardrail operation", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_CONTENT" }, + "CamelAwsBedrockGuardrailContent": { "index": 17, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.GuardrailContentBlock>", "deprecated": false, "autowired": false, "secret": false, "description": "The content blocks for ApplyGuardrail operation. When not set, the body as text is checked.", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GU [...] "CamelAwsBedrockGuardrailSource": { "index": 18, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "String", "deprecated": false, "autowired": false, "secret": false, "description": "The source type for ApplyGuardrail operation (INPUT or OUTPUT)", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_SOURCE" }, "CamelAwsBedrockGuardrailOutput": { "index": 19, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "List<software.amazon.awssdk.services.bedrockruntime.model.GuardrailOutputContent>", "deprecated": false, "autowired": false, "secret": false, "description": "The output content of the ApplyGuardrail response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_OUTPUT" }, "CamelAwsBedrockGuardrailTrace": { "index": 20, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.GuardrailTraceAssessment", "deprecated": false, "autowired": false, "secret": false, "description": "The trace information from guardrail evaluation", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_TRACE" }, @@ -80,7 +80,7 @@ "CamelAwsBedrockGuardrailUsage": { "index": 22, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "software.amazon.awssdk.services.bedrockruntime.model.GuardrailUsage", "deprecated": false, "autowired": false, "secret": false, "description": "The guardrail usage metrics from ApplyGuardrail response", "constantName": "org.apache.camel.component.aws2.bedrock.runtime.BedrockConstants#GUARDRAIL_USAGE" } }, "bodyTypes": { - "producerIn": { "carriers": [ "text", "bytes", "stream", "file", "map" ], "byOption": [ { "option": "operation", "value": "converse", "carriers": [ "any" ] }, { "option": "operation", "value": "converseStream", "carriers": [ "any" ] }, { "option": "operation", "value": "applyGuardrail", "carriers": [ "any" ] } ], "description": "The request of the model as JSON text for the invoke operations, which also need the CamelAwsBedrockContentType and CamelAwsBedrockAcceptContentType headers. [...] + "producerIn": { "carriers": [ "text", "bytes", "stream", "file", "map" ], "byOption": [ { "option": "operation", "value": "converse", "carriers": [ "text" ] }, { "option": "operation", "value": "converseStream", "carriers": [ "text" ] }, { "option": "operation", "value": "applyGuardrail", "carriers": [ "text" ] } ], "description": "The request of the model as JSON text for the invoke operations, which also need the CamelAwsBedrockContentType and CamelAwsBedrockAcceptContentType heade [...] "producerOut": { "carriers": [ "text", "list" ], "byOption": [ { "option": "operation", "value": "invokeTextModel", "carriers": [ "text", "pojo" ] }, { "option": "operation", "value": "invokeImageModel", "carriers": [ "pojo" ] }, { "option": "operation", "value": "invokeEmbeddingsModel", "carriers": [ "pojo" ], "javaType": "software.amazon.awssdk.services.bedrockruntime.model.InvokeModelResponse" }, { "option": "operation", "value": "converse", "carriers": [ "text" ], "javaType": "ja [...] }, "properties": { diff --git a/components/camel-aws/camel-aws-bedrock/src/main/docs/aws-bedrock-component.adoc b/components/camel-aws/camel-aws-bedrock/src/main/docs/aws-bedrock-component.adoc index dfc5e17b4020..d83ada2e0a22 100644 --- a/components/camel-aws/camel-aws-bedrock/src/main/docs/aws-bedrock-component.adoc +++ b/components/camel-aws/camel-aws-bedrock/src/main/docs/aws-bedrock-component.adoc @@ -178,7 +178,9 @@ aws-bedrock://label?operation=converseStream&modelId=anthropic.claude-3-sonnet-2 Converse API uses message headers for configuration: -- `CamelAwsBedrockConverseMessages` (required): List of Message objects representing the conversation +- `CamelAwsBedrockConverseMessages`: List of Message objects representing the conversation. When the header is not + set, the message body as text is sent as one user message, so a route in YAML or XML (or a Kamelet) can call + `converse` with a text prompt. Without the header and without a body, the exchange fails. - `CamelAwsBedrockConverseSystem`: List of SystemContentBlock for system-level instructions - `CamelAwsBedrockConverseInferenceConfig`: InferenceConfiguration for temperature, maxTokens, etc. - `CamelAwsBedrockConverseToolConfig`: ToolConfiguration for function calling support @@ -187,6 +189,20 @@ Converse API uses message headers for configuration: For streaming operations, you can also use: - `CamelAwsBedrockStreamOutputMode`: Set to "complete" (default) or "chunks" +For example, the same prompt for every model, with a guardrail, from a text body: + +[source,yaml] +---- +- route: + from: + uri: timer:t?repeatCount=1 + steps: + - setBody: + constant: "What is Apache Camel?" + - to: aws-bedrock:label?operation=converse&modelId=anthropic.claude-3-haiku-20240307-v1:0®ion=us-east-1&guardrailIdentifier=abc123&guardrailVersion=1 + - log: "${body}" +---- + ==== Converse Response Headers When a conversation completes, the following headers are set: @@ -254,7 +270,8 @@ aws-bedrock://label?operation=converseStream&modelId=anthropic.claude-3-sonnet-2 &guardrailIdentifier=abc123xyz&guardrailVersion=DRAFT ---- -*applyGuardrail*: Standalone operation to validate content against a guardrail without invoking a model +*applyGuardrail*: Standalone operation to validate content against a guardrail without invoking a model. The content +is the `CamelAwsBedrockGuardrailContent` header, or else the message body as text. [source] ---- diff --git a/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockConstants.java b/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockConstants.java index 05ff69c06942..a2eaa5be110f 100644 --- a/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockConstants.java +++ b/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockConstants.java @@ -36,7 +36,8 @@ public interface BedrockConstants { String STREAMING_TOKEN_COUNT = "CamelAwsBedrockTokenCount"; @Metadata(description = "The number of chunks received in streaming response", javaType = "Integer") String STREAMING_CHUNK_COUNT = "CamelAwsBedrockChunkCount"; - @Metadata(description = "The conversation messages for Converse API", + @Metadata(description = "The conversation messages for Converse API. When not set, the body as text is sent as one" + + " user message.", javaType = "List<software.amazon.awssdk.services.bedrockruntime.model.Message>") String CONVERSE_MESSAGES = "CamelAwsBedrockConverseMessages"; @Metadata(description = "The system prompts for Converse API", @@ -64,7 +65,7 @@ public interface BedrockConstants { String GUARDRAIL_CONFIG = "CamelAwsBedrockGuardrailConfig"; @Metadata(description = "The guardrail identifier to use for the ApplyGuardrail operation", javaType = "String") String GUARDRAIL_IDENTIFIER = "CamelAwsBedrockGuardrailIdentifier"; - @Metadata(description = "The content blocks for ApplyGuardrail operation", + @Metadata(description = "The content blocks for ApplyGuardrail operation. When not set, the body as text is checked.", javaType = "List<software.amazon.awssdk.services.bedrockruntime.model.GuardrailContentBlock>") String GUARDRAIL_CONTENT = "CamelAwsBedrockGuardrailContent"; @Metadata(description = "The source type for ApplyGuardrail operation (INPUT or OUTPUT)", javaType = "String") diff --git a/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockEndpoint.java b/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockEndpoint.java index 8007cb65dbc9..ab0e3739844d 100644 --- a/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockEndpoint.java +++ b/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockEndpoint.java @@ -44,18 +44,19 @@ import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; headersClass = BedrockConstants.class) @BodyTypes(producerIn = @BodyType(value = { Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE, Carrier.MAP }, byOption = { - @BodyType.When(option = "operation", value = "converse", carriers = Carrier.ANY), + @BodyType.When(option = "operation", value = "converse", carriers = Carrier.TEXT), @BodyType.When(option = "operation", value = "converseStream", - carriers = Carrier.ANY), + carriers = Carrier.TEXT), @BodyType.When(option = "operation", value = "applyGuardrail", - carriers = Carrier.ANY) }, + carriers = Carrier.TEXT) }, description = "The request of the model as JSON text for the invoke operations, which also" + " need the CamelAwsBedrockContentType and CamelAwsBedrockAcceptContentType" + " headers. Bytes, a stream or a file are sent as they are, and a Map or a" + " Jackson JsonNode is marshalled to JSON: marshal another POJO to JSON first." - + " No body fails. converse," - + " converseStream and applyGuardrail take their input from headers, such as" - + " CamelAwsBedrockConverseMessages. With pojoRequest=true an" + + " No body fails. converse and converseStream take the messages of the" + + " CamelAwsBedrockConverseMessages header, or else the body as text as one user" + + " message; applyGuardrail takes the CamelAwsBedrockGuardrailContent header, or" + + " else the body as text. With pojoRequest=true an" + " InvokeModelRequest of the AWS SDK: another body fails"), producerOut = @BodyType(value = { Carrier.TEXT, Carrier.LIST }, byOption = { diff --git a/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockProducer.java b/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockProducer.java index 90be3b5aaa45..6d93c4bb342d 100644 --- a/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockProducer.java +++ b/components/camel-aws/camel-aws-bedrock/src/main/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockProducer.java @@ -39,6 +39,7 @@ import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; import software.amazon.awssdk.services.bedrockruntime.model.ApplyGuardrailRequest; import software.amazon.awssdk.services.bedrockruntime.model.ApplyGuardrailResponse; import software.amazon.awssdk.services.bedrockruntime.model.ContentBlock; +import software.amazon.awssdk.services.bedrockruntime.model.ConversationRole; import software.amazon.awssdk.services.bedrockruntime.model.ConverseRequest; import software.amazon.awssdk.services.bedrockruntime.model.ConverseResponse; import software.amazon.awssdk.services.bedrockruntime.model.ConverseStreamRequest; @@ -47,6 +48,7 @@ import software.amazon.awssdk.services.bedrockruntime.model.GuardrailContentBloc import software.amazon.awssdk.services.bedrockruntime.model.GuardrailContentSource; import software.amazon.awssdk.services.bedrockruntime.model.GuardrailStreamConfiguration; import software.amazon.awssdk.services.bedrockruntime.model.GuardrailStreamProcessingMode; +import software.amazon.awssdk.services.bedrockruntime.model.GuardrailTextBlock; import software.amazon.awssdk.services.bedrockruntime.model.GuardrailTrace; import software.amazon.awssdk.services.bedrockruntime.model.InferenceConfiguration; import software.amazon.awssdk.services.bedrockruntime.model.InvokeModelRequest; @@ -742,15 +744,7 @@ public class BedrockProducer extends DefaultProducer { builder.modelId(getConfiguration().getModelId()); // Get messages from header or body - @SuppressWarnings("unchecked") - List<software.amazon.awssdk.services.bedrockruntime.model.Message> messages - = exchange.getMessage().getHeader(BedrockConstants.CONVERSE_MESSAGES, List.class); - if (ObjectHelper.isNotEmpty(messages)) { - builder.messages(messages); - } else { - throw new IllegalArgumentException( - "Converse operation requires messages in header " + BedrockConstants.CONVERSE_MESSAGES); - } + builder.messages(determineConverseMessages(exchange, "Converse")); // Optional: System prompts @SuppressWarnings("unchecked") @@ -839,6 +833,30 @@ public class BedrockProducer extends DefaultProducer { } } + /** + * The messages of converse and converseStream: the CamelAwsBedrockConverseMessages header, or else the body as text + * as one user message. + */ + private static List<software.amazon.awssdk.services.bedrockruntime.model.Message> determineConverseMessages( + Exchange exchange, String operation) { + @SuppressWarnings("unchecked") + List<software.amazon.awssdk.services.bedrockruntime.model.Message> messages + = exchange.getMessage().getHeader(BedrockConstants.CONVERSE_MESSAGES, List.class); + if (ObjectHelper.isNotEmpty(messages)) { + return messages; + } + String text = exchange.getMessage().getBody(String.class); + if (ObjectHelper.isEmpty(text)) { + throw new IllegalArgumentException( + operation + " operation requires messages in header " + BedrockConstants.CONVERSE_MESSAGES + + " or a text body"); + } + return List.of(software.amazon.awssdk.services.bedrockruntime.model.Message.builder() + .role(ConversationRole.USER) + .content(ContentBlock.fromText(text)) + .build()); + } + private void converseStream(Exchange exchange) throws InvalidPayloadException { ConverseStreamRequest request; @@ -858,15 +876,7 @@ public class BedrockProducer extends DefaultProducer { builder.modelId(getConfiguration().getModelId()); // Get messages from header or body - @SuppressWarnings("unchecked") - List<software.amazon.awssdk.services.bedrockruntime.model.Message> messages - = exchange.getMessage().getHeader(BedrockConstants.CONVERSE_MESSAGES, List.class); - if (ObjectHelper.isNotEmpty(messages)) { - builder.messages(messages); - } else { - throw new IllegalArgumentException( - "ConverseStream operation requires messages in header " + BedrockConstants.CONVERSE_MESSAGES); - } + builder.messages(determineConverseMessages(exchange, "ConverseStream")); // Optional: System prompts @SuppressWarnings("unchecked") @@ -1038,15 +1048,20 @@ public class BedrockProducer extends DefaultProducer { } builder.source(GuardrailContentSource.fromValue(source)); - // Content blocks from header + // Content blocks from header, or the text of the body @SuppressWarnings("unchecked") List<GuardrailContentBlock> content = exchange.getMessage().getHeader(BedrockConstants.GUARDRAIL_CONTENT, List.class); if (ObjectHelper.isNotEmpty(content)) { builder.content(content); } else { - throw new IllegalArgumentException( - "ApplyGuardrail operation requires content in header " + BedrockConstants.GUARDRAIL_CONTENT); + String text = exchange.getMessage().getBody(String.class); + if (ObjectHelper.isEmpty(text)) { + throw new IllegalArgumentException( + "ApplyGuardrail operation requires content in header " + BedrockConstants.GUARDRAIL_CONTENT + + " or a text body"); + } + builder.content(GuardrailContentBlock.fromText(GuardrailTextBlock.builder().text(text).build())); } request = builder.build(); diff --git a/components/camel-aws/camel-aws-bedrock/src/test/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockApplyGuardrailTest.java b/components/camel-aws/camel-aws-bedrock/src/test/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockApplyGuardrailTest.java index c0e0a05418cc..2c5dcc476e3d 100644 --- a/components/camel-aws/camel-aws-bedrock/src/test/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockApplyGuardrailTest.java +++ b/components/camel-aws/camel-aws-bedrock/src/test/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockApplyGuardrailTest.java @@ -43,6 +43,7 @@ import software.amazon.awssdk.services.bedrockruntime.model.GuardrailTextBlock; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.lenient; @@ -162,6 +163,39 @@ public class BedrockApplyGuardrailTest { "Error message should mention guardrailIdentifier — was: " + cause.getMessage()); } + @Test + public void applyGuardrailChecksTheTextBodyWhenNoContentHeader() { + Exchange exchange = template.send("direct:apply-guardrail-config", + ex -> ex.getMessage().setBody("Is this allowed?")); + + assertNull(exchange.getException()); + ApplyGuardrailRequest request = capturedRequest.get(); + assertNotNull(request); + assertEquals(1, request.content().size()); + assertEquals("Is this allowed?", request.content().get(0).text().text()); + } + + @Test + public void applyGuardrailContentHeaderWinsOverTheBody() { + template.send("direct:apply-guardrail-config", ex -> { + ex.getMessage().setHeader(BedrockConstants.GUARDRAIL_CONTENT, sampleContent()); + ex.getMessage().setBody("not used"); + }); + + ApplyGuardrailRequest request = capturedRequest.get(); + assertNotNull(request); + assertEquals("test", request.content().get(0).text().text()); + } + + @Test + public void applyGuardrailFailsWithoutContentHeaderOrBody() { + Exchange exchange = template.send("direct:apply-guardrail-config", ex -> ex.getMessage().setBody(null)); + + assertInstanceOf(IllegalArgumentException.class, exchange.getException()); + assertTrue(exchange.getException().getMessage().contains("or a text body"), + exchange.getException().getMessage()); + } + private static List<GuardrailContentBlock> sampleContent() { List<GuardrailContentBlock> content = new ArrayList<>(); content.add(GuardrailContentBlock.builder() diff --git a/components/camel-aws/camel-aws-bedrock/src/test/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockConverseTextBodyTest.java b/components/camel-aws/camel-aws-bedrock/src/test/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockConverseTextBodyTest.java new file mode 100644 index 000000000000..153a36fccf28 --- /dev/null +++ b/components/camel-aws/camel-aws-bedrock/src/test/java/org/apache/camel/component/aws2/bedrock/runtime/BedrockConverseTextBodyTest.java @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.aws2.bedrock.runtime; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.SimpleRegistry; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient; +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; +import software.amazon.awssdk.services.bedrockruntime.model.ContentBlock; +import software.amazon.awssdk.services.bedrockruntime.model.ConversationRole; +import software.amazon.awssdk.services.bedrockruntime.model.ConverseOutput; +import software.amazon.awssdk.services.bedrockruntime.model.ConverseRequest; +import software.amazon.awssdk.services.bedrockruntime.model.ConverseResponse; +import software.amazon.awssdk.services.bedrockruntime.model.ConverseStreamRequest; +import software.amazon.awssdk.services.bedrockruntime.model.ConverseStreamResponseHandler; +import software.amazon.awssdk.services.bedrockruntime.model.Message; +import software.amazon.awssdk.services.bedrockruntime.model.StopReason; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +/** + * converse and converseStream send a text body as one user message when the CamelAwsBedrockConverseMessages header is + * not set, so that a route without Java code (YAML or XML DSL, a Kamelet) can call them. + */ +@ExtendWith(MockitoExtension.class) +public class BedrockConverseTextBodyTest { + + private static final String MODEL = "anthropic.claude-3-haiku-20240307-v1:0"; + private static final String PROMPT = "What is Apache Camel?"; + + @Mock + private BedrockRuntimeClient client; + + @Mock + private BedrockRuntimeAsyncClient asyncClient; + + private CamelContext camelContext; + private ProducerTemplate template; + + @BeforeEach + public void setup() { + SimpleRegistry registry = new SimpleRegistry(); + registry.bind("bedrockClient", client); + registry.bind("bedrockAsyncClient", asyncClient); + camelContext = new DefaultCamelContext(registry); + camelContext.start(); + template = camelContext.createProducerTemplate(); + } + + @AfterEach + public void teardown() { + if (template != null) { + template.stop(); + } + if (camelContext != null) { + camelContext.stop(); + } + } + + private static String converseUri(String operation, String options) { + return "aws-bedrock://label?bedrockRuntimeClient=#bedrockClient&bedrockRuntimeAsyncClient=#bedrockAsyncClient" + + "&operation=" + operation + "&modelId=" + MODEL + "®ion=us-east-1&accessKey=unused&secretKey=unused" + + options; + } + + private ArgumentCaptor<ConverseRequest> answer(String text) { + ArgumentCaptor<ConverseRequest> request = ArgumentCaptor.forClass(ConverseRequest.class); + when(client.converse(request.capture())).thenReturn(ConverseResponse.builder() + .output(ConverseOutput.builder().message(Message.builder().role(ConversationRole.ASSISTANT) + .content(ContentBlock.fromText(text)).build()).build()) + .stopReason(StopReason.END_TURN) + .build()); + return request; + } + + @Test + public void converseSendsTheTextBodyAsOneUserMessage() { + ArgumentCaptor<ConverseRequest> request = answer("An integration framework"); + + Exchange exchange = template.send(converseUri("converse", "&guardrailIdentifier=abc123&guardrailVersion=1"), + ex -> ex.getMessage().setBody(PROMPT)); + + assertNull(exchange.getException()); + List<Message> messages = request.getValue().messages(); + assertEquals(1, messages.size()); + assertEquals(ConversationRole.USER, messages.get(0).role()); + assertEquals(PROMPT, messages.get(0).content().get(0).text()); + // the guardrail options of the endpoint apply + assertEquals("abc123", request.getValue().guardrailConfig().guardrailIdentifier()); + assertEquals("1", request.getValue().guardrailConfig().guardrailVersion()); + assertEquals("An integration framework", exchange.getMessage().getBody()); + } + + @Test + public void theMessagesHeaderWinsOverTheBody() { + ArgumentCaptor<ConverseRequest> request = answer("Hi"); + Message fromHeader = Message.builder().role(ConversationRole.USER).content(ContentBlock.fromText("Hello")).build(); + + template.send(converseUri("converse", ""), ex -> { + ex.getMessage().setHeader(BedrockConstants.CONVERSE_MESSAGES, List.of(fromHeader)); + ex.getMessage().setBody(PROMPT); + }); + + assertEquals(List.of(fromHeader), request.getValue().messages()); + } + + @Test + public void converseFailsWithoutMessagesOrBody() { + Exchange exchange = template.send(converseUri("converse", ""), ex -> ex.getMessage().setBody(null)); + + assertInstanceOf(IllegalArgumentException.class, exchange.getException()); + assertTrue(exchange.getException().getMessage().contains(BedrockConstants.CONVERSE_MESSAGES + " or a text body"), + exchange.getException().getMessage()); + } + + @Test + public void converseStreamSendsTheTextBodyAsOneUserMessage() { + ArgumentCaptor<ConverseStreamRequest> request = ArgumentCaptor.forClass(ConverseStreamRequest.class); + when(asyncClient.converseStream(request.capture(), any(ConverseStreamResponseHandler.class))) + .thenReturn(CompletableFuture.completedFuture(null)); + + Exchange exchange = template.send(converseUri("converseStream", ""), ex -> ex.getMessage().setBody(PROMPT)); + + assertNull(exchange.getException()); + List<Message> messages = request.getValue().messages(); + assertEquals(1, messages.size()); + assertEquals(ConversationRole.USER, messages.get(0).role()); + assertEquals(PROMPT, messages.get(0).content().get(0).text()); + } +} diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/BedrockEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/BedrockEndpointBuilderFactory.java index 4abf3d011866..d1e59a6639b1 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/BedrockEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/BedrockEndpointBuilderFactory.java @@ -841,7 +841,8 @@ public interface BedrockEndpointBuilderFactory { return "CamelAwsBedrockChunkCount"; } /** - * The conversation messages for Converse API. + * The conversation messages for Converse API. When not set, the body as + * text is sent as one user message. * * The option is a: {@code * List<software.amazon.awssdk.services.bedrockruntime.model.Message>} @@ -975,7 +976,8 @@ public interface BedrockEndpointBuilderFactory { return "CamelAwsBedrockGuardrailIdentifier"; } /** - * The content blocks for ApplyGuardrail operation. + * The content blocks for ApplyGuardrail operation. When not set, the + * body as text is checked. * * The option is a: {@code * List<software.amazon.awssdk.services.bedrockruntime.model.GuardrailContentBlock>} type.
