|
import os |
|
import requests |
|
|
|
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions" |
|
GROQ_API_KEY = os.getenv("GROQ_API_KEY") |
|
|
|
if not GROQ_API_KEY: |
|
raise ValueError("GROQ_API_KEY is missing! Set it in Hugging Face Secrets.") |
|
|
|
|
|
def load_prompt(filename): |
|
with open(f'config/system_prompts/{filename}', 'r') as file: |
|
return file.read() |
|
|
|
|
|
def call_groq(system_prompt, user_input): |
|
headers = {"Authorization": f"Bearer {GROQ_API_KEY}"} |
|
payload = { |
|
"model": "mixtral-8x7b-32768", |
|
"messages": [ |
|
{"role": "system", "content": system_prompt}, |
|
{"role": "user", "content": user_input} |
|
] |
|
} |
|
print("Sending payload to Groq API...") |
|
print(payload) |
|
response = requests.post(GROQ_API_URL, json=payload, headers=headers) |
|
print("Groq API response status:", response.status_code) |
|
print("Groq API response body:", response.text) |
|
|
|
if response.status_code != 200: |
|
raise Exception(f"Groq API error: {response.status_code} - {response.text}") |
|
|
|
data = response.json() |
|
if 'choices' not in data or not data['choices']: |
|
raise Exception("Groq API returned no choices.") |
|
|
|
return data['choices'][0]['message']['content'] |
|
|
|
def ad_copy_agent(product, description, audience, tone): |
|
system_prompt = load_prompt("ad_copy_prompt.txt") |
|
user_input = f"Product: {product}\nDescription: {description}\nAudience: {audience}\nTone: {tone}" |
|
return call_groq(system_prompt, user_input) |
|
|
|
def sentiment_agent(social_data): |
|
system_prompt = load_prompt("sentiment_prompt.txt") |
|
return call_groq(system_prompt, social_data) |
|
|
|
def timing_agent(platform): |
|
system_prompt = load_prompt("timing_prompt.txt") |
|
return call_groq(system_prompt, f"Platform: {platform}") |