thinking_actions.py
1 # Copyright (c) 2024-2026 Tencent Zhuque Lab. All rights reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 # Requirement: Any integration or derivative work must explicitly attribute 16 # Tencent Zhuque Lab (https://github.com/Tencent/AI-Infra-Guard) in its 17 # documentation or user interface, as detailed in the NOTICE file. 18 19 20 from tools.registry import register_tool 21 from utils.tool_context import ToolContext 22 23 24 @register_tool(sandbox_execution=False) 25 def think(thought: str, context: ToolContext = None): 26 """ 27 Deep Thinking Tool. 28 Use this tool when you are stuck, facing a complex problem, or need to plan a multi-step task. 29 It will pause the current execution and use a specialized reasoning model to analyze the situation. 30 31 Args: 32 thought: The specific problem, question, or situation you need to think about. 33 Be detailed about what you know and what you are unsure about. 34 context: Tool context (automatically injected). 35 36 Returns: 37 A structured analysis containing reasoning, plan, and next steps. 38 """ 39 try: 40 if not thought or not thought.strip(): 41 return {"message": "Thought cannot be empty"} 42 43 # 如果有context,使用思考模型深度分析 44 # system_prompt = """你是一个专业的思考助手,擅长深度分析和逻辑推理。 45 # 你的任务是对用户提出的问题进行深入思考,提供: 46 # 1. 问题分析 47 # 2. 当前信息和背景整合 48 # 3. 可能的解决方案 49 # 4. 潜在风险和注意事项 50 # 5. 推荐的行动步骤 51 # 52 # 请用简洁、结构化的方式回答。""" 53 # 54 # # 使用专门的思考模型(如果配置了),否则使用默认LLM 55 # thinking_result = context.call_llm( 56 # prompt=f"请对以下内容进行深度思考和分析:\n\n{thought}", 57 # purpose="thinking", 58 # system_prompt=system_prompt, 59 # use_history=True # 思考时需要历史记录 60 # ) 61 62 return { 63 "success": True, 64 "thought": thought, 65 # "thinking_result": thinking_result, 66 } 67 68 except (ValueError, TypeError) as e: 69 return {"success": False, "message": f"Failed to record thought: {e!s}"} 70 except Exception as e: 71 return {"success": False, "message": f"Error during thinking: {str(e)}"}