Skip to content

Commit

Permalink
Implementation of Dynamic User and Form Management
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabreu-levio committed Jun 28, 2024
1 parent 0832f7e commit af90ef1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module "lambda_function_container_image" {
QUEUE_URL = var.queue_url
MASTER_PROMPT = var.master_prompt
FORM_S3_URI = var.form_s3_uri
TABLE_NAME = var.table_name
}

policy_statements = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ variable "aws_region" {
variable "form_s3_uri" {
type = string
nullable = false
}

variable "table_name" {
type = string
nullable = false
}
5 changes: 5 additions & 0 deletions lambdas/FormProcessor/FormRequestProcessorFunction/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<artifactId>lambda</artifactId>
<version>2.20.52</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.123</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>apache-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.levio.awsdemo.formrequestprocessor;

import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
Expand All @@ -20,13 +21,16 @@

public class App implements RequestHandler<SQSEvent, Void> {
private static final String FORM_S3_URI = System.getenv("FORM_S3_URI");
private static final String TABLE_NAME = System.getenv("TABLE_NAME");

private final DocumentService documentService;

private final ClaudeService claudeService;

private final S3Service s3Service;

private final DynamoDbService dynamoDbService;

private final SqsProducerService sqsProducerService;

private final MailService mailService;
Expand All @@ -37,14 +41,15 @@ public App() {
this.mailService = new MailService();
this.sqsProducerService = new SqsProducerService();
this.s3Service = new S3Service();
this.dynamoDbService = new DynamoDbService();
this.documentService = new DocumentService(s3Service);
this.claudeService = new ClaudeService(new LambdaService());
}

public App(S3Service s3Service,
DocumentService documentService,
ClaudeService claudeService, SqsProducerService sqsProducerService, MailService mailService) {
public App(S3Service s3Service, DocumentService documentService, ClaudeService claudeService,
SqsProducerService sqsProducerService, MailService mailService, DynamoDbService dynamoDbService) {
this.s3Service = s3Service;
this.dynamoDbService = dynamoDbService;
this.documentService = documentService;
this.claudeService = claudeService;
this.sqsProducerService = sqsProducerService;
Expand All @@ -71,15 +76,25 @@ public Void handleRequest(final SQSEvent input, final Context context) {

String content = s3Service.getFile(formFillRequest.getEmailAttachmentS3URI());

var questionsMapper = retrieveDocumentMapper(FORM_S3_URI);
String formS3Uri = FORM_S3_URI;
String prompt;
Item item = dynamoDbService.getItem(TABLE_NAME, "email", sender);
if (item != null) {
formS3Uri = (String) item.get("form-uri");
prompt = (String) item.get("prompt");
} else {
prompt = null;
}

var questionsMapper = retrieveDocumentMapper(formS3Uri);
questionsMapper.entrySet().parallelStream()
.forEach(positionQuestionAnswerMapper -> {
Map<String, String> questionAnswerMap = positionQuestionAnswerMapper.getValue();
System.out.print(questionAnswerMap.toString());
String answer = claudeService.getResponse(questionAnswerMap.get("question"), content);
String answer = claudeService.getResponse(questionAnswerMap.get("question"), content, prompt);
questionAnswerMap.put("answer", answer);
});
ByteArrayOutputStream fileOutputStream = documentService.fillFile(questionsMapper, FORM_S3_URI);
ByteArrayOutputStream fileOutputStream = documentService.fillFile(questionsMapper, formS3Uri);
String formDocxUri = s3Service.saveFile("formulaire/" + formFillRequest.getEmailId() + ".docx", fileOutputStream.toByteArray());
sqsProducerService.send(emailBody, getMessageAttributes(sender, subject, formDocxUri), formFillRequest.getEmailId());
} catch (IOException | MessagingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class ClaudeService {

private final LambdaService lambdaService;

public String getResponse(String question, String content) {
public String getResponse(String question, String content, String prompt) {
JSONObject payload = new JSONObject()
.put("master_prompt", MASTER_PROMPT)
.put("master_prompt", prompt != null ? prompt : MASTER_PROMPT)
.put("prompt", question)
.put("text", content);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.levio.awsdemo.formrequestprocessor.service;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;

public class DynamoDbService {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
DynamoDB dynamoDB = new DynamoDB(client);

public Item getItem(String tableName, String primaryKeyName, String primaryKeyValue) {
Table table = dynamoDB.getTable(tableName);

return table.getItem(primaryKeyName, primaryKeyValue);
}
}
2 changes: 2 additions & 0 deletions terraform/modules.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
locals {
memory_lambda_name = "levio-demo-fev-memory-dev"
dynamo_history_table_name = "levio-demo-fev-chat-history-dev"
form_request_processor_table_name = "levio-demo-fev-form-request-dev"
storage_bucket_name = "levio-demo-fev-storage-dev"
queue_name = "levio-demo-fev-ingestion-queue-dev"
ingestion_lambda_name = "levio-demo-fev-ingestion-dev"
Expand Down Expand Up @@ -213,6 +214,7 @@ module "form_request_processor" {
master_prompt = var.master_prompt
sqs_name = local.form_request_processor_queue_name
form_s3_uri = local.form_s3_uri
table_name = local.form_request_processor_table_name
}

module "form_request_preprocessor" {
Expand Down

0 comments on commit af90ef1

Please sign in to comment.