-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…vice feat : openAi 기반 request 추가
- Loading branch information
Showing
8 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/com/ssafy/home/domain/openai/controller/ChatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.ssafy.home.domain.openai.controller; | ||
|
||
import com.ssafy.home.domain.openai.dto.OpenAIParam; | ||
import com.ssafy.home.domain.openai.dto.OpenAIResponseDto; | ||
import com.ssafy.home.domain.openai.service.ChatService; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "OpenAi", description = "openai 기반 응답 서비스") | ||
@RestController | ||
@RequestMapping("/chat") | ||
@RequiredArgsConstructor | ||
public class ChatController { | ||
private final ChatService chatService; | ||
|
||
@GetMapping("") | ||
public ResponseEntity<OpenAIResponseDto> chat(@ModelAttribute OpenAIParam openAIParam){ | ||
return ResponseEntity.ok(chatService.chat(openAIParam)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/com/ssafy/home/domain/openai/dto/OpenAIParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.ssafy.home.domain.openai.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class OpenAIParam { | ||
String city; | ||
String dong; | ||
|
||
public OpenAIParam(String city, String dong){ | ||
this.city = city; | ||
this.dong = dong; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/ssafy/home/domain/openai/dto/OpenAIResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.ssafy.home.domain.openai.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class OpenAIResponseDto { | ||
private final String comment; | ||
|
||
public OpenAIResponseDto(String comment){ | ||
this.comment = comment; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/com/ssafy/home/domain/openai/service/ChatService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.ssafy.home.domain.openai.service; | ||
|
||
import com.ssafy.home.domain.openai.dto.OpenAIParam; | ||
import com.ssafy.home.domain.openai.dto.OpenAIResponseDto; | ||
import com.ssafy.home.external.openai.dto.request.ChatRequest; | ||
import com.ssafy.home.external.openai.dto.response.ChatResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Slf4j | ||
@Service | ||
public class ChatService { | ||
@Qualifier("openaiRestTemplate") | ||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
@Value("${openai.model}") | ||
private String model; | ||
|
||
@Value("${openai.api.url}") | ||
private String apiUrl; | ||
|
||
public OpenAIResponseDto chat(OpenAIParam openAIParam) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(openAIParam.getCity()).append(" ").append(openAIParam.getDong()).append("의 ").append("근처 인기있는 편의시설을 주소, 특징들을 5가지 알려줄래?"); | ||
ChatRequest request = new ChatRequest(model, sb.toString()); | ||
ChatResponse response = restTemplate.postForObject(apiUrl, request, ChatResponse.class); | ||
|
||
if(response==null || response.getChoices() == null || response.getChoices().isEmpty()){ | ||
return new OpenAIResponseDto("No Response"); | ||
} | ||
return new OpenAIResponseDto(response.getChoices().get(0).getMessage().getContent()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/com/ssafy/home/external/openai/dto/request/ChatRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ssafy.home.external.openai.dto.request; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Getter | ||
public class ChatRequest { | ||
private String model; | ||
private List<Message> messages; | ||
private int n; | ||
// private double temperature; | ||
|
||
public ChatRequest(String model, String prompt) { | ||
this.model = model; | ||
|
||
this.n = 1; | ||
this.messages = new ArrayList<>(); | ||
this.messages.add(new Message("user", prompt)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/com/ssafy/home/external/openai/dto/request/Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.ssafy.home.external.openai.dto.request; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class Message { | ||
private String role; | ||
private String content; | ||
|
||
public Message(String role, String content) { | ||
this.role = role; | ||
this.content = content; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/com/ssafy/home/external/openai/dto/response/ChatResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.ssafy.home.external.openai.dto.response; | ||
|
||
import com.ssafy.home.external.openai.dto.request.Message; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class ChatResponse { | ||
private List<Choice> choices; | ||
|
||
@Getter | ||
public static class Choice{ | ||
private int index; | ||
private Message message; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/com/ssafy/home/global/config/openai/OpenAIRestTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.ssafy.home.global.config.openai; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class OpenAIRestTemplateConfig { | ||
|
||
@Value("${openai.api.key}") | ||
private String openAiApiKey; | ||
|
||
@Bean | ||
@Qualifier("openaiRestTemplate") | ||
public RestTemplate openAiRestTemplate() { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
restTemplate.getInterceptors().add((request, body, execution) -> { | ||
request.getHeaders().add("Authorization", "Bearer " + openAiApiKey); | ||
return execution.execute(request, body); | ||
}); | ||
return restTemplate; | ||
} | ||
} |