Skip to content

Commit

Permalink
JIRA-135 #bugfix #comment "flux내에서 다른 api 호출 시 조기 종료되는 버그 발생으로 api 분리"
Browse files Browse the repository at this point in the history
  • Loading branch information
tank3a committed Nov 2, 2023
1 parent c3226c9 commit 9148a04
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 43 deletions.
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 @@ -29,7 +30,7 @@ public Flux<SubTaskResolver> getTask(@RequestParam String task) {

@PostMapping(path = "/save")
public ResponseEntity<String> saveCode(@RequestBody TaskStep taskStep) {
log.info("[/save] Save Code - " + taskStep);
log.info("[/save] Save Code : " + taskStep);
String response = taskSerivce.saveCode(taskStep);
HttpStatusCode status;
if(response.equals("not code")){
Expand All @@ -41,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 @@ -31,4 +31,8 @@ public String getProperty() {
public String getLanguage() {
return language.getLanguage();
}

public String getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import uos.capstone.epimetheus.adapter.LlamaAdapter;
import uos.capstone.epimetheus.dtos.TaskStep;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

@Service
Expand All @@ -19,27 +16,27 @@ public class CosineSimilarityService implements SimilarityService {
private final DatabaseService databaseService;

@Override
public Mono<TaskStep> getSimilarStep(String step) {
return llamaAdapter.getVectorFromSentence(step)
.flatMap(inputVector -> {
return Mono.justOrEmpty(
databaseService.getAllData().stream()
.filter(data -> cosineSimilarity(inputVector, data.getValues()) >= 0.9f)
.min(Comparator.comparing(data -> -cosineSimilarity(inputVector, data.getValues())))
.orElse(TaskStep.of(step))
);
});
public TaskStep getSimilarStep(String step) {

double[] inputVector = llamaAdapter.getVectorFromSentence(step).block();
Optional<TaskStep> similar = databaseService.getAllData().stream()
.filter(data -> data.getValues() != null && data.getValues().length == inputVector.length)
.filter(data -> cosineSimilarity(inputVector, data.getValues()) >= 0.8)
.sorted(Comparator.comparing(data -> (-1) * cosineSimilarity(inputVector, data.getValues())))
.findFirst();

return similar.orElseGet(() -> databaseService.saveByTitle(step, inputVector));
}


private float cosineSimilarity(float[] input, float[] toCompare) {
private double cosineSimilarity(double[] input, double[] toCompare) {
if (input.length != toCompare.length) {
throw new IllegalArgumentException("Vectors must have the same length");
}

float dotProduct = 0.0f;
float normA = 0.0f;
float normB = 0.0f;
double dotProduct = 0.0;
double normA = 0.0;
double normB = 0.0;

for (int i = 0; i < input.length; i++) {
dotProduct += input[i] * toCompare[i];
Expand All @@ -52,6 +49,7 @@ private float cosineSimilarity(float[] input, float[] toCompare) {
return 0;
}

return dotProduct / (float) (Math.sqrt(normA) * Math.sqrt(normB));
double result = dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import java.util.List;

public interface DatabaseService {
TaskStep getTaskStepByTitle(String id);

TaskStep saveByTitle(String step, double[] vector);

void saveCode(TaskStep taskStep);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class MongoDBServiceImpl implements DatabaseService {
private final MongoDBRepository mongoRepository;

@Override
public TaskStep getTaskStepByTitle(String id){
return mongoRepository.findById(id).orElse(mongoRepository.save(TaskStep.of(id)));
public TaskStep saveByTitle(String step, double[] vector) {
return mongoRepository.save(TaskStep.of(step, vector));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package uos.capstone.epimetheus.service;

import reactor.core.publisher.Mono;
import uos.capstone.epimetheus.dtos.TaskStep;

public interface SimilarityService {
Mono<TaskStep> getSimilarStep(String step);
TaskStep getSimilarStep(String step);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

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;

public interface TaskSerivce {

Flux<SubTaskResolver> getSubTaskListInStream(String task);

String saveCode(TaskStep taskStep);

SubTaskCode getSimilarCode(String step);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SignalType;
import uos.capstone.epimetheus.adapter.LlamaAdapter;
import uos.capstone.epimetheus.dtos.LlamaStepResponse;
import uos.capstone.epimetheus.dtos.TaskStep;
import uos.capstone.epimetheus.dtos.llamaTasks.*;


import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -38,7 +33,8 @@ public Flux<SubTaskResolver> getSubTaskListInStream(String task) {
Pattern pattern = Pattern.compile("!!(\\d+)\\.");


return llamaAdapter.getAllTaskSteps(task).handle((llamaStepResponse, sink) -> {
return llamaAdapter.getAllTaskSteps(task).flatMap(llamaStepResponse -> {
Flux<SubTaskResolver> subTask = Flux.empty();
String data = llamaStepResponse.parseContent();
Matcher matcher = pattern.matcher(buffer);
boolean patternFound = matcher.find();
Expand All @@ -50,52 +46,48 @@ public Flux<SubTaskResolver> getSubTaskListInStream(String task) {
buffer.setLength(0);
stepNo.set(0);
} else if (data.equals("[DONE]")) {
sink.next(SubTaskWrap.builder()
subTask = Flux.just(SubTaskWrap.builder()
.stepNo(0)
.wrapper(endOfFluxParse(buffer))
.property(ResponseStreamProperty.OUTRO)
.build());
} else if (buffer.indexOf(stopWord) == -1 && !patternFound) {
subTask = Flux.empty();
} else {
boolean type = buffer.indexOf(stopWord) != -1;
String content = type ? stopWordParse(buffer) : matcherParse(buffer, matcher.start());
switch (state.get()) {
case 0:
intro.append(content);
buffer.setLength(0);
sink.next(SubTaskWrap.builder()
subTask = Flux.just(SubTaskWrap.builder()
.stepNo(0)
.wrapper(content)
.property(ResponseStreamProperty.INTRO)
.build());
break;
case 1:
if(stepNo.get() == 0)
if(stepNo.get() == 0) {
subTask = Flux.empty();
break;
sink.next(SubTaskTitle.builder()
}
subTask = Flux.just(SubTaskTitle.builder()
.stepNo(stepNo.get())
.title(content)
.property(ResponseStreamProperty.TITLE)
.build());
similarityService.getSimilarStep(content).subscribe(taskStep -> sink.next((SubTaskCode.builder()
.stepNo(stepNo.get())
.code(taskStep.getCode())
.property(ResponseStreamProperty.CODE)
.language(CodeLanguage.of(taskStep.getLanguage()))
.build()
)));
break;
case 2:
if(stepNo.get() == 0)
break;
sink.next(SubTaskDescription.builder()
subTask = Flux.just(SubTaskDescription.builder()
.stepNo(stepNo.get())
.description(content)
.property(ResponseStreamProperty.DESCRIPTION)
.build());
break;
default:
sink.error(new RuntimeException("Invalid Prompt"));
subTask = Flux.error(new RuntimeException("Invalid Prompt"));
}

if (type) {
Expand All @@ -108,6 +100,7 @@ public Flux<SubTaskResolver> getSubTaskListInStream(String task) {
buffer.setLength(0);
}
buffer.append(data);
return subTask;
});
}

Expand Down Expand Up @@ -137,5 +130,16 @@ private String endOfFluxParse(StringBuffer stringBuffer){
private String matcherParse(StringBuffer stringBuffer, int m){
return stringBuffer.substring(0, m).trim();
}

@Override
public SubTaskCode getSimilarCode(String step) {
TaskStep stepCode = similarityService.getSimilarStep(step);

return SubTaskCode.builder()
.code(stepCode.getCode())
.property(ResponseStreamProperty.CODE)
.language(CodeLanguage.of(stepCode.getLanguage()))
.build();
}
}

0 comments on commit 9148a04

Please sign in to comment.