Building an Intelligent Multi-Agent Workflow with Python, OpenAI, and PrimisAI Nexus
Learn how to build an advanced multi-agent task automation system with Python, OpenAI API, and PrimisAI Nexus that coordinates agents for coding, analysis, and project planning.
Overview of Multi-Agent System Architecture
This tutorial demonstrates creating a multi-agent task automation system leveraging the PrimisAI Nexus framework integrated with the OpenAI API. The system uses hierarchical supervision combined with intelligent tool utilization and structured outputs to enable multiple AI agents to collaborate on complex tasks such as planning, development, quality assurance, and data analysis.
Setting Up Environment and Dependencies
We begin by installing essential packages: PrimisAI for agent orchestration, OpenAI for accessing language models, and nest_asyncio to manage asynchronous execution within notebook environments:
!pip install primisai openai nest-asyncio
import os
import nest_asyncio
from primisai.nexus.core import AI, Agent, Supervisor
from primisai.nexus.utils.debugger import Debugger
import json
nest_asyncio.apply()This setup ensures seamless asynchronous task management necessary for multi-agent workflows.
Configuring OpenAI Models
The system uses OpenAI’s GPT models with flexible configurations allowing to switch between models like 'gpt-3.5-turbo' or 'gpt-4o-mini' based on task needs and cost considerations. API keys are securely stored in environment variables.
os.environ["OPENAI_API_KEY"] = "Use Your Own API Key Here"
llm_config = {
"api_key": os.environ["OPENAI_API_KEY"],
"model": "gpt-3.5-turbo",
"base_url": "https://api.openai.com/v1",
"temperature": 0.7
}Defining Structured Output Schemas
JSON schemas are defined for different agent types such as CodeWriter, DataAnalyst, and ProjectPlanner to enforce structured, machine-readable outputs. These schemas increase consistency and reliability across agent responses.
Custom Tool Integration
Several utility tools are implemented and integrated to expand agent capabilities:
calculate_metrics: Computes statistical measures for numerical data.validate_code: Performs syntax validation and security checks on Python code.search_documentation: Simulates searching programming documentation.
These tools enable agents to autonomously reason and validate outputs beyond simple text generation.
Establishing Multi-Tiered Agent Hierarchy
A hierarchical supervision structure is created with a main ProjectManager supervising assistant supervisors responsible for development, analysis, and QA domains. This modular design allows clear delegation and coordination:
main_supervisor = Supervisor(name="ProjectManager", llm_config=llm_config, system_message="You are a senior project manager coordinating development and analysis tasks...")
dev_supervisor = Supervisor(name="DevManager", llm_config=llm_config, is_assistant=True, system_message="You manage development tasks...")
analysis_supervisor = Supervisor(name="AnalysisManager", llm_config=llm_config, is_assistant=True, system_message="You manage data analysis and research tasks...")
qa_supervisor = Supervisor(name="QAManager", llm_config=llm_config, is_assistant=True, system_message="You manage quality assurance and testing...")Specialized Agents with Domain-Specific Roles
Various agents are defined with specific responsibilities and tools:
CodeWriter: Writes Python code with validation and documentation search tools.CodeReviewer: Reviews code for quality, security, and efficiency.DataAnalyst: Performs statistical analysis with confidence and actionable insights.ProjectPlanner: Breaks down projects into tasks with priorities and dependencies.QATester: Conducts testing and validation of code.
Each agent is registered under the appropriate supervisor, creating a fully connected ecosystem.
Testing and Demonstration
The multi-agent system is tested with real-world scenarios including generating Fibonacci functions, analyzing sales data, and executing complex tasks like creating, reviewing, testing, and planning a binary search algorithm implementation.
Workflow Simulation
A complete project lifecycle is simulated with stages from planning to analysis, each stage handled by the relevant agent, showcasing end-to-end automation without manual intervention.
System Monitoring
A Debugger tool logs performance and system events, and workflow identifiers are printed for traceability in production environments.
This framework showcases the power of hierarchical, tool-enabled multi-agent workflows, enabling scalable, autonomous task management across software development, data analysis, and project planning domains.
Сменить язык
Читать эту статью на русском