Saturday, May 31, 2025
HomeArtificial IntelligenceA Coding Information for Constructing a Self-Bettering AI Agent Utilizing Google's Gemini...

A Coding Information for Constructing a Self-Bettering AI Agent Utilizing Google’s Gemini API with Clever Adaptation Options

On this tutorial, we’ll discover the right way to create a complicated Self-Bettering AI Agent utilizing Google’s cutting-edge Gemini API. This self-improving agent demonstrates autonomous problem-solving, dynamically evaluates efficiency, learns from successes and failures, and iteratively enhances its capabilities via reflective evaluation and self-modification. The tutorial walks via structured code implementation, detailing mechanisms for reminiscence administration, functionality monitoring, iterative job evaluation, resolution era, and efficiency analysis, all built-in inside a strong self-learning suggestions loop.

import google.generativeai as genai
import json
import time
import re
from typing import Dict, Checklist, Any
from datetime import datetime
import traceback

We arrange the foundational elements to construct an AI-powered self-improving agent using Google’s Generative AI API. Libraries resembling json, time, re, and datetime facilitate structured information administration, efficiency monitoring, and textual content processing, whereas sort hints (Dict, Checklist, Any) assist guarantee strong and maintainable code.

class SelfImprovingAgent:
    def __init__(self, api_key: str):
        """Initialize the self-improving agent with Gemini API"""
        genai.configure(api_key=api_key)
        self.mannequin = genai.GenerativeModel('gemini-1.5-flash')
       
        self.reminiscence = {
            'successful_strategies': [],
            'failed_attempts': [],
            'learned_patterns': [],
            'performance_metrics': [],
            'code_improvements': []
        }
       
        self.capabilities = {
            'problem_solving': 0.5,
            'code_generation': 0.5,
            'learning_efficiency': 0.5,
            'error_handling': 0.5
        }
       
        self.iteration_count = 0
        self.improvement_history = []
   
    def analyze_task(self, job: str) -> Dict[str, Any]:
        """Analyze a given job and decide method"""
        analysis_prompt = f"""
        Analyze this job and supply a structured method:
        Job: {job}
       
        Please present:
        1. Job complexity (1-10)
        2. Required expertise
        3. Potential challenges
        4. Really useful method
        5. Success standards
       
        Format as JSON.
        """
       
        attempt:
            response = self.mannequin.generate_content(analysis_prompt)
            json_match = re.search(r'{.*}', response.textual content, re.DOTALL)
            if json_match:
                return json.masses(json_match.group())
            else:
                return {
                    "complexity": 5,
                    "expertise": ["general problem solving"],
                    "challenges": ["undefined requirements"],
                    "method": "iterative enchancment",
                    "success_criteria": ["task completion"]
                }
        besides Exception as e:
            print(f"Job evaluation error: {e}")
            return {"complexity": 5, "expertise": [], "challenges": [], "method": "primary", "success_criteria": []}
   
    def solve_problem(self, downside: str) -> Dict[str, Any]:
        """Try to unravel an issue utilizing present capabilities"""
        self.iteration_count += 1
        print(f"n=== Iteration {self.iteration_count} ===")
        print(f"Drawback: {downside}")
       
        task_analysis = self.analyze_task(downside)
        print(f"Job Evaluation: {task_analysis}")
       
        solution_prompt = f"""
        Based mostly on my earlier studying and capabilities, remedy this downside:
        Drawback: {downside}
       
        My present capabilities: {self.capabilities}
        Earlier profitable methods: {self.reminiscence['successful_strategies'][-3:]}  # Final 3
        Identified patterns: {self.reminiscence['learned_patterns'][-3:]}  # Final 3
       
        Present an in depth resolution with:
        1. Step-by-step method
        2. Code implementation (if relevant)
        3. Anticipated final result
        4. Potential enhancements
        """
       
        attempt:
            start_time = time.time()
            response = self.mannequin.generate_content(solution_prompt)
            solve_time = time.time() - start_time
           
            resolution = {
                'downside': downside,
                'resolution': response.textual content,
                'solve_time': solve_time,
                'iteration': self.iteration_count,
                'task_analysis': task_analysis
            }
           
            quality_score = self.evaluate_solution(resolution)
            resolution['quality_score'] = quality_score
           
            self.reminiscence['performance_metrics'].append({
                'iteration': self.iteration_count,
                'high quality': quality_score,
                'time': solve_time,
                'complexity': task_analysis.get('complexity', 5)
            })
           
            if quality_score > 0.7:
                self.reminiscence['successful_strategies'].append(resolution)
                print(f"✅ Answer High quality: {quality_score:.2f} (Success)")
            else:
                self.reminiscence['failed_attempts'].append(resolution)
                print(f"❌ Answer High quality: {quality_score:.2f} (Wants Enchancment)")
           
            return resolution
           
        besides Exception as e:
            print(f"Drawback fixing error: {e}")
            error_solution = {
                'downside': downside,
                'resolution': f"Error occurred: {str(e)}",
                'solve_time': 0,
                'iteration': self.iteration_count,
                'quality_score': 0.0,
                'error': str(e)
            }
            self.reminiscence['failed_attempts'].append(error_solution)
            return error_solution
   
    def evaluate_solution(self, resolution: Dict[str, Any]) -> float:
        """Consider the standard of an answer"""
        evaluation_prompt = f"""
        Consider this resolution on a scale of 0.0 to 1.0:
       
        Drawback: {resolution['problem']}
        Answer: {resolution['solution'][:500]}...  # Truncated for analysis
       
        Price primarily based on:
        1. Completeness (addresses all points)
        2. Correctness (logically sound)
        3. Readability (effectively defined)
        4. Practicality (implementable)
        5. Innovation (artistic method)
       
        Reply with only a decimal quantity between 0.0 and 1.0.
        """
       
        attempt:
            response = self.mannequin.generate_content(evaluation_prompt)
            score_match = re.search(r'(d+.?d*)', response.textual content)
            if score_match:
                rating = float(score_match.group(1))
                return min(max(rating, 0.0), 1.0)  
            return 0.5  
        besides:
            return 0.5
   
    def learn_from_experience(self):
        """Analyze previous efficiency and enhance capabilities"""
        print("n🧠 Studying from expertise...")
       
        if len(self.reminiscence['performance_metrics']) < 2:
            return
       
        learning_prompt = f"""
        Analyze my efficiency and counsel enhancements:
       
        Current Efficiency Metrics: {self.reminiscence['performance_metrics'][-5:]}
        Profitable Methods: {len(self.reminiscence['successful_strategies'])}
        Failed Makes an attempt: {len(self.reminiscence['failed_attempts'])}
       
        Present Capabilities: {self.capabilities}
       
        Present:
        1. Efficiency tendencies evaluation
        2. Recognized weaknesses
        3. Particular enchancment strategies
        4. New functionality scores (0.0-1.0 for every functionality)
        5. New patterns realized
       
        Format as JSON with keys: evaluation, weaknesses, enhancements, new_capabilities, patterns
        """
       
        attempt:
            response = self.mannequin.generate_content(learning_prompt)
           
            json_match = re.search(r'{.*}', response.textual content, re.DOTALL)
            if json_match:
                learning_results = json.masses(json_match.group())
               
                if 'new_capabilities' in learning_results:
                    old_capabilities = self.capabilities.copy()
                    for functionality, rating in learning_results['new_capabilities'].objects():
                        if functionality in self.capabilities:
                            self.capabilities[capability] = min(max(float(rating), 0.0), 1.0)
                   
                    print(f"📈 Functionality Updates:")
                    for cap, (previous, new) in zip(self.capabilities.keys(),
                                             zip(old_capabilities.values(), self.capabilities.values())):
                        change = new - previous
                        print(f"  {cap}: {previous:.2f} → {new:.2f} ({change:+.2f})")
               
                if 'patterns' in learning_results:
                    self.reminiscence['learned_patterns'].prolong(learning_results['patterns'])
               
                self.improvement_history.append({
                    'iteration': self.iteration_count,
                    'timestamp': datetime.now().isoformat(),
                    'learning_results': learning_results,
                    'capabilities_before': old_capabilities,
                    'capabilities_after': self.capabilities.copy()
                })
               
                print(f"✨ Realized {len(learning_results.get('patterns', []))} new patterns")
               
        besides Exception as e:
            print(f"Studying error: {e}")
   
    def generate_improved_code(self, current_code: str, improvement_goal: str) -> str:
        """Generate improved model of code"""
        improvement_prompt = f"""
        Enhance this code primarily based on the aim:
       
        Present Code:
        {current_code}
       
        Enchancment Objective: {improvement_goal}
        My present capabilities: {self.capabilities}
        Realized patterns: {self.reminiscence['learned_patterns'][-3:]}
       
        Present improved code with:
        1. Enhanced performance
        2. Higher error dealing with
        3. Improved effectivity
        4. Clear feedback explaining enhancements
        """
       
        attempt:
            response = self.mannequin.generate_content(improvement_prompt)
           
            improved_code = {
                'authentic': current_code,
                'improved': response.textual content,
                'aim': improvement_goal,
                'iteration': self.iteration_count
            }
           
            self.reminiscence['code_improvements'].append(improved_code)
            return response.textual content
           
        besides Exception as e:
            print(f"Code enchancment error: {e}")
            return current_code
   
    def self_modify(self):
        """Try to enhance the agent's personal code"""
        print("n🔧 Trying self-modification...")
       
        current_method = """
        def solve_problem(self, downside: str) -> Dict[str, Any]:
            # Present implementation
            go
        """
       
        improved_method = self.generate_improved_code(
            current_method,
            "Make downside fixing extra environment friendly and correct"
        )
       
        print("Generated improved methodology construction")
        print("Be aware: Precise self-modification requires cautious implementation in manufacturing")
   
    def run_improvement_cycle(self, issues: Checklist[str], cycles: int = 3):
        """Run a whole enchancment cycle"""
        print(f"🚀 Beginning {cycles} enchancment cycles with {len(issues)} issues")
       
        for cycle in vary(cycles):
            print(f"n{'='*50}")
            print(f"IMPROVEMENT CYCLE {cycle + 1}/{cycles}")
            print(f"{'='*50}")
           
            cycle_results = []
            for downside in issues:
                outcome = self.solve_problem(downside)
                cycle_results.append(outcome)
                time.sleep(1)  
           
            self.learn_from_experience()
           
            if cycle < cycles - 1:
                self.self_modify()
           
            avg_quality = sum(r.get('quality_score', 0) for r in cycle_results) / len(cycle_results)
            print(f"n📊 Cycle {cycle + 1} Abstract:")
            print(f"  Common Answer High quality: {avg_quality:.2f}")
            print(f"  Present Capabilities: {self.capabilities}")
            print(f"  Complete Patterns Realized: {len(self.reminiscence['learned_patterns'])}")
           
            time.sleep(2)
   
    def get_performance_report(self) -> str:
        """Generate a complete efficiency report"""
        if not self.reminiscence['performance_metrics']:
            return "No efficiency information out there but."
       
        metrics = self.reminiscence['performance_metrics']
        avg_quality = sum(m['quality'] for m in metrics) / len(metrics)
        avg_time = sum(m['time'] for m in metrics) / len(metrics)
       
        report = f"""
        📈 AGENT PERFORMANCE REPORT
        {'='*40}
       
        Complete Iterations: {self.iteration_count}
        Common Answer High quality: {avg_quality:.3f}
        Common Resolve Time: {avg_time:.2f}s
       
        Profitable Options: {len(self.reminiscence['successful_strategies'])}
        Failed Makes an attempt: {len(self.reminiscence['failed_attempts'])}
        Success Price: {len(self.reminiscence['successful_strategies']) / max(1, self.iteration_count) * 100:.1f}%
       
        Present Capabilities:
        {json.dumps(self.capabilities, indent=2)}
       
        Patterns Realized: {len(self.reminiscence['learned_patterns'])}
        Code Enhancements: {len(self.reminiscence['code_improvements'])}
        """
       
        return report

We outline the above class, SelfImprovingAgent, as implementing a strong framework leveraging Google’s Gemini API for autonomous task-solving, self-assessment, and adaptive studying. It incorporates structured reminiscence methods, functionality monitoring, iterative problem-solving with steady enchancment cycles, and even makes an attempt managed self-modification. This superior implementation permits the agent to progressively improve its accuracy, effectivity, and problem-solving sophistication over time, making a dynamic AI that may autonomously evolve and adapt.

def important():
    """Predominant operate to reveal the self-improving agent"""
   
    API_KEY = "Use Your GEMINI KEY Right here"
   
    if API_KEY == "Use Your GEMINI KEY Right here":
        print("⚠️  Please set your Gemini API key within the API_KEY variable")
        print("Get your API key from: https://makersuite.google.com/app/apikey")
        return
   
    agent = SelfImprovingAgent(API_KEY)
   
    test_problems = [
        "Write a function to calculate the factorial of a number",
        "Create a simple text-based calculator that handles basic operations",
        "Design a system to find the shortest path between two points in a graph",
        "Implement a basic recommendation system for movies based on user preferences",
        "Create a machine learning model to predict house prices based on features"
    ]
   
    print("🤖 Self-Bettering Agent Demo")
    print("This agent will try to unravel issues and enhance over time")
   
    agent.run_improvement_cycle(test_problems, cycles=3)
   
    print("n" + agent.get_performance_report())
   
    print("n" + "="*50)
    print("TESTING IMPROVED AGENT")
    print("="*50)
   
    final_problem = "Create an environment friendly algorithm to type a big dataset"
    final_result = agent.solve_problem(final_problem)
   
    print(f"nFinal Drawback Answer High quality: {final_result.get('quality_score', 0):.2f}")

The primary() operate serves because the entry level for demonstrating the SelfImprovingAgent class. It initializes the agent with the consumer’s Gemini API key and defines sensible programming and system design duties. The agent then iteratively tackles these duties, analyzing its efficiency to refine its problem-solving skills over a number of enchancment cycles. Lastly, it checks the agent’s enhanced capabilities with a brand new advanced job, showcasing measurable progress and offering an in depth efficiency report.

def setup_instructions():
    """Print setup directions for Google Colab"""
    directions = """
    📋 SETUP INSTRUCTIONS FOR GOOGLE COLAB:
   
    1. Set up the Gemini API shopper:
       !pip set up google-generativeai
   
    2. Get your Gemini API key:
       - Go to https://makersuite.google.com/app/apikey
       - Create a brand new API key
       - Copy the important thing
   
    3. Change 'your-gemini-api-key-here' together with your precise API key
   
    4. Run the code!
   
    🔧 CUSTOMIZATION OPTIONS:
    - Modify test_problems checklist so as to add your individual challenges
    - Regulate enchancment cycles rely
    - Add new capabilities to trace
    - Lengthen the educational mechanisms
   
    💡 IMPROVEMENT IDEAS:
    - Add persistent reminiscence (save/load agent state)
    - Implement extra subtle analysis metrics
    - Add domain-specific downside sorts
    - Create visualization of enchancment over time
    """
    print(directions)


if __name__ == "__main__":
    setup_instructions()
    print("n" + "="*60)
    important()

Lastly, we outline the setup_instructions() operate, which guides customers via making ready their Google Colab setting to run the self-improving agent. It explains step-by-step the right way to set up dependencies, arrange and configure the Gemini API key, and spotlight numerous choices for customizing and enhancing the agent’s performance. This method simplifies consumer onboarding, facilitating simple experimentation and increasing the agent’s capabilities additional.

In conclusion, the implementation demonstrated on this tutorial presents a complete framework for creating AI brokers that carry out duties and actively improve their capabilities over time. By harnessing the Gemini API’s superior generative energy and integrating a structured self-improvement loop, builders can construct brokers able to subtle reasoning, iterative studying, and self-modification.


Try the Pocket book on GitHub. All credit score for this analysis goes to the researchers of this challenge. Additionally, be happy to comply with us on Twitter and don’t neglect to affix our 95k+ 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 recognition amongst audiences.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments