Agent Architecture
Kaise agents ka design hota hai, kaise components connect hote hain, aur different architectures kya kya kar sakte hain � ye sab samajhte hain.
Why Agent Architecture?
Agent architecture samajhna zaroori hai kyunki ye decide karta hai ki tumhara agent kitna capable hoga. Sahi architecture choose karne se agent efficiently task complete kar sakta hai, galat architecture se agent stuck ho jaata hai.
Ek simple chatbot aur ek powerful agent mein difference sirf architecture ka hai. Architecture decides karta hai ki agent kitna soch sakta hai, kitne tools use kar sakta hai, aur kitna context yaad rakh sakta hai.
Core Concepts
✓ Agent Architecture Overview
REACT Pattern � Deep Dive
REACT (Reason + Act) sabse popular agent pattern hai. Ye 3 steps mein kaam karta hai:
Step 1: Reason (Socho)
Agent sochta hai ki current situation mein kya karna chahiye. Ye apne memory aur context ko dekh kar decide karta hai.
Step 2: Act (Karo)
Agent koi tool call karta hai � jaise search karna, calculate karna, ya database se data nikalna.
Step 3: Observe (Dekho)
Agent result dekhta hai. Kya goal achieve hua✓ Agar nahi, toh wapas reason pe jaata hai.
ReAct Agent � Code
Ye ek complete ReAct agent implementation hai. Isko samjho � har function ka kya kaam hai:
# ReAct Agent Pattern class ReActAgent: def __init__(self, tools): self.tools = tools self.memory = [] def reason(self, task): # Think about what to do thought = f"I need to: {task}" self.memory.append({"thought": thought}) return thought def act(self, tool_name, input_data): # Execute action if tool_name in self.tools: result = self.tools[tool_name](input_data) self.memory.append({ "action": tool_name, "result": result }) return result return "Tool not found" def observe(self, result): # Check if goal achieved return len(self.memory) > 3 # Usage tools = { "search": lambda x: f"Results for: {x}", "calculate": lambda x: eval(x) } agent = ReActAgent(tools) print(agent.reason("Find Python tutorials")) print(agent.act("search", "Python tutorials"))
✓ Function Calling Pattern
Function calling mein agent directly tools ko call karta hai. Ye structured output deta hai:
import json class FunctionCallingAgent: def __init__(self): self.functions = {} def register(self, name, func, schema): self.functions[name] = { "func": func, "schema": schema } def get_schemas(self): return { name: info["schema"] for name, info in self.functions.items() } def execute(self, name, args): if name in self.functions: return self.functions[name]["func"](**args) return {"error": f"Unknown function: {name}"} # Register tools agent = FunctionCallingAgent() agent.register( "search_web", lambda query=None: {"results": [f"Found: {query}"]}, { "name": "search_web", "description": "Search the web", "parameters": { "type": "object", "properties": { "query": {"type": "string"} } } } ) # Get schemas for LLM print(json.dumps(agent.get_schemas(), indent=2))
✓ Observation Loop � Complete Cycle
Observation loop mein agent continuously perceive-think-act karta hai. Ye real-time systems ke liye perfect hai:
class ObservationLoopAgent: def __init__(self, env): self.env = env self.state = {} self.history = [] def perceive(self): # Observe environment observation = self.env.get_state() self.history.append({ "type": "perception", "data": observation }) return observation def think(self, observation): # Decide action based on observation if observation.get("danger"): return {"action": "flee"} elif observation.get("opportunity"): return {"action": "approach"} return {"action": "wait"} def act(self, decision): # Execute the chosen action result = self.env.execute(decision["action"]) self.history.append({ "type": "action", "decision": decision, "result": result }) return result def run(self, max_steps=100): for i in range(max_steps): obs = self.perceive() decision = self.think(obs) result = self.act(decision) if result.get("done"): break return self.history
Interactive Editor � Build Your Agent
Neeche code likho ya edit karo, phir "Run" pe click karo. Dekho kaise agent kaam karta hai:
✓ State Management
Agent ko context yaad rakhna hota hai. Ye 3 types ka hota hai:
Key Takeaways
- REACT Pattern sabse fundamental hai � Reason ✓ Act ✓ Observe cycle
- Function Calling se agent tools ko structured way mein call kar sakta hai
- Observation Loop continuous feedback se agent improve hota rehta hai
- State Management agent ko context yaad rakhne mein help karta hai
- Sahi architecture choose karna project requirements pe depend karta hai
ReAct pattern ka matlab hai Reason + Act. Agent pehle sochta hai (reason), phir koi action leta hai (act), aur phir result dekhta hai (observe). Ye cycle tab tak repeat hoti hai jab tak goal achieve na ho jaaye.