AI Agent(智能体)是指能够感知环境、自主决策并执行动作的人工智能系统。与传统的单次问答模式不同,Agent 具备持续记忆、工具调用和任务规划能力,可以独立完成复杂的多步骤任务。
现代 AI Agent 通常采用分层架构:
ReAct(Reasoning + Acting)是当前主流的 Agent 推理框架,它让模型交替进行思考和行动:
# ReAct 循环示例
def react_loop(query, max_steps=10):
memory = []
for step in range(max_steps):
# 思考:分析当前状态
thought = llm.generate_thought(query, memory)
# 决策:选择下一步行动
action = llm.decide_action(thought, available_tools)
if action.type == "finish":
return action.result
# 执行:调用工具
observation = execute_tool(action)
# 记忆更新
memory.append({
"thought": thought,
"action": action,
"observation": observation
})
return memory
以下是一个基于 Python 的极简 Agent 实现,展示了核心逻辑:
import json
from typing import Dict, List, Callable
from openai import OpenAI
class SimpleAgent:
def __init__(self, api_key: str):
self.client = OpenAI(api_key=api_key)
self.tools: Dict[str, Callable] = {}
self.memory: List[Dict] = []
def register_tool(self, name: str, func: Callable, description: str):
"""注册工具供 Agent 调用"""
self.tools[name] = {
"func": func,
"description": description
}
def think_and_act(self, user_input: str) -> str:
"""核心 ReAct 循环"""
# 构建系统提示
system_prompt = """You are an AI Agent. Follow these steps:
1. Analyze the user's request
2. Decide if you need to use a tool or can answer directly
3. If using a tool, specify which one and what parameters
4. After tool execution, provide the final answer
Available tools:
"""
for name, info in self.tools.items():
system_prompt += f"- {name}: {info['description']}\n"
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_input}
]
# 第一次 LLM 调用:决定行动
response = self.client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
temperature=0.3
)
decision = response.choices[0].message.content
# 解析是否需要调用工具(简化版)
for tool_name in self.tools:
if tool_name in decision.lower():
result = self.tools[tool_name]["func"]()
# 第二次调用:基于工具结果生成回答
messages.append({"role": "assistant", "content": decision})
messages.append({
"role": "user",
"content": f"Tool result: {result}. Please provide the final answer."
})
final_response = self.client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
return final_response.choices[0].message.content
return decision
# 使用示例
def get_weather() -> str:
return "Sunny, 25°C"
def search_database(query: str) -> str:
return f"Found 3 results for: {query}"
agent = SimpleAgent(api_key="your-api-key")
agent.register_tool("get_weather", get_weather, "Get current weather")
agent.register_tool("search_database", search_database, "Search internal database")
result = agent.think_and_act("What's the weather like?")
print(result)
AI Agent 正在重塑软件交互范式:
AI Agent 不是未来概念,而是正在发生的现实。从 OpenAI 的 GPTs 到 AutoGPT,从 Claude 的 Computer Use 到各类企业级 Agent 平台,技术栈日趋成熟。对于开发者而言,现在正是入场布局的最佳时机。