Friday, May 29, 2026
banner
Top Selling Multipurpose WP Theme

This tutorial reveals you find out how to use Gemini to design and run an entire agent AI orchestration pipeline that leverages semantic routing, symbolic guardrails, and self-correcting loops. Learn to construction your brokers, dispatch duties, apply constraints, and tune output utilizing a clear, modular structure. As you progress via every snippet, you may see how the system intelligently selects the correct agent, validates its output, and improves itself via iterative reflection. Please test Full code here.

import os
import json
import time
import typing
from dataclasses import dataclass, asdict
from google import genai
from google.genai import varieties


API_KEY = os.environ.get("GEMINI_API_KEY", "API Key")
shopper = genai.Consumer(api_key=API_KEY)


@dataclass
class AgentMessage:
   supply: str
   goal: str
   content material: str
   metadata: dict
   timestamp: float = time.time()

Arrange the core atmosphere by importing the required libraries, defining the API key, and initializing the Gemini shopper. It additionally establishes the AgentMessage construction, which serves as a shared type of communication between brokers. Please test Full code here.

class CognitiveEngine:
   @staticmethod
   def generate(immediate: str, system_instruction: str, json_mode: bool = False) -> str:
       config = varieties.GenerateContentConfig(
           temperature=0.1,
           response_mime_type="software/json" if json_mode else "textual content/plain"
       )
       strive:
           response = shopper.fashions.generate_content(
               mannequin="gemini-2.0-flash",
               contents=immediate,
               config=config
           )
           return response.textual content
       besides Exception as e:
           increase ConnectionError(f"Gemini API Error: {e}")


class SemanticRouter:
   def __init__(self, agents_registry: dict):
       self.registry = agents_registry


   def route(self, user_query: str) -> str:
       immediate = f"""
       You're a Grasp Dispatcher. Analyze the person request and map it to the ONE finest agent.
       AVAILABLE AGENTS:
       {json.dumps(self.registry, indent=2)}
       USER REQUEST: "{user_query}"
       Return ONLY a JSON object: {{"selected_agent": "agent_name", "reasoning": "transient motive"}}
       """
       response_text = CognitiveEngine.generate(immediate, "You're a routing system.", json_mode=True)
       strive:
           determination = json.hundreds(response_text)
           print(f"   [Router] Chosen: {determination['selected_agent']} (Purpose: {determination['reasoning']})")
           return determination['selected_agent']
       besides:
           return "general_agent"

Use Gemini to construct a cognitive layer that may generate each textual content and JSON output on command. We additionally implement a semantic router that analyzes queries and selects the perfect agent. Please test Full code here.

class Agent:
   def __init__(self, identify: str, instruction: str):
       self.identify = identify
       self.instruction = instruction


   def execute(self, message: AgentMessage) -> str:
       return CognitiveEngine.generate(
           immediate=f"Enter: {message.content material}",
           system_instruction=self.instruction
       )


class Orchestrator:
   def __init__(self):
       self.agents_info = {
           "analyst_bot": "Analyzes information, logic, and math. Returns structured JSON summaries.",
           "creative_bot": "Writes poems, tales, and inventive textual content. Returns plain textual content.",
           "coder_bot": "Writes Python code snippets."
       }
       self.employees = {
           "analyst_bot": Agent("analyst_bot", "You're a Information Analyst. output strict JSON."),
           "creative_bot": Agent("creative_bot", "You're a Inventive Author."),
           "coder_bot": Agent("coder_bot", "You're a Python Skilled. Return solely code.")
       }
       self.router = SemanticRouter(self.agents_info)

