Skip to content

Commit

Permalink
Merge pull request #38 from FTHON-6TEAM/main
Browse files Browse the repository at this point in the history
main 브랜치의 작업내용을 test 브랜치로 병합합니다.
  • Loading branch information
rhkdgur authored Sep 12, 2024
2 parents e332f70 + 08301af commit e48efa1
Show file tree
Hide file tree
Showing 23 changed files with 546 additions and 65 deletions.
36 changes: 36 additions & 0 deletions src/main/java/com/challenger/challengerbe/config/OpenAiConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.challenger.challengerbe.config;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

/**
* packageName : com.challenger.challengerbe.config
* fileName : OpenAiConfig
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/
@Configuration
public class OpenAiConfig {
@Value("${spring.ai.openai.api-key}")
private String openAiApiKey;

@Bean
public RestTemplate template() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add((request, body, execution) -> {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.getHeaders().set("Authorization", "Bearer " + openAiApiKey);
return execution.execute(request, body);
});
return restTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public SecurityFilterChain securityFilterChain(
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(
List.of("http://localhost:5173", "http://localhost:5174", "http://localhost:4173"));
List.of("http://localhost:3000"));
config.setAllowedMethods(
List.of("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
config.setAllowCredentials(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers)
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:5173","http://localhost:8080")
.allowedOrigins("http://localhost:3000","http://localhost:8080")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("Authorization", "Content-Type", "Bearer", "credentials")
.allowCredentials(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AsyncAnswerCreateDto {
private Long questionIdx;
private String questionContent;
private String content;
private Long fileIdx;

@Builder
public AsyncAnswerCreateDto(String userIdk, Long questionIdx, String questionContent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.challenger.challengerbe.modules.answer.openai.controller;

import com.challenger.challengerbe.modules.answer.openai.service.AiCallService;
import com.challenger.challengerbe.modules.answer.openai.dto.ChatGPTResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : OpenAiAPIController
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class OpenAiAPIController {
private final AiCallService aiCallService;

@PostMapping("/image")
public String imageAnalysis(@RequestParam MultipartFile image, @RequestParam String requestText)
throws IOException {
ChatGPTResponse response = aiCallService.requestImageAnalysis(image, requestText);
return response.getChoices().get(0).getMessage().getContent();
}

@PostMapping("/text")
public String textAnalysis(@RequestParam String requestText) {
ChatGPTResponse response = aiCallService.requestTextAnalysis(requestText);
return response.getChoices().get(0).getMessage().getContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.challenger.challengerbe.modules.answer.openai.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : Choice
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Choice {
private int index;
private TextMessage message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.challenger.challengerbe.modules.answer.openai.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : Content
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Content {
private String type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.challenger.challengerbe.modules.answer.openai.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : ImageContent
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/

@Getter
@Setter
@AllArgsConstructor
public class ImageContent extends Content {
private ImageUrl image_url;

public ImageContent(String type, ImageUrl image_url) {
super(type);
this.image_url = image_url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.challenger.challengerbe.modules.answer.openai.domain;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : ImageMessage
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/
@Getter
@Setter
@AllArgsConstructor
public class ImageMessage extends Message {
private List<Content> content;

public ImageMessage(String role, List<Content> content) {
super(role);
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.challenger.challengerbe.modules.answer.openai.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : ImageUrl
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/
@Getter
@Setter
@AllArgsConstructor
public class ImageUrl {
private String url;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.challenger.challengerbe.modules.answer.openai.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : Message
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public abstract class Message {
private String role;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.challenger.challengerbe.modules.answer.openai.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : TextContent
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/
@Getter
@Setter
@AllArgsConstructor
public class TextContent extends Content {
private String text;

public TextContent(String type, String text) {
super(type);
this.text = text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.challenger.challengerbe.modules.answer.openai.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : TextMessage
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class TextMessage extends Message {
private String content;

public TextMessage(String role, String content) {
super(role);
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.challenger.challengerbe.modules.answer.openai.dto;

import com.challenger.challengerbe.modules.answer.openai.domain.ImageContent;
import com.challenger.challengerbe.modules.answer.openai.domain.ImageMessage;
import com.challenger.challengerbe.modules.answer.openai.domain.ImageUrl;
import com.challenger.challengerbe.modules.answer.openai.domain.Message;
import com.challenger.challengerbe.modules.answer.openai.domain.TextContent;
import com.challenger.challengerbe.modules.answer.openai.domain.TextMessage;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Collections;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* packageName : com.challenger.challengerbe.modules.answer.openai
* fileName : ChatGPTRequest
* author : jongh
* date : 2024-09-12
* description :
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2024-09-12 jongh 최초 생성
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ChatGPTRequest {
@JsonProperty("model")
private String model;
@JsonProperty("messages")
private List<Message> messages;
@JsonProperty("max_tokens")
private int maxTokens;

public static ChatGPTRequest createImageRequest(String model, int maxTokens, String role, String requestText, String imageUrl) {
TextContent textContent = new TextContent("text", requestText);
ImageContent imageContent = new ImageContent("image_url", new ImageUrl(imageUrl));
Message message = new ImageMessage(role, List.of(textContent, imageContent));
return createChatGPTRequest(model, maxTokens, Collections.singletonList(message));
}

public static ChatGPTRequest createTextRequest(String model, int maxTokens, String role, String requestText) {
Message message = new TextMessage(role, requestText);
return createChatGPTRequest(model, maxTokens, Collections.singletonList(message));
}

private static ChatGPTRequest createChatGPTRequest(String model, int maxTokens, List<Message> messages) {
return ChatGPTRequest.builder()
.model(model)
.maxTokens(maxTokens)
.messages(messages)
.build();
}
}
Loading

0 comments on commit e48efa1

Please sign in to comment.