HomeArtificial IntelligenceImplementing a Instrument-Enabled Multi-Agent Workflow with Python, OpenAI API, and PrimisAI Nexus

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


On this superior tutorial, we goal to construct a multi-agent process automation system utilizing the PrimisAI Nexus framework, which is totally built-in with the OpenAI API. Our main goal is to display how hierarchical supervision, clever device utilization, and structured outputs can facilitate the coordination of a number of AI brokers to carry out complicated duties, starting from planning and improvement to high quality assurance and knowledge evaluation. As we stroll by means of every part, we don’t simply construct particular person brokers; we architect a collaborative ecosystem the place every agent has a transparent function, duties, and good instruments to perform the duty.

!pip set up 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()

We start by putting in the core dependencies: Primisai for agent orchestration, OpenAI for LLM entry, and nest_asyncio to deal with Colab’s occasion loop quirks. After making use of nest_asyncio, we make sure the pocket book is able to execute asynchronous duties seamlessly, a key requirement for multi-agent execution.

print("🚀 PrimisAI Nexus Superior Tutorial with OpenAI API")
print("=" * 55)


os.environ["OPENAI_API_KEY"] = "Use Your Personal API Key Here5"


# llm_config = {
#     "api_key": os.environ["OPENAI_API_KEY"],
#     "mannequin": "gpt-4o-mini", 
#     "base_url": "https://api.openai.com/v1",
#     "temperature": 0.7
# }




llm_config = {
   "api_key": os.environ["OPENAI_API_KEY"], 
   "mannequin": "gpt-3.5-turbo",                
   "base_url": "https://api.openai.com/v1",
   "temperature": 0.7
}




print("📋 API Configuration:")
print(f"• Mannequin: {llm_config['model']}")
print(f"• Base URL: {llm_config['base_url']}")
print("• Word: OpenAI has restricted free tokens by means of April 2025")
print("• Different: Contemplate Puter.js for limitless free entry")

To energy our brokers, we connect with OpenAI’s fashions, beginning with gpt-3.5-turbo for cost-efficient duties. We retailer our API key in setting variables and assemble a configuration dictionary specifying the mannequin, temperature, and base URL. This part permits us to flexibly swap between fashions, equivalent to gpt-4o-mini or gpt-4o, relying on process complexity and price.

code_schema = {
   "sort": "object",
   "properties": {
       "description": {"sort": "string", "description": "Code rationalization"},
       "code": {"sort": "string", "description": "Python code implementation"},
       "language": {"sort": "string", "description": "Programming language"},
       "complexity": {"sort": "string", "enum": ["beginner", "intermediate", "advanced"]},
       "test_cases": {"sort": "array", "gadgets": {"sort": "string"}, "description": "Instance utilization"}
   },
   "required": ["description", "code", "language"]
}


analysis_schema = {
   "sort": "object",
   "properties": {
       "abstract": {"sort": "string", "description": "Transient evaluation abstract"},
       "insights": {"sort": "array", "gadgets": {"sort": "string"}, "description": "Key insights"},
       "suggestions": {"sort": "array", "gadgets": {"sort": "string"}, "description": "Motion gadgets"},
       "confidence": {"sort": "quantity", "minimal": 0, "most": 1},
       "methodology": {"sort": "string", "description": "Evaluation strategy used"}
   },
   "required": ["summary", "insights", "confidence"]
}


planning_schema = {
   "sort": "object",
   "properties": {
       "duties": {"sort": "array", "gadgets": {"sort": "string"}, "description": "Record of duties to finish"},
       "precedence": {"sort": "string", "enum": ["low", "medium", "high"]},
       "estimated_time": {"sort": "string", "description": "Time estimate"},
       "dependencies": {"sort": "array", "gadgets": {"sort": "string"}, "description": "Activity dependencies"}
   },
   "required": ["tasks", "priority"]
}

