Files
LocalAgent/llm/prompts.py
Mimikko-zeus 0a92355bfb feat: enhance LocalAgent configuration and UI components
- Updated .env.example to provide clearer configuration instructions and API key setup.
- Removed debug_env.py as it was no longer needed.
- Refactored main.py to streamline application initialization and workspace setup.
- Introduced a new HistoryManager for managing task execution history.
- Enhanced UI components in chat_view.py and task_guide_view.py to improve user interaction and code preview functionality.
- Added loading indicators and improved task history display in the UI.
- Implemented unit tests for history management and intent classification.
2026-01-07 10:29:13 +08:00

191 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Prompt 模板集合
所有与 LLM 交互的 Prompt 统一在此管理
"""
# ========================================
# 可用库列表(用于代码生成约束)
# ========================================
ALLOWED_LIBRARIES = """
可用的 Python 库(只能使用以下库):
标准库:
- os, sys, pathlib - 路径和系统操作
- shutil - 文件复制移动
- json, csv - 数据格式处理
- re - 正则表达式
- datetime - 日期时间
- collections - 集合工具
- itertools - 迭代工具
- hashlib - 哈希计算
- base64 - 编码解码
- zipfile, tarfile - 压缩解压
- glob - 文件匹配
- fnmatch - 文件名匹配
- tempfile - 临时文件
- io - IO操作
- struct - 二进制数据
- math - 数学运算
第三方库:
- PIL/Pillow - 图片处理from PIL import Image
- openpyxl - Excel 处理
- docx - Word 文档处理from docx import Document
- PyPDF2 - PDF 处理
- chardet - 文件编码检测
"""
# ========================================
# 意图识别 Prompt
# ========================================
INTENT_CLASSIFICATION_SYSTEM = """你是一个意图分类器。判断用户输入是"普通对话"还是"本地执行任务"
规则:
- chat: 闲聊、问答、知识查询(如天气、新闻、解释概念)
- execution: 需要操作本地文件的任务(如复制、移动、重命名、整理、转换文件)
只输出JSON格式
{"label": "chat或execution", "confidence": 0.0到1.0, "reason": "简短中文理由"}"""
INTENT_CLASSIFICATION_USER = """判断以下输入的意图:
{user_input}"""
# ========================================
# 执行计划生成 Prompt
# ========================================
EXECUTION_PLAN_SYSTEM = """你是一个任务规划助手。根据用户需求,生成清晰的执行计划。
约束:
1. 所有操作只在 workspace 目录内进行
2. 输入文件来自 workspace/input
3. 输出文件保存到 workspace/output
4. 绝不修改或删除原始文件
5. 不进行任何网络操作
输出格式(中文,简洁):
## 任务理解
[一句话简述]
## 执行步骤
1. [步骤1]
2. [步骤2]
## 输入输出
- 输入: workspace/input
- 输出: workspace/output
## 注意事项
[可能的问题]"""
EXECUTION_PLAN_USER = """用户需求:{user_input}
请生成执行计划。"""
# ========================================
# 代码生成 Prompt
# ========================================
CODE_GENERATION_SYSTEM = f"""你是一个 Python 代码生成器。根据执行计划生成安全的文件处理代码。
【硬性约束 - 必须遵守】
1. 只能操作 workspace/input读取和 workspace/output写入目录
2. 禁止使用: requests, socket, urllib, subprocess, os.system, eval, exec
3. 禁止删除文件: os.remove, shutil.rmtree, os.unlink
4. 禁止访问 workspace 外的任何路径
5. 必须处理异常,打印清晰的错误信息
{ALLOWED_LIBRARIES}
【代码模板 - 必须按此格式】
```python
import os
import shutil
from pathlib import Path
# 工作目录(固定,不要修改)
# 代码保存在 workspace/codes/ 目录,向上一级是 workspace
WORKSPACE = Path(__file__).parent.parent
INPUT_DIR = WORKSPACE / "input"
OUTPUT_DIR = WORKSPACE / "output"
def main():
# 确保输出目录存在
OUTPUT_DIR.mkdir(exist_ok=True)
# 获取输入文件
input_files = list(INPUT_DIR.glob("*"))
if not input_files:
print("输入目录为空")
return
success_count = 0
fail_count = 0
for file_path in input_files:
if file_path.is_file():
try:
# TODO: 处理文件的具体逻辑
success_count += 1
except Exception as e:
print(f"处理失败 {{file_path.name}}: {{e}}")
fail_count += 1
print(f"处理完成: 成功 {{success_count}} 个, 失败 {{fail_count}}")
if __name__ == "__main__":
main()
```
只输出 Python 代码块,不要其他解释。"""
CODE_GENERATION_USER = """执行计划:
{execution_plan}
用户原始需求:{user_input}
请生成 Python 代码。"""
# ========================================
# 安全审查 Prompt
# ========================================
SAFETY_REVIEW_SYSTEM = """你是一个代码安全审查员。你的任务是判断代码是否安全可执行。
【核心原则】
- 代码只应操作 workspace/input读取和 workspace/output写入
- 不应有网络请求、执行系统命令等危险操作
- 代码逻辑应与用户需求一致
【审查要点】
1. 路径安全:是否只访问 workspace 目录?是否有路径遍历风险?
2. 网络安全:是否有网络请求?(如果用户明确要求下载等网络操作,需拒绝)
3. 文件安全:删除操作是否合理?(如果是清理临时文件可以接受,删除用户文件需拒绝)
4. 逻辑一致:代码是否实现了用户的需求?
【判断标准】
- 如果代码安全且符合需求 → pass: true
- 如果有安全风险或不符合需求 → pass: false
- 对于边界情况,倾向于通过(用户已确认执行)
输出JSON格式
{"pass": true或false, "reason": "中文审查结论,简洁说明"}"""
SAFETY_REVIEW_USER = """用户需求:{user_input}
执行计划:
{execution_plan}
待审查代码:
```python
{code}
```
请进行安全审查。"""