-
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.
adding draft llm-based extractors (wheel count)
- Loading branch information
1 parent
caecad1
commit b243fe5
Showing
14 changed files
with
177 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.irw.hawk.scraper.ai; | ||
|
||
import com.theokanning.openai.completion.chat.ChatMessage; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Builder.Default; | ||
import lombok.Getter; | ||
import lombok.Value; | ||
|
||
@Value | ||
@Builder | ||
public class LlmQuery { | ||
|
||
List<ChatMessage> messages; | ||
|
||
@Default | ||
OpenAiModel model = OpenAiModel.GPT_3_5_TURBO; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum OpenAiModel { | ||
GPT_3_5_TURBO("gpt-3.5-turbo"), | ||
GPT_3_5_TURBO_16K("gpt-3.5-turbo-16k"), | ||
GPT_4_TURBO("gpt-4-1106-preview"); | ||
|
||
private String name; | ||
|
||
} | ||
|
||
} |
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
5 changes: 2 additions & 3 deletions
5
hawk/src/main/java/io/irw/hawk/scraper/service/extractors/ItemSummaryDataExtractor.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 |
---|---|---|
@@ -1,14 +1,13 @@ | ||
package io.irw.hawk.scraper.service.extractors; | ||
|
||
import com.ebay.buy.browse.model.ItemSummary; | ||
import io.irw.hawk.dto.merchandise.ProductVariantEnum; | ||
import io.irw.hawk.dto.ebay.EbayHighlightDto; | ||
import io.irw.hawk.scraper.model.ProcessingPipelineStep; | ||
|
||
public interface ItemSummaryDataExtractor extends ProcessingPipelineStep { | ||
|
||
boolean isApplicableTo(ProductVariantEnum productVariantEnum); | ||
boolean isApplicableTo(EbayHighlightDto highlightDto); | ||
|
||
void extractDataFromItemSummary(ItemSummary itemSummary, EbayHighlightDto highlightDto); | ||
void extractDataFromItem(ItemSummary itemSummary, EbayHighlightDto highlightDto); | ||
|
||
} |
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
44 changes: 44 additions & 0 deletions
44
hawk/src/main/java/io/irw/hawk/scraper/service/openai/LlmQueryService.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,44 @@ | ||
package io.irw.hawk.scraper.service.openai; | ||
|
||
import com.theokanning.openai.completion.CompletionRequest; | ||
import com.theokanning.openai.completion.chat.ChatCompletionRequest; | ||
import com.theokanning.openai.service.OpenAiService; | ||
import io.irw.hawk.scraper.ai.LlmQuery; | ||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.FieldDefaults; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class LlmQueryService { | ||
|
||
OpenAiProperties openAiProperties; | ||
|
||
public void chatCompletion(LlmQuery llmQuery) { | ||
OpenAiService service = new OpenAiService(openAiProperties.getToken()); | ||
CompletionRequest completionRequest = CompletionRequest.builder() | ||
.prompt(llmQuery.getPrompt()) | ||
.model(llmQuery.getModel().getName()) | ||
.echo(false) | ||
.build(); | ||
|
||
// TODO: return the result | ||
service.createCompletion(completionRequest).getChoices().forEach(System.out::println); | ||
} | ||
|
||
public void function(LlmQuery llmQuery) { | ||
OpenAiService service = new OpenAiService(openAiProperties.getToken()); | ||
ChatCompletionRequest completionRequest = ChatCompletionRequest.builder() | ||
.messages(llmQuery.getPrompt()) | ||
.model(llmQuery.getModel().getName()) | ||
.echo(false) | ||
.build(); | ||
service.createChatCompletion(completionRequest).getChoices().forEach(System.out::println); | ||
// TODO: return the result | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
hawk/src/main/java/io/irw/hawk/scraper/service/openai/OpenAiProperties.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,14 @@ | ||
package io.irw.hawk.scraper.service.openai; | ||
|
||
import lombok.Value; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "openai") | ||
@Value | ||
public class OpenAiProperties { | ||
|
||
private String token; | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...io/irw/hawk/scraper/service/processors/skates/parts/extractors/AiWheelCountExtractor.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,53 @@ | ||
package io.irw.hawk.scraper.service.processors.skates.parts.extractors; | ||
|
||
import static org.apache.commons.lang3.StringUtils.lowerCase; | ||
|
||
import com.ebay.buy.browse.model.ItemSummary; | ||
import com.theokanning.openai.completion.chat.ChatMessage; | ||
import com.theokanning.openai.completion.chat.ChatMessageRole; | ||
import io.irw.hawk.dto.ebay.EbayHighlightDto; | ||
import io.irw.hawk.scraper.ai.LlmQuery; | ||
import io.irw.hawk.scraper.model.ProcessingPipelineStep; | ||
import io.irw.hawk.scraper.service.extractors.PriceExtractor; | ||
import io.irw.hawk.scraper.service.openai.LlmQueryService; | ||
import java.util.List; | ||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.FieldDefaults; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | ||
@Slf4j | ||
public class AiWheelCountExtractor extends WheelCountExtractor { | ||
|
||
public static final String HOW_MANY_WHEELS_ARE_THERE = """ | ||
How many inline skate wheels are sold in this Ebay listing\s | ||
according to the listing description provided by the user?"""; | ||
LlmQueryService llmQueryService; | ||
|
||
@Override | ||
public List<Class<? extends ProcessingPipelineStep>> dependencyFor() { | ||
return List.of(PriceExtractor.class); | ||
} | ||
|
||
@Override | ||
public void extractDataFromItem(ItemSummary itemSummary, EbayHighlightDto highlightDto) { | ||
String title = highlightDto.getEbayFinding().getTitle().toLowerCase(); | ||
String shortDescription = lowerCase(highlightDto.getEbayFinding() | ||
.getItemDescription() | ||
.orElse("")); | ||
|
||
llmQueryService.chatCompletion(LlmQuery.builder() | ||
.messages(List.of( | ||
new ChatMessage(ChatMessageRole.SYSTEM.value(), HOW_MANY_WHEELS_ARE_THERE), | ||
new ChatMessage(ChatMessageRole.USER.value(), title + "\n" + shortDescription))) | ||
.build()); | ||
|
||
highlightDto.getEbayFinding().setNumberOfPieces(numberOfWheels); | ||
} | ||
|
||
|
||
} |
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