We outline JSON schemas for 3 agent varieties: CodeWriter, Knowledge Analyst, and Mission Planner. These schemas implement construction within the agent’s responses, making the output machine-readable and predictable. It helps us be sure that the system returns constant knowledge, equivalent to code blocks, insights, or undertaking timelines, even when completely different LLMs are behind the scenes.

def calculate_metrics(data_str):
   """Calculate complete statistics for numerical knowledge"""
   attempt:
       knowledge = json.masses(data_str) if isinstance(data_str, str) else data_str
       if isinstance(knowledge, checklist) and all(isinstance(x, (int, float)) for x in knowledge):
           import statistics
           return {
               "imply": statistics.imply(knowledge),
               "median": statistics.median(knowledge),
               "mode": statistics.mode(knowledge) if len(set(knowledge))  1 else 0,
               "max": max(knowledge),
               "min": min(knowledge),
               "rely": len(knowledge),
               "sum": sum(knowledge)
           }
       return {"error": "Invalid knowledge format - anticipating array of numbers"}
   besides Exception as e:
       return {"error": f"Couldn't parse knowledge: {str(e)}"}


def validate_code(code):
   """Superior code validation with syntax and primary safety checks"""
   attempt:
       dangerous_imports = ['os', 'subprocess', 'eval', 'exec', '__import__']
       security_warnings = []
      
       for hazard in dangerous_imports:
           if hazard in code:
               security_warnings.append(f"Doubtlessly harmful: {hazard}")
      
       compile(code, '', 'exec')
      
       return {
           "legitimate": True,
           "message": "Code syntax is legitimate",
           "security_warnings": security_warnings,
           "strains": len(code.cut up('n'))
       }
   besides SyntaxError as e:
       return {
           "legitimate": False,
           "message": f"Syntax error: {e}",
           "line": getattr(e, 'lineno', 'unknown'),
           "security_warnings": []
       }


def search_documentation(question):
   """Simulate looking out documentation (placeholder operate)"""
   docs = {
       "python": "Python is a high-level programming language",
       "checklist": "Lists are ordered, mutable collections in Python",
       "operate": "Capabilities are reusable blocks of code",
       "class": "Courses outline objects with attributes and strategies"
   }
  
   outcomes = []
   for key, worth in docs.gadgets():
       if question.decrease() in key.decrease():
           outcomes.append(f"{key}: {worth}")
  
   return {
       "question": question,
       "outcomes": outcomes if outcomes else ["No documentation found"],
       "total_results": len(outcomes)
   }

Subsequent, we add customized instruments that brokers may name, equivalent to calculate_metrics for statistical summaries, validate_code for syntax and safety checks, and search_documentation for simulated programming assist. These instruments prolong the brokers’ talents, turning them from easy chatbots into interactive, utility-driven employees able to autonomous reasoning and validation.

print("n📋 Organising Multi-Agent Hierarchy with OpenAI")


main_supervisor = Supervisor(
   title="ProjectManager",
   llm_config=llm_config,
   system_message="You're a senior undertaking supervisor coordinating improvement and evaluation duties. Delegate appropriately, present clear summaries, and guarantee high quality supply. At all times think about time estimates and dependencies."
)


dev_supervisor = Supervisor(
   title="DevManager",
   llm_config=llm_config,
   is_assistant=True,
   system_message="You handle improvement duties. Coordinate between coding, testing, and code assessment. Guarantee finest practices and safety."
)


analysis_supervisor = Supervisor(
   title="AnalysisManager",
   llm_config=llm_config,
   is_assistant=True,
   system_message="You handle knowledge evaluation and analysis duties. Guarantee thorough evaluation, statistical rigor, and actionable insights."
)


qa_supervisor = Supervisor(
   title="QAManager",
   llm_config=llm_config,
   is_assistant=True,
   system_message="You handle high quality assurance and testing. Guarantee thorough validation and documentation."
)

To simulate a real-world administration construction, we create a multi-tiered hierarchy. A ProjectManager serves as the foundation supervisor, overseeing three assistant supervisors (DevManager, AnalysisManager, and QAManager), every answerable for domain-specific brokers. This modular hierarchy permits duties to stream down from high-level technique to granular execution.

