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

add summary from text, add interaction to database for all scenarios,… #21

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'dev.victormartin.oci.genai.backend'
version = '0.0.5'
version = '0.0.6'

java {
sourceCompatibility = '17'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import com.oracle.bmc.model.BmcException;
import dev.victormartin.oci.genai.backend.backend.dao.Answer;
import dev.victormartin.oci.genai.backend.backend.data.Interaction;
import dev.victormartin.oci.genai.backend.backend.data.InteractionRepository;
import dev.victormartin.oci.genai.backend.backend.data.InteractionType;
import dev.victormartin.oci.genai.backend.backend.service.OCIGenAIService;
import dev.victormartin.oci.genai.backend.backend.service.PDFConvertorService;
import org.slf4j.Logger;
Expand All @@ -11,12 +14,15 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils;

import java.io.File;
import java.util.Date;

@RestController
public class PDFConvertorController {
Expand All @@ -34,8 +40,13 @@ public class PDFConvertorController {
@Autowired
PDFConvertorService pdfConvertorService;

@Autowired
private InteractionRepository interactionRepository;

@PostMapping("/api/upload")
public Answer fileUploading(@RequestParam("file") MultipartFile multipartFile) {
public Answer fileUploading(@RequestParam("file") MultipartFile multipartFile,
@RequestHeader("conversationID") String conversationId,
@RequestHeader("modelId") String modelId) {
String filename = StringUtils.cleanPath(multipartFile.getOriginalFilename());
log.info("File uploaded {} {} bytes ({})", filename, multipartFile.getSize(), multipartFile.getContentType());
try {
Expand All @@ -50,19 +61,26 @@ public Answer fileUploading(@RequestParam("file") MultipartFile multipartFile) {
multipartFile.transferTo(file);
log.info("File destination path: {}", file.getAbsolutePath());
String convertedText = pdfConvertorService.convert(file.getAbsolutePath());
String summaryText = ociGenAIService.summaryText(convertedText, summarizationModelId);
String textEscaped = HtmlUtils.htmlEscape(convertedText);
Interaction interaction = new Interaction();
interaction.setType(InteractionType.SUMMARY_FILE);
interaction.setConversationId(conversationId);
interaction.setDatetimeRequest(new Date());
interaction.setModelId(summarizationModelId);
interaction.setRequest(textEscaped);
Interaction saved = interactionRepository.save(interaction);
String summaryText = ociGenAIService.summaryText(textEscaped, summarizationModelId);
saved.setDatetimeResponse(new Date());
saved.setResponse(summaryText);
interactionRepository.save(saved);
log.info("Summary text: {}(...)", summaryText.substring(0, 40));
Answer answer = new Answer(summaryText, "");
return answer;
} catch (MaxUploadSizeExceededException maxUploadSizeExceededException) {
log.error(maxUploadSizeExceededException.getMessage());
throw new RuntimeException(maxUploadSizeExceededException);
} catch (BmcException exception) {
log.error("Message: {}", exception.getMessage());
log.error("Original Message: {}", exception.getOriginalMessage());
log.error("Unmodified Message: {}", exception.getUnmodifiedMessage());
log.error("Service Details: {}", exception.getServiceDetails());
log.error("Status Code: {}", exception.getStatusCode());
String unmodifiedMessage = exception.getUnmodifiedMessage();
int statusCode = exception.getStatusCode();
String errorMessage = statusCode + " " + unmodifiedMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.oracle.bmc.model.BmcException;
import dev.victormartin.oci.genai.backend.backend.InvalidPromptRequest;
import dev.victormartin.oci.genai.backend.backend.data.InteractionType;
import dev.victormartin.oci.genai.backend.backend.service.OCIGenAIService;
import dev.victormartin.oci.genai.backend.backend.dao.Answer;
import dev.victormartin.oci.genai.backend.backend.dao.Prompt;
Expand Down Expand Up @@ -44,6 +45,7 @@ public Answer handlePrompt(Prompt prompt) {
logger.info("Prompt " + promptEscaped + " received, on model " + prompt.modelId() + " but using hardcoded one" +
" " + hardcodedChatModelId);
Interaction interaction = new Interaction();
interaction.setType(InteractionType.CHAT);
interaction.setConversationId(prompt.conversationId());
interaction.setDatetimeRequest(new Date());
interaction.setModelId(hardcodedChatModelId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package dev.victormartin.oci.genai.backend.backend.controller;

import com.oracle.bmc.model.BmcException;
import dev.victormartin.oci.genai.backend.backend.dao.Answer;
import dev.victormartin.oci.genai.backend.backend.dao.SummaryRequest;
import dev.victormartin.oci.genai.backend.backend.data.Interaction;
import dev.victormartin.oci.genai.backend.backend.data.InteractionRepository;
import dev.victormartin.oci.genai.backend.backend.data.InteractionType;
import dev.victormartin.oci.genai.backend.backend.service.OCIGenAIService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;

import java.util.Date;

@RestController
public class SummaryController {
Logger logger = LoggerFactory.getLogger(SummaryController.class);

@Value("${genai.summarization_model_id}")
String summarizationModelId;

@Autowired
OCIGenAIService ociGenAIService;

@Autowired
private InteractionRepository interactionRepository;

@PostMapping("/api/genai/summary")
public Answer postSummaryText(@RequestBody SummaryRequest summaryRequest,
@RequestHeader("conversationID") String conversationId,
@RequestHeader("modelId") String modelId) {
logger.info("postSummaryText()");
String contentEscaped = HtmlUtils.htmlEscape(summaryRequest.content());
logger.info("contentEscaped: {}...", contentEscaped.substring(0, 50));
Interaction interaction = new Interaction();
interaction.setType(InteractionType.SUMMARY_TEXT);
interaction.setConversationId(conversationId);
interaction.setDatetimeRequest(new Date());
interaction.setModelId(summarizationModelId);
interaction.setRequest(contentEscaped);
Interaction saved = interactionRepository.save(interaction);
try {
String summaryText = ociGenAIService.summaryText(contentEscaped, summarizationModelId);
saved.setDatetimeResponse(new Date());
saved.setResponse(summaryText);
interactionRepository.save(saved);
logger.info("summaryText: {}...", summaryText.substring(0, 50));
Answer answer = new Answer();
answer.setContent(summaryText);
answer.setErrorMessage("");
return answer;
} catch (BmcException e) {
String unmodifiedMessage = e.getUnmodifiedMessage();
int statusCode = e.getStatusCode();
String errorMessage = statusCode + " " + unmodifiedMessage;
logger.error(errorMessage);
saved.setErrorMessage(errorMessage);
interactionRepository.save(saved);
Answer answer = new Answer("", errorMessage);
return answer;
} catch (Exception e) {
String errorMessage = e.getLocalizedMessage();
logger.error(errorMessage);
saved.setErrorMessage(errorMessage);
interactionRepository.save(saved);
Answer answer = new Answer("", errorMessage);
return answer;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.victormartin.oci.genai.backend.backend.dao;

public record SummaryRequest(String content) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class Interaction {

String conversationId;

@Enumerated(EnumType.STRING)
InteractionType type;

@Temporal(TemporalType.DATE)
Date datetimeRequest;

Expand Down Expand Up @@ -41,12 +44,12 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Interaction that = (Interaction) o;
return Objects.equals(id, that.id) && Objects.equals(conversationId, that.conversationId) && Objects.equals(datetimeRequest, that.datetimeRequest) && Objects.equals(modelId, that.modelId) && Objects.equals(request, that.request) && Objects.equals(datetimeResponse, that.datetimeResponse) && Objects.equals(response, that.response) && Objects.equals(errorMessage, that.errorMessage);
return Objects.equals(id, that.id) && Objects.equals(conversationId, that.conversationId) && type == that.type && Objects.equals(datetimeRequest, that.datetimeRequest) && Objects.equals(modelId, that.modelId) && Objects.equals(request, that.request) && Objects.equals(datetimeResponse, that.datetimeResponse) && Objects.equals(response, that.response) && Objects.equals(errorMessage, that.errorMessage);
}

@Override
public int hashCode() {
return Objects.hash(id, conversationId, datetimeRequest, modelId, request, datetimeResponse, response, errorMessage);
return Objects.hash(id, conversationId, type, datetimeRequest, modelId, request, datetimeResponse, response, errorMessage);
}

public Long getId() {
Expand All @@ -61,6 +64,14 @@ public void setConversationId(String conversationId) {
this.conversationId = conversationId;
}

public InteractionType getType() {
return type;
}

public void setType(InteractionType type) {
this.type = type;
}

public Date getDatetimeRequest() {
return datetimeRequest;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.victormartin.oci.genai.backend.backend.data;

public enum InteractionType {
CHAT, SUMMARY_FILE, SUMMARY_TEXT
}
110 changes: 55 additions & 55 deletions deploy/terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions web/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web",
"private": true,
"version": "0.0.3",
"version": "0.0.4",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -33,4 +33,4 @@
"eslint-plugin-react-refresh": "^0.4.5",
"vite": "^5.0.13"
}
}
}
Loading