←back to Blog

Implementing a Tool-Enabled Multi-Agent Workflow with Python, OpenAI API, and PrimisAI Nexus

«`html

Implementing a Tool-Enabled Multi-Agent Workflow with Python, OpenAI API, and PrimisAI Nexus

In this advanced tutorial, we will build a multi-agent task automation system using the PrimisAI Nexus framework, fully integrated with the OpenAI API. Our primary objective is to demonstrate how hierarchical supervision, intelligent tool utilization, and structured outputs can facilitate the coordination of multiple AI agents to perform complex tasks such as planning, development, quality assurance, and data analysis. Throughout this tutorial, we will not only build individual agents but also architect a collaborative ecosystem where each agent has a clear role, responsibilities, and smart tools to accomplish tasks.

Target Audience Analysis

The target audience for this tutorial includes:

  • Data Scientists looking to automate their workflows
  • Software Engineers interested in AI integration
  • Project Managers aiming to streamline project coordination via AI
  • Business Analysts wanting to leverage AI for data insights

Common pain points include:

  • Complexity in managing multi-faceted projects
  • Difficulty in integrating AI tools into existing workflows
  • Need for efficient task delegation and quality assurance

Goals include:

  • Streamlining automation of repetitive tasks
  • Enhancing collaboration among team members
  • Ensuring high-quality output through structured processes

Interests often revolve around:

  • Latest advancements in AI technology
  • Practical applications of AI in business management
  • Effective use cases for multi-agent architectures

Preferred communication methods include:

  • Clear, concise technical documentation
  • Tutorials with practical examples
  • Interactive code demonstrations

Setting Up the Environment

We begin by installing the core dependencies: PrimisAI for agent orchestration, OpenAI for LLM access, and nest_asyncio to handle asynchronous tasks in Python. The following command installs the necessary libraries:

!pip install primisai openai nest-asyncio

Next, we set up the environment for our OpenAI API key and configuration:


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()

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 Agent Schemas

We define JSON schemas for three agent types: CodeWriter, Data Analyst, and Project Planner. These schemas enforce structure in the agent’s responses, ensuring consistent and predictable outputs.


code_schema = {
   "type": "object",
   "properties": {
       "description": {"type": "string", "description": "Code explanation"},
       "code": {"type": "string", "description": "Python code implementation"},
       "language": {"type": "string", "description": "Programming language"},
       "complexity": {"type": "string", "enum": ["beginner", "intermediate", "advanced"]},
       "test_cases": {"type": "array", "items": {"type": "string"}, "description": "Example usage"}
   },
   "required": ["description", "code", "language"]
}

Agent Hierarchy Setup

To simulate a real-world management structure, we create a multi-tiered hierarchy. A ProjectManager serves as the root supervisor, overseeing three assistant supervisors (DevManager, AnalysisManager, and QAManager), each in charge of domain-specific agents:


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."
)

Building Specialized Agents

We create specialized agents for various tasks, such as CodeWriter for generating Python code, CodeReviewer for reviewing logic and security, and DataAnalyst for performing structured data analysis. Each agent has domain-specific tools, output schemas, and system instructions tailored to their role:


code_agent = Agent(
   name="CodeWriter",
   llm_config=llm_config,
   system_message="You are an expert Python developer."
)

review_agent = Agent(
   name="CodeReviewer",
   llm_config=llm_config,
   system_message="You are a senior code reviewer."
)

analyst_agent = Agent(
   name="DataAnalyst",
   llm_config=llm_config,
   system_message="You are a data scientist specializing in statistical analysis."
)

planner_agent = Agent(
   name="ProjectPlanner",
   llm_config=llm_config,
   system_message="You are a project planning specialist."
)

tester_agent = Agent(
   name="QATester",
   llm_config=llm_config,
   system_message="You are a QA specialist focused on comprehensive testing strategies."
)

Testing Multi-Agent Communication

We visualize the entire hierarchy and confirm the structure, ensuring that instructions can cascade from the top-level agent to any specialist agent in the network:


print("\n Agent Hierarchy:")
main_supervisor.display_agent_graph()

Complex Task Execution

We give the full system a real-world task: create a binary search function, review it, test it, and plan its integration into a larger project. The ProjectManager seamlessly coordinates agents across development, QA, and planning:


complex_task = "Create a Python function that implements a binary search algorithm."
complex_response = main_supervisor.chat(complex_task)

Conclusion

In conclusion, we have successfully built a fully automated, OpenAI-compatible multi-agent system using PrimisAI Nexus. Each agent operates with clarity, precision, and autonomy, whether writing code, validating logic, analyzing data, or breaking down complex workflows. Our hierarchical structure allows for seamless task delegation and modular scalability. The PrimisAI Nexus framework establishes a robust foundation for automating real-world tasks through intelligent collaboration between specialized agents.

For more information and resources, please refer to the official documentation of PrimisAI and OpenAI.

«`