Nick: refactor openai swarm example

This commit is contained in:
Nicolas 2024-10-12 16:12:12 -03:00
parent c2d79e1842
commit 2eff7c29bb

View File

@ -19,38 +19,43 @@ def scrape_website(url):
) )
return scrape_status return scrape_status
def analyze_website_content(content): def generate_completion(role, task, content):
"""Analyze the scraped website content using OpenAI.""" """Generate a completion using OpenAI."""
response = client.chat.completions.create( response = client.chat.completions.create(
model="gpt-4o-mini", model="gpt-4o-mini",
messages=[ messages=[
{"role": "system", "content": "You are a marketing analyst. Analyze the following website content and provide key insights for marketing strategy."}, {"role": "system", "content": f"You are a {role}. {task}"},
{"role": "user", "content": content} {"role": "user", "content": content}
] ]
) )
return {"analysis": response.choices[0].message.content} return response.choices[0].message.content
def analyze_website_content(content):
"""Analyze the scraped website content using OpenAI."""
analysis = generate_completion(
"marketing analyst",
"Analyze the following website content and provide key insights for marketing strategy.",
content
)
return {"analysis": analysis}
def generate_copy(brief): def generate_copy(brief):
"""Generate marketing copy based on a brief using OpenAI.""" """Generate marketing copy based on a brief using OpenAI."""
response = client.chat.completions.create( copy = generate_completion(
model="gpt-4o-mini", "copywriter",
messages=[ "Create compelling marketing copy based on the following brief.",
{"role": "system", "content": "You are a copywriter. Create compelling marketing copy based on the following brief."}, brief
{"role": "user", "content": brief}
]
) )
return {"copy": response.choices[0].message.content} return {"copy": copy}
def create_campaign_idea(target_audience, goals): def create_campaign_idea(target_audience, goals):
"""Create a campaign idea based on target audience and goals using OpenAI.""" """Create a campaign idea based on target audience and goals using OpenAI."""
response = client.chat.completions.create( campaign_idea = generate_completion(
model="gpt-4o-mini", "marketing strategist",
messages=[ "Create an innovative campaign idea based on the target audience and goals provided.",
{"role": "system", "content": "You are a marketing strategist. Create an innovative campaign idea based on the target audience and goals provided."}, f"Target Audience: {target_audience}\nGoals: {goals}"
{"role": "user", "content": f"Target Audience: {target_audience}\nGoals: {goals}"}
]
) )
return {"campaign_idea": response.choices[0].message.content} return {"campaign_idea": campaign_idea}
def handoff_to_copywriter(): def handoff_to_copywriter():
"""Hand off the campaign idea to the copywriter agent.""" """Hand off the campaign idea to the copywriter agent."""