lead-qualifier-api / ai_engine.py
Wall06's picture
Upload folder using huggingface_hub
56a8c27 verified
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."
}