Skip to content

Commit

Permalink
Merge pull request #18 from ssafy-19-final-pjt/feature/#17-openai-ser…
Browse files Browse the repository at this point in the history
…vice

feat : openAi 기반 request 추가
  • Loading branch information
chwangmin authored May 22, 2024
2 parents 9c76ca0 + 69cdca1 commit 2269628
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 0 deletions.
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));
}
}
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;
}
}
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;
}
}
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());
}
}
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));
}
}
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;
}
}
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;
}
}
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;
}
}

0 comments on commit 2269628

Please sign in to comment.