2025-07-31 15:35:23 +08:00

140 lines
8.9 KiB
JSON
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.

{
"file_path": "poster/text_generator.py",
"file_size": 2350,
"line_count": 80,
"functions": [
{
"name": "__init__",
"line_start": 19,
"line_end": 27,
"args": [
{
"name": "self"
},
{
"name": "ai_agent",
"type_hint": "AIAgent"
}
],
"return_type": null,
"docstring": "初始化内容生成器。\n\nArgs:\n ai_agent (AIAgent): 用于与AI模型交互的代理。",
"is_async": false,
"decorators": [],
"code": " def __init__(self, ai_agent: AIAgent):\n \"\"\"\n 初始化内容生成器。\n\n Args:\n ai_agent (AIAgent): 用于与AI模型交互的代理。\n \"\"\"\n self.ai_agent = ai_agent\n self.logger = logging.getLogger(__name__)",
"code_hash": "d4242ab8f31b36684a6b1ccb6f5136da"
}
],
"classes": [
{
"name": "PosterContentGenerator",
"line_start": 15,
"line_end": 81,
"bases": [],
"methods": [
{
"name": "__init__",
"line_start": 19,
"line_end": 27,
"args": [
{
"name": "self"
},
{
"name": "ai_agent",
"type_hint": "AIAgent"
}
],
"return_type": null,
"docstring": "初始化内容生成器。\n\nArgs:\n ai_agent (AIAgent): 用于与AI模型交互的代理。",
"is_async": false,
"decorators": [],
"code": " def __init__(self, ai_agent: AIAgent):\n \"\"\"\n 初始化内容生成器。\n\n Args:\n ai_agent (AIAgent): 用于与AI模型交互的代理。\n \"\"\"\n self.ai_agent = ai_agent\n self.logger = logging.getLogger(__name__)",
"code_hash": "d4242ab8f31b36684a6b1ccb6f5136da"
},
{
"name": "generate_text_for_poster",
"line_start": 29,
"line_end": 81,
"args": [
{
"name": "self"
},
{
"name": "system_prompt",
"type_hint": "str"
},
{
"name": "user_prompt",
"type_hint": "str"
},
{
"name": "context_data",
"type_hint": "Optional[Dict[str, Any]]"
},
{
"name": "temperature",
"type_hint": "Optional[float]"
},
{
"name": "top_p",
"type_hint": "Optional[float]"
}
],
"return_type": "Optional[Dict[str, Any]]",
"docstring": "为单个海报任务生成文本内容。\n\nArgs:\n system_prompt (str): 提供给AI的系统级指令。\n user_prompt (str): 提供给AI的用户级指令可包含占位符。\n context_data (Optional[Dict[str, Any]]): 用于填充用户指令占位符的数据。\n temperature (Optional[float]): AI模型温度参数。\n top_p (Optional[float]): AI模型top_p参数。\n\nReturns:\n Optional[Dict[str, Any]]: 解析后的JSON对象包含生成的文本如果失败则返回None。",
"is_async": true,
"decorators": [],
"code": " async def generate_text_for_poster(\n self,\n system_prompt: str,\n user_prompt: str,\n context_data: Optional[Dict[str, Any]] = None,\n temperature: Optional[float] = None,\n top_p: Optional[float] = None,\n ) -> Optional[Dict[str, Any]]:\n \"\"\"\n 为单个海报任务生成文本内容。\n\n Args:\n system_prompt (str): 提供给AI的系统级指令。\n user_prompt (str): 提供给AI的用户级指令可包含占位符。\n context_data (Optional[Dict[str, Any]]): 用于填充用户指令占位符的数据。\n temperature (Optional[float]): AI模型温度参数。\n top_p (Optional[float]): AI模型top_p参数。\n\n Returns:\n Optional[Dict[str, Any]]: 解析后的JSON对象包含生成的文本如果失败则返回None。\n \"\"\"\n if context_data:\n try:\n # 使用上下文数据格式化用户提示\n final_user_prompt = user_prompt.format(**context_data)\n except KeyError as e:\n self.logger.error(f\"格式化用户提示失败,缺少键: {e}\")\n return None\n else:\n final_user_prompt = user_prompt\n\n self.logger.info(\"正在调用AI生成海报文案...\")\n self.logger.debug(f\"System Prompt: {system_prompt[:200]}...\")\n self.logger.debug(f\"User Prompt: {final_user_prompt[:200]}...\")\n\n try:\n raw_response, _, _, _ = await self.ai_agent.generate_text(\n system_prompt=system_prompt,\n user_prompt=final_user_prompt,\n temperature=temperature,\n top_p=top_p\n )\n\n if not raw_response:\n self.logger.error(\"AI未能返回任何内容。\")\n return None\n\n # 使用通用JSON解析函数处理响应\n return process_llm_json_text(raw_response)\n\n except Exception as e:\n self.logger.error(f\"调用AI生成文案时发生严重错误: {e}\", exc_info=True)\n return None ",
"code_hash": "eb82547f3fe0aefb95850fa0b9eeddfe"
}
],
"docstring": "使用AI模型为海报生成文本内容。",
"decorators": [],
"code": "class PosterContentGenerator:\n \"\"\"\n 使用AI模型为海报生成文本内容。\n \"\"\"\n def __init__(self, ai_agent: AIAgent):\n \"\"\"\n 初始化内容生成器。\n\n Args:\n ai_agent (AIAgent): 用于与AI模型交互的代理。\n \"\"\"\n self.ai_agent = ai_agent\n self.logger = logging.getLogger(__name__)\n\n async def generate_text_for_poster(\n self,\n system_prompt: str,\n user_prompt: str,\n context_data: Optional[Dict[str, Any]] = None,\n temperature: Optional[float] = None,\n top_p: Optional[float] = None,\n ) -> Optional[Dict[str, Any]]:\n \"\"\"\n 为单个海报任务生成文本内容。\n\n Args:\n system_prompt (str): 提供给AI的系统级指令。\n user_prompt (str): 提供给AI的用户级指令可包含占位符。\n context_data (Optional[Dict[str, Any]]): 用于填充用户指令占位符的数据。\n temperature (Optional[float]): AI模型温度参数。\n top_p (Optional[float]): AI模型top_p参数。\n\n Returns:\n Optional[Dict[str, Any]]: 解析后的JSON对象包含生成的文本如果失败则返回None。\n \"\"\"\n if context_data:\n try:\n # 使用上下文数据格式化用户提示\n final_user_prompt = user_prompt.format(**context_data)\n except KeyError as e:\n self.logger.error(f\"格式化用户提示失败,缺少键: {e}\")\n return None\n else:\n final_user_prompt = user_prompt\n\n self.logger.info(\"正在调用AI生成海报文案...\")\n self.logger.debug(f\"System Prompt: {system_prompt[:200]}...\")\n self.logger.debug(f\"User Prompt: {final_user_prompt[:200]}...\")\n\n try:\n raw_response, _, _, _ = await self.ai_agent.generate_text(\n system_prompt=system_prompt,\n user_prompt=final_user_prompt,\n temperature=temperature,\n top_p=top_p\n )\n\n if not raw_response:\n self.logger.error(\"AI未能返回任何内容。\")\n return None\n\n # 使用通用JSON解析函数处理响应\n return process_llm_json_text(raw_response)\n\n except Exception as e:\n self.logger.error(f\"调用AI生成文案时发生严重错误: {e}\", exc_info=True)\n return None ",
"code_hash": "7cbdd2b9ce079ad3bd1e3cd408023334"
}
],
"imports": [
{
"type": "import",
"modules": [
"logging"
],
"aliases": []
},
{
"type": "from_import",
"module": "typing",
"names": [
"Dict",
"Any",
"Optional",
"List"
],
"aliases": [],
"level": 0
},
{
"type": "from_import",
"module": "core.ai",
"names": [
"AIAgent"
],
"aliases": [],
"level": 0
},
{
"type": "from_import",
"module": "utils.file_io",
"names": [
"ResourceLoader",
"process_llm_json_text"
],
"aliases": [],
"level": 0
}
],
"constants": [],
"docstring": "海报文案内容生成器",
"content_hash": "56eef0aeda44b5fe49e0305a0267651a"
}