हाथों-हाथ करके देखें: मिनी-AGI इंजन
एक फ़ाइल, लगभग सौ लाइनें। यह प्लान बनाता है, कोड लिखता है, उसे चलाता है, एरर पढ़ता है, और खुद को ठीक करता है। ऊपर बताई गई हर चीज़, अब असलियत में।
60 सेकंड में
हाथों-हाथ करके देखें: मिनी-AGI इंजन
एक फ़ाइल, लगभग सौ लाइनें। यह प्लान बनाता है, कोड लिखता है, उसे चलाता है, एरर पढ़ता है, और खुद को ठीक करता है। ऊपर बताई गई हर चीज़, अब असलियत में।
pip install google-genai
export GEMINI_API_KEY="your-key-here"भाग 1 · निष्पादक (एग्ज़ीक्यूटर)
import os
import subprocess
import sys
from google import genai
from google.genai import types
client = genai.Client() # reads GEMINI_API_KEY from the environment
def run_python_code(code_string):
"""Execute python code in a child process and return output or error."""
filename = "temp_agent_sandbox.py"
with open(filename, "w") as f:
f.write(code_string)
try:
result = subprocess.run(
[sys.executable, filename],
capture_output=True,
text=True,
timeout=10, # a runaway loop must not hang the agent
)
if result.returncode != 0:
return f"EXECUTION ERROR:\n{result.stderr}"
return f"SUCCESS OUTPUT:\n{result.stdout}"
except Exception as e:
return f"SYSTEM EXCEPTION: {str(e)}"
finally:
if os.path.exists(filename):
os.remove(filename)temp_agent_sandbox.py कोई सैंडबॉक्स नहीं है। एक सब-प्रोसेस पैरेंट प्रोसेस को क्रैश होने से बचाता है; यह आपकी मशीन को कोड से नहीं बचाता। उस चाइल्ड प्रोसेस के पास आपका फ़ाइल सिस्टम, आपका नेटवर्क और आपके एनवायरनमेंट वेरिएबल्स होते हैं। यह आपके अपने लैपटॉप पर, आपके अपने लिखे लक्ष्य के साथ एक ट्यूटोरियल के लिए ठीक है। यह किसी और चीज़ के लिए बिल्कुल ठीक नहीं है — मॉड्यूल 66 इसे सही तरीके से रिप्लेस करेगा।भाग 2 · कॉग्निटिव लूप
def agi_cognitive_loop(goal: str, max_iterations: int = 3):
"""Plan -> execute -> evaluate -> self-correct -> achieve goal."""
print(f"\n[AGI Goal Initialized]: {goal}\n" + "=" * 40)
system_prompt = (
"You are an autonomous AGI reasoning agent. Your job is to achieve the "
"user's goal by writing Python code blocks. Respond in two parts:\n"
"1. THOUGHT: explain your step-by-step logic.\n"
"2. CODE: enclosed in a python fenced block, containing executable code "
"that solves or tests the step.\n"
"If a previous step failed, analyse the error output and write "
"corrected code."
)
# Working memory: the growing record of what has been tried.
conversation_history = f"Goal: {goal}"
for iteration in range(1, max_iterations + 1):
print(f"\n--- [Iteration {iteration} of {max_iterations}] ---")
# DECIDE
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=conversation_history,
config=types.GenerateContentConfig(
system_instruction=system_prompt,
temperature=0.2, # low temperature: strict logical adherence
),
)
model_output = response.text
print(f"Agent thought and plan:\n{model_output}\n")
fence = chr(96) * 3 + "python" # the python code fence marker
if fence not in model_output:
print("No executable code block found this turn.")
conversation_history += (
f"\n\nIteration {iteration}: no code provided. "
"Write code to test your logic."
)
continue
# ACT
code_block = model_output.split(fence)[1].split(chr(96) * 3)[0].strip()
print("Executing agent-generated code...")
execution_result = run_python_code(code_block)
print(execution_result)
# OBSERVE — the result becomes context for the next decision
conversation_history += (
f"\n\nIteration {iteration} code output:\n{execution_result}"
)
if "SUCCESS OUTPUT" in execution_result:
print("\n[AGI Status]: goal reached and verified via execution.")
return
print("\n[AGI Status]: max iterations reached without closure.")
if __name__ == "__main__":
goal = (
"Write a python script that calculates the first 10 numbers of the "
"Fibonacci sequence, verifies mathematically that each number is the "
"sum of the two preceding ones, and prints 'VERIFICATION PASSED'."
)
agi_cognitive_loop(goal)इसे चलाने से पहले ठीक करने वाली दो चीज़ें
- 1
कमेंट मार्कर
पायथन में कमेंट#से शुरू होते हैं,//से नहीं।temperature=0.2 // Low temperatureजैसी लाइन एक सिंटैक्स एरर है — पायथन//को फ़्लोर डिवीज़न समझता है और फिर शब्दों पर अटक जाता है। ऊपर इसे ठीक कर दिया गया है। - 2
कोड फ़ेंस को बाहर निकालना
पायथन स्ट्रिंग के अंदर तीन बैक-टिक मार्कर लिखना अजीब होता है और इसमें गलती होना आसान है।chr(96) * 3बिना किसी एस्केपिंग झंझट के इसे बना देता है, इसीलिए ऊपर वाला वर्ज़न ऐसा करता है।
इसे चलाएं
[AGI Goal Initialized]: Write a python script that calculates...
========================================
--- [Iteration 1 of 3] ---
Agent thought and plan:
THOUGHT: I will generate the sequence iteratively, then assert the
recurrence relation for every element from index 2 onward...
Executing agent-generated code...
SUCCESS OUTPUT:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
VERIFICATION PASSED
[AGI Status]: goal reached and verified via execution.और देखें और पढ़ें
लैब
मिनी-AGI इंजन, चल रहा है और खुद को सुधार रहा है।
समस्या
goal = ("Parse this malformed CSV and print the mean of column 3. "
"Some rows have missing fields and one has a stray quote.")आपका काम तब पूरा होगा जब
कठिन सवाल
उत्तर देखने से पहले ख़ुद जवाब देने की कोशिश करें। अगर आप इनके जवाब दे सकते हैं, तो आप पाठ समझ गए हैं।
Q1आपके एजेंट ने एरर तो ठीक कर दिया, लेकिन सुधार यह था कि सब कुछ try/except में लपेट दिया और कुछ भी प्रिंट नहीं किया। यह एग्ज़िट कोड 0 के साथ बाहर निकला। क्या फ़ेल हुआ?दिखाएं
अक्सर पूछे जाने वाले सवाल
टेंपरेचर 0.2 क्यों?
कम टेम्परेचर मॉडल से हाई-प्रोबेबिलिटी वाले टोकन चुनवाता है, जिसका मतलब कोड के लिए पारंपरिक, सिंटैक्स के लिहाज़ से सुरक्षित बनावट है। यहाँ रचनात्मकता एक नुक़सान है — आपको उबाऊ हल चाहिए जो बस चल जाए।
हर मोड़ पर पूरा इतिहास दोबारा क्यों भेजा जाता है?
क्योंकि मॉडल के पास कोई मेमोरी नहीं होती (मॉड्यूल 5)। बढ़ती हुई conversation_history स्ट्रिंग ही वर्किंग मेमोरी है। यही कारण भी है कि यह डिज़ाइन अंततः कॉन्टेक्स्ट लिमिट तक पहुँच जाता है — मॉड्यूल 67 इसे ठीक करता है।
क्या मैं कोई दूसरा मॉडल इस्तेमाल कर सकता हूँ?
हाँ। क्लाइंट को किसी भी ऐसे प्रोवाइडर से बदल लें जिसके पास कम्प्लीशन API हो। आर्किटेक्चर प्रोवाइडर पर निर्भर नहीं करता (provider-agnostic), और यही मुख्य बात है: बुद्धिमत्ता लूप में है, वेंडर में नहीं।
तीन इटरेशन कम लगते हैं।
यह एक समझदारी भरा डिफ़ॉल्ट है। तीसरे प्रयास के बाद सफलता की दर तेज़ी से गिरती है — जो मॉडल तीन बार फ़ेल हो चुका है, वह आमतौर पर सही रास्ते पर पहुँचने के बजाय किसी गलत रास्ते पर अड़ जाता है। अगर आप चाहें तो इसे बढ़ा लें, लेकिन साथ में ख़र्च की एक सीमा (spend cap) भी जोड़ें।
पाठ का टेस्ट
5 सवाल। पास होने और इस पाठ को पूरा करने के लिए 3 सही होने चाहिए (60%)।