TravelContentCreator/api/services/prompt_builder.py

485 lines
19 KiB
Python
Raw Normal View History

2025-07-11 13:50:08 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
提示词构建服务
负责根据选题信息构建完整的提示词
"""
import logging
2025-07-11 17:39:51 +08:00
from typing import Dict, Any, Optional, Tuple, List
2025-07-11 13:50:08 +08:00
from pathlib import Path
from core.config import ConfigManager, GenerateContentConfig, GenerateTopicConfig, PosterConfig
2025-07-11 13:50:08 +08:00
from utils.prompts import PromptTemplate
from api.services.prompt_service import PromptService
logger = logging.getLogger(__name__)
class PromptBuilderService:
"""提示词构建服务类"""
def __init__(self, config_manager: ConfigManager, prompt_service: PromptService):
"""
初始化提示词构建服务
Args:
config_manager: 配置管理器
prompt_service: 提示词服务
"""
self.config_manager = config_manager
self.prompt_service = prompt_service
def _ensure_content_config(self) -> GenerateContentConfig:
"""确保内容生成配置已加载"""
# 按需加载内容生成配置
if not self.config_manager.load_task_config('content_gen'):
logger.warning("未找到内容生成配置,将使用默认配置")
return self.config_manager.get_config('content_gen', GenerateContentConfig)
def _ensure_topic_config(self) -> GenerateTopicConfig:
"""确保选题生成配置已加载"""
# 按需加载选题生成配置
if not self.config_manager.load_task_config('topic_gen'):
logger.warning("未找到选题生成配置,将使用默认配置")
return self.config_manager.get_config('topic_gen', GenerateTopicConfig)
def _ensure_poster_config(self) -> PosterConfig:
"""确保海报生成配置已加载"""
# 按需加载海报生成配置
if not self.config_manager.load_task_config('poster_gen'):
logger.warning("未找到海报生成配置,将使用默认配置")
return self.config_manager.get_config('poster_gen', PosterConfig)
2025-07-11 13:50:08 +08:00
def build_content_prompt(self, topic: Dict[str, Any], step: str = "content") -> Tuple[str, str]:
"""
构建内容生成提示词
Args:
topic: 选题信息
step: 当前步骤用于过滤参考内容
Returns:
系统提示词和用户提示词的元组
"""
# 获取内容生成配置
content_config = self._ensure_content_config()
2025-07-11 13:50:08 +08:00
# 加载系统提示词和用户提示词模板
system_prompt_path = content_config.content_system_prompt
user_prompt_path = content_config.content_user_prompt
2025-07-11 13:50:08 +08:00
# 创建提示词模板
template = PromptTemplate(system_prompt_path, user_prompt_path)
# 获取风格内容
style_filename = topic.get("style", "")
style_content = self.prompt_service.get_style_content(style_filename)
# 获取目标受众内容
demand_filename = topic.get("target_audience", "")
demand_content = self.prompt_service.get_audience_content(demand_filename)
# 获取景区信息
object_name = topic.get("object", "")
object_content = self.prompt_service.get_scenic_spot_info(object_name)
# 获取产品信息
product_name = topic.get("product", "")
product_content = self.prompt_service.get_product_info(product_name)
# 获取参考内容
refer_content = self.prompt_service.get_refer_content(step)
# 构建系统提示词
system_prompt = template.get_system_prompt()
# 构建用户提示词
user_prompt = template.build_user_prompt(
style_content=f"{style_filename}\n{style_content}",
demand_content=f"{demand_filename}\n{demand_content}",
object_content=f"{object_name}\n{object_content}",
product_content=f"{product_name}\n{product_content}",
refer_content=refer_content
)
return system_prompt, user_prompt
def build_content_prompt_with_params(self, topic: Dict[str, Any],
styles: Optional[List[str]] = None,
audiences: Optional[List[str]] = None,
scenic_spots: Optional[List[str]] = None,
products: Optional[List[str]] = None,
step: str = "content") -> Tuple[str, str]:
"""
使用额外参数构建内容生成提示词
Args:
topic: 选题信息
styles: 风格列表
audiences: 受众列表
scenic_spots: 景区列表
products: 产品列表
step: 当前步骤用于过滤参考内容
Returns:
系统提示词和用户提示词的元组
"""
# 获取内容生成配置
content_config = self._ensure_content_config()
# 加载系统提示词和用户提示词模板
system_prompt_path = content_config.content_system_prompt
user_prompt_path = content_config.content_user_prompt
# 创建提示词模板
template = PromptTemplate(system_prompt_path, user_prompt_path)
# 获取风格内容
style_content = ''
if styles:
style_content = '\n'.join([f"{style}: {self.prompt_service.get_style_content(style)}" for style in styles])
else:
style_filename = topic.get("style", "")
style_content = f"{style_filename}\n{self.prompt_service.get_style_content(style_filename)}"
# 获取目标受众内容
demand_content = ''
if audiences:
demand_content = '\n'.join([f"{audience}: {self.prompt_service.get_audience_content(audience)}" for audience in audiences])
else:
demand_filename = topic.get("target_audience", "")
demand_content = f"{demand_filename}\n{self.prompt_service.get_audience_content(demand_filename)}"
# 获取景区信息
object_content = ''
if scenic_spots:
object_content = '\n'.join([f"{spot}: {self.prompt_service.get_scenic_spot_info(spot)}" for spot in scenic_spots])
else:
object_name = topic.get("object", "")
object_content = f"{object_name}\n{self.prompt_service.get_scenic_spot_info(object_name)}"
# 获取产品信息
product_content = ''
if products:
product_content = '\n'.join([f"{product}: {self.prompt_service.get_product_info(product)}" for product in products])
else:
product_name = topic.get("product", "")
product_content = f"{product_name}\n{self.prompt_service.get_product_info(product_name)}"
# 获取参考内容
refer_content = self.prompt_service.get_refer_content(step)
# 构建系统提示词
system_prompt = template.get_system_prompt()
# 构建用户提示词
user_prompt = template.build_user_prompt(
style_content=style_content,
demand_content=demand_content,
object_content=object_content,
product_content=product_content,
refer_content=refer_content
)
return system_prompt, user_prompt
def build_poster_prompt(self, topic: Dict[str, Any], content: Dict[str, Any]) -> Tuple[str, str]:
"""
构建海报生成提示词
Args:
topic: 选题信息
content: 生成的内容
Returns:
系统提示词和用户提示词的元组
"""
# 获取海报生成配置
poster_config = self._ensure_poster_config()
# 从配置中获取海报提示词模板路径
system_prompt_path = poster_config.poster_system_prompt
user_prompt_path = poster_config.poster_user_prompt
if not system_prompt_path or not user_prompt_path:
raise ValueError("海报提示词模板路径不完整")
# 创建提示词模板
template = PromptTemplate(system_prompt_path, user_prompt_path)
# 获取景区信息
object_name = topic.get("object", "")
object_content = self.prompt_service.get_scenic_spot_info(object_name)
# 构建系统提示词
system_prompt = template.get_system_prompt()
# 构建用户提示词
user_prompt = template.build_user_prompt(
content=content.get("content", ""),
title=content.get("title", ""),
object_name=object_name,
object_content=object_content
)
return system_prompt, user_prompt
2025-07-11 17:39:51 +08:00
def build_topic_prompt(self, products: Optional[List[str]] = None, scenic_spots: Optional[List[str]] = None, styles: Optional[List[str]] = None, audiences: Optional[List[str]] = None, dates: Optional[str] = None, num_topics: int = 5) -> Tuple[str, str]:
2025-07-11 13:50:08 +08:00
"""
构建选题生成提示词
Args:
2025-07-11 17:39:51 +08:00
products: 产品列表
scenic_spots: 景区列表
styles: 风格列表
audiences: 受众列表
dates: 日期字符串可能为单个日期多个日期用逗号分隔或范围如'2023-01-01 to 2023-01-31'
2025-07-11 13:50:08 +08:00
num_topics: 要生成的选题数量
Returns:
系统提示词和用户提示词的元组
"""
# 获取选题生成配置
topic_config = self._ensure_topic_config()
2025-07-11 13:50:08 +08:00
system_prompt_path = topic_config.topic_system_prompt
user_prompt_path = topic_config.topic_user_prompt
2025-07-11 13:50:08 +08:00
if not system_prompt_path or not user_prompt_path:
raise ValueError("选题提示词模板路径不完整")
# 创建提示词模板
template = PromptTemplate(system_prompt_path, user_prompt_path)
2025-07-11 17:39:51 +08:00
# 处理日期
if dates:
if ' to ' in dates:
start_date, end_date = dates.split(' to ')
month = f"{start_date}{end_date}"
elif ',' in dates:
month = ', '.join(dates.split(','))
else:
month = dates
else:
month = ''
2025-07-11 13:50:08 +08:00
2025-07-11 17:39:51 +08:00
# 获取风格内容
style_content = ''
if styles:
style_content = '\n'.join([f"{style}: {self.prompt_service.get_style_content(style)}" for style in styles])
else:
all_styles = self.prompt_service.get_all_styles()
style_content = "Style文件列表:\n" + "\n".join([f"- {style['name']}" for style in all_styles])
# 获取受众内容
demand_content = ''
if audiences:
demand_content = '\n'.join([f"{audience}: {self.prompt_service.get_audience_content(audience)}" for audience in audiences])
else:
all_audiences = self.prompt_service.get_all_audiences()
demand_content = "Demand文件列表:\n" + "\n".join([f"- {audience['name']}" for audience in all_audiences])
2025-07-11 13:50:08 +08:00
# 获取参考内容
refer_content = self.prompt_service.get_refer_content("topic")
2025-07-11 17:39:51 +08:00
# 获取景区内容
object_content = ''
if scenic_spots:
object_content = '\n'.join([f"{spot}: {self.prompt_service.get_scenic_spot_info(spot)}" for spot in scenic_spots])
else:
all_spots = self.prompt_service.get_all_scenic_spots()
object_content = "Object信息:\n" + "\n".join([f"- {spot['name']}" for spot in all_spots])
# 获取产品内容
product_content = ''
if products:
product_content = '\n'.join([f"{product}: {self.prompt_service.get_product_info(product)}" for product in products])
else:
product_content = '' # 假设没有默认产品列表
2025-07-11 13:50:08 +08:00
# 构建系统提示词
system_prompt = template.get_system_prompt()
# 构建创作资料
creative_materials = (
f"你拥有的创作资料如下:\n"
2025-07-11 17:39:51 +08:00
f"风格信息:\n{style_content}\n\n"
f"受众信息:\n{demand_content}\n\n"
f"参考内容:\n{refer_content}\n\n"
f"景区信息:\n{object_content}\n\n"
f"产品信息:\n{product_content}"
2025-07-11 13:50:08 +08:00
)
# 构建用户提示词
user_prompt = template.build_user_prompt(
creative_materials=creative_materials,
num_topics=num_topics,
month=month
)
return system_prompt, user_prompt
def build_judge_prompt(self, topic: Dict[str, Any], content: Dict[str, Any]) -> Tuple[str, str]:
"""
构建内容审核提示词
Args:
topic: 选题信息
content: 生成的内容
Returns:
系统提示词和用户提示词的元组
"""
# 获取内容生成配置
content_config = self._ensure_content_config()
2025-07-11 13:50:08 +08:00
# 从配置中获取审核提示词模板路径
system_prompt_path = content_config.judger_system_prompt
user_prompt_path = content_config.judger_user_prompt
2025-07-11 13:50:08 +08:00
# 创建提示词模板
template = PromptTemplate(system_prompt_path, user_prompt_path)
# 获取景区信息
object_name = topic.get("object", "")
object_content = self.prompt_service.get_scenic_spot_info(object_name)
# 获取产品信息
product_name = topic.get("product", "")
product_content = self.prompt_service.get_product_info(product_name)
# 获取参考内容
refer_content = self.prompt_service.get_refer_content("judge")
# 构建系统提示词
system_prompt = template.get_system_prompt()
# 格式化内容
import json
tweet_content = json.dumps(content, ensure_ascii=False, indent=4)
# 构建用户提示词
user_prompt = template.build_user_prompt(
tweet_content=tweet_content,
object_content=object_content,
product_content=product_content,
refer_content=refer_content
)
return system_prompt, user_prompt
def build_judge_prompt_with_params(self, topic: Dict[str, Any], content: Dict[str, Any],
styles: Optional[List[str]] = None,
audiences: Optional[List[str]] = None,
scenic_spots: Optional[List[str]] = None,
products: Optional[List[str]] = None) -> Tuple[str, str]:
"""
使用额外参数构建内容审核提示词
Args:
topic: 选题信息
content: 生成的内容
styles: 风格列表
audiences: 受众列表
scenic_spots: 景区列表
products: 产品列表
Returns:
系统提示词和用户提示词的元组
"""
# 获取内容生成配置
content_config = self._ensure_content_config()
# 从配置中获取审核提示词模板路径
system_prompt_path = content_config.judger_system_prompt
user_prompt_path = content_config.judger_user_prompt
# 创建提示词模板
template = PromptTemplate(system_prompt_path, user_prompt_path)
# 获取景区信息
object_content = ''
if scenic_spots:
object_content = '\n'.join([f"{spot}: {self.prompt_service.get_scenic_spot_info(spot)}" for spot in scenic_spots])
else:
object_name = topic.get("object", "")
object_content = f"{object_name}\n{self.prompt_service.get_scenic_spot_info(object_name)}"
# 获取产品信息
product_content = ''
if products:
product_content = '\n'.join([f"{product}: {self.prompt_service.get_product_info(product)}" for product in products])
else:
product_name = topic.get("product", "")
product_content = f"{product_name}\n{self.prompt_service.get_product_info(product_name)}"
# 获取参考内容
refer_content = self.prompt_service.get_refer_content("judge")
# 构建系统提示词
system_prompt = template.get_system_prompt()
# 格式化内容
import json
tweet_content = json.dumps(content, ensure_ascii=False, indent=4)
# 构建用户提示词
user_prompt = template.build_user_prompt(
tweet_content=tweet_content,
object_content=object_content,
product_content=product_content,
refer_content=refer_content
)
return system_prompt, user_prompt
def build_judge_prompt_simple(self, topic: Dict[str, Any], content: Dict[str, Any]) -> Tuple[str, str]:
"""
构建简化的内容审核提示词只需要产品信息景区信息和文章
Args:
topic: 选题信息
content: 生成的内容
Returns:
系统提示词和用户提示词的元组
"""
# 获取内容生成配置
content_config = self._ensure_content_config()
# 从配置中获取审核提示词模板路径
system_prompt_path = content_config.judger_system_prompt
user_prompt_path = content_config.judger_user_prompt
# 创建提示词模板
template = PromptTemplate(system_prompt_path, user_prompt_path)
# 获取景区信息
object_name = topic.get("object", "")
object_content = self.prompt_service.get_scenic_spot_info(object_name)
# 获取产品信息
product_name = topic.get("product", "")
product_content = self.prompt_service.get_product_info(product_name)
# 构建系统提示词
system_prompt = template.get_system_prompt()
# 格式化内容
import json
tweet_content = json.dumps(content, ensure_ascii=False, indent=4)
# 构建用户提示词(简化版,不包含参考内容)
user_prompt = template.build_user_prompt(
tweet_content=tweet_content,
object_content=object_content,
product_content=product_content,
refer_content="" # 简化版不使用参考内容
)
2025-07-11 13:50:08 +08:00
return system_prompt, user_prompt