code_agent = Agent(
   title="CodeWriter",
   llm_config=llm_config,
   system_message="You might be an skilled Python developer. Write clear, environment friendly, well-documented code with correct error dealing with. At all times embrace take a look at circumstances and comply with PEP 8 requirements.",
   output_schema=code_schema,
   instruments=[{
       "metadata": {
           "function": {
               "name": "validate_code",
               "description": "Validates Python code syntax and checks for security issues",
               "parameters": {
                   "type": "object",
                   "properties": {
                       "code": {"type": "string", "description": "Python code to validate"}
                   },
                   "required": ["code"]
               }
           }
       },
       "device": validate_code
   }, {
       "metadata": {
           "operate": {
               "title": "search_documentation",
               "description": "Seek for programming documentation and examples",
               "parameters": {
                   "sort": "object",
                   "properties": {
                       "question": {"sort": "string", "description": "Documentation matter to seek for"}
                   },
                   "required": ["query"]
               }
           }
       },
       "device": search_documentation
   }],
   use_tools=True
)


review_agent = Agent(
   title="CodeReviewer",
   llm_config=llm_config,
   system_message="You're a senior code reviewer. Analyze code for finest practices, effectivity, safety, maintainability, and potential points. Present constructive suggestions and recommendations.",
   keep_history=True,
   instruments=[{
       "metadata": {
           "function": {
               "name": "validate_code",
               "description": "Validates code syntax and security",
               "parameters": {
                   "type": "object",
                   "properties": {
                       "code": {"type": "string", "description": "Code to validate"}
                   },
                   "required": ["code"]
               }
           }
       },
       "device": validate_code
   }],
   use_tools=True
)


analyst_agent = Agent(
   title="DataAnalyst",
   llm_config=llm_config,
   system_message="You're a knowledge scientist specializing in statistical evaluation and insights technology. Present thorough evaluation with confidence metrics and actionable suggestions.",
   output_schema=analysis_schema,
   instruments=[{
       "metadata": {
           "function": {
               "name": "calculate_metrics",
               "description": "Calculates comprehensive statistics for numerical data",
               "parameters": {
                   "type": "object",
                   "properties": {
                       "data_str": {"type": "string", "description": "JSON string of numerical data array"}
                   },
                   "required": ["data_str"]
               }
           }
       },
       "device": calculate_metrics
   }],
   use_tools=True
)


planner_agent = Agent(
   title="ProjectPlanner",
   llm_config=llm_config,
   system_message="You're a undertaking planning specialist. Break down complicated tasks into manageable duties with reasonable time estimates and clear dependencies.",
   output_schema=planning_schema
)


tester_agent = Agent(
   title="QATester",
   llm_config=llm_config,
   system_message="You're a QA specialist targeted on complete testing methods, edge circumstances, and high quality assurance.",
   instruments=[{
       "metadata": {
           "function": {
               "name": "validate_code",
               "description": "Validates code for testing",
               "parameters": {
                   "type": "object",
                   "properties": {
                       "code": {"type": "string", "description": "Code to test"}
                   },
                   "required": ["code"]
               }
           }
       },
       "device": validate_code
   }],
   use_tools=True
)

We then construct a various set of specialised brokers: CodeWriter for producing Python code, CodeReviewer for reviewing logic and safety, DataAnalyst for performing structured knowledge evaluation, ProjectPlanner for process breakdown, and QATester for high quality checks. Every agent has domain-specific instruments, output schemas, and system directions tailor-made to their function.

dev_supervisor.register_agent(code_agent)
dev_supervisor.register_agent(review_agent)
analysis_supervisor.register_agent(analyst_agent)
qa_supervisor.register_agent(tester_agent)


main_supervisor.register_agent(dev_supervisor)
main_supervisor.register_agent(analysis_supervisor)
main_supervisor.register_agent(qa_supervisor)
main_supervisor.register_agent(planner_agent)

