Skip to content

Commit

Permalink
fix: 검색 프롬프트 변경:
Browse files Browse the repository at this point in the history
  • Loading branch information
nowgnas committed Jan 23, 2024
1 parent d825bc0 commit f7a3017
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import kr.bb.product.domain.product.entity.Product;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.mapper.ProductCommand;
import kr.bb.product.domain.product.mapper.ProductCommand.SearchData;
import kr.bb.product.domain.product.mapper.ProductCommand.SelectOption;
import kr.bb.product.domain.product.mapper.ProductCommand.SortOption;
import lombok.AccessLevel;
Expand Down Expand Up @@ -288,13 +289,28 @@ public Page<Product> findProductsForAdmin(
}

@Override
public Page<Product> findProductsByFlowerId(Long flowerId, Pageable pageable) {
public Page<Product> findProductsByFlowerId(SearchData searchData, Pageable pageable) {
Query query =
Query.query(
Criteria.where("product_flowers.flowerId")
.is(flowerId)
.is(searchData.getFlowerId())
.and("product_sale_status")
.is("SALE"));

if (searchData.getSort() != null) {
if ("gt".equals(searchData.getSort()))
query.addCriteria(Criteria.where("productPrice").gt(searchData.getMoney()));
if ("gte".equals(searchData.getSort()))
query.addCriteria(Criteria.where("productPrice").gte(searchData.getMoney()));
if ("lt".equals(searchData.getSort()))
query.addCriteria(Criteria.where("productPrice").lt(searchData.getMoney()));
if ("lte".equals(searchData.getSort()))
query.addCriteria(Criteria.where("productPrice").lte(searchData.getMoney()));
}

if (searchData.getCategory() != null)
query.addCriteria(Criteria.where("category.categoryId").is(searchData.getCategory()));

query.with(Sort.by(Order.desc("createdAt")));
query.with(pageable);
List<Product> products = mongoTemplate.find(query, Product.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import kr.bb.product.domain.product.mapper.ProductCommand.ProductListItem;
import kr.bb.product.domain.product.mapper.ProductCommand.ProductsForAdmin;
import kr.bb.product.domain.product.mapper.ProductCommand.RepresentativeFlowerId;
import kr.bb.product.domain.product.mapper.ProductCommand.SearchData;
import kr.bb.product.domain.product.mapper.ProductCommand.SelectOption;
import kr.bb.product.domain.product.mapper.ProductCommand.SortOption;
import kr.bb.product.domain.product.mapper.ProductCommand.StoreManagerSubscriptionProduct;
Expand Down Expand Up @@ -374,42 +375,63 @@ public ProductsForAdmin getProductsForAdmin(

@Override
public ProductList searchByUser(String sentence, Pageable pageable) {
Long flowerId = getFlowerId(sentence);
SearchData flowerId = getFlowerId(sentence);
Page<Product> products = productQueryOutPort.findProductsByFlowerId(flowerId, pageable);
List<ProductListItem> product = getProduct(products);
return ProductList.getData(product, products.getTotalElements());
}

@Override
public ProductList searchByUser(Long userId, String sentence, Pageable pageable) {
Long flowerId = getFlowerId(sentence);
Page<Product> products = productQueryOutPort.findProductsByFlowerId(flowerId, pageable);
ProductCommand.SearchData searchData = getFlowerId(sentence);

Page<Product> products = productQueryOutPort.findProductsByFlowerId(searchData, pageable);
List<ProductListItem> product = getProduct(products);
List<String> ids = getProductIdsFromProducts(products.getContent());
List<String> data = getProductsIsLiked(userId, ids);
return ProductList.getData(product, data, products.getTotalElements());
}

@NotNull
private Long getFlowerId(String sentence) {
private ProductCommand.SearchData getFlowerId(String sentence) {
String regex = "[^0-9]";
String prompt = getPrompt(sentence);
String response = chatgptService.sendMessage(prompt);
String response = chatgptService.sendMessage(prompt).trim();
log.info("::::" + response + "::::");
String[] split = response.split(":");
Long flowerId = Long.valueOf(split[0].replaceAll(regex, ""));
String sort = "";
Long money = null;
Long category = null;
for (int i = 1; i < split.length; i++) {
if (split[i].contains("category")) {
category = Long.valueOf(split[i].replaceAll(regex, ""));
} else {
String[] strings = split[i].split("-");
sort = strings[0];
money = Long.valueOf(strings[1]);
}
}

return Long.valueOf(response.replaceAll(regex, ""));
return ProductCommand.SearchData.MakeSearchData(flowerId, sort, money, category);
}

private String getPrompt(String sentence) {
return "- Please choose one of several flowers. Types include "
+ "\n"
+ "red rose:1, white rose:2, orange rose:3, pink rose:4, blue rose:5, purple rose:6, yellow rose:7, lisianthus:8, hydrangea:9, lavender:10, chrysanthemum:11, sunflower:12, carnation:13, gerbera:14, freesia:15, tulip:16, ranunculus:17, gypsophila:18, statis:19, daisy:20, peony:21, delphinium:22 \n"
+ "- red rose-1, white rose-2, orange rose-3, pink rose-4, blue rose-5, purple rose-6, yellow rose-7, lisianthus-8, hydrangea-9, lavender-10, chrysanthemum-11, sunflower-12, carnation-13, gerbera-14, freesia-15, tulip-16, ranunculus-17, gypsophila-18, statis-19, daisy-20, peony-21, delphinium-22\n"
+ "- The format is flowername:flowerid and only one flowerId is responded\n"
+ "- Just choose the flower whose language is most related to this sentence\n"
+ "sentence:"
+ "\""
+ sentence
+ "\""
+ "\n"
+ "- response must be number like 'red rose:1'";
+ "- if sentence contains about money add response"
+ "- if sentence contains about money sorting conditions add response gt or lt or gte or lte"
+ "- if sentence contains if \"꽃다발\" add response category-1. if \"꽃바구니\" add response category-2, if \"꽃상자\" add response category-3, if \"화환\" add response category-4 "
+ "- no explanation just strict response format "
+ "- response format must be like 'red rose-1:gt-50000:category-1'";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import kr.bb.product.domain.product.entity.Product;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.mapper.ProductCommand;
import kr.bb.product.domain.product.mapper.ProductCommand.SearchData;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

Expand Down Expand Up @@ -48,5 +49,5 @@ Page<Product> findStoreProducts(
Page<Product> findProductsForAdmin(
ProductCommand.AdminSelectOption adminSelectOption, Pageable pageable);

Page<Product> findProductsByFlowerId(Long flowerId, Pageable pageable);
Page<Product> findProductsByFlowerId(SearchData flowerId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -712,4 +712,24 @@ public static ProductsForAdmin getData(
.build();
}
}

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class SearchData {
private Long flowerId;
private String sort;
private Long money;
private Long category;

public static SearchData MakeSearchData(Long flowerId, String sort, Long money, Long category) {
return SearchData.builder()
.flowerId(flowerId)
.sort(sort)
.money(money)
.category(category)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.mapper.ProductCommand.AdminSelectOption;
import kr.bb.product.domain.product.mapper.ProductCommand.RepresentativeFlowerId;
import kr.bb.product.domain.product.mapper.ProductCommand.SearchData;
import kr.bb.product.domain.product.mapper.ProductCommand.SortOption;
import kr.bb.product.domain.tag.entity.Tag;
import kr.bb.product.exception.errors.ProductNotFoundException;
Expand Down Expand Up @@ -360,7 +361,8 @@ void findProductsByFlowerId() {
productMongoRepository.save(product);
} // 10개 상품 저장
PageRequest pageRequest = PageRequest.of(0, 7);
Page<Product> productsByFlowerId = productQueryOutPort.findProductsByFlowerId(1L, pageRequest);
Page<Product> productsByFlowerId =
productQueryOutPort.findProductsByFlowerId(SearchData.builder().build(), pageRequest);
assertThat(productsByFlowerId.getContent().size()).isEqualTo(10);
}

Expand Down

0 comments on commit f7a3017

Please sign in to comment.