Skip to content

Commit

Permalink
Merge pull request #140 from dongyeon1031/main
Browse files Browse the repository at this point in the history
Feat: 학교공지 요약기능 추가
  • Loading branch information
TaetaetaE01 authored Oct 17, 2024
2 parents 3e068de + 42c1664 commit 40f6bdb
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 93 deletions.
8 changes: 0 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,6 @@ dependencies {
implementation 'com.amazonaws:aws-java-sdk-core:1.12.50'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.1'

// SageMaker
// implementation 'software.amazon.awssdk:sagemaker:2.20.0'
// implementation 'software.amazon.awssdk:sdk-core:2.20.0'
// implementation 'software.amazon.awssdk:http-client-netty:2.20.0'
implementation 'software.amazon.awssdk:sagemakerruntime:2.20.92'
implementation 'software.amazon.awssdk:sdk-core:2.20.92'
// implementation 'software.amazon.awssdk:http-client-netty:2.20.92'

// OCR
implementation 'org.apache.pdfbox:pdfbox:2.0.27'
implementation 'net.sourceforge.tess4j:tess4j:4.5.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.example.bigbrotherbe.domain.campusNotice.entity.CampusNotice;
import com.example.bigbrotherbe.domain.campusNotice.entity.CampusNoticeType;
import com.example.bigbrotherbe.domain.campusNotice.service.CampusNoticeService;
import com.example.bigbrotherbe.domain.campusNotice.util.LambdaUtil;
import com.example.bigbrotherbe.domain.campusNotice.util.CrawlerUtil;
import com.example.bigbrotherbe.global.common.exception.response.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -24,7 +24,7 @@
@RequiredArgsConstructor
public class CampusNoticeAdminController {
private final CampusNoticeService campusNoticeService;
private final LambdaUtil lambdaUtil;
private final CrawlerUtil crawlerUtil;

@GetMapping("/{campusNoticeId}")
public ResponseEntity<ApiResponse<CampusNoticeResponse>> getCampusNoticeById(@PathVariable("campusNoticeId") Long campusNoticeId) {
Expand All @@ -50,7 +50,7 @@ public ResponseEntity<ApiResponse<Page<CampusNotice>>> getCampusNoticeList(@Requ

@PostMapping
public ResponseEntity<ApiResponse<Void>> crawlerNotice() {
lambdaUtil.invokeLambda();
crawlerUtil.invokeLambda();
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Component
@RequiredArgsConstructor
public class LambdaUtil {
public class CrawlerUtil {

private final CampusNoticeService campusNoticeService;

Expand All @@ -18,9 +18,9 @@ public void invokeLambda() {
for (CampusNoticeType noticeType : CampusNoticeType.values()){
String payload = "{\"queryStringParameters\": {" +
"\"url\": \"" + noticeType.getUrl() + "\"," +
"\"base_url\": \"" + Constant.Lambda.BASE_URL + "\"" +
"\"base_url\": \"" + Constant.Crawler.BASE_URL + "\"" +
"}}";
campusNoticeService.invokeLambda(Constant.Lambda.FUNCTION_NAME, payload, noticeType);
campusNoticeService.invokeLambda(Constant.Crawler.FUNCTION_NAME, payload, noticeType);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.bigbrotherbe.domain.campusNotice.config;
package com.example.bigbrotherbe.global.common.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
Expand All @@ -21,11 +21,13 @@ public class LambdaConfig {
private String region;

@Bean
// 자격증명 생성
public BasicAWSCredentials awsCredentials() {
return new BasicAWSCredentials(accessKey, secretKey);
}

@Bean
// Lambda 서비스 접근 클라이언트 생성
public AWSLambda awsLambda(BasicAWSCredentials awsCredentials) {
return AWSLambdaClientBuilder.standard()
.withRegion(Regions.fromName(region))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class GetContent {
public static final String SIZE_DEFAULT_VALUE = "10";
}

public static class Lambda{
public static class Crawler {
public static final String FUNCTION_NAME = "campusNoticeCrawler";
public static final String BASE_URL = "https://www.mju.ac.kr";
}
Expand All @@ -19,6 +19,10 @@ public static class Url{
public static final String DOMAIN_URL = "https://api.mju-bigbrother.xyz";
}

public static class LLM{
public static final String FUNCTION_NAME = "callBedrock";
}

public static class Entity{
public static final String NOTICE = "notice";
public static final String FAQ = "faq";
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package com.example.bigbrotherbe.global.llm.controller;
import com.example.bigbrotherbe.global.llm.service.SageMakerService;
import com.example.bigbrotherbe.domain.campusNotice.dto.CampusNoticeResponse;
import com.example.bigbrotherbe.domain.campusNotice.service.CampusNoticeService;
import com.example.bigbrotherbe.global.common.exception.response.ApiResponse;
import com.example.bigbrotherbe.global.llm.dto.SummarizeRequest;
import com.example.bigbrotherbe.global.llm.service.BedrockService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import static com.example.bigbrotherbe.global.common.exception.enums.SuccessCode.SUCCESS;

@RestController
@RequestMapping("/api/v1/admin/llm")
@RequiredArgsConstructor
@RequestMapping("/api/v1/summarize")
public class SummarizationController {

@Autowired
private SageMakerService sageMakerService;
private final BedrockService bedrockService;
private final CampusNoticeService campusNoticeService;

@GetMapping()
public String summarize(@RequestPart(value = "text") String inputText) {
return sageMakerService.getSummary(inputText);
public ResponseEntity<ApiResponse<String>> getSummarizeById(@RequestBody SummarizeRequest summarizeRequest) {
CampusNoticeResponse campusNoticeResponse = campusNoticeService.getCampusNoticeById(summarizeRequest.getCampusNoticeId());
String content = campusNoticeResponse.getContent();
String response = bedrockService.getSummary(content);
return ResponseEntity.ok(ApiResponse.success(SUCCESS, response));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.bigbrotherbe.global.llm.dto;

import lombok.Getter;

@Getter
public class SummarizeRequest {
Long campusNoticeId;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.bigbrotherbe.global.llm.service;

public interface BedrockService {
public String getSummary(String content);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.bigbrotherbe.global.llm.service;
import com.amazonaws.services.lambda.AWSLambda;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.services.lambda.model.InvokeResult;
import com.example.bigbrotherbe.global.common.constant.Constant;
import com.example.bigbrotherbe.global.common.exception.BusinessException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.example.bigbrotherbe.global.common.exception.enums.ErrorCode.FAIL_TO_JSON_PARSING;
import static com.example.bigbrotherbe.global.common.exception.enums.ErrorCode.LAMBDA_FUNCTION_ERROR;

@Service
@RequiredArgsConstructor
public class BedrockServiceImpl implements BedrockService{

private final AWSLambda awsLambda;
private final ObjectMapper objectMapper;

public String getSummary(String content) {
String payload = this.createPayload(content);
InvokeRequest invokeRequest = new InvokeRequest()
.withFunctionName(Constant.LLM.FUNCTION_NAME)
.withPayload(payload);

InvokeResult invokeResult = awsLambda.invoke(invokeRequest);
String jsonResponse = new String(invokeResult.getPayload().array());

try{
JsonNode root = objectMapper.readTree(jsonResponse);
return root.get("body").asText();
} catch (
JsonProcessingException e) {
throw new BusinessException(FAIL_TO_JSON_PARSING);
} catch (NullPointerException e) {
throw new BusinessException(LAMBDA_FUNCTION_ERROR);
}
}

private String createPayload(String content) {
return "{" +
"\"content\": \"" + content + "\"" +
"}";
}

}

This file was deleted.

0 comments on commit 40f6bdb

Please sign in to comment.