Skip to content
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
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bundle/default-bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@
<groupId>io.camunda.connector</groupId>
<artifactId>connector-email</artifactId>
</dependency>
<dependency>
<groupId>io.camunda.connector</groupId>
<artifactId>connector-idp-extraction</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 5 additions & 0 deletions bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@
<artifactId>connector-email</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.camunda.connector</groupId>
<artifactId>connector-idp-extraction</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions connectors/idp-extraction/pom.xml
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>
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);
}
}
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();
Comment on lines +72 to +76

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'Message message' is never read.

ConverseData converseData = extractionRequest.getInput().converseData();
Fixed Show fixed Hide fixed
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();
}
}
Loading