All brokers are registered below their respective supervisors, and the assistant supervisors are, in flip, registered with the primary supervisor. This setup creates a totally linked agent ecosystem, the place directions may cascade from the top-level agent to any specialist agent within the community.

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


print("n🧪 Testing Full Multi-Agent Communication")
print("-" * 45)


attempt:
   test_response = main_supervisor.chat("Hi there! Please introduce your staff and clarify the way you coordinate complicated tasks.")
   print(f"✅ Supervisor communication take a look at profitable!")
   print(f"Response preview: {test_response[:200]}...")
besides Exception as e:
   print(f"❌ Supervisor take a look at failed: {str(e)}")
   print("Falling again to direct agent testing...")

We visualize the whole hierarchy utilizing display_agent_graph() to verify our construction. It gives a transparent view of how every agent is related inside the broader process administration stream, a useful diagnostic earlier than deployment.

print("n🎯 Complicated Multi-Agent Activity Execution")
print("-" * 40)


complex_task = """Create a Python operate that implements a binary search algorithm,
have it reviewed for optimization, examined totally, and supply a undertaking plan
for integrating it into a bigger search system."""


print(f"Complicated Activity: {complex_task}")


attempt:
   complex_response = main_supervisor.chat(complex_task)
   print(f"✅ Complicated process accomplished")
   print(f"Response: {complex_response[:300]}...")
besides Exception as e:
   print(f"❌ Complicated process failed: {str(e)}")

We give the total system a real-world process: create a binary search operate, assessment it, take a look at it, and plan its integration into a bigger undertaking. The ProjectManager seamlessly coordinates brokers throughout improvement, QA, and planning, demonstrating the true energy of hierarchical, tool-driven agent orchestration.

print("n🔧 Instrument Integration & Structured Outputs")
print("-" * 43)


print("Testing Code Agent with instruments...")
attempt:
   code_response = code_agent.chat("Create a operate to calculate fibonacci numbers with memoization")
   print(f"✅ Code Agent with instruments: Working")
   print(f"Response sort: {sort(code_response)}")
  
   if isinstance(code_response, str) and code_response.strip().startswith('{'):
       code_data = json.masses(code_response)
       print(f"  - Description: {code_data.get('description', 'N/A')[:50]}...")
       print(f"  - Language: {code_data.get('language', 'N/A')}")
       print(f"  - Complexity: {code_data.get('complexity', 'N/A')}")
   else:
       print(f"  - Uncooked response: {code_response[:100]}...")
      
besides Exception as e:
   print(f"❌ Code Agent error: {str(e)}")


print("nTesting Analyst Agent with instruments...")
attempt:
   analysis_response = analyst_agent.chat("Analyze this gross sales knowledge: [100, 150, 120, 180, 200, 175, 160, 190, 220, 185]. What developments do you see?")
   print(f"✅ Analyst Agent with instruments: Working")
  
   if isinstance(analysis_response, str) and analysis_response.strip().startswith('{'):
       analysis_data = json.masses(analysis_response)
       print(f"  - Abstract: {analysis_data.get('abstract', 'N/A')[:50]}...")
       print(f"  - Confidence: {analysis_data.get('confidence', 'N/A')}")
       print(f"  - Insights rely: {len(analysis_data.get('insights', []))}")
   else:
       print(f"  - Uncooked response: {analysis_response[:100]}...")
      
besides Exception as e:
   print(f"❌ Analyst Agent error: {str(e)}")

We instantly take a look at the capabilities of two specialised brokers utilizing actual prompts. We first ask the CodeWriter agent to generate a Fibonacci operate with memoization and validate that it returns structured output containing a code description, language, and complexity degree. Then, we consider the DataAnalyst agent by feeding it pattern gross sales knowledge to extract developments.

print("n🔨 Guide Instrument Utilization")
print("-" * 22)


