·11 min

Integrating OpenAI Swarm & Microsoft Autogen: Multi-Agent AI for Persona Generation

DK

Daniel Kliewer

Author, Sovereign AI

OpenAI SwarmMicrosoft AutogenMulti-Agent SystemsAI FrameworksPersona Generator
Sovereign AI book cover

From the Book

This is from Sovereign AI: Building Local-First Intelligent Systems.

Get the Book — $88
Integrating OpenAI Swarm & Microsoft Autogen: Multi-Agent AI for Persona Generation

Image

Enhancing your existing Python script by integrating OpenAI Swarm and Microsoft Autogen can significantly improve its capabilities, scalability, and maintainability. Below, I’ll guide you through understanding these tools, integrating them into your project, and adding new features to make your script more robust and feature-rich.

Table of Contents

  1. Overview of OpenAI Swarm and Microsoft Autogen
  2. Prerequisites
  3. Integrating OpenAI Swarm
  4. Integrating Microsoft Autogen
  5. Enhancing the Existing Script
  6. Adding New Features
  7. Security Improvements
  8. Final Thoughts

1. Overview of OpenAI Swarm and Microsoft Autogen

OpenAI Swarm is a framework designed to manage and coordinate multiple AI agents, enabling them to work collaboratively to solve complex tasks. It facilitates communication, task delegation, and aggregation of results from various agents.

Microsoft Autogen is a framework that simplifies the orchestration of large language models (LLMs) to build complex applications. It provides tools for chaining model calls, managing context, and integrating additional functionalities like data retrieval or transformation.

By integrating these frameworks, you can leverage multi-agent collaboration and advanced orchestration capabilities, making your persona generator and responder more powerful and flexible.


2. Prerequisites

Before proceeding, ensure you have the following:

  1. Python 3.8+ installed.
  2. Git installed to clone repositories.
  3. Virtual Environment set up to manage dependencies.
  4. API Keys for OpenAI and any other services you intend to use.

3. Integrating OpenAI Swarm

Step 1: Clone and Install OpenAI Swarm

bash
1git clone https://github.com/openai/swarm.git
2cd swarm
3pip install -r requirements.txt
4python setup.py install

Step 2: Understanding Swarm Structure

OpenAI Swarm allows you to define multiple agents that can perform specific tasks. For your application, you can create agents for:

  • Persona Generation
  • Response Generation
  • Validation and Formatting
  • Exporting

Step 3: Define Swarm Agents

Create separate modules for each agent. For example:

  • persona_agent.py
  • response_agent.py
  • validation_agent.py
  • export_agent.py

Example: persona_agent.py

python
1from swarm.agent import Agent
2import json
3import os
4from openai import OpenAI
5
6class PersonaAgent(Agent):
7 def __init__(self, api_key, persona_file='persona.json'):
8 super().__init__()
9 self.client = OpenAI(api_key=api_key)
10 self.persona_file = persona_file
11
12 def generate_persona(self, sample_text: str) -> dict:
13 prompt = (
14 "Please analyze the writing style and personality of the given writing sample. "
15 "You are a persona generation assistant. Analyze the following text and create a persona profile "
16 "that captures the writing style and personality characteristics of the author. "
17 "YOU MUST RESPOND WITH A VALID JSON OBJECT ONLY, no other text or analysis. "
18 "The response must start with '{' and end with '}' and use the following exact structure:\n\n"
19 "{...}" # Truncated for brevity
20 f"Sample Text:\n{sample_text}"
21 )
22 payload = {
23 "model": "gpt-4",
24 "messages": [{"role": "user", "content": prompt}],
25 "temperature": 1
26 }
27 response = self.client.chat.completions.create(**payload)
28 content = response.choices[0].message.content.strip()
29 # Extract and parse JSON
30 start_idx = content.find('{')
31 end_idx = content.rfind('}') + 1
32 json_str = content[start_idx:end_idx]
33 persona = json.loads(json_str)
34 return persona
35
36 def save_persona(self, persona: dict) -> bool:
37 try:
38 if not persona:
39 print("Error: Cannot save empty persona")
40 return False
41 os.makedirs(os.path.dirname(self.persona_file) if os.path.dirname(self.persona_file) else '.', exist_ok=True)
42 with open(self.persona_file, 'w', encoding='utf-8') as f:
43 json.dump(persona, f, indent=4, ensure_ascii=False)
44 print(f"Successfully saved persona to {self.persona_file}")
45 return True
46 except Exception as e:
47 print(f"Error saving persona: {str(e)}")
48 return False

