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

feat: api 추가 개발 #26

Merged
merged 1 commit into from
Jan 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
25 changes: 20 additions & 5 deletions src/main/java/com/hackerton/demo/domain/keyword/ChatService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.hackerton.demo.domain.keyword;

import com.hackerton.demo.domain.User.User;
import com.hackerton.demo.domain.User.UserRepository;
import com.theokanning.openai.completion.chat.ChatCompletionChoice;
import com.theokanning.openai.service.OpenAiService;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -25,8 +28,9 @@ public class ChatService {
private final KeywordRespository keywordRespository;

private final PlaceRepository placeRepository;
private final UserRepository userRepository;

public List<KeywordDto> createGptComment(String content) {
public List<KeywordDto> createGptComment(String content, Long userId) {
this.openAiService = new OpenAiService(apiKey, Duration.ofSeconds(20));
String prompt = content + "대신 딱 10개만 아무런 부가 설명 없이 키워드만 알려줘.";

Expand All @@ -41,10 +45,10 @@ public List<KeywordDto> createGptComment(String content) {
ChatCompletionChoice chatCompletionResult = openAiService.createChatCompletion(requester).getChoices().get(0);
String contentResult = chatCompletionResult.getMessage().getContent();

return extractKeywords(contentResult);
return extractKeywords(contentResult, userId);
}

private List<KeywordDto> extractKeywords(String content) {
private List<KeywordDto> extractKeywords(String content, Long userId) {
List<KeywordDto> keywordDtos = new ArrayList<>();

// 개행 문자로 분할하여 리스트로 변환
Expand All @@ -56,16 +60,18 @@ private List<KeywordDto> extractKeywords(String content) {
keywordDtos.add(keywordDto);
}

List<Keyword> keywords = keywordRespository.saveAll(convertToKeywords(keywordDtos));
List<Keyword> keywords = keywordRespository.saveAll(convertToKeywords(keywordDtos, userId));

return convertToKeywordDtos(keywords);
}

private List<Keyword> convertToKeywords(List<KeywordDto> keywordDtos) {
private List<Keyword> convertToKeywords(List<KeywordDto> keywordDtos, Long userId) {
User user = userRepository.getReferenceById(userId);
List<Keyword> keywords = new ArrayList<>();
for (KeywordDto dto : keywordDtos) {
Keyword keyword = new Keyword();
keyword.setOptionTitle(dto.getOptionTitle());
keyword.setUser(user);
keywords.add(keyword);
}
return keywords;
Expand Down Expand Up @@ -138,4 +144,13 @@ private List<PlaceDto> convertToPlaceDtos(List<Place> places) {
return placeDtos;
}

public List<KeywordDto> getKeywordList(Long userId) {
List<Keyword> keywords = keywordRespository.findAllByUserId(userId);
List<KeywordDto> keywordDtos = keywords.stream()
.map(keyword -> new KeywordDto(keyword.getOptionTitle()))
.collect(Collectors.toList());

return keywordDtos;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
public class GptController {
private final ChatService chatService;

@GetMapping("/result")
@Operation(summary = "음성인식/텍스트 ChatGPT로 전달하여 키워드 추출", description = "음성인식/텍스트를 통해 ChatGPT 검색을 실행합니다. " + "검색 결과를 List로 반환하여 Keyword에 저장합니다.")
@GetMapping("/result/{userId}")
@Operation(summary = "음성인식/텍스트 ChatGPT로 전달하여 키워드 추출", description = "음성인식/텍스트를 통해 ChatGPT 검색을 실행합니다. " + "검색 결과를 List로 반환하여 Keyword와 pathvariable을 저장합니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "GET_SUCCESS", description = "조회 성공"),
})
public ResponseEntity<ApiResponse<List<KeywordDto>>> createGptComment(@RequestParam String content) {
List<KeywordDto> keywordDtos = chatService.createGptComment(content);
public ResponseEntity<ApiResponse<List<KeywordDto>>> createGptComment(@RequestParam(name = "content") String content, @PathVariable(name = "userId")Long userId) {
List<KeywordDto> keywordDtos = chatService.createGptComment(content, userId);
ApiResponse<List<KeywordDto>> apiResponse = new ApiResponse<>(ApiResponseStatus.GET_SUCCESS, keywordDtos);
return ResponseEntity.ok(apiResponse);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.hackerton.demo.domain.keyword;

import com.hackerton.demo.domain.common.ApiResponse;
import com.hackerton.demo.domain.common.ApiResponseStatus;
import io.swagger.v3.oas.annotations.Operation;

import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/keyword")
public class KeywordController {
private final ChatService chatService;

@GetMapping("/{userId}")
@Operation(summary = "해당 유저의 오늘의 키워드 조회 ", description = "pathvariable로 들어오는 유저의 키워드를 조회합니다."
+ "토큰 미구현으로 1~6번 인덱스 유저 조회 가능.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "GET_SUCCESS", description = "조회 성공"),
})
public ResponseEntity<ApiResponse<List<KeywordDto>>> getKeyword(@PathVariable(name = "userId") Long userId) {
List<KeywordDto> keywordDtos = chatService.getKeywordList(userId);
ApiResponse<List<KeywordDto>> apiResponse = new ApiResponse<>(ApiResponseStatus.GET_SUCCESS, keywordDtos);
return ResponseEntity.ok(apiResponse);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.hackerton.demo.domain.keyword;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface KeywordRespository extends JpaRepository<Keyword, Long> {

List<Keyword> findAllByUserId(Long userId);
}
Loading