From 2eff7c29bbc50fdf4246174eea3d894f90887eb6 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sat, 12 Oct 2024 16:12:12 -0300 Subject: [PATCH] Nick: refactor openai swarm example --- examples/openai_swarm_firecrawl/main.py | 41 ++++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/examples/openai_swarm_firecrawl/main.py b/examples/openai_swarm_firecrawl/main.py index cef53fdf..63a1b898 100644 --- a/examples/openai_swarm_firecrawl/main.py +++ b/examples/openai_swarm_firecrawl/main.py @@ -19,38 +19,43 @@ def scrape_website(url): ) return scrape_status -def analyze_website_content(content): - """Analyze the scraped website content using OpenAI.""" +def generate_completion(role, task, content): + """Generate a completion using OpenAI.""" response = client.chat.completions.create( model="gpt-4o-mini", 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} ] ) - 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): """Generate marketing copy based on a brief using OpenAI.""" - response = client.chat.completions.create( - model="gpt-4o-mini", - messages=[ - {"role": "system", "content": "You are a copywriter. Create compelling marketing copy based on the following brief."}, - {"role": "user", "content": brief} - ] + copy = generate_completion( + "copywriter", + "Create compelling marketing copy based on the following brief.", + brief ) - return {"copy": response.choices[0].message.content} + return {"copy": copy} def create_campaign_idea(target_audience, goals): """Create a campaign idea based on target audience and goals using OpenAI.""" - response = client.chat.completions.create( - model="gpt-4o-mini", - messages=[ - {"role": "system", "content": "You are a marketing strategist. Create an innovative campaign idea based on the target audience and goals provided."}, - {"role": "user", "content": f"Target Audience: {target_audience}\nGoals: {goals}"} - ] + campaign_idea = generate_completion( + "marketing strategist", + "Create an innovative campaign idea based on the target audience and goals provided.", + f"Target Audience: {target_audience}\nGoals: {goals}" ) - return {"campaign_idea": response.choices[0].message.content} + return {"campaign_idea": campaign_idea} def handoff_to_copywriter(): """Hand off the campaign idea to the copywriter agent."""