AI Agents
AI যেভাবে নিজে decision নিয়ে কাজ করে।
Chatbot prompt এর জবাব দেয়। কিন্তু AI Agent? সে নিজে decide করে — কোন tool ব্যবহার করবে, কত step এ task complete হবে, কোথায় থামবে। AutoGPT, BabyAGI, Cursor agent, Devin — সব Agent। মানুষ এর মত decision-making AI — NLP এর সর্বোচ্চ frontier।
AI Agent = LLM + Tools + Memory + Planning + Loop। Core pattern: ReAct (Reason → Act → Observe → Repeat)। LLM ভাবে কোন tool call করতে হবে (search, calculator, code, API), tool execute হয়, result observe করে — পরের step decide। Modern frameworks: LangChain Agents, LangGraph, AutoGen, CrewAI। Multi-agent system এ multiple agent collaborate করে।
ভাবুন একজন assistant কে বললেন 'কাল ঢাকা থেকে কক্সবাজার এর flight book কর'। সে: (1) calendar check করে kal এর date, (2) flight search API call, (3) result দেখে cheapest পায়, (4) booking API call, (5) confirmation email পাঠায়। প্রতিটা step এ decide করে পরের কী। AI Agent ঠিক এই — autonomous decision + tool use + multi-step planning।
import os
import json
from openai import OpenAI
client = OpenAI(
base_url="https://ai.gateway.lovable.dev/v1",
api_key=os.environ["LOVABLE_API_KEY"],
)
def calculator(expression: str) -> str:
try:
return str(eval(expression, {"__builtins__": {}}, {}))
except Exception as e:
return f"Error: {e}"
def get_weather(city: str) -> str:
fake = {"Dhaka": "32C sunny", "London": "15C rainy", "Tokyo": "20C cloudy"}
return fake.get(city, "Unknown city")
tools = [
{"type": "function", "function": {
"name": "calculator",
"description": "Evaluate a math expression.",
"parameters": {"type": "object", "properties": {"expression": {"type": "string"}}, "required": ["expression"]},
}},
{"type": "function", "function": {
"name": "get_weather",
"description": "Get current weather of a city.",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
}},
]
def run_agent(user_goal):
messages = [
{"role": "system", "content": "You are a helpful agent. Use tools when needed. Reply concisely."},
{"role": "user", "content": user_goal},
]
for step in range(5):
res = client.chat.completions.create(
model="google/gemini-2.5-flash",
messages=messages,
tools=tools,
)
msg = res.choices[0].message
messages.append(msg.model_dump(exclude_none=True))
if not msg.tool_calls:
print("Final:", msg.content)
return
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
fn = {"calculator": calculator, "get_weather": get_weather}[call.function.name]
result = fn(**args)
print(f" [tool] {call.function.name}({args}) -> {result}")
messages.append({"role": "tool", "tool_call_id": call.id, "content": result})
run_agent("What is the weather in Dhaka, and what is 15 percent of 240?")Two tool define — calculator + weather। Agent loop: LLM call → যদি tool_calls থাকে, execute করে result feedback → আবার call। Final answer (tool_calls নেই) এলে break। ReAct pattern এর simplified implementation।
একটা agent যাকে topic দিলে — (1) web search করে relevant article খুঁজে, (2) প্রতিটা থেকে key point extract, (3) সব combine করে structured report লেখে। Tools: search, fetch_url, summarize। Final output: markdown report।