# Check all instruments manually
sample_data = "[95, 87, 92, 88, 91, 89, 94, 90, 86, 93]"
metrics_result = calculate_metrics(sample_data)
print(f"Statistics for {sample_data}:")
for key, worth in metrics_result.gadgets():
   print(f"  {key}: {worth}")


print("nCode validation take a look at:")
test_code = """
def binary_search(arr, goal):
   left, proper = 0, len(arr) - 1
   whereas left 

We step outdoors the agent framework to check every device instantly. First, we use the calculate_metrics device on a dataset of ten numbers, confirming it accurately returned statistics equivalent to imply, median, mode, and customary deviation. Subsequent, we run the validate_code device on a pattern binary search operate, which confirms each syntactic correctness and flags no safety warnings. Lastly, we take a look at the search_documentation device with the question “python operate” and obtain related documentation snippets, verifying its potential to effectively simulate contextual lookup.

print("n🚀 Superior Multi-Agent Workflow")
print("-" * 35)


workflow_stages = [
   ("Planning", "Create a project plan for building a web scraper for news articles"),
   ("Development", "Implement the web scraper with error handling and rate limiting"),
   ("Review", "Review the web scraper code for security and efficiency"),
   ("Testing", "Create comprehensive test cases for the web scraper"),
   ("Analysis", "Analyze sample scraped data: [45, 67, 23, 89, 12, 56, 78, 34, 91, 43]")
]


workflow_results = {}


for stage, process in workflow_stages:
   print(f"n{stage} Stage: {process}")
   attempt:
       if stage == "Planning":
           response = planner_agent.chat(process)
       elif stage == "Improvement":
           response = code_agent.chat(process)
       elif stage == "Evaluation":
           response = review_agent.chat(process)
       elif stage == "Testing":
           response = tester_agent.chat(process)
       elif stage == "Evaluation":
           response = analyst_agent.chat(process)
      
       workflow_results[stage] = response
       print(f"✅ {stage} accomplished: {response[:80]}...")
      
   besides Exception as e:
       print(f"❌ {stage} failed: {str(e)}")
       workflow_results[stage] = f"Error: {str(e)}"

We simulate a five-stage undertaking lifecycle: planning, improvement, assessment, testing, and evaluation. Every process is handed to essentially the most related agent, and responses are collected to guage efficiency. This demonstrates the framework’s functionality to handle end-to-end workflows with out guide intervention.

print("n📊 System Monitoring & Efficiency")
print("-" * 37)


debugger = Debugger(title="OpenAITutorialDebugger")
debugger.log("Superior OpenAI tutorial execution accomplished efficiently")


print(f"Fundamental Supervisor ID: {main_supervisor.workflow_id}")

We activate the Debugger device to trace the efficiency of our session and log system occasions. We additionally print the primary supervisor’s workflow_id as a traceable identifier, helpful when managing a number of workflows in manufacturing.

In conclusion, now we have efficiently constructed a totally automated, OpenAI-compatible multi-agent system utilizing PrimisAI Nexus. Every agent operates with readability, precision, and autonomy, whether or not writing code, validating logic, analyzing knowledge, or breaking down complicated workflows. Our hierarchical construction permits for seamless process delegation and modular scalability. PrimisAI Nexus framework establishes a strong basis for automating real-world duties, whether or not in software program improvement, analysis, planning, or knowledge operations, by means of clever collaboration between specialised brokers.


Try the Codes. All credit score for this analysis goes to the researchers of this undertaking. Additionally, be happy to comply with us on Twitter, Youtube and Spotify and don’t neglect to affix our 100k+ ML SubReddit and Subscribe to our E-newsletter.


Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is dedicated to harnessing the potential of Synthetic Intelligence for social good. His most up-to-date endeavor is the launch of an Synthetic Intelligence Media Platform, Marktechpost, which stands out for its in-depth protection of machine studying and deep studying information that’s each technically sound and simply comprehensible by a large viewers. The platform boasts of over 2 million month-to-month views, illustrating its reputation amongst audiences.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments