Build an AI agent frame based on multiple graphics for complex tasks automation

by Brenden Burgess

When you buy through links on our site, we may earn a commission at no extra cost to you. However, this does not influence our evaluations.

In this tutorial, we guide you through the development of an advanced graphic agent, fueled by the Google Gemini API. Our goal is to build smart agents in several stages that perform tasks through a well -defined graphical structure of the interconnected nodes. Each node represents a specific function, ranging from the input taking, the execution of the logical treatment, the decision -making and the production of outputs. We use Python, Networkx for graphics modeling and Matplotlib for visualization. At the end, we implement and execute two complete examples, a research assistant and a problem solver, to demonstrate how the frame can effectively manage complex reasoning workflows.

!pip install -q google-generativeai networkx matplotlib


import google.generativeai as genai
import networkx as nx
import matplotlib.pyplot as plt
from typing import Dict, List, Any, Callable
import json
import asyncio
from dataclasses import dataclass
from enum import Enum


API_KEY = "use your API key here"
genai.configure(api_key=API_KEY)

We start by installing the necessary libraries, Google-Generatifai, Networkx and Matplotlib, to take care of our manager based on graphics. After importing essential modules, we configure the Gemini API using our API key to activating powerful content generation capacities within our agent system.

Discover the Codes.

class NodeType(Enum):
    INPUT = "input"
    PROCESS = "process"
    DECISION = "decision"
    OUTPUT = "output"


@dataclass
class AgentNode:
    id: str
    type: NodeType
    prompt: str
    function: Callable = None
    dependencies: List(str) = None

We define an enumeration of NODYPE to classify different types of agent nodes: entry, process, decision and exit. Then, using an agentnod for data closing, we structure each node with an ID, a type, a prompt, an optional function and a list of outbuildings, allowing us to create a modular and flexible graphic.

def create_research_agent():
    agent = GraphAgent()
   
    # Input node
    agent.add_node(AgentNode(
        id="topic_input",
        type=NodeType.INPUT,
        prompt="Research topic input"
    ))
   
    agent.add_node(AgentNode(
        id="research_plan",
        type=NodeType.PROCESS,
        prompt="Create a comprehensive research plan for the topic. Include 3-5 key research questions and methodology.",
        dependencies=("topic_input")
    ))
   
    agent.add_node(AgentNode(
        id="literature_review",
        type=NodeType.PROCESS,
        prompt="Conduct a thorough literature review. Identify key papers, theories, and current gaps in knowledge.",
        dependencies=("research_plan")
    ))
   
    agent.add_node(AgentNode(
        id="analysis",
        type=NodeType.PROCESS,
        prompt="Analyze the research findings. Identify patterns, contradictions, and novel insights.",
        dependencies=("literature_review")
    ))
   
    agent.add_node(AgentNode(
        id="quality_check",
        type=NodeType.DECISION,
        prompt="Evaluate research quality. Is the analysis comprehensive? Are there missing perspectives? Return 'APPROVED' or 'NEEDS_REVISION' with reasons.",
        dependencies=("analysis")
    ))
   
    agent.add_node(AgentNode(
        id="final_report",
        type=NodeType.OUTPUT,
        prompt="Generate a comprehensive research report with executive summary, key findings, and recommendations.",
        dependencies=("quality_check")
    ))
   
    return agent

We create a research agent by sequentially adding the specialized nodes to the graphic. From a subject entry, we define a process flow that includes planning, literature review and analysis. The agent then makes a quality -based quality decision and finally generates a complete research report, capturing the full life cycle of a structured research workflow.

Discover the Codes.

def create_problem_solver():
    agent = GraphAgent()
   
    agent.add_node(AgentNode(
        id="problem_input",
        type=NodeType.INPUT,
        prompt="Problem statement"
    ))
   
    agent.add_node(AgentNode(
        id="problem_analysis",
        type=NodeType.PROCESS,
        prompt="Break down the problem into components. Identify constraints and requirements.",
        dependencies=("problem_input")
    ))
   
    agent.add_node(AgentNode(
        id="solution_generation",
        type=NodeType.PROCESS,
        prompt="Generate 3 different solution approaches. For each, explain the methodology and expected outcomes.",
        dependencies=("problem_analysis")
    ))
   
    agent.add_node(AgentNode(
        id="solution_evaluation",
        type=NodeType.DECISION,
        prompt="Evaluate each solution for feasibility, cost, and effectiveness. Rank them and select the best approach.",
        dependencies=("solution_generation")
    ))
   
    agent.add_node(AgentNode(
        id="implementation_plan",
        type=NodeType.OUTPUT,
        prompt="Create a detailed implementation plan with timeline, resources, and success metrics.",
        dependencies=("solution_evaluation")
    ))
   
    return agent

