210 lines
8.1 KiB
Python
210 lines
8.1 KiB
Python
|
|
#!/usr/bin/env python
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
|
|||
|
|
"""
|
|||
|
|
测试PromptManager组件
|
|||
|
|
|
|||
|
|
该脚本演示如何独立使用prompt_manager模块构建不同场景的提示词。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
import tempfile
|
|||
|
|
import json
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
# 将项目根目录添加到PATH
|
|||
|
|
project_root = str(Path(__file__).parent.parent.absolute())
|
|||
|
|
if project_root not in sys.path:
|
|||
|
|
sys.path.append(project_root)
|
|||
|
|
|
|||
|
|
from utils.prompt_manager import PromptManager
|
|||
|
|
|
|||
|
|
def create_test_files():
|
|||
|
|
"""创建测试用的提示词文件和资源文件"""
|
|||
|
|
# 创建临时目录
|
|||
|
|
temp_dir = tempfile.mkdtemp()
|
|||
|
|
|
|||
|
|
# 创建提示词目录结构
|
|||
|
|
prompts_dir = os.path.join(temp_dir, "prompts")
|
|||
|
|
os.makedirs(prompts_dir, exist_ok=True)
|
|||
|
|
|
|||
|
|
# 创建Style和Demand子目录
|
|||
|
|
style_dir = os.path.join(prompts_dir, "Style")
|
|||
|
|
demand_dir = os.path.join(prompts_dir, "Demand")
|
|||
|
|
refer_dir = os.path.join(prompts_dir, "Refer")
|
|||
|
|
|
|||
|
|
os.makedirs(style_dir, exist_ok=True)
|
|||
|
|
os.makedirs(demand_dir, exist_ok=True)
|
|||
|
|
os.makedirs(refer_dir, exist_ok=True)
|
|||
|
|
|
|||
|
|
# 创建Topic提示词文件
|
|||
|
|
topic_system_path = os.path.join(temp_dir, "topic_system.txt")
|
|||
|
|
with open(topic_system_path, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write("您是旅游内容选题专家。请根据提供的信息,生成高质量的旅游选题。")
|
|||
|
|
|
|||
|
|
topic_user_path = os.path.join(temp_dir, "topic_user.txt")
|
|||
|
|
with open(topic_user_path, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write("生成{num}个旅游内容选题,日期为{date}。")
|
|||
|
|
|
|||
|
|
# 创建Content提示词文件
|
|||
|
|
content_system_path = os.path.join(temp_dir, "content_system.txt")
|
|||
|
|
with open(content_system_path, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write("您是旅游内容创作专家。请根据选题和参考资料,生成优质旅游内容。")
|
|||
|
|
|
|||
|
|
# 创建Style文件
|
|||
|
|
style_file = os.path.join(style_dir, "摄影指南.txt")
|
|||
|
|
with open(style_file, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write("这是一篇摄影风格的旅游文章,应该包含构图技巧、最佳拍摄时间和角度建议。")
|
|||
|
|
|
|||
|
|
# 创建Demand文件
|
|||
|
|
demand_file = os.path.join(demand_dir, "亲子游.txt")
|
|||
|
|
with open(demand_file, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write("这是一篇面向家庭旅游者的文章,应该包含适合儿童的活动和安全提示。")
|
|||
|
|
|
|||
|
|
# 创建Resource目录和文件
|
|||
|
|
resource_dir = os.path.join(temp_dir, "resource")
|
|||
|
|
os.makedirs(resource_dir, exist_ok=True)
|
|||
|
|
|
|||
|
|
# 创建一个景点描述文件
|
|||
|
|
desc_file = os.path.join(resource_dir, "泰宁古城描述.txt")
|
|||
|
|
with open(desc_file, 'w', encoding='utf-8') as f:
|
|||
|
|
f.write("泰宁古城是一座有着八百年历史的古城,保存了大量明清时期的建筑。")
|
|||
|
|
|
|||
|
|
# 返回创建的文件路径
|
|||
|
|
return {
|
|||
|
|
"temp_dir": temp_dir,
|
|||
|
|
"prompts_dir": prompts_dir,
|
|||
|
|
"topic_system_path": topic_system_path,
|
|||
|
|
"topic_user_path": topic_user_path,
|
|||
|
|
"content_system_path": content_system_path,
|
|||
|
|
"resource_dir": resource_dir,
|
|||
|
|
"desc_file": desc_file,
|
|||
|
|
"style_file": style_file,
|
|||
|
|
"demand_file": demand_file
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
def create_topic_item():
|
|||
|
|
"""创建测试用的选题项"""
|
|||
|
|
return {
|
|||
|
|
"index": 1,
|
|||
|
|
"date": "2024-05-30",
|
|||
|
|
"logic": "这是一个著名的景点,有丰富的历史文化底蕴,适合文化和历史爱好者。",
|
|||
|
|
"object": "泰宁古城",
|
|||
|
|
"product": "文化体验套票.txt",
|
|||
|
|
"product_logic": "泰宁古城有多处明清古建筑,游客可以通过体验套票更全面了解当地历史文化。",
|
|||
|
|
"style": "摄影指南.txt",
|
|||
|
|
"style_logic": "古城拥有800多年历史,古建筑保存完好,适合摄影风格。",
|
|||
|
|
"target_audience": "亲子游.txt",
|
|||
|
|
"target_audience_logic": "古城内有适合家庭参观的展览和互动活动,老人孩子都能找到感兴趣的内容。"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主函数"""
|
|||
|
|
print("=== 测试 PromptManager 组件 ===")
|
|||
|
|
|
|||
|
|
# 创建测试文件
|
|||
|
|
test_files = create_test_files()
|
|||
|
|
print(f"创建临时测试文件夹: {test_files['temp_dir']}")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 创建资源文件配置
|
|||
|
|
resource_dir_config = [
|
|||
|
|
{
|
|||
|
|
"type": "Description",
|
|||
|
|
"file_path": [test_files["desc_file"]]
|
|||
|
|
}
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
# 初始化PromptManager
|
|||
|
|
prompt_manager = PromptManager(
|
|||
|
|
topic_system_prompt_path=test_files["topic_system_path"],
|
|||
|
|
topic_user_prompt_path=test_files["topic_user_path"],
|
|||
|
|
content_system_prompt_path=test_files["content_system_path"],
|
|||
|
|
prompts_dir=test_files["prompts_dir"],
|
|||
|
|
resource_dir_config=resource_dir_config,
|
|||
|
|
topic_gen_num=3,
|
|||
|
|
topic_gen_date="2024-05-30"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
print("\n测试1: 获取选题提示词")
|
|||
|
|
system_prompt, user_prompt = prompt_manager.get_topic_prompts()
|
|||
|
|
|
|||
|
|
print(f"系统提示词 ({len(system_prompt)} 字符):")
|
|||
|
|
print(system_prompt)
|
|||
|
|
print(f"\n用户提示词 ({len(user_prompt)} 字符):")
|
|||
|
|
print(user_prompt)
|
|||
|
|
|
|||
|
|
# 验证变量替换
|
|||
|
|
if "{num}" not in user_prompt and "3" in user_prompt:
|
|||
|
|
print("测试通过: {num} 成功替换为 3")
|
|||
|
|
else:
|
|||
|
|
print("测试失败: {num} 未被正确替换")
|
|||
|
|
|
|||
|
|
if "{date}" not in user_prompt and "2024-05-30" in user_prompt:
|
|||
|
|
print("测试通过: {date} 成功替换为 2024-05-30")
|
|||
|
|
else:
|
|||
|
|
print("测试失败: {date} 未被正确替换")
|
|||
|
|
|
|||
|
|
print("\n测试2: 获取内容生成提示词")
|
|||
|
|
topic_item = create_topic_item()
|
|||
|
|
|
|||
|
|
system_prompt, user_prompt = prompt_manager.get_content_prompts(topic_item)
|
|||
|
|
|
|||
|
|
print(f"系统提示词 ({len(system_prompt)} 字符):")
|
|||
|
|
print(system_prompt[:200] + "..." if len(system_prompt) > 200 else system_prompt)
|
|||
|
|
print(f"\n用户提示词 ({len(user_prompt)} 字符):")
|
|||
|
|
print(user_prompt[:200] + "..." if len(user_prompt) > 200 else user_prompt)
|
|||
|
|
|
|||
|
|
# 验证样式和目标受众内容是否包含在提示词中
|
|||
|
|
with open(test_files["style_file"], 'r', encoding='utf-8') as f:
|
|||
|
|
style_content = f.read()
|
|||
|
|
|
|||
|
|
with open(test_files["demand_file"], 'r', encoding='utf-8') as f:
|
|||
|
|
demand_content = f.read()
|
|||
|
|
|
|||
|
|
if style_content in user_prompt:
|
|||
|
|
print("测试通过: 样式文件内容成功包含在用户提示词中")
|
|||
|
|
else:
|
|||
|
|
print("测试失败: 样式文件内容未包含在用户提示词中")
|
|||
|
|
|
|||
|
|
if demand_content in user_prompt:
|
|||
|
|
print("测试通过: 目标受众文件内容成功包含在用户提示词中")
|
|||
|
|
else:
|
|||
|
|
print("测试失败: 目标受众文件内容未包含在用户提示词中")
|
|||
|
|
|
|||
|
|
# 验证景点描述是否包含
|
|||
|
|
with open(test_files["desc_file"], 'r', encoding='utf-8') as f:
|
|||
|
|
desc_content = f.read()
|
|||
|
|
|
|||
|
|
if desc_content in user_prompt:
|
|||
|
|
print("测试通过: 景点描述文件内容成功包含在用户提示词中")
|
|||
|
|
else:
|
|||
|
|
print("测试失败: 景点描述文件内容未包含在用户提示词中")
|
|||
|
|
|
|||
|
|
print("\n测试3: 异常处理")
|
|||
|
|
# 测试不存在的风格文件
|
|||
|
|
non_existent_topic = topic_item.copy()
|
|||
|
|
non_existent_topic["style"] = "不存在的风格.txt"
|
|||
|
|
|
|||
|
|
system_prompt, user_prompt = prompt_manager.get_content_prompts(non_existent_topic)
|
|||
|
|
print("获取提示词成功,即使风格文件不存在")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"测试过程中出错: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
|
|||
|
|
finally:
|
|||
|
|
# 清理临时文件
|
|||
|
|
import shutil
|
|||
|
|
try:
|
|||
|
|
shutil.rmtree(test_files["temp_dir"])
|
|||
|
|
print(f"\n清理临时文件: {test_files['temp_dir']}")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"清理临时文件失败: {e}")
|
|||
|
|
|
|||
|
|
print("\n=== 测试完成 ===")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|