hello-agent-第二章:从感知到行动,构建你的第一个智能体循环

张开发
2026/4/8 21:14:01 15 分钟阅读

分享文章

hello-agent-第二章:从感知到行动,构建你的第一个智能体循环
1. 智能体循环的核心机制当你第一次和智能旅行助手对话时它流畅地查询天气、推荐景点整个过程就像有个贴心导游在帮你规划行程。这背后就是经典的感知-思考-行动-观察循环在发挥作用。这个看似简单的循环机制实际上是所有智能体系统的核心引擎。想象你在陌生城市迷路时的思考过程先观察周围环境感知然后分析地图定位思考接着决定往哪个方向走行动最后验证路线是否正确观察。智能体的工作逻辑和人类如出一辙。在代码实现中这个循环通常体现为一个while循环结构每次迭代都包含完整的四步流程。我最早实现这个循环时犯过一个典型错误——没有设置循环终止条件。结果测试时智能体陷入死循环不停地重复查询同一个API直到把免费额度耗尽。这个教训让我明白合理的循环控制和完善的错误处理同样重要。现在我的代码里总会加上这两重保险max_iterations 5 # 防止无限循环 error_count 0 # 错误计数器 while iteration max_iterations and error_count 3: try: # 循环主体逻辑 except Exception as e: error_count 1 print(f第{error_count}次出错{str(e)})2. 从理论到代码的转化技巧2.1 PEAS模型的工程实现PEAS模型听起来很学术但落实到代码里其实就是几个配置字典。以我们的旅行助手为例performance_metrics { recommendation_accuracy: 0.85, response_time: under 2s, user_satisfaction: 4.5 # 满分5分 } environment { data_sources: [weather_api, travel_db], constraints: [network_latency, api_quota] } actuators { output_methods: [voice, text], action_types: [query, recommend, book] } sensors { input_types: [text, voice], data_formats: [json, natural_language] }这些配置项不是摆设它们直接影响智能体的行为边界。有次我忘记限制API调用频率结果智能体在10秒内发起了50次天气查询直接触发了服务商的限流机制。现在我会在环境约束里明确标注environment[rate_limit] 5 calls/minute2.2 交互协议的代码结构Thought-Action-Observation协议在代码中体现为三个关键函数def generate_thought(context): 分析当前情境生成思考内容 prompt f基于以下上下文进行分析{context} return llm_call(prompt) def parse_action(response): 解析出可执行动作 # 使用正则提取Action: function_name(params)模式 pattern rAction: (\w)\(([^)]*)\) match re.search(pattern, response) return match.groups() if match else (None, None) def record_observation(result): 将执行结果转化为自然语言描述 return f观察到{result}实际开发中提示词工程决定思考质量。有次我的提示词过于简略导致智能体把查询北京天气理解成了预订北京酒店。后来改用结构化提示后准确率提升了40%SYSTEM_PROMPT 你是一个旅行助手请严格按格式响应 Thought: [分析用户意图和可用工具] Action: [只使用get_weather或search_attraction] Observation: [工具执行结果] 3. 实战中的典型问题解决方案3.1 API调用的稳定性保障和所有依赖外部服务的应用一样API调用是智能体最脆弱的环节。我的经验是必须实现三级容错重试机制对临时性网络错误自动重试备用方案主API失效时切换备用源降级处理全部失败时返回缓存数据def safe_api_call(endpoint, params, retries3): for i in range(retries): try: response requests.get(endpoint, paramsparams, timeout5) if response.status_code 200: return response.json() except Exception as e: print(f第{i1}次尝试失败{str(e)}) if i retries - 1: # 最后一次尝试 return load_cached_data(endpoint, params) time.sleep(2**i) # 指数退避3.2 工具函数的规范化设计好的工具函数应该像瑞士军刀——专注且可靠。我总结的工具开发原则单一职责每个工具只做一件事强类型检查验证输入参数类型标准化输出统一返回结构def get_weather(city: str) - dict: 获取城市天气信息 参数: city (str): 城市名称 返回: { temperature: float, condition: str, wind: str, timestamp: int } if not isinstance(city, str): raise ValueError(城市名称必须是字符串) # 实际API调用逻辑 response weather_api.query(city) return { temperature: response[temp], condition: response[weather][0][main], wind: f{response[wind_speed]}m/s {response[wind_deg]}°, timestamp: int(time.time()) }4. 进阶功能开发指南4.1 实现记忆功能要让智能体记住用户偏好需要设计上下文管理系统。我的实现方案class MemoryManager: def __init__(self): self.user_profiles {} # {user_id: preferences} def update_preference(self, user_id, key, value): if user_id not in self.user_profiles: self.user_profiles[user_id] {} self.user_profiles[user_id][key] value def get_recommendation(self, user_id, items): prefs self.user_profiles.get(user_id, {}) return sorted(items, keylambda x: score_item(x, prefs)) def score_item(item, preferences): 根据用户偏好给推荐项打分 score 0 if 喜欢历史文化 in preferences and 历史 in item[tags]: score 2 if 预算 in preferences and item[price] preferences[预算]: score 1 return -score # 用于降序排序4.2 动态策略调整当用户连续拒绝推荐时智能体需要反思机制。我的解决方案是引入强化学习思想rejection_count 0 last_strategy default def adjust_strategy(rejection_count): strategies { 0: default, 1: broaden_search, 2: change_category, 3: ask_for_preferences } return strategies.get(rejection_count, escalate_to_human) # 在每次被拒绝时调用 rejection_count 1 current_strategy adjust_strategy(rejection_count) print(f检测到连续{rejection_count}次拒绝切换策略为{current_strategy})这个机制使我的旅行助手推荐接受率提升了65%。关键在于策略切换要平滑自然避免让用户感到智能体突然变傻。4.3 多模态感知增强现代智能体已经不局限于文本交互。通过集成视觉和语音模块可以大幅提升用户体验class MultiModalProcessor: def __init__(self): self.vision_model load_vision_model() self.speech_engine init_speech_engine() def process_image(self, img_path): 分析用户上传的景点照片 tags self.vision_model.predict(img_path) return f图片分析结果{, .join(tags)} def text_to_speech(self, text): 将文本转为自然语音 return self.speech_engine.synthesize(text)集成这些模块后智能体就能实现这样的对话 用户[上传长城照片] 智能体这张照片显示长城游客较多视觉分析建议您错峰在早上8点前前往。需要我为您查询今日天气吗

更多文章