File size: 1,679 Bytes
334ff37
 
56a8c27
334ff37
56a8c27
 
334ff37
 
 
 
 
 
56a8c27
 
 
 
 
 
334ff37
56a8c27
 
 
334ff37
 
 
 
56a8c27
 
 
 
 
 
 
 
 
 
 
 
334ff37
 
 
56a8c27
334ff37
 
 
 
 
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
import os
import json
from groq import Groq

# Initialize the Groq client
client = Groq(api_key=os.getenv("GROQ_API_KEY"))

def analyze_lead(name: str, company: str, company_summary: str) -> dict:
    prompt = f"""
    You are a B2B sales expert. Analyze this lead.
    Lead: {name} at {company}. Info: {company_summary}
    
    1. Score the lead from 1 to 10 based on how likely they are to need AI software automation. 
       (E.g., outdated businesses = high score, modern tech = lower score).
    2. Write a 1-sentence reason for the score.
    3. Write a short, highly personalized 100-word cold email offering our AI services to them. Start with Hi {name},. End by asking for a 10-min chat.

    Output EXACTLY in this JSON format:
    {{
        "score": 8,
        "score_reason": "They have a lot of manual processes...",
        "cold_email": "Hi {name}..."
    }}
    """
    
    try:
        response = client.chat.completions.create(
            messages=[
                {"role": "system", "content": "You are an expert B2B sales qualifier. Output ONLY a valid JSON object."},
                {"role": "user", "content": prompt}
            ],
            model="llama3-70b-8192",
            response_format={"type": "json_object"}
        )
        
        # Parse the JSON returned by Llama 3
        result = json.loads(response.choices[0].message.content)
        return result
        
    except Exception as e:
        # Fallback if something goes wrong
        print(f"Error calling Groq: {e}")
        return {
            "score": 0, 
            "score_reason": f"AI Error: {str(e)}", 
            "cold_email": "Error generating email."
        }