【大白话说Java面试题 第82题】【Mysql篇】第12题:为什么 MySQL 索引底层不用二叉树?
2026/5/30 1:09:05
AI Agent是能够自主完成任务的智能实体:
AI Agent特征 自主性: 自主决策 目标导向: 目标驱动 学习能力: 持续学习 交互能力: 与环境交互 Agent类型: 任务型Agent: 完成特定任务 对话型Agent: 自然语言交互 协作型Agent: 多Agent协作Agent架构组件 感知模块: 获取信息 决策模块: 制定计划 执行模块: 执行动作 学习模块: 优化行为 核心能力: 记忆管理 工具使用 反思能力 多步推理应用场景 自动化办公: 自动处理任务 智能助手: 个人助手 代码生成: 自动编程 数据分析: 自动分析 发展趋势: 多模态能力 长上下文理解 工具调用扩展 自主学习import json class AIAgent: def __init__(self, name, model): self.name = name self.model = model self.memory = [] self.tools = {} def add_tool(self, tool_name, tool_func, description): self.tools[tool_name] = { 'func': tool_func, 'description': description } def add_memory(self, content): self.memory.append({ 'timestamp': '2024-01-01', 'content': content }) def get_relevant_memory(self, query): return [m for m in self.memory if any(word in query.lower() for word in m['content'].lower())] def think(self, prompt): context = { 'memory': self.get_relevant_memory(prompt), 'available_tools': [{'name': name, 'description': desc['description']} for name, desc in self.tools.items()] } full_prompt = f""" Agent: {self.name} Context: {json.dumps(context)} Prompt: {prompt} Think step by step and decide what to do. """ return self.model.generate(full_prompt) def act(self, action): if action['type'] == 'tool_call': tool_name = action['tool'] params = action['params'] if tool_name in self.tools: result = self.tools[tool_name]['func'](**params) return result elif action['type'] == 'response': return action['content'] return None def execute(self, prompt): thought = self.think(prompt) action = self._parse_action(thought) result = self.act(action) return result def _parse_action(self, thought): return {'type': 'response', 'content': thought}class ToolSystem: def __init__(self): self.registry = {} def register_tool(self, name, func, schema): self.registry[name] = { 'func': func, 'schema': schema } def call_tool(self, name, **kwargs): tool = self.registry.get(name) if not tool: raise ValueError(f"Tool {name} not found") return tool['func'](**kwargs) def get_tool_info(self, name): return self.registry.get(name, {}).get('schema', {}) def list_tools(self): return list(self.registry.keys())class MemorySystem: def __init__(self): self.short_term = [] self.long_term = {} def add_short_term(self, content): self.short_term.append(content) if len(self.short_term) > 100: self.short_term = self.short_term[-50:] def add_long_term(self, key, content): self.long_term[key] = content def retrieve(self, query): results = [] for item in self.short_term: if self._similarity(query, item) > 0.5: results.append(item) for key, content in self.long_term.items(): if self._similarity(query, key) > 0.5: results.append(content) return results def _similarity(self, s1, s2): words1 = set(s1.lower().split()) words2 = set(s2.lower().split()) if not words1 or not words2: return 0 return len(words1 & words2) / len(words1 | words2)| 类型 | 复杂度 | 自主性 | 应用场景 |
|---|---|---|---|
| 任务型 | 低 | 中 | 自动化 |
| 对话型 | 中 | 中 | 客服 |
| 协作型 | 高 | 高 | 复杂任务 |
| 平台 | 能力 | 扩展性 | 易用性 |
|---|---|---|---|
| LangChain | 高 | 高 | 中 |
| LlamaIndex | 中 | 中 | 高 |
| AutoGPT | 高 | 中 | 低 |
| 工具类型 | 用途 | 难度 | 价值 |
|---|---|---|---|
| API调用 | 数据获取 | 低 | 高 |
| 代码执行 | 计算分析 | 中 | 高 |
| 文件操作 | 数据处理 | 低 | 中 |
def create_agent_example(): class DummyModel: def generate(self, prompt): return "I need to use the search tool to find information." agent = AIAgent('ResearchAgent', DummyModel()) def search_tool(query): return f"Search results for: {query}" agent.add_tool('search', search_tool, 'Search the web for information') result = agent.execute('What is AI Agent?') print(f"Agent response: {result}")def tool_system_example(): tools = ToolSystem() def get_weather(city): return {'city': city, 'temperature': 25, 'condition': 'sunny'} def calculate(a, b, operation): if operation == 'add': return a + b elif operation == 'multiply': return a * b tools.register_tool('get_weather', get_weather, {'city': 'string'}) tools.register_tool('calculate', calculate, {'a': 'number', 'b': 'number', 'operation': 'string'}) weather = tools.call_tool('get_weather', city='Beijing') print(f"Weather: {weather}") result = tools.call_tool('calculate', a=10, b=5, operation='multiply') print(f"Calculation result: {result}")AI Agent是AI的下一个发展阶段:
对比数据如下:
AI Agent将在自动化、客服、编程等领域带来革命性变化。