We build a problem -solving agent by defining a logical sequence of nodes, from receiving the problem of the problem. The agent analyzes the problem, generates several solution approaches, assesses them according to feasibility and efficiency, and concludes by producing a structured implementation plan, allowing automated resolution and step by step of the problem.

Discover the Codes.

def run_research_demo():
    """Run the research agent demo"""
    print("🚀 Advanced Graph Agent Framework Demo")
    print("=" * 50)
   
    research_agent = create_research_agent()
    print("\n📊 Research Agent Graph Structure:")
    research_agent.visualize()
   
    print("\n🔍 Executing Research Task...")
   
    research_agent.results("topic_input") = "Artificial Intelligence in Healthcare"
   
    execution_order = list(nx.topological_sort(research_agent.graph))
   
    for node_id in execution_order:
        if node_id == "topic_input":
            continue
           
        context = {}
        node = research_agent.nodes(node_id)
       
        if node.dependencies:
            for dep in node.dependencies:
                context(dep) = research_agent.results.get(dep, "")
       
        prompt = node.prompt
        if context:
            context_str = "\n".join((f"{k}: {v}" for k, v in context.items()))
            prompt = f"Context:\n{context_str}\n\nTask: {prompt}"
       
        try:
            response = research_agent.model.generate_content(prompt)
            result = response.text.strip()
            research_agent.results(node_id) = result
            print(f"✓ {node_id}: {result(:100)}...")
        except Exception as e:
            research_agent.results(node_id) = f"Error: {str(e)}"
            print(f"✗ {node_id}: Error - {str(e)}")
   
    print("\n📋 Research Results:")
    for node_id, result in research_agent.results.items():
        print(f"\n{node_id.upper()}:")
        print("-" * 30)
        print(result)
   
    return research_agent.results


def run_problem_solver_demo():
    """Run the problem solver demo"""
    print("\n" + "=" * 50)
    problem_solver = create_problem_solver()
    print("\n🛠️ Problem Solver Graph Structure:")
    problem_solver.visualize()
   
    print("\n⚙️ Executing Problem Solving...")
   
    problem_solver.results("problem_input") = "How to reduce carbon emissions in urban transportation"
   
    execution_order = list(nx.topological_sort(problem_solver.graph))
   
    for node_id in execution_order:
        if node_id == "problem_input":
            continue
           
        context = {}
        node = problem_solver.nodes(node_id)
       
        if node.dependencies:
            for dep in node.dependencies:
                context(dep) = problem_solver.results.get(dep, "")
       
        prompt = node.prompt
        if context:
            context_str = "\n".join((f"{k}: {v}" for k, v in context.items()))
            prompt = f"Context:\n{context_str}\n\nTask: {prompt}"
       
        try:
            response = problem_solver.model.generate_content(prompt)
            result = response.text.strip()
            problem_solver.results(node_id) = result
            print(f"✓ {node_id}: {result(:100)}...")
        except Exception as e:
            problem_solver.results(node_id) = f"Error: {str(e)}"
            print(f"✗ {node_id}: Error - {str(e)}")
   
    print("\n📋 Problem Solving Results:")
    for node_id, result in problem_solver.results.items():
        print(f"\n{node_id.upper()}:")
        print("-" * 30)
        print(result)
   
    return problem_solver.results


print("🎯 Running Research Agent Demo:")
research_results = run_research_demo()


print("\n🎯 Running Problem Solver Demo:")
problem_results = run_problem_solver_demo()


print("\n✅ All demos completed successfully!")

We conclude the tutorial by performing two powerful demonstration agents, one for research and another for problem solving. In each case, we view the structure of the graph, initialize the entry and enforce the node by the agent's node using a topological order. With Gemini generating contextual responses at each stage, we observe how each agent progresses independently through planning, analysis, decision -making and exit generation, ultimately presenting the full potential of our frame based on graphics.

In conclusion, we managed to develop and execute smart agents who break down and resolve steps step by step, using architecture focused on graphics. We see how each node deals with the proportion of the context, exploits Gemini's capacities for the generation of content and transmits the results to the following nodes. This modular design improves flexibility and also allows us to clearly visualize the logical flow.

Discover the Codes. All the merit of this research goes to researchers in this project. Subscribe now to our newsletter IA


Screen Shot 2021 09 14 at 9.02.24 AM

Asif Razzaq is the CEO of Marktechpost Media Inc .. as a visionary entrepreneur and engineer, AIF undertakes to exploit the potential of artificial intelligence for social good. His most recent company is the launch of an artificial intelligence media platform, Marktechpost, which stands out from its in-depth coverage of automatic learning and in-depth learning news which are both technically solid and easily understandable by a large audience. The platform has more than 2 million monthly views, illustrating its popularity with the public.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.