File size: 2,039 Bytes
0fc3485
 
 
 
 
 
 
 
 
 
48ab112
 
0fc3485
 
 
 
 
48ab112
 
0fc3485
48ab112
 
 
 
 
0fc3485
48ab112
0fc3485
48ab112
 
 
 
0fc3485
 
 
 
48ab112
 
0fc3485
 
 
 
 
 
 
 
 
48ab112
0fc3485
 
48ab112
0fc3485
 
 
48ab112
 
 
 
0fc3485
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import json
from groq import Groq
from dotenv import load_dotenv

load_dotenv()

class WriterAgent:
    def __init__(self):
        self.api_key = os.getenv("GROQ_API_KEY")
        self.client = Groq(api_key=self.api_key) if self.api_key else None
        self.model = "llama-3.3-70b-versatile"

    def write_listing(self, visual_data: dict, seo_keywords: list) -> dict:
        if not self.client:
            return {"error": "No API Key"}

        system_prompt = """You are a professional copywriter.
        Write a JSON product listing.
        
        RULES:
        1. Output strictly valid JSON.
        2. "description": Single paragraph, plain text, no newlines.
        3. "title": Concise SEO title.
        4. "features": List of 3 distinct features.
        
        Example Output:
        {
            "title": "Classic Leather Jacket",
            "description": "A timeless piece crafted from premium leather.",
            "features": ["Genuine Leather", "Slim Fit", "Zip Closure"],
            "price_estimate": "$100-$150"
        }
        """

        user_content = f"""
        DATA: {json.dumps(visual_data)}
        KEYWORDS: {', '.join(seo_keywords)}
        """

        try:
            completion = self.client.chat.completions.create(
                model=self.model,
                messages=[
                    {"role": "system", "content": system_prompt},
                    {"role": "user", "content": user_content}
                ],
                temperature=0.1,
                response_format={"type": "json_object"}
            )
            return json.loads(completion.choices[0].message.content)
        except Exception as e:
            print(f"❌ Writer Error: {e}")
            return {
                "title": visual_data.get('product_type', 'Product Name'),
                "description": "High-quality fashion item matching your style.",
                "features": visual_data.get('visual_features', []),
                "price_estimate": "$50-$100"
            }