Skip to content

Commit

Permalink
DEPLOY : BE - Similar Step 찾는 API 생성 (#49)
Browse files Browse the repository at this point in the history
* be-feat/serverSetUp (#14)

* [BE] JIRA-73 #comment springboot server initialize

* [BE] JIRA-73 #test #comment springboot server initialize test

* JIRA-73 #BUG_FIX #comment IDEA gitignore 추가

* JIRA-97 #DOCS #comment 'PR workflow reviewer 반영안되는 오류 수정'

* JIRA-97 #DOCS #comment 'PR workflow reviewer 반영안되는 오류 수정'

* JIRA-97 #DOCS #comment 'PR workflow reviewer api 요청 결과에 맞게 fail하도록 변경'

* JIRA-97 #DOCS #comment 'PR workflow token 설정'

---------

Co-authored-by: tank3a <[email protected]>

* JIRA-42 #feat #comment "CodeDeploy 실행을 위한 Script 파일 추가"

* JIRA-42 #bug_fix #comment "CodeDeploy 실행 스크립트 디렉토리 위치 오류 해결"

* Revert "JIRA-42 #bug_fix #comment "CodeDeploy 실행 스크립트 디렉토리 위치 오류 해결""

This reverts commit e522d54.

* Revert "JIRA-42 #feat #comment "CodeDeploy 실행을 위한 Script 파일 추가""

This reverts commit 289da5c.

* JIRA-42 #feat #comment "CodeDeploy 실행을 위한 Script 파일 추가"

* JIRA-42 #bug_fix #comment "CodeDeploy 실행 스크립트 디렉토리 위치 오류 해결"

* JIRA-42 #bug_fix #comment "start_server.sh 파일 위치 오류 해결"

* JIRA-42 #bug_fix #comment "stop_server.sh 파일 위치 오류 해결"

* JIRA-93 #feat #comment "Llama로부터 API 요청 및 응답 처리"

* JIRA-60 #feat #comment "Client로부터 Task 요청 받는 API 프레임 생성"

* JIRA-123 #feat #comment "MongoDB 연결 위한 Repository 및 Entity, properties 설정. build.gradle 추가"

* JIRA-93 #feat #comment "Llama 응답 timeout 설정"

* JIRA-93 #feat #comment "Llama subTask 요청 구현"

* JIRA-38 #feat #comment "Client Stream 처리 구현"

* JIRA-60 #feat #comment "Task 요청 API 구현"

* JIRA-60 #close #comment "불필요한 부분 제거"

* JIRA-93 #close #comment "직관적이지 않은 이름 수정"

* JIRA-93 #close #comment "수정시 빠진 부분 재수정"

* JIRA-93 #close #comment "수정시 빠진 부분 재수정"

* Delete application.properties

* JIRA-91 #feat #comment "클라이언트의 code를 저장하는 api 구성 완료"

* JIRA-37 #feat #comment "전체 http메소드 cors설정 완료"

* JIRA-91 #refac #comment "오타수정 및 리팩토링"

* JIRA-133 #refac #comment "Task 요청 API Get으로 메서드 변경"

* JIRA-112 #refac #comment "matcherParse 오탈자 수정"

* JIRA-134 #chore #comment "불필요한 dependency 삭제"

* JIRA-134 #feat #comment "Step을 Embedding Vector로 바꾸기 위한 DTO 추가 및 float[] 추가"

* JIRA-134 #feat #comment "Similarity 계산 위한 Service Class 생성"

* JIRA-134 #refac #comment "새로운 Llama API 추가로 인한 기존 코드의 명칭 개선"

* JIRA-134 #feat #comment "Llama Server에서 Sent2vec Response 받는 로직 완성"

* JIRA-135 #feat #comment "Reactive 처리 위한 Llama Server API Response를
Mono로 받음"

* JIRA-135 #feat #comment "Task 요청 관련 추적용 Log 생성"

* JIRA-134 #feat #comment "받아온 Vector값으로 Top Rank의 Step 반환"

* JIRA-135 #chore #comment "불필요한 gradle build 파일 업로드 제거"

* JIRA-135 #refac #comment "TASK 요청 시 [DONE] 키워드가 오면 버퍼 모두 내보내도록 처리 로직 수정"

* JIRA-135 #refac #comment "vector value 크기가 64bit이므로 float->double 변경"

* JIRA-135 #bugfix #comment "flux내에서 다른 api 호출 시 조기 종료되는 버그 발생으로 api 분리"

* JIRA-136 #bugfix #comment "gradle ignore 취소"

* JIRA-136 #bugfix #comment "gradle ignore 취소"

---------

Co-authored-by: humang <[email protected]>
  • Loading branch information
tank3a and humang authored Nov 2, 2023
1 parent 527b4ff commit 0ebb56a
Show file tree
Hide file tree
Showing 20 changed files with 368 additions and 165 deletions.
4 changes: 2 additions & 2 deletions backend/epimetheus/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!gradle/wrapper/gradle-wrapper.properties
!**/src/main/**/build/
!**/src/test/**/build/

Expand Down Expand Up @@ -37,7 +38,6 @@ out/
.vscode/

### properties ###
*.properties
*.yml
*.yaml
src/main/resources/*.yml
src/main/resources/*.yml
11 changes: 2 additions & 9 deletions backend/epimetheus/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,18 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
// implementation 'org.springframework.boot:spring-boot-starter-data-redis'
// implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.2'
// implementation 'org.springframework.session:spring-session-data-redis'
// implementation 'org.springframework.session:spring-session-jdbc'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
// developmentOnly 'org.springframework.boot:spring-boot-docker-compose'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'
// runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.2'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.15.2'
implementation group: 'info.debatty', name: 'java-string-similarity', version: '2.0.0'

}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package uos.capstone.epimetheus.adapter;

import reactor.core.publisher.Flux;
import uos.capstone.epimetheus.dtos.LlamaResponse;
import reactor.core.publisher.Mono;
import uos.capstone.epimetheus.dtos.LlamaStepResponse;

public interface LlamaAdapter {
Flux<LlamaResponse> fetchDataAsStream(String json);
Flux<LlamaStepResponse> getAllTaskSteps(String json);

Mono<double[]> getVectorFromSentence(String sentence);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Flux;
import uos.capstone.epimetheus.dtos.LlamaRequest;
import uos.capstone.epimetheus.dtos.LlamaResponse;
import uos.capstone.epimetheus.dtos.LlamaRequestMessage;
import reactor.core.publisher.Mono;
import uos.capstone.epimetheus.dtos.*;


import java.io.IOException;
Expand All @@ -32,22 +31,24 @@ public class LlamaServerStreamAdapter implements LlamaAdapter{
private final ObjectMapper objectMapper;


@Value("${llama.url}")
String url;
@Value("${llama.generate.step}")
String stepGenerateUrl;
@Value("${llama.generate.vector}")
String vectorGenerateUrl;
@Value("${prompt}")
Resource prompt;

private String requestBodyBuilder(String task){
private String stepGenerateRequestBodyBuilder(String task){
Gson gson = new Gson();
LlamaRequest request = LlamaRequest.builder()
LlamaStepRequest request = LlamaStepRequest.builder()
.max_tokens(1024)
.temperature(0)
.messages(List.of(
LlamaRequestMessage.builder()
.content(readTextFile())
LlamaPromptRequestMessage.builder()
.content(readPrompt())
.role("system")
.build(),
LlamaRequestMessage.builder()
LlamaPromptRequestMessage.builder()
.content(task)
.role("user")
.build()
Expand All @@ -58,7 +59,7 @@ private String requestBodyBuilder(String task){
return gson.toJson(request);
}

private String readTextFile(){
private String readPrompt(){
try {
InputStream inputStream = prompt.getInputStream();
return new String(FileCopyUtils.copyToByteArray(inputStream));
Expand All @@ -70,23 +71,25 @@ private String readTextFile(){
}

@Override
public Flux<LlamaResponse> fetchDataAsStream(String json){
public Flux<LlamaStepResponse> getAllTaskSteps(String task){

String body = requestBodyBuilder(json);
String body = stepGenerateRequestBodyBuilder(task);
try{
log.info("Step Generate Request to Llama with task: " + task);
return webClient.post()
.uri(url)
.uri(stepGenerateUrl)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(body))
.retrieve()
.bodyToFlux(String.class)
.doOnError(e -> log.info("Error occurred while making web request", e))
.flatMap(responseString -> {
if ("[DONE]".equals(responseString.trim())) {
return Flux.just(new LlamaResponse());
return Flux.just(LlamaStepResponse.eof());
} else {
try {
LlamaResponse llamaResponse = objectMapper.readValue(responseString, LlamaResponse.class);
return Flux.just(llamaResponse);
LlamaStepResponse llamaStepResponse = objectMapper.readValue(responseString, LlamaStepResponse.class);
return Flux.just(llamaStepResponse);
} catch (JsonProcessingException e) {
return Flux.error(e);
}
Expand All @@ -101,4 +104,30 @@ public Flux<LlamaResponse> fetchDataAsStream(String json){
}

}

@Override
public Mono<double[]> getVectorFromSentence(String sentence) {
String body = vectorGenerateRequestBodyBuilder(sentence);

return webClient.post()
.uri(vectorGenerateUrl)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(body))
.retrieve()
.bodyToMono(LlamaVectorResponse.class)
.map(LlamaVectorResponse::getVector);
}


private String vectorGenerateRequestBodyBuilder(String sentence) {
Gson gson = new Gson();

LlamaVectorRequest llamaVectorRequest = LlamaVectorRequest.builder()
.input(sentence)
.build();

return gson.toJson(llamaVectorRequest);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import uos.capstone.epimetheus.dtos.TaskStep;
import uos.capstone.epimetheus.dtos.llamaTasks.SubTaskCode;
import uos.capstone.epimetheus.dtos.llamaTasks.SubTaskResolver;
import uos.capstone.epimetheus.service.TaskSerivce;

Expand All @@ -23,11 +24,13 @@ public class TaskController {

@GetMapping(path = "/tasks", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<SubTaskResolver> getTask(@RequestParam String task) {
log.info("[/tasks] Task : " + task);
return taskSerivce.getSubTaskListInStream(task);
}

@PostMapping(path = "/save")
public ResponseEntity<String> saveCode(@RequestBody TaskStep taskStep){
public ResponseEntity<String> saveCode(@RequestBody TaskStep taskStep) {
log.info("[/save] Save Code : " + taskStep);
String response = taskSerivce.saveCode(taskStep);
HttpStatusCode status;
if(response.equals("not code")){
Expand All @@ -39,4 +42,10 @@ public ResponseEntity<String> saveCode(@RequestBody TaskStep taskStep){
}
return ResponseEntity.status(status).body(response);
}

@GetMapping("/code")
public SubTaskCode getSimilar(@RequestParam String input) {
log.info("[/code] Similar Task Input : " + input);
return taskSerivce.getSimilarCode(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import lombok.Getter;

@Getter
public class LlamaRequestMessage {
public class LlamaPromptRequestMessage {

private final String content;
private final String role;

@Builder
public LlamaRequestMessage(String content, String role) {
public LlamaPromptRequestMessage(String content, String role) {
this.content = content;
this.role = role;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import java.util.List;

@Getter
public class LlamaRequest {
public class LlamaStepRequest {

private final List<LlamaRequestMessage> messages;
private final List<LlamaPromptRequestMessage> messages;
private final int temperature;
private final int max_tokens;
private final boolean stream;

@Builder
public LlamaRequest(List<LlamaRequestMessage> messages, int temperature, int max_tokens, boolean stream) {
public LlamaStepRequest(List<LlamaPromptRequestMessage> messages, int temperature, int max_tokens, boolean stream) {
this.messages = messages;
this.temperature = temperature;
this.max_tokens = max_tokens;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package uos.capstone.epimetheus.dtos;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Getter
public class LlamaStepResponse {
private List<Choice> choices;

public String parseContent() {
try {
return choices.get(0).getDelta().getContent();
} catch (NullPointerException e) {
return "";
}
}

public static LlamaStepResponse eof() {
LlamaStepResponse response = new LlamaStepResponse();
response.choices = new ArrayList<>();
response.choices.add(new Choice("[DONE]"));

return response;
}
}
@Getter
@NoArgsConstructor
class Choice {
private Delta delta;

Choice(String content) {
this.delta = new Delta(content);
}

}
@Getter
@NoArgsConstructor
class Delta {
private String content = "";

Delta(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package uos.capstone.epimetheus.dtos;

import lombok.Builder;
import lombok.Getter;

@Getter
public class LlamaVectorRequest {

private String input;

@Builder
public LlamaVectorRequest(String input) {
this.input = input;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package uos.capstone.epimetheus.dtos;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;

import java.util.List;

@Getter
public class LlamaVectorResponse {

private List<EmbeddingData> data;
private TokenUsage usage;

public double[] getVector() {
if((data != null ? data.size() : 0) != 1) {
throw new RuntimeException("Invalid Data Came");
}

return data.get(0).getEmbedding();
}
}

@Getter
class EmbeddingData {

private String object;
private double[] embedding;
private int index;
}

@Getter
class TokenUsage {
private int prompt_tokens;
private int total_tokens;
}
Loading

0 comments on commit 0ebb56a

Please sign in to comment.