Skip to content

Commit

Permalink
Merge pull request #77 from lotteon2/feat/search-using-gpt
Browse files Browse the repository at this point in the history
[FEAT] query with gpt
  • Loading branch information
CokeLee777 authored Jan 24, 2024
2 parents 8e605d3 + 62e87b5 commit eb8f77f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import com.dailyon.productservice.category.dto.response.ReadChildrenCategoryResponse;
import com.dailyon.productservice.category.repository.CategoryRepository;
import com.dailyon.productservice.common.enums.Gender;
import com.dailyon.productservice.common.feign.response.OpenAIResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -23,7 +21,6 @@

@RequiredArgsConstructor
@Service
@Slf4j
public class OpenAIClient {

private final Environment environment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,23 @@ public ReadProductSearchResponse searchProducts(String query) {
String response = openAIClient.getSearchResults(query);
OpenAIResponse responseFromGpt = gson.fromJson(response, OpenAIResponse.class);
String jsonContent = responseFromGpt.getChoices().get(0).getMessage().getContent();
log.info("================================");
log.info(jsonContent);
log.info("================================");
OpenAIResponse.Content content = gson.fromJson(jsonContent, OpenAIResponse.Content.class);

// Use content object to search products
products = productService.searchAfterGpt(
content.getBrands().stream().map(OpenAIResponse.ReadBrandResponse::getId).collect(Collectors.toList()),
content.getCategories().stream().map(OpenAIResponse.ReadChildrenCategoryResponse::getId).collect(Collectors.toList()),
content.getGenders().get(0)
);
List<Long> brandIds = content.getBrands().stream()
.map(OpenAIResponse.ReadBrandResponse::getId)
.collect(Collectors.toList());

List<Long> categoryIds = content.getCategories().stream()
.map(OpenAIResponse.ReadChildrenCategoryResponse::getId)
.collect(Collectors.toList());

Gender gender = content.getGenders().get(0);

products = productService.searchAfterGpt(categoryIds, brandIds, gender);
} catch (Exception e) {
// Properly log and handle the exception as per your application's requirements
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ public List<Product> searchProducts(String query) {

@Override
public List<Product> searchAfterGpt(List<Long> categoryIds, List<Long> brandIds, Gender gender) {
return jpaQueryFactory.selectFrom(product)
return jpaQueryFactory.selectDistinct(product)
.from(product)
.leftJoin(product.brand, brand).fetchJoin()
.leftJoin(product.category, category).fetchJoin()
.where(product.deleted.eq(false)
.and(productTypeEq(ProductType.NORMAL))
.and(product.category.id.in(categoryIds)
.and(product.brand.id.in(brandIds))
.and(genderEq(gender))
)
.leftJoin(product.reviewAggregate, reviewAggregate).fetchJoin()
.where(product.brand.id.in(brandIds)
.and(product.category.id.in(categoryIds))
.and(product.type.eq(ProductType.NORMAL))
.and(product.gender.eq(gender))
)
.orderBy(orderSpecifier("createdAt", "desc"))
.fetch();
Expand Down

0 comments on commit eb8f77f

Please sign in to comment.