diff --git a/api/__pycache__/dependencies.cpython-312.pyc b/api/__pycache__/dependencies.cpython-312.pyc index 3a61995..d295e19 100644 Binary files a/api/__pycache__/dependencies.cpython-312.pyc and b/api/__pycache__/dependencies.cpython-312.pyc differ diff --git a/api/dependencies.py b/api/dependencies.py index d88fce8..ffe5ed9 100644 --- a/api/dependencies.py +++ b/api/dependencies.py @@ -6,6 +6,8 @@ API依赖注入模块 """ from typing import Optional +from datetime import datetime +import uuid from fastapi import Depends from core.config import get_config_manager, ConfigManager from core.ai import AIAgent @@ -14,21 +16,15 @@ from utils.file_io import OutputManager # 全局依赖 config_manager: Optional[ConfigManager] = None ai_agent: Optional[AIAgent] = None -output_manager: Optional[OutputManager] = None def initialize_dependencies(): """初始化全局依赖""" - global config_manager, ai_agent, output_manager + global config_manager, ai_agent # 初始化配置 - 使用服务器模式 config_manager = get_config_manager() config_manager.load_from_directory("config", server_mode=True) - # 初始化输出管理器 - from datetime import datetime - run_id = f"api_{datetime.now().strftime('%Y%m%d_%H%M%S')}" - output_manager = OutputManager("result", run_id) - # 初始化AI代理 from core.config import AIModelConfig ai_config = config_manager.get_config('ai_model', AIModelConfig) @@ -46,13 +42,30 @@ def get_ai_agent() -> AIAgent: raise RuntimeError("AI代理未初始化") return ai_agent +def create_output_manager() -> OutputManager: + """为每个请求创建新的输出管理器""" + # 为每个请求生成唯一的run_id + run_id = f"api_request-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}" + return OutputManager("result", run_id) + def get_output_manager() -> OutputManager: - """获取输出管理器""" - if output_manager is None: - raise RuntimeError("输出管理器未初始化") - return output_manager + """获取输出管理器(每次调用创建新实例)""" + return create_output_manager() def get_tweet_service(): """获取文字内容服务""" from api.services.tweet import TweetService - return TweetService(get_ai_agent(), get_config(), get_output_manager()) \ No newline at end of file + return TweetService(get_ai_agent(), get_config(), get_output_manager()) + +def get_poster_service(): + """获取海报服务""" + from api.services.poster import PosterService + return PosterService(get_ai_agent(), get_config(), get_output_manager()) + +def get_prompt_builder(): + """获取提示词构建器服务""" + from api.services.prompt_builder import PromptBuilderService + from api.services.prompt_service import PromptService + + prompt_service = PromptService(get_config()) + return PromptBuilderService(get_config(), prompt_service) \ No newline at end of file diff --git a/api/models/__pycache__/poster.cpython-312.pyc b/api/models/__pycache__/poster.cpython-312.pyc index 639d214..dfee064 100644 Binary files a/api/models/__pycache__/poster.cpython-312.pyc and b/api/models/__pycache__/poster.cpython-312.pyc differ diff --git a/api/models/__pycache__/tweet.cpython-312.pyc b/api/models/__pycache__/tweet.cpython-312.pyc index 108141e..66fafa7 100644 Binary files a/api/models/__pycache__/tweet.cpython-312.pyc and b/api/models/__pycache__/tweet.cpython-312.pyc differ diff --git a/api/models/poster.py b/api/models/poster.py index 010ba7b..17afb23 100644 --- a/api/models/poster.py +++ b/api/models/poster.py @@ -39,7 +39,7 @@ class PosterResponse(BaseModel): class Config: schema_extra = { "example": { - "request_id": "poster_20230715_123456", + "request_id": "poster-20240715-123456-a1b2c3d4", "topic_index": "1", "poster_path": "/result/run_20230715_123456/topic_1/poster_vibrant.png", "template_name": "vibrant" @@ -92,7 +92,7 @@ class PosterTextResponse(BaseModel): class Config: schema_extra = { "example": { - "request_id": "text_20230715_123456", + "request_id": "text-20240715-123456-a1b2c3d4", "text_content": { "title": "紫禁城的秘密", "subtitle": "600年历史,等你探索", diff --git a/api/models/tweet.py b/api/models/tweet.py index 0ae49df..21ff406 100644 --- a/api/models/tweet.py +++ b/api/models/tweet.py @@ -39,7 +39,7 @@ class TopicResponse(BaseModel): class Config: schema_extra = { "example": { - "request_id": "topic_20230715_123456", + "request_id": "topic-20240715-123456-a1b2c3d4", "topics": [ { "index": "1", @@ -97,7 +97,7 @@ class ContentResponse(BaseModel): class Config: schema_extra = { "example": { - "request_id": "content_20230715_123456", + "request_id": "content-20240715-123456-a1b2c3d4", "topic_index": "1", "content": { "title": "【北京故宫】避开人潮的秘密路线,90%的人都不知道!", @@ -153,7 +153,7 @@ class JudgeResponse(BaseModel): class Config: schema_extra = { "example": { - "request_id": "judge_20230715_123456", + "request_id": "judge-20240715-123456-a1b2c3d4", "topic_index": "1", "content": { "title": "【北京故宫】避开人潮的秘密路线,90%的人都不知道!", @@ -201,7 +201,7 @@ class PipelineResponse(BaseModel): class Config: schema_extra = { "example": { - "request_id": "pipeline_20230715_123456", + "request_id": "pipeline-20240715-123456-a1b2c3d4", "topics": [ { "index": "1", diff --git a/api/routers/__pycache__/prompt.cpython-312.pyc b/api/routers/__pycache__/prompt.cpython-312.pyc index 81afe52..5d84379 100644 Binary files a/api/routers/__pycache__/prompt.cpython-312.pyc and b/api/routers/__pycache__/prompt.cpython-312.pyc differ diff --git a/api/routers/prompt.py b/api/routers/prompt.py index 5618937..ddf2237 100644 --- a/api/routers/prompt.py +++ b/api/routers/prompt.py @@ -110,7 +110,7 @@ class GenerateContentResponse(BaseModel): class Config: schema_extra = { "example": { - "request_id": "content_20230715_123456", + "request_id": "content-20240715-123456-a1b2c3d4", "topic_index": "1", "content": { "title": "【北京故宫】避开人潮的秘密路线,90%的人都不知道!", diff --git a/api/services/__pycache__/poster.cpython-312.pyc b/api/services/__pycache__/poster.cpython-312.pyc index 135d452..fc910e4 100644 Binary files a/api/services/__pycache__/poster.cpython-312.pyc and b/api/services/__pycache__/poster.cpython-312.pyc differ diff --git a/api/services/__pycache__/tweet.cpython-312.pyc b/api/services/__pycache__/tweet.cpython-312.pyc index 0bcf407..6934a93 100644 Binary files a/api/services/__pycache__/tweet.cpython-312.pyc and b/api/services/__pycache__/tweet.cpython-312.pyc differ diff --git a/api/services/poster.py b/api/services/poster.py index a375fb4..709264d 100644 --- a/api/services/poster.py +++ b/api/services/poster.py @@ -63,7 +63,7 @@ class PosterService: template_name = self.poster_generator._select_template() # 生成请求ID - request_id = f"poster_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{str(uuid.uuid4())[:8]}" + request_id = f"poster-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}" logger.info(f"海报生成完成,请求ID: {request_id}, 主题索引: {topic_index}, 模板: {template_name}") return request_id, topic_index, poster_path, template_name @@ -117,7 +117,7 @@ class PosterService: ) # 生成请求ID - request_id = f"text_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{str(uuid.uuid4())[:8]}" + request_id = f"text-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}" logger.info(f"海报文案生成完成,请求ID: {request_id}") return request_id, text_content \ No newline at end of file diff --git a/api/services/tweet.py b/api/services/tweet.py index f758028..8f30153 100644 --- a/api/services/tweet.py +++ b/api/services/tweet.py @@ -92,7 +92,7 @@ class TweetService: return str(uuid.uuid4()), [] # 生成请求ID - request_id = f"topic_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{str(uuid.uuid4())[:8]}" + request_id = f"topic-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}" logger.info(f"选题生成完成,请求ID: {request_id}, 数量: {len(topics)}") return request_id, topics @@ -165,7 +165,7 @@ class TweetService: content['judge_success'] = False # 生成请求ID - request_id = f"content_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{str(uuid.uuid4())[:8]}" + request_id = f"content-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}" logger.info(f"内容生成完成,请求ID: {request_id}, 选题索引: {topic_index}") return request_id, topic_index, content @@ -189,7 +189,7 @@ class TweetService: content = await self.content_generator.generate_content_with_prompt(topic, system_prompt, user_prompt) # 生成请求ID - request_id = f"content_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{str(uuid.uuid4())[:8]}" + request_id = f"content-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}" logger.info(f"内容生成完成,请求ID: {request_id}, 选题索引: {topic_index}") return request_id, topic_index, content @@ -243,7 +243,7 @@ class TweetService: judge_success = judged_data.get('judge_success', False) # 生成请求ID - request_id = f"judge_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{str(uuid.uuid4())[:8]}" + request_id = f"judge-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}" logger.info(f"内容审核完成,请求ID: {request_id}, 选题索引: {topic_index}, 审核结果: {judge_success}") return request_id, topic_index, judged_data, judge_success @@ -274,7 +274,7 @@ class TweetService: logger.info(f"开始运行完整流水线,日期: {dates}, 数量: {num_topics}, 内嵌审核: {auto_judge}") # 生成请求ID - request_id = f"pipeline_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{str(uuid.uuid4())[:8]}" + request_id = f"pipeline-{datetime.now().strftime('%Y%m%d-%H%M%S')}-{str(uuid.uuid4())[:8]}" # 步骤1: 生成选题 _, topics = await self.generate_topics(dates, num_topics, styles, audiences, scenic_spots, products)