siddhartharya commited on
Commit
b8778bd
·
verified ·
1 Parent(s): 9ada6bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -42
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import requests
3
  import openai
4
  import gradio as gr
5
- from openai_swarm import Agent, Swarm
6
 
7
  # Fetch API keys from environment variables
8
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
@@ -47,40 +47,29 @@ def generate_email_content(api_key, prompt):
47
 
48
  # Function to validate the generated email for professional tone and completeness
49
  def validate_email(email_content):
50
- # Basic validation: check if the email contains key sections (can be expanded)
51
  return "Why" in email_content and "How" in email_content and "What" in email_content
52
 
53
- # Define the ReAct Agent using Swarm framework
54
- class EmailAgent(Agent):
55
- def __init__(self, user_data):
56
- super().__init__()
57
- self.user_data = user_data
58
- self.iterations = 0
59
-
60
- def act(self):
61
- linkedin_info = fetch_linkedin_data(self.user_data['linkedin_url'])
62
- company_info = fetch_company_info(self.user_data['company_url'])
63
- if "error" in linkedin_info or "error" in company_info:
64
- return "Error fetching data. Please check the LinkedIn and company URLs."
65
-
66
- prompt = structure_email(self.user_data, linkedin_info, company_info)
67
- email_content = generate_email_content(OPENAI_API_KEY, prompt)
68
- return email_content
69
-
70
- def react(self, output):
71
- # React based on the generated email's validity
72
- if validate_email(output):
73
- return output
74
- else:
75
- # If invalid, refine prompt or retry (up to 3 iterations)
76
- self.iterations += 1
77
- if self.iterations < 3:
78
- refined_prompt = f"Refined: {output}"
79
- return generate_email_content(OPENAI_API_KEY, refined_prompt)
80
- else:
81
- return "Unable to generate a valid email after 3 attempts."
82
-
83
- # Define the main function to run the agent within the Swarm
84
  def run_agent(name, email, phone, linkedin_url, company_url, role):
85
  user_data = {
86
  "name": name,
@@ -90,15 +79,31 @@ def run_agent(name, email, phone, linkedin_url, company_url, role):
90
  "company_url": company_url,
91
  "role": role
92
  }
93
-
94
- # Initialize the swarm and add the EmailAgent
95
- email_swarm = Swarm()
96
- agent = EmailAgent(user_data)
97
- email_swarm.add_agent(agent)
98
-
99
- # Run the swarm to get the final email content
100
- result = email_swarm.run()
101
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  # Set up the Gradio interface
104
  final_interface = gr.Interface(
 
2
  import requests
3
  import openai
4
  import gradio as gr
5
+ from swarm import Swarm, Agent
6
 
7
  # Fetch API keys from environment variables
8
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
 
47
 
48
  # Function to validate the generated email for professional tone and completeness
49
  def validate_email(email_content):
 
50
  return "Why" in email_content and "How" in email_content and "What" in email_content
51
 
52
+ # Define Agent B - The agent responsible for processing and refining the email
53
+ def transfer_to_email_generation():
54
+ return email_agent_b
55
+
56
+ # Define Agent A - The primary agent to initiate data collection and hand over to Agent B
57
+ email_agent_a = Agent(
58
+ name="Data Collection Agent",
59
+ instructions="Collect user inputs and relevant data (LinkedIn and company details).",
60
+ functions=[transfer_to_email_generation]
61
+ )
62
+
63
+ # Define Agent B - The agent that structures and generates the email
64
+ email_agent_b = Agent(
65
+ name="Email Generation Agent",
66
+ instructions="Structure the email using the 'Start with Why' model and generate professional content.",
67
+ )
68
+
69
+ # Set up the Swarm client
70
+ client = Swarm()
71
+
72
+ # Main function that interacts with the agents using the Swarm framework
 
 
 
 
 
 
 
 
 
 
73
  def run_agent(name, email, phone, linkedin_url, company_url, role):
74
  user_data = {
75
  "name": name,
 
79
  "company_url": company_url,
80
  "role": role
81
  }
82
+
83
+ # Step 1: Fetch LinkedIn and Company Data
84
+ linkedin_info = fetch_linkedin_data(linkedin_url)
85
+ company_info = fetch_company_info(company_url)
86
+ if "error" in linkedin_info or "error" in company_info:
87
+ return "Error fetching data. Please check the LinkedIn and company URLs."
88
+
89
+ # Step 2: Use the Swarm framework to structure and generate the email
90
+ messages = [{"role": "user", "content": "Initiate email generation."}]
91
+ response = client.run(agent=email_agent_a, messages=messages)
92
+
93
+ # Step 3: Agent B structures and refines the email using the provided data
94
+ prompt = structure_email(user_data, linkedin_info, company_info)
95
+ email_content = generate_email_content(OPENAI_API_KEY, prompt)
96
+
97
+ # Validate the generated email content using the ReAct pattern (with a max of 3 iterations)
98
+ for i in range(3):
99
+ if validate_email(email_content):
100
+ return email_content
101
+ else:
102
+ # Refine the prompt based on the feedback or iteration logic
103
+ refined_prompt = f"Refine: {prompt}"
104
+ email_content = generate_email_content(OPENAI_API_KEY, refined_prompt)
105
+
106
+ return "Unable to generate a valid email after 3 attempts."
107
 
108
  # Set up the Gradio interface
109
  final_interface = gr.Interface(