Agent ReAct模式

Agent模式1 — ReAct模式 ReAct Agent - 基于 Reasoning + Acting 模式的智能代理 ReAct 模式的核心思想是让 LLM 交替进行: 1. Thought (思考): 分析当前情况,决定下一步行动 2. Action (行动): 选择并执行一个工具 3. Observation (观察): 获取工具执行结果 这个循环持续进行,直到 Agent 认为可以给出最终答案。 回答中的解析可以按照 Thought: Action: Action Input: 进行正则匹配获取 比如 def _parse_response(self, response: str) -> tuple[str, Optional[str], Optional[dict], Optional[str]]: """ 解析 LLM 响应 Returns: (thought, action, action_input, final_answer) """ thought = "" action = None action_input = None final_answer = None # 提取 Thought thought_match = re.search(r"Thought:\s*(.+?)(?=Action:|Final Answer:|$)", response, re.DOTALL) if thought_match: thought = thought_match.group(1).strip() # 检查是否有 Final Answer final_match = re.search(r"Final Answer:\s*(.+?)$", response, re.DOTALL) if final_match: final_answer = final_match.group(1).strip() return thought, None, None, final_answer # 提取 Action action_match = re.search(r"Action:\s*(\w+)", response) if action_match: action = action_match.group(1).strip() # 提取 Action Input input_match = re.search(r"Action Input:\s*(\{.+?\})", response, re.DOTALL) if input_match: try: import json action_input = json.loads(input_match.group(1)) except json.JSONDecodeError: # 尝试更宽松的解析 action_input = {"input": input_match.group(1)} else: # 尝试匹配非 JSON 格式的输入 input_match = re.search(r"Action Input:\s*(.+?)(?=Thought:|Action:|$)", response, re.DOTALL) if input_match: raw_input = input_match.group(1).strip() # 根据工具类型推断参数名 if action == "calculator": action_input = {"expression": raw_input} elif action == "web_search": action_input = {"query": raw_input} elif action == "shell": action_input = {"command": raw_input} elif action == "http_request": action_input = {"url": raw_input} elif action == "python_repl": action_input = {"code": raw_input} else: action_input = {"input": raw_input} return thought, action, action_input, final_answer 提示词模板 ...

2026-01-26 11:12:34    |    2 分钟    |    303 字    |    Fengbin

Prompts

告别“人工智障”:7个拿来即用的AI提示词(Prompt)万能模板 你是否觉得 AI 有时候很聪明,有时候又像个“人工智障”?其实,90% 的回答质量取决于你如何提问。 正如编程界的名言“Garbage In, Garbage Out”(垃圾输入,垃圾输出),对于 ChatGPT、Claude 或文心一言这类大模型来说,提示词(Prompt)就是你的源代码。 为了帮你节省反复调试的时间,我总结了 7 个经过实战验证的 Prompt 模板。无论你是要写文案、做策划、学知识,还是单纯想找灵感,总有一款适合你。 1. BROKE 框架:通用的“万金油” 这是最基础也最稳健的框架,适用于 80% 的日常工作场景,如策划、写报告、做方案。 适用场景: 任务明确,需要结构化输出的场景。 核心逻辑: 背景(B) + 角色(R) + 目标(O) + 关键结果(K) + 演变/改进(E)。 模板内容: **角色 (Role):** 你是一位 [职业/身份],擅长 [核心技能]。 **背景 (Background):** [描述当前的情况、背景信息]。 **目标 (Objective):** 请帮我 [具体的任务目标]。 **关键结果 (Key Results):** 输出必须包含:[要素1]、[要素2]、[要素3]。 **风格与限制 (Style & Constraints):** 1. [限制条件1,如:预算、字数] 2. [限制条件2,如:语气、格式] **输出格式 (Format):** 请使用 [表格/列表/Markdown] 格式。 例子: 策划团建 “你是一位资深行政主管。背景是我们部门20人刚完成大项目,身心疲惫。目标是策划3个预算人均300元以内的北京近郊团建方案。输出必须包含主题、行程、预算。请用表格形式输出。” 2. CO-STAR 框架:精细化内容创作 当你需要 AI 模仿特定风格(如小红书、LinkedIn、公关稿)时,这个框架能精准控制语气和受众。 ...

2025-12-26 17:40:49    |    2 分钟    |    308 字    |    Fengbin