Skip to content

Commit

Permalink
Merge pull request #4 from Team-Clody/Feat/#3
Browse files Browse the repository at this point in the history
Feat : Custom property Source를 추가합니다.
  • Loading branch information
Yangdaehan authored Jul 16, 2024
2 parents 4ab8d08 + e4a3062 commit 7894858
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/sopt/gptapi/GptApiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@


import jakarta.annotation.PostConstruct;
import org.sopt.gptapi.config.PromptProperty;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
//@EnableR2dbcRepositories(basePackages = "org.sopt.gptapi.domain")
@EnableConfigurationProperties(PromptProperty.class)
@AutoConfigureBefore({ DataSourceAutoConfiguration.class, SqlInitializationAutoConfiguration.class })
public class GptApiApplication {

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/sopt/gptapi/config/PromptProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.sopt.gptapi.config;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Setter
@Getter
@Component
@ConfigurationProperties(prefix = "app")
@EnableConfigurationProperties(PromptProperty.class)
@PropertySource(value = "classpath:prompt.yml", factory = YamlPropertySourceFactory.class)
@Configuration
public class PromptProperty {

private String prompt;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.sopt.gptapi.config;

import io.micrometer.common.lang.Nullable;
import java.util.Objects;
import java.util.Properties;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

public class YamlPropertySourceFactory implements PropertySourceFactory {

@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource){
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());

Properties properties = factory.getObject();
return new PropertiesPropertySource(resource.getResource().getFilename(), properties);
}
}
16 changes: 0 additions & 16 deletions src/main/java/org/sopt/gptapi/config/YmlPropertyUtil.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void onMessage(Message message, byte[] pattern) {

CompletableFuture<String> future = chatService.getChatResponse(listenedMessage.message(), listenedMessage.userId(),
listenedMessage.date().toString()).toFuture();

future.join();
} finally {
redisLockService.unlock(lockKey);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/sopt/gptapi/service/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.sopt.gptapi.config.YmlPropertyUtil;
import org.sopt.gptapi.config.PromptProperty;
import org.sopt.gptapi.service.reply.ReplyService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClientResponseException.BadRequest;
import reactor.core.publisher.Mono;
Expand All @@ -17,10 +18,10 @@ public class ChatService {
public static final Logger logger = LoggerFactory.getLogger(ChatService.class);
private final AsyncChatgptService asyncChatgptService;
private final ReplyService replyService;
private final YmlPropertyUtil ymlPropertyUtil;
private final PromptProperty promptProperty;

public Mono<String> getChatResponse(String content, Long userId, String createdDate) {
String message = ymlPropertyUtil.getProperty("prompt")+ content + "\n칭찬 :";
String message = promptProperty.getPrompt() + content + "\n칭찬 :";
return asyncChatgptService.sendMessage(message)
.flatMap(response -> {
logger.info("ChatGPT response: {}", response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public record DiaryListenedMessage(
String message,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
LocalDateTime date
) {
) {

}

0 comments on commit 7894858

Please sign in to comment.