-
Notifications
You must be signed in to change notification settings - Fork 1
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 #76 from lotteon2/develop
Develop
- Loading branch information
Showing
14 changed files
with
427 additions
and
52 deletions.
There are no files selected for viewing
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
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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/dailyon/productservice/common/config/RestTemplateConfig.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,15 @@ | ||
package com.dailyon.productservice.common.config; | ||
|
||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/main/java/com/dailyon/productservice/common/feign/client/OpenAIClient.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,98 @@ | ||
package com.dailyon.productservice.common.feign.client; | ||
|
||
import com.dailyon.productservice.brand.dto.response.ReadBrandListResponse; | ||
import com.dailyon.productservice.brand.dto.response.ReadBrandResponse; | ||
import com.dailyon.productservice.brand.repository.BrandRepository; | ||
import com.dailyon.productservice.category.dto.response.ReadChildrenCategoryListResponse; | ||
import com.dailyon.productservice.category.dto.response.ReadChildrenCategoryResponse; | ||
import com.dailyon.productservice.category.repository.CategoryRepository; | ||
import com.dailyon.productservice.common.enums.Gender; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class OpenAIClient { | ||
|
||
private final Environment environment; | ||
private final RestTemplate restTemplate; | ||
private final ObjectMapper objectMapper; | ||
private final BrandRepository brandRepository; | ||
private final CategoryRepository categoryRepository; | ||
|
||
public String getSearchResults(String searchQuery) throws Exception { | ||
List<ReadBrandResponse> brands = ReadBrandListResponse | ||
.fromEntity(brandRepository.findAll()) | ||
.getBrandResponses(); | ||
|
||
String allBrands = brands.stream() | ||
.map(ReadBrandResponse::toString) | ||
.collect(Collectors.joining(",")); | ||
|
||
List<ReadChildrenCategoryResponse> categories = ReadChildrenCategoryListResponse | ||
.fromEntity(categoryRepository.findLeafCategories()) | ||
.getCategoryResponses(); | ||
|
||
String allCategories = categories.stream() | ||
.map(ReadChildrenCategoryResponse::toString) | ||
.collect(Collectors.joining(",")); | ||
|
||
Gender[] genders = Gender.values(); | ||
String genderStrings = Arrays.toString(genders); | ||
|
||
List<Map<String, Object>> messages = new ArrayList<>(); | ||
Map<String, Object> message = new HashMap<>(); | ||
message.put("role", "user"); | ||
message.put("content", createPrompt(searchQuery, allBrands, allCategories, genderStrings)); | ||
messages.add(message); | ||
|
||
Map<String, Object> requestData = new HashMap<>(); | ||
requestData.put("model", "gpt-3.5-turbo-1106"); | ||
requestData.put("messages", messages); | ||
requestData.put("temperature", 0.3); | ||
requestData.put("max_tokens", 300); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.setBearerAuth(environment.getProperty("open-ai.secret-key")); | ||
|
||
String requestBody = objectMapper.writeValueAsString(requestData); | ||
|
||
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers); | ||
|
||
String apiEndpoint = "https://api.openai.com/v1/chat/completions"; | ||
Object result = restTemplate.postForObject(apiEndpoint, entity, Object.class); | ||
return objectMapper.writeValueAsString(result); | ||
} | ||
|
||
private String createPrompt(String searchQuery, String brands, String categories, String genders) { | ||
return "{" + | ||
"\"categories\": [" + categories + "], " + | ||
"\"brands\": [" + brands + "], " + | ||
"\"genders\": " + genders + ", " + | ||
"\"priceRanges\": [" + | ||
"{\"id\": 1, \"name\": \"$0-$99\"}, " + | ||
"{\"id\": 2, \"name\": \"$100-$199\"}, " + | ||
"{\"id\": 3, \"name\": \"$200-$299\"}, " + | ||
"{\"id\": 4, \"name\": \"$300-$399\"}, " + | ||
"{\"id\": 5, \"name\": \"over $400\"}" + | ||
"], " + | ||
"Search Query: \"" + searchQuery + "\"." + | ||
"Based on the search query, let me know the relevant 3 categories, 3 brands, 1 gender, and 1 price range. " + | ||
"Please provide the answer in the json object format. " + | ||
"{\"categories\":[{\"id\":1, \"name\":\"Fashion\"}, {\"id\":2, \"name\":\"Electronics\"}, {\"id\":3, \"name\": \"Home & Living\"}], " + | ||
"\"brands\":[{\"id\":1, \"name\":\"Nike\"}, {\"id\":2, \"name\":\"Samsung\"}, {\"id\":3, \"name\":\"Apple\"}], " + | ||
"\"genders\":[\"MALE\"], " + | ||
"\"priceRanges\":[{\"id\":1, \"name\":\"$0-$99\"}]}" + | ||
"}"; | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
src/main/java/com/dailyon/productservice/common/feign/response/OpenAIResponse.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,127 @@ | ||
package com.dailyon.productservice.common.feign.response; | ||
|
||
import com.dailyon.productservice.brand.entity.Brand; | ||
import com.dailyon.productservice.category.entity.Category; | ||
import com.dailyon.productservice.common.enums.Gender; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.*; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class OpenAIResponse { | ||
private String id; | ||
private String object; | ||
private Long created; | ||
private String model; | ||
private List<Choice> choices; | ||
private Usage usage; | ||
@JsonProperty("system_fingerprint") | ||
private String systemFingerprint; | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Choice { | ||
private int index; | ||
private Message message; | ||
private Object logprobs; | ||
@JsonProperty("finish_reason") | ||
private String finishReason; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Message { | ||
private String role; | ||
private String content; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Content { | ||
private List<ReadChildrenCategoryResponse> categories; | ||
private List<ReadBrandResponse> brands; | ||
private List<Gender> genders; | ||
private List<PriceRange> priceRanges; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class ReadChildrenCategoryResponse { | ||
private Long id; | ||
private String name; | ||
|
||
@Override | ||
public String toString() { | ||
return "{'id':" + this.id + ", 'name':" + "'" + this.name + "'" +"}"; | ||
} | ||
|
||
public static ReadChildrenCategoryResponse fromEntity(Category category) { | ||
return ReadChildrenCategoryResponse.builder() | ||
.id(category.getId()) | ||
.name(category.getName()) | ||
.build(); | ||
} | ||
} | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class ReadBrandResponse { | ||
private Long id; | ||
private String name; | ||
|
||
@Override | ||
public String toString() { | ||
return "{'id':" + this.id + ", 'name':" + "'" + this.name + "'" +"}"; | ||
} | ||
|
||
public static ReadBrandResponse fromEntity(Brand brand) { | ||
return ReadBrandResponse.builder() | ||
.id(brand.getId()) | ||
.name(brand.getName()) | ||
.build(); | ||
} | ||
} | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class PriceRange { | ||
private Long id; | ||
private String name; | ||
} | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class Usage { | ||
@JsonProperty("prompt_tokens") | ||
private Long promptTokens; | ||
|
||
@JsonProperty("completion_tokens") | ||
private Long completionTokens; | ||
|
||
@JsonProperty("total_tokens") | ||
private Long totalTokens; | ||
} | ||
} |
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
Oops, something went wrong.