-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(idp extraction connector): implement new idp extraction outbound connector #3482
Open
sahilbhatoacamunda
wants to merge
9
commits into
main
Choose a base branch
from
new-idp-extraction-outbound-connector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9c2f451
feat(idp extraction connector): implement new idp extraction outbound…
sahilbhatoacamunda 7b59ec7
Removed new idp connector reference from pom.xml
sahilbhatoacamunda a93e599
Merge branch 'refs/heads/main' into new-idp-extraction-outbound-conne…
sahilbhatoacamunda 86795c8
Updated pom.xml to include new connector in the bundle.
sahilbhatoacamunda 1fab739
Fixed code formatting
sahilbhatoacamunda 7522cac
Added `io.camunda.connector:connector-aws-base` as dependency in conn…
sahilbhatoacamunda c2c6001
Merge branch 'refs/heads/main' into new-idp-extraction-outbound-conne…
sahilbhatoacamunda 95a9342
Create the new class to extend AWS base request. ExtractorRequest is …
sahilbhatoacamunda 6193711
Modified ExtractionResult to have Object as response instead of strin…
sahilbhatoacamunda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
268 changes: 268 additions & 0 deletions
268
...ors/idp-extraction/element-templates/hybrid/idp-extraction-outbound-connector-hybrid.json
Large diffs are not rendered by default.
Oops, something went wrong.
263 changes: 263 additions & 0 deletions
263
connectors/idp-extraction/element-templates/idp-extraction-outbound-connector.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.camunda.connector</groupId> | ||
<artifactId>connectors-parent</artifactId> | ||
<version>8.7.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>connector-idp-extraction</artifactId> | ||
<name>connector-idp-extraction</name> | ||
<description>Camunda IDP extraction outbound Connector</description> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<maven.compiler.source>21</maven.compiler.source> | ||
<maven.compiler.target>21</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.camunda.connector</groupId> | ||
<artifactId>connector-aws-base</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>software.amazon.awssdk</groupId> | ||
<artifactId>bedrockruntime</artifactId> | ||
<version>${version.software-aws-java-sdk}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>software.amazon.awssdk</groupId> | ||
<artifactId>textract</artifactId> | ||
<version>${version.software-aws-java-sdk}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>software.amazon.awssdk</groupId> | ||
<artifactId>s3</artifactId> | ||
<version>${version.software-aws-java-sdk}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.pdfbox</groupId> | ||
<artifactId>pdfbox</artifactId> | ||
<version>${version.pdfbox}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>io.camunda.connector</groupId> | ||
<artifactId>element-template-generator-maven-plugin</artifactId> | ||
<version>${project.version}</version> | ||
<configuration> | ||
<connectors> | ||
<connector> | ||
<connectorClass>io.camunda.connector.idp.extraction.ExtractionConnectorFunction</connectorClass> | ||
<files> | ||
<file> | ||
<templateId>io.camunda.connectors.idp.extraction.v1</templateId> | ||
<templateFileName>idp-extraction-outbound-connector.json</templateFileName> | ||
</file> | ||
</files> | ||
<generateHybridTemplates>true</generateHybridTemplates> | ||
</connector> | ||
</connectors> | ||
<includeDependencies> | ||
<includeDependency>io.camunda.connector:connector-aws-base</includeDependency> | ||
</includeDependencies> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
117 changes: 117 additions & 0 deletions
117
...action/src/main/java/io/camunda/connector/idp/extraction/ExtractionConnectorFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. Licensed under a proprietary license. | ||
* See the License.txt file for more information. You may not use this file | ||
* except in compliance with the proprietary license. | ||
*/ | ||
package io.camunda.connector.idp.extraction; | ||
|
||
import io.camunda.connector.api.annotation.OutboundConnector; | ||
import io.camunda.connector.api.error.ConnectorException; | ||
import io.camunda.connector.api.outbound.OutboundConnectorContext; | ||
import io.camunda.connector.api.outbound.OutboundConnectorFunction; | ||
import io.camunda.connector.generator.java.annotation.ElementTemplate; | ||
import io.camunda.connector.idp.extraction.caller.BedrockCaller; | ||
import io.camunda.connector.idp.extraction.caller.PollingTextractCaller; | ||
import io.camunda.connector.idp.extraction.model.ExtractionRequest; | ||
import io.camunda.connector.idp.extraction.model.ExtractionResult; | ||
import io.camunda.connector.idp.extraction.supplier.BedrockRuntimeClientSupplier; | ||
import io.camunda.connector.idp.extraction.supplier.S3ClientSupplier; | ||
import io.camunda.connector.idp.extraction.supplier.TextractClientSupplier; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import org.apache.pdfbox.Loader; | ||
import org.apache.pdfbox.io.IOUtils; | ||
import org.apache.pdfbox.pdmodel.PDDocument; | ||
import org.apache.pdfbox.text.PDFTextStripper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@OutboundConnector( | ||
name = "IDP extraction outbound Connector", | ||
inputVariables = {"authentication", "configuration", "input"}, | ||
type = "io.camunda:idp-extraction-connector-template:1") | ||
@ElementTemplate( | ||
id = "io.camunda.connector.IdpExtractionOutBoundTemplate.v1", | ||
name = "IDP extraction outbound Connector", | ||
version = 1, | ||
description = "Execute IDP extraction requests", | ||
icon = "icon.svg", | ||
documentationRef = "https://docs.camunda.io/docs/guides/", | ||
propertyGroups = { | ||
@ElementTemplate.PropertyGroup(id = "authentication", label = "Authentication"), | ||
@ElementTemplate.PropertyGroup(id = "configuration", label = "Configuration"), | ||
@ElementTemplate.PropertyGroup(id = "input", label = "Input message data") | ||
}, | ||
inputDataClass = ExtractionRequest.class) | ||
public class ExtractionConnectorFunction implements OutboundConnectorFunction { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ExtractionConnectorFunction.class); | ||
|
||
private final TextractClientSupplier textractClientSupplier; | ||
|
||
private final S3ClientSupplier s3ClientSupplier; | ||
|
||
private final BedrockRuntimeClientSupplier bedrockRuntimeClientSupplier; | ||
|
||
private final PollingTextractCaller pollingTextractCaller; | ||
|
||
private final BedrockCaller bedrockCaller; | ||
|
||
public ExtractionConnectorFunction() { | ||
this.textractClientSupplier = new TextractClientSupplier(); | ||
this.s3ClientSupplier = new S3ClientSupplier(); | ||
this.bedrockRuntimeClientSupplier = new BedrockRuntimeClientSupplier(); | ||
this.pollingTextractCaller = new PollingTextractCaller(); | ||
this.bedrockCaller = new BedrockCaller(); | ||
} | ||
|
||
public ExtractionConnectorFunction( | ||
PollingTextractCaller pollingTextractCaller, BedrockCaller bedrockCaller) { | ||
this.textractClientSupplier = new TextractClientSupplier(); | ||
this.s3ClientSupplier = new S3ClientSupplier(); | ||
this.bedrockRuntimeClientSupplier = new BedrockRuntimeClientSupplier(); | ||
this.pollingTextractCaller = pollingTextractCaller; | ||
this.bedrockCaller = bedrockCaller; | ||
} | ||
|
||
@Override | ||
public Object execute(OutboundConnectorContext context) { | ||
final var extractionRequest = context.bindVariables(ExtractionRequest.class); | ||
|
||
try { | ||
String extractedText = | ||
switch (extractionRequest.getInput().extractionEngineType()) { | ||
case AWS_TEXTRACT -> extractTextUsingAwsTextract(extractionRequest); | ||
case APACHE_PDFBOX -> extractTextUsingApachePdf(extractionRequest); | ||
}; | ||
|
||
String bedrockResponse = | ||
bedrockCaller.call( | ||
extractionRequest, | ||
extractedText, | ||
bedrockRuntimeClientSupplier.getBedrockRuntimeClient(extractionRequest)); | ||
|
||
return new ExtractionResult(bedrockResponse); | ||
} catch (Exception e) { | ||
LOGGER.error("Document extraction failed: {}", e.getMessage()); | ||
throw new ConnectorException(e); | ||
} | ||
} | ||
|
||
private String extractTextUsingAwsTextract(ExtractionRequest extractionRequest) throws Exception { | ||
return pollingTextractCaller.call( | ||
extractionRequest.getInput().documentUrl(), | ||
extractionRequest.getInput().s3BucketName(), | ||
textractClientSupplier.getTextractClient(extractionRequest), | ||
s3ClientSupplier.getAsyncS3Client(extractionRequest)); | ||
} | ||
|
||
private String extractTextUsingApachePdf(ExtractionRequest extractionRequest) throws Exception { | ||
String documentUrl = extractionRequest.getInput().documentUrl(); | ||
URL url = URI.create(documentUrl).toURL(); | ||
PDDocument document = Loader.loadPDF(IOUtils.toByteArray(url.openStream())); | ||
PDFTextStripper pdfStripper = new PDFTextStripper(); | ||
return pdfStripper.getText(document); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...dp-extraction/src/main/java/io/camunda/connector/idp/extraction/caller/BedrockCaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. Licensed under a proprietary license. | ||
* See the License.txt file for more information. You may not use this file | ||
* except in compliance with the proprietary license. | ||
*/ | ||
package io.camunda.connector.idp.extraction.caller; | ||
|
||
import io.camunda.connector.idp.extraction.model.ConverseData; | ||
import io.camunda.connector.idp.extraction.model.ExtractionRequest; | ||
import java.util.stream.Collectors; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
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.ConverseResponse; | ||
import software.amazon.awssdk.services.bedrockruntime.model.Message; | ||
|
||
public class BedrockCaller { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(BedrockCaller.class); | ||
|
||
private static final String EXTRACTED_TEXT_PLACEHOLDER_FOR_PROMPT = "{{extractedText}}"; | ||
|
||
private static final String TAXONOMY_PLACEHOLDER_FOR_PROMPT = "{{taxonomy}}"; | ||
|
||
private static final String SYSTEM_PROMPT_TEMPLATE = | ||
""" | ||
You will receive extracted text from a PDF document. This text will be between the <DOCUMENT_TEXT> tags. | ||
Your task is to extract certain variables from the text. The description how to extract the variables is | ||
between the <EXTRACTION> tags. Every variable is represented by a <VAR> tag. Every variable has a name, | ||
which is represented by the <NAME> tag, as well as instructions which data to extract, which is represented | ||
by the <PROMPT> tag. | ||
|
||
Respond in JSON format, without any preamble. Example response: | ||
{ | ||
"name": "John Smith", | ||
"age": 32 | ||
} | ||
|
||
Here is the document text as well as your instructions on which variables to extract: | ||
<DOCUMENT_TEXT>%s</DOCUMENT_TEXT> | ||
<EXTRACTION>%s</EXTRACTION> | ||
""" | ||
.formatted(EXTRACTED_TEXT_PLACEHOLDER_FOR_PROMPT, TAXONOMY_PLACEHOLDER_FOR_PROMPT); | ||
|
||
private static final String SYSTEM_PROMPT_VARIABLE_TEMPLATE = | ||
""" | ||
<VAR> | ||
<NAME>%s</NAME> | ||
<PROMPT>%s</PROMPT> | ||
</VAR> | ||
"""; | ||
|
||
public String call( | ||
ExtractionRequest extractionRequest, | ||
String extractedText, | ||
BedrockRuntimeClient bedrockRuntimeClient) { | ||
LOGGER.debug("Calling AWS Bedrock model with extraction request: {}", extractionRequest); | ||
|
||
String taxonomyItems = | ||
extractionRequest.getInput().taxonomyItems().stream() | ||
.map(item -> String.format(SYSTEM_PROMPT_VARIABLE_TEMPLATE, item.name(), item.prompt())) | ||
.collect(Collectors.joining()); | ||
|
||
String prompt = | ||
SYSTEM_PROMPT_TEMPLATE | ||
.replace(EXTRACTED_TEXT_PLACEHOLDER_FOR_PROMPT, extractedText) | ||
.replace(TAXONOMY_PLACEHOLDER_FOR_PROMPT, taxonomyItems); | ||
|
||
Message message = | ||
Message.builder() | ||
.content(ContentBlock.fromText(prompt)) | ||
.role(ConversationRole.USER) | ||
.build(); | ||
|
||
ConverseData converseData = extractionRequest.getInput().converseData(); | ||
|
||
ConverseResponse response = | ||
bedrockRuntimeClient.converse( | ||
request -> | ||
request | ||
.modelId(converseData.modelId()) | ||
.messages(message) | ||
.inferenceConfig( | ||
config -> | ||
config | ||
.maxTokens(converseData.maxTokens()) | ||
.temperature(converseData.temperature()) | ||
.topP(converseData.topP()))); | ||
Comment on lines
+79
to
+90
Check notice Code scanning / CodeQL Unread local variable Note
Variable 'ConverseResponse response' is never read.
|
||
|
||
return response.output().message().content().getFirst().text(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unread local variable Note