-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from FTHON-6TEAM/main
main 브랜치의 작업내용을 test 브랜치로 병합합니다.
- Loading branch information
Showing
23 changed files
with
546 additions
and
65 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/com/challenger/challengerbe/config/OpenAiConfig.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,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; | ||
} | ||
} |
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
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
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
42 changes: 42 additions & 0 deletions
42
...ava/com/challenger/challengerbe/modules/answer/openai/controller/OpenAiAPIController.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,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(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/challenger/challengerbe/modules/answer/openai/domain/Choice.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,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; | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/challenger/challengerbe/modules/answer/openai/domain/Content.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,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; | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/challenger/challengerbe/modules/answer/openai/domain/ImageContent.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,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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/challenger/challengerbe/modules/answer/openai/domain/ImageMessage.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,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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/challenger/challengerbe/modules/answer/openai/domain/ImageUrl.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,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; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/challenger/challengerbe/modules/answer/openai/domain/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,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; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/challenger/challengerbe/modules/answer/openai/domain/TextContent.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,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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/challenger/challengerbe/modules/answer/openai/domain/TextMessage.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,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; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/challenger/challengerbe/modules/answer/openai/dto/ChatGPTRequest.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,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(); | ||
} | ||
} |
Oops, something went wrong.