Step 4: Orchestrate Agents with Swarm

Create a main_swarm.py to coordinate agents.

python
1from swarm import Swarm
2from persona_agent import PersonaAgent
3from response_agent import ResponseAgent
4from validation_agent import ValidationAgent
5from export_agent import ExportAgent
6
7def main():
8 swarm = Swarm()
9 api_key = os.getenv("OPENAI_API_KEY")
10
11 persona_agent = PersonaAgent(api_key)
12 response_agent = ResponseAgent(api_key)
13 validation_agent = ValidationAgent()
14 export_agent = ExportAgent()
15
16 swarm.add_agent(persona_agent)
17 swarm.add_agent(response_agent)
18 swarm.add_agent(validation_agent)
19 swarm.add_agent(export_agent)
20
21 # Example workflow
22 sample_text = "Your sample text here..."
23 persona = persona_agent.generate_persona(sample_text)
24 if validation_agent.validate(persona):
25 persona_agent.save_persona(persona)
26 prompt = "Your prompt here..."
27 response = response_agent.generate_response(persona, prompt)
28 export_agent.export_to_markdown(response)
29 else:
30 print("Persona validation failed.")
31
32if __name__ == "__main__":
33 main()

4. Integrating Microsoft Autogen

Step 1: Clone and Install Microsoft Autogen

bash
1git clone https://github.com/microsoft/autogen.git
2cd autogen
3pip install -r requirements.txt
4python setup.py install

Step 2: Understanding Autogen Structure

Microsoft Autogen allows you to create chains of model calls, manage context, and integrate additional functionalities seamlessly.

Step 3: Define Autogen Chains

You can create chains for tasks like persona generation, response generation, and exporting.

Example: autogen_chain.py

python
1from autogen import Chain, Step
2from openai import OpenAI
3import json
4
5class PersonaGenerationChain(Chain):
6 def __init__(self, api_key):
7 super().__init__()
8 self.client = OpenAI(api_key=api_key)
9
10 @Step
11 def generate_persona(self, sample_text: str) -> dict:
12 prompt = (
13 "Please analyze the writing style and personality of the given writing sample. "
14 "You are a persona generation assistant. Analyze the following text and create a persona profile "
15 "that captures the writing style and personality characteristics of the author. "
16 "YOU MUST RESPOND WITH A VALID JSON OBJECT ONLY, no other text or analysis. "
17 "The response must start with '{' and end with '}' and use the following exact structure:\n\n"
18 "{...}" # Truncated for brevity
19 f"Sample Text:\n{sample_text}"
20 )
21 response = self.client.chat.completions.create(
22 model="gpt-4",
23 messages=[{"role": "user", "content": prompt}],
24 temperature=1
25 )
26 content = response.choices[0].message.content.strip()
27 # Extract and parse JSON
28 start_idx = content.find('{')
29 end_idx = content.rfind('}') + 1
30 json_str = content[start_idx:end_idx]
31 persona = json.loads(json_str)
32 return persona

Step 4: Orchestrate Chains with Autogen

Create a main_autogen.py to manage chains.

