47 lines
2.3 KiB
Python
47 lines
2.3 KiB
Python
import os
|
|
import json
|
|
from openai import OpenAI
|
|
from agents.deals import Deal, QualityDealSelection
|
|
|
|
class ScannerAgent:
|
|
|
|
MODEL = "gpt-4o-mini"
|
|
|
|
SYSTEM_PROMPT = """You identify and summarize the 5 most detailed deals from a list, by selecting deals that have the most detailed, high quality description and the most clear price.
|
|
Respond strictly in JSON with no explanation, using this format. You should provide the price as a number derived from the description. If the price of a deal isn't clear, do not include that deal in your response.
|
|
Most important is that you respond with the 5 deals that have the most detailed product description with price. It's not important to mention the terms of the deal; most important is a thorough description of the product.
|
|
|
|
{"quality_deals": [
|
|
{
|
|
"product_description": "Your clearly expressed summary of the product in 4-5 sentences. Details of the item are much more important than why it's a good deal. Avoid mentioning discounts and coupons; focus on the item itself. There should be a paragpraph of text for each item you choose.",
|
|
"price": 99.99,
|
|
"url": "the url as provided"
|
|
},
|
|
...
|
|
]}"""
|
|
|
|
USER_PROMPT_PREFIX = """Respond with the most promising 5 deals from this list, selecting those which have the most detailed, high quality product description and a clear price.
|
|
Respond strictly in JSON, and only JSON. You should rephrase the description to be a summary of the product itself, not the terms of the deal.
|
|
Remember to respond with a paragraph of text in the product_description field for each of the 5 items that you select.
|
|
|
|
Deals:
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.openai = OpenAI()
|
|
|
|
def scan(self) -> QualityDealSelection:
|
|
deals = Deal.fetch()
|
|
user_prompt = self.USER_PROMPT_PREFIX + '\n\n'.join([deal.describe() for deal in deals])
|
|
completion = self.openai.beta.chat.completions.parse(
|
|
model=self.MODEL,
|
|
messages=[
|
|
{"role": "system", "content": self.SYSTEM_PROMPT},
|
|
{"role": "user", "content": user_prompt}
|
|
],
|
|
response_format=QualityDealSelection
|
|
)
|
|
result = completion.choices[0].message.parsed
|
|
return result
|