Construct employee brokers and a central orchestrator. Every agent has a transparent function, reminiscent of analyst, inventive, or coder, and also you configure the orchestrator to handle them. Assessment this part to discover ways to outline your agent ecosystem and put together for clever job delegation. Please test Full code here.

 def validate_constraint(self, content material: str, constraint_type: str) -> tuple[bool, str]:
       if constraint_type == "json_only":
           strive:
               json.hundreds(content material)
               return True, "Legitimate JSON"
           besides:
               return False, "Output was not legitimate JSON."
       if constraint_type == "no_markdown":
           if "```" in content material:
               return False, "Output accommodates Markdown code blocks, that are forbidden."
           return True, "Legitimate Textual content"
       return True, "Go"


   def run_task(self, user_input: str, constraint: str = None, max_retries: int = 2):
       print(f"n--- New Process: {user_input} ---")
       target_name = self.router.route(user_input)
       employee = self.employees.get(target_name)
       current_input = user_input
       historical past = []
       for try in vary(max_retries + 1):
           strive:
               msg = AgentMessage(supply="Person", goal=target_name, content material=current_input, metadata={})
               print(f"   [Exec] {employee.identify} working... (Try {try+1})")
               outcome = employee.execute(msg)
               if constraint:
                   is_valid, error_msg = self.validate_constraint(outcome, constraint)
                   if not is_valid:
                       print(f"   [Guardrail] VIOLATION: {error_msg}")
                       current_input = f"Your earlier reply failed a test.nOriginal Request: {user_input}nYour Reply: {outcome}nError: {error_msg}nFIX IT instantly."
                       proceed
               print(f"   [Success] Closing Output:n{outcome[:100]}...")
               return outcome
           besides Exception as e:
               print(f"   [System Error] {e}")
               time.sleep(1)
       print("   [Failed] Max retries reached or self-correction failed.")
       return None

Implement symbolic guardrails and self-correcting loops to implement constraints reminiscent of strict JSON and no markdown. Carry out iterative enchancment every time the output violates necessities, permitting the agent to appropriate its personal errors. Please test Full code here.

if __name__ == "__main__":
   orchestrator = Orchestrator()
   orchestrator.run_task(
       "Examine the GDP of France and Germany in 2023.",
       constraint="json_only"
   )
   orchestrator.run_task(
       "Write a Python operate for Fibonacci numbers.",
       constraint="no_markdown"
   )

We run two full situations to exhibit routing, agent execution, and constraint validation in motion. Observe recursive habits by performing evaluation duties with JSON and coding duties with markdown constraints.

In conclusion, you may see how a number of parts reminiscent of routing, employee brokers, guardrails, and self-modification work collectively to create a dependable and clever agent system. See how every half contributes to sturdy job execution, protecting the output correct, calibrated, and constraint-aware. Trying again at your structure, you may see how simply it may be prolonged with new brokers, richer constraints, or extra superior inference methods.


Please test Full code here. Please be at liberty to test it out GitHub page for tutorials, code, and notebooks. Additionally, be at liberty to observe us Twitter Remember to affix us 100,000+ ML subreddits and subscribe our newsletter. cling on! Are you on telegram? You can now also participate by telegram.


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 newest endeavor is the launch of Marktechpost, a man-made intelligence media platform. It stands out for its thorough protection of machine studying and deep studying information, which is technically sound and simply understood by a large viewers. The platform boasts over 2 million views per thirty days, demonstrating its reputation amongst viewers.

banner
Top Selling Multipurpose WP Theme

Converter

Top Selling Multipurpose WP Theme

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

banner
Top Selling Multipurpose WP Theme

Leave a Comment

banner
Top Selling Multipurpose WP Theme

Latest

Best selling

22000,00 $
16000,00 $
6500,00 $
5999,00 $

Top rated

6500,00 $
22000,00 $
900000,00 $

Products

Knowledge Unleashed
Knowledge Unleashed

Welcome to Ivugangingo!

At Ivugangingo, we're passionate about delivering insightful content that empowers and informs our readers across a spectrum of crucial topics. Whether you're delving into the world of insurance, navigating the complexities of cryptocurrency, or seeking wellness tips in health and fitness, we've got you covered.