python
1from autogen_chain import PersonaGenerationChain
2from validation_agent import ValidationAgent
3from export_agent import ExportAgent
4
5def main():
6 api_key = os.getenv("OPENAI_API_KEY")
7 persona_chain = PersonaGenerationChain(api_key)
8 validation_agent = ValidationAgent()
9 export_agent = ExportAgent()
10
11 sample_text = "Your sample text here..."
12 persona = persona_chain.generate_persona(sample_text)
13
14 if validation_agent.validate(persona):
15 # Save persona
16 with open('persona.json', 'w', encoding='utf-8') as f:
17 json.dump(persona, f, indent=4)
18 # Generate response
19 prompt = "Your prompt here..."
20 # Define response generation logic, possibly another chain
21 # Export response
22 export_agent.export_to_markdown("Generated response")
23 else:
24 print("Persona validation failed.")
25
26if __name__ == "__main__":
27 main()

5. Enhancing the Existing Script

Now, let's enhance your existing script by integrating both OpenAI Swarm and Microsoft Autogen. Below are the key modifications and additions:

Step 1: Refactor Code into Modular Components

Organize your code into modules to separate concerns:

  • agents/: Contains Swarm agents.

    • persona_agent.py
    • response_agent.py
    • validation_agent.py
    • export_agent.py
  • chains/: Contains Autogen chains.

    • persona_chain.py
    • response_chain.py
  • utils/: Utility functions.

    • file_utils.py
    • input_utils.py
  • main.py: Main orchestrator.

Step 2: Implement Agents and Chains

As shown in the previous sections, implement agents and chains in their respective modules. Ensure each agent or chain has a single responsibility.

Step 3: Update main.py to Use Swarm and Autogen

Here's an example of how to integrate both frameworks into your main application.

python
1import os
2from swarm import Swarm
3from agents.persona_agent import PersonaAgent
4from agents.response_agent import ResponseAgent
5from agents.validation_agent import ValidationAgent
6from agents.export_agent import ExportAgent
7from utils.file_utils import load_sample_text, create_backup
8from utils.input_utils import get_multiline_input
9
10def main():
11 print("\n=== Enhanced Persona Generator and Responder ===")
12 swarm = Swarm()
13 api_key = os.getenv("OPENAI_API_KEY")
14
15 # Initialize agents
16 persona_agent = PersonaAgent(api_key)
17 response_agent = ResponseAgent(api_key)
18 validation_agent = ValidationAgent()
19 export_agent = ExportAgent()
20
21 # Add agents to swarm
22 swarm.add_agent(persona_agent)
23 swarm.add_agent(response_agent)
24 swarm.add_agent(validation_agent)
25 swarm.add_agent(export_agent)
26
27 while True:
28 print("\nOptions:")
29 print("1. Use existing Persona")
30 print("2. Generate new Persona from sample text")
31 print("3. Load sample text from file")
32 print("4. Exit")
33
34 choice = input("\nEnter your choice (1-4): ").strip()
35
36 if choice == '4':
37 print("Exiting program...")
38 break
39
40 persona = {}
41
42 if choice == '1':
43 persona = swarm.get_agent('PersonaAgent').load_persona()
44 if persona:
45 print("\nCurrent Persona:")
46 print(swarm.get_agent('PersonaAgent').format_persona_summary(persona))
47 else:
48 if input("\nNo persona loaded. Generate new one? (y/n): ").lower() == 'y':
49 choice = '2'
50 else:
51 continue
52
53 if choice == '2':
54 sample_text = get_multiline_input("\nEnter sample text:")
55 if not sample_text.strip():
56 print("Error: Empty sample text provided")
57 continue
58 print("\nGenerating persona from sample text...")
59 persona = swarm.get_agent('PersonaAgent').generate_persona(sample_text)
60 if persona and swarm.get_agent('ValidationAgent').validate(persona):
61 swarm.get_agent('PersonaAgent').create_backup()
62 if swarm.get_agent('PersonaAgent').save_persona(persona):
63 print("\nGenerated Persona:")
64 print(swarm.get_agent('PersonaAgent').format_persona_summary(persona))
65 else:
66 print("Warning: Persona generated but not saved")
67 else:
68 print("Error: Failed to generate valid persona")
69 continue
70
71 elif choice == '3':
72 filename = input("\nEnter the path to the text file: ").strip()
73 sample_text = load_sample_text(filename)
74 if not sample_text:
75 continue
76 print("\nGenerating persona from file...")
77 persona = swarm.get_agent('PersonaAgent').generate_persona(sample_text)
78 if persona and swarm.get_agent('ValidationAgent').validate(persona):
79 swarm.get_agent('PersonaAgent').create_backup()
80 if swarm.get_agent('PersonaAgent').save_persona(persona):
81 print("\nGenerated Persona:")
82 print(swarm.get_agent('PersonaAgent').format_persona_summary(persona))
83 else:
84 print("Warning: Persona generated but not saved")
85 else:
86 print("Error: Failed to generate valid persona")
87 continue
88
89 # Get prompt and generate response
90 while True:
91 prompt = get_multiline_input("\nEnter your prompt:")
92 if not prompt.strip():
93 print("Error: Empty prompt provided")
94 if input("Try again? (y/n): ").lower() != 'y':
95 break
96 continue
97 print("\nGenerating response...")
98 response = swarm.get_agent('ResponseAgent').generate_response(persona, prompt)
99 print("\n=== Generated Response ===")
100 print(response)
101 if input("\nExport response to Markdown? (y/n): ").lower() == 'y':
102 default_filename = f"response_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
103 custom_filename = input(f"Enter filename (default: {default_filename}): ").strip()
104 filename = custom_filename if custom_filename else default_filename
105 if swarm.get_agent('ExportAgent').export_to_markdown(response, filename):
106 print("Response exported successfully")
107 else:
108 print("Error: Failed to export response")
109 if input("\nGenerate another response with current persona? (y/n): ").lower() != 'y':
110 break
111
112 if input("\nStart over with a different persona? (y/n): ").lower() != 'y':
113 print("Exiting program...")
114 break
115
116if __name__ == "__main__":
117 try:
118 main()
119 except KeyboardInterrupt:
120 print("\nProgram terminated by user")
121 except Exception as e:
122 print(f"\nProgram terminated due to error: {str(e)}")
123 finally:
124 print("\nThank you for using the Enhanced Persona Generator and Responder!")

Explanation:

  • Swarm Initialization: Initializes the swarm and adds the necessary agents.
  • User Interaction: Maintains the existing user interface while leveraging the swarm for operations.
  • Agent Utilization: Delegates tasks like persona generation, validation, and exporting to respective agents, promoting modularity and scalability.

6. Adding New Features

With OpenAI Swarm and Microsoft Autogen integrated, you can introduce several new features:

a. Multi-Threaded Persona Generation

Allow multiple personas to be generated simultaneously from different sample texts.

Implementation:

  • Utilize Swarm’s multi-agent capabilities to handle concurrent persona generation requests.
  • Modify the PersonaAgent to handle asynchronous tasks.

b. Enhanced Validation and Error Handling

Implement more robust validation using multiple validation agents.

Implementation:

  • Create additional ValidationAgents focusing on different aspects (e.g., schema validation, content quality).
  • Aggregate validation results before proceeding.

c. Persona Management Dashboard

Develop a simple dashboard to manage personas, view summaries, and export options.

Implementation:

  • Use a lightweight web framework like Flask or FastAPI.
  • Integrate with Swarm to fetch and display persona data.

Example: Flask Integration

python
1from flask import Flask, jsonify, request
2from swarm import Swarm
3
4app = Flask(__name__)
5swarm = Swarm()
6# Initialize and add agents...
7
8@app.route('/personas', methods=['GET'])
9def get_personas():
10 # Implement logic to list all saved personas
11 pass
12
13@app.route('/persona', methods=['POST'])
14def create_persona():
15 sample_text = request.json.get('sample_text')
16 persona = swarm.get_agent('PersonaAgent').generate_persona(sample_text)
17 if swarm.get_agent('ValidationAgent').validate(persona):
18 swarm.get_agent('PersonaAgent').save_persona(persona)
19 return jsonify(persona), 201
20 else:
21 return jsonify({'error': 'Invalid persona'}), 400
22
23# Additional routes...
24
25if __name__ == '__main__':
26 app.run(debug=True)

d. Integration with External APIs

Enhance responses by integrating with APIs for data retrieval, sentiment analysis, or knowledge bases.

Implementation:

  • Create new agents or steps in Autogen chains to handle API interactions.
  • Example: An agent that fetches current events to make responses more relevant.

7. Security Improvements

Your current script includes the OpenAI API key hardcoded within the script. This is a significant security risk. Here's how to improve it:

a. Use Environment Variables

Store sensitive information like API keys in environment variables instead of hardcoding them.

Implementation:

  1. Set Environment Variable:

    bash
    1export OPENAI_API_KEY="your-api-key-here"
  2. Access in Python:

    python
    1import os
    2
    3api_key = os.getenv("OPENAI_API_KEY")
    4if not api_key:
    5 raise ValueError("OpenAI API key not found in environment variables.")
  3. Remove Hardcoded Keys:

    Remove the hardcoded api_key from your script.

b. Use .env Files with python-dotenv

For easier management, especially during development, use a .env file.

Implementation:

  1. Install python-dotenv:

    bash
    1pip install python-dotenv
  2. Create a .env File:

    OPENAI_API_KEY=your-api-key-here
    
  3. Load .env in Python:

    python
    1from dotenv import load_dotenv
    2import os
    3
    4load_dotenv()
    5api_key = os.getenv("OPENAI_API_KEY")
  4. Add .env to .gitignore:

    gitignore
    1# .gitignore
    2.env

c. Secure File Handling

Ensure that sensitive files like persona.json are stored securely.

Implementation:

  • File Permissions: Set appropriate file permissions to restrict access.

    python
    1import os
    2
    3def save_persona(persona: dict, filename: str = PERSONA_FILE) -> bool:
    4 try:
    5 if not persona:
    6 print("Error: Cannot save empty persona")
    7 return False
    8 os.makedirs(os.path.dirname(filename) if os.path.dirname(filename) else '.', exist_ok=True)
    9 with open(filename, 'w', encoding='utf-8') as f:
    10 json.dump(persona, f, indent=4, ensure_ascii=False)
    11 os.chmod(filename, 0o600) # Read and write permissions for the owner only
    12 print(f"Successfully saved persona to {filename}")
    13 return True
    14 except Exception as e:
    15 print(f"Error saving persona: {str(e)}")
    16 return False
  • Encryption: For highly sensitive data, consider encrypting the JSON files.


8. Final Thoughts

Integrating OpenAI Swarm and Microsoft Autogen into your existing persona generator and responder script can significantly enhance its capabilities by promoting modularity, scalability, and maintainability. Here's a summary of the steps and recommendations:

  1. Modular Architecture: Break down your script into modular components (agents and chains) to separate concerns and facilitate easier maintenance.

  2. Leverage Swarm for Multi-Agent Collaboration: Use Swarm to manage different agents responsible for specific tasks, enabling concurrent processing and better task management.

  3. Utilize Autogen for Advanced Orchestration: Implement Autogen chains to handle complex workflows, context management, and integration with external services.

  4. Enhance User Experience: Introduce new features like a management dashboard, multi-threaded processing, and integration with external APIs to provide a richer user experience.

  5. Prioritize Security: Always handle sensitive information securely by using environment variables, securing file permissions, and avoiding hardcoded credentials.

  6. Continuous Improvement: Regularly update dependencies, monitor performance, and seek user feedback to iteratively improve your application.

By following these guidelines and integrating the mentioned frameworks, your application will be better equipped to handle complex tasks, scale efficiently, and provide a more robust and feature-rich experience.

If you encounter specific challenges during the integration or need further assistance with particular components, feel free to ask!

Sovereign AI book cover

Sovereign AI: Building Local-First Intelligent Systems

by Daniel Kliewer · Paperback · 72 pages

The hands-on guide to building AI that runs on your hardware, keeps your data private, and eliminates cloud dependence. Working code included.