AI agenti sú ďalšou evolúciou LLM - namiesto jednorázových odpovedí vykonávajú komplexné úlohy samostatne.
Čo je AI Agent?
Agent je systém, ktorý:
- Vníma - získava informácie z prostredia
- Rozhoduje - plánuje ďalšie kroky
- Koná - vykonáva akcie pomocou nástrojov
- Učí sa - zlepšuje sa na základe výsledkov
┌────────────────────────────────────────┐
│ AGENT │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Perceive│─►│ Plan │─►│ Act │ │
│ └─────────┘ └─────────┘ └─────────┘ │
│ ▲ │ │
│ └──────────────────────────┘ │
│ Feedback Loop │
└────────────────────────────────────────┘
ReAct Pattern
Reasoning + Acting - najpopulárnejší prístup:
def react_agent(task: str):
thought = ""
while not is_complete(thought):
# Reasoning
thought = llm(f"""
Task: {task}
Previous thoughts: {thought}
Think step by step. What should I do next?
""")
# Acting
if needs_tool(thought):
action = extract_action(thought)
result = execute_tool(action)
thought += f"\nObservation: {result}"
return extract_answer(thought)
Nástroje pre agentov
Web Search
from langchain.tools import DuckDuckGoSearchRun
search = DuckDuckGoSearchRun()
result = search.run("latest AI news 2024")
Code Execution
from langchain_experimental.tools import PythonREPLTool
python_repl = PythonREPLTool()
result = python_repl.run("print(2 + 2)")
File Operations
from langchain.tools import ReadFileTool, WriteFileTool
read_tool = ReadFileTool()
write_tool = WriteFileTool()
Implementácia s LangChain
from langchain.agents import initialize_agent, AgentType
from langchain.chat_models import ChatOpenAI
from langchain.tools import Tool
# Definícia nástrojov
tools = [
Tool(
name="Calculator",
func=lambda x: eval(x),
description="Vykoná matematické výpočty"
),
Tool(
name="Search",
func=search.run,
description="Vyhľadá informácie na webe"
)
]
# Vytvorenie agenta
agent = initialize_agent(
tools,
ChatOpenAI(model="gpt-4"),
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
# Spustenie
result = agent.run("Koľko je 15% z tržieb Tesly za posledný kvartál?")
Bezpečnosť a limity
Guardrails
def safe_execute(agent, task):
# Limit iterations
max_steps = 10
# Blacklist dangerous actions
forbidden = ["rm -rf", "DELETE FROM", "DROP TABLE"]
# Human approval for sensitive actions
if is_sensitive(task):
approval = get_human_approval(task)
if not approval:
return "Task requires human approval"
return agent.run(task, max_iterations=max_steps)
Observability
Logujte každý krok agenta pre debugging a audit.
Use Cases
- Research Assistant - zhromažďovanie a syntéza informácií
- Code Assistant - písanie, testovanie, debugging
- Data Analyst - SQL queries, vizualizácie
- Customer Support - riešenie ticketov end-to-end
Záver
AI agenti sú mocné, ale vyžadujú starostlivý dizajn. Začnite s jednoduchými úlohami, pridajte guardrails a postupne rozširujte schopnosti.