修改了request_id格式和ouput——handle
This commit is contained in:
parent
28ff0979c1
commit
8f4a7c502d
Binary file not shown.
@ -6,6 +6,8 @@ API依赖注入模块
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from datetime import datetime
|
||||||
|
import uuid
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
from core.config import get_config_manager, ConfigManager
|
from core.config import get_config_manager, ConfigManager
|
||||||
from core.ai import AIAgent
|
from core.ai import AIAgent
|
||||||
@ -14,21 +16,15 @@ from utils.file_io import OutputManager
|
|||||||
# 全局依赖
|
# 全局依赖
|
||||||
config_manager: Optional[ConfigManager] = None
|
config_manager: Optional[ConfigManager] = None
|
||||||
ai_agent: Optional[AIAgent] = None
|
ai_agent: Optional[AIAgent] = None
|
||||||
output_manager: Optional[OutputManager] = None
|
|
||||||
|
|
||||||
def initialize_dependencies():
|
def initialize_dependencies():
|
||||||
"""初始化全局依赖"""
|
"""初始化全局依赖"""
|
||||||
global config_manager, ai_agent, output_manager
|
global config_manager, ai_agent
|
||||||
|
|
||||||
# 初始化配置 - 使用服务器模式
|
# 初始化配置 - 使用服务器模式
|
||||||
config_manager = get_config_manager()
|
config_manager = get_config_manager()
|
||||||
config_manager.load_from_directory("config", server_mode=True)
|
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代理
|
# 初始化AI代理
|
||||||
from core.config import AIModelConfig
|
from core.config import AIModelConfig
|
||||||
ai_config = config_manager.get_config('ai_model', AIModelConfig)
|
ai_config = config_manager.get_config('ai_model', AIModelConfig)
|
||||||
@ -46,13 +42,30 @@ def get_ai_agent() -> AIAgent:
|
|||||||
raise RuntimeError("AI代理未初始化")
|
raise RuntimeError("AI代理未初始化")
|
||||||
return ai_agent
|
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:
|
def get_output_manager() -> OutputManager:
|
||||||
"""获取输出管理器"""
|
"""获取输出管理器(每次调用创建新实例)"""
|
||||||
if output_manager is None:
|
return create_output_manager()
|
||||||
raise RuntimeError("输出管理器未初始化")
|
|
||||||
return output_manager
|
|
||||||
|
|
||||||
def get_tweet_service():
|
def get_tweet_service():
|
||||||
"""获取文字内容服务"""
|
"""获取文字内容服务"""
|
||||||
from api.services.tweet import TweetService
|
from api.services.tweet import TweetService
|
||||||
return TweetService(get_ai_agent(), get_config(), get_output_manager())
|
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)
|
||||||
Binary file not shown.
Binary file not shown.
@ -39,7 +39,7 @@ class PosterResponse(BaseModel):
|
|||||||
class Config:
|
class Config:
|
||||||
schema_extra = {
|
schema_extra = {
|
||||||
"example": {
|
"example": {
|
||||||
"request_id": "poster_20230715_123456",
|
"request_id": "poster-20240715-123456-a1b2c3d4",
|
||||||
"topic_index": "1",
|
"topic_index": "1",
|
||||||
"poster_path": "/result/run_20230715_123456/topic_1/poster_vibrant.png",
|
"poster_path": "/result/run_20230715_123456/topic_1/poster_vibrant.png",
|
||||||
"template_name": "vibrant"
|
"template_name": "vibrant"
|
||||||
@ -92,7 +92,7 @@ class PosterTextResponse(BaseModel):
|
|||||||
class Config:
|
class Config:
|
||||||
schema_extra = {
|
schema_extra = {
|
||||||
"example": {
|
"example": {
|
||||||
"request_id": "text_20230715_123456",
|
"request_id": "text-20240715-123456-a1b2c3d4",
|
||||||
"text_content": {
|
"text_content": {
|
||||||
"title": "紫禁城的秘密",
|
"title": "紫禁城的秘密",
|
||||||
"subtitle": "600年历史,等你探索",
|
"subtitle": "600年历史,等你探索",
|
||||||
|
|||||||
@ -39,7 +39,7 @@ class TopicResponse(BaseModel):
|
|||||||
class Config:
|
class Config:
|
||||||
schema_extra = {
|
schema_extra = {
|
||||||
"example": {
|
"example": {
|
||||||
"request_id": "topic_20230715_123456",
|
"request_id": "topic-20240715-123456-a1b2c3d4",
|
||||||
"topics": [
|
"topics": [
|
||||||
{
|
{
|
||||||
"index": "1",
|
"index": "1",
|
||||||
@ -97,7 +97,7 @@ class ContentResponse(BaseModel):
|
|||||||
class Config:
|
class Config:
|
||||||
schema_extra = {
|
schema_extra = {
|
||||||
"example": {
|
"example": {
|
||||||
"request_id": "content_20230715_123456",
|
"request_id": "content-20240715-123456-a1b2c3d4",
|
||||||
"topic_index": "1",
|
"topic_index": "1",
|
||||||
"content": {
|
"content": {
|
||||||
"title": "【北京故宫】避开人潮的秘密路线,90%的人都不知道!",
|
"title": "【北京故宫】避开人潮的秘密路线,90%的人都不知道!",
|
||||||
@ -153,7 +153,7 @@ class JudgeResponse(BaseModel):
|
|||||||
class Config:
|
class Config:
|
||||||
schema_extra = {
|
schema_extra = {
|
||||||
"example": {
|
"example": {
|
||||||
"request_id": "judge_20230715_123456",
|
"request_id": "judge-20240715-123456-a1b2c3d4",
|
||||||
"topic_index": "1",
|
"topic_index": "1",
|
||||||
"content": {
|
"content": {
|
||||||
"title": "【北京故宫】避开人潮的秘密路线,90%的人都不知道!",
|
"title": "【北京故宫】避开人潮的秘密路线,90%的人都不知道!",
|
||||||
@ -201,7 +201,7 @@ class PipelineResponse(BaseModel):
|
|||||||
class Config:
|
class Config:
|
||||||
schema_extra = {
|
schema_extra = {
|
||||||
"example": {
|
"example": {
|
||||||
"request_id": "pipeline_20230715_123456",
|
"request_id": "pipeline-20240715-123456-a1b2c3d4",
|
||||||
"topics": [
|
"topics": [
|
||||||
{
|
{
|
||||||
"index": "1",
|
"index": "1",
|
||||||
|
|||||||
Binary file not shown.
@ -110,7 +110,7 @@ class GenerateContentResponse(BaseModel):
|
|||||||
class Config:
|
class Config:
|
||||||
schema_extra = {
|
schema_extra = {
|
||||||
"example": {
|
"example": {
|
||||||
"request_id": "content_20230715_123456",
|
"request_id": "content-20240715-123456-a1b2c3d4",
|
||||||
"topic_index": "1",
|
"topic_index": "1",
|
||||||
"content": {
|
"content": {
|
||||||
"title": "【北京故宫】避开人潮的秘密路线,90%的人都不知道!",
|
"title": "【北京故宫】避开人潮的秘密路线,90%的人都不知道!",
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -63,7 +63,7 @@ class PosterService:
|
|||||||
template_name = self.poster_generator._select_template()
|
template_name = self.poster_generator._select_template()
|
||||||
|
|
||||||
# 生成请求ID
|
# 生成请求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}")
|
logger.info(f"海报生成完成,请求ID: {request_id}, 主题索引: {topic_index}, 模板: {template_name}")
|
||||||
return request_id, topic_index, poster_path, template_name
|
return request_id, topic_index, poster_path, template_name
|
||||||
@ -117,7 +117,7 @@ class PosterService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 生成请求ID
|
# 生成请求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}")
|
logger.info(f"海报文案生成完成,请求ID: {request_id}")
|
||||||
return request_id, text_content
|
return request_id, text_content
|
||||||
@ -92,7 +92,7 @@ class TweetService:
|
|||||||
return str(uuid.uuid4()), []
|
return str(uuid.uuid4()), []
|
||||||
|
|
||||||
# 生成请求ID
|
# 生成请求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)}")
|
logger.info(f"选题生成完成,请求ID: {request_id}, 数量: {len(topics)}")
|
||||||
return request_id, topics
|
return request_id, topics
|
||||||
@ -165,7 +165,7 @@ class TweetService:
|
|||||||
content['judge_success'] = False
|
content['judge_success'] = False
|
||||||
|
|
||||||
# 生成请求ID
|
# 生成请求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}")
|
logger.info(f"内容生成完成,请求ID: {request_id}, 选题索引: {topic_index}")
|
||||||
return request_id, topic_index, content
|
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)
|
content = await self.content_generator.generate_content_with_prompt(topic, system_prompt, user_prompt)
|
||||||
|
|
||||||
# 生成请求ID
|
# 生成请求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}")
|
logger.info(f"内容生成完成,请求ID: {request_id}, 选题索引: {topic_index}")
|
||||||
return request_id, topic_index, content
|
return request_id, topic_index, content
|
||||||
@ -243,7 +243,7 @@ class TweetService:
|
|||||||
judge_success = judged_data.get('judge_success', False)
|
judge_success = judged_data.get('judge_success', False)
|
||||||
|
|
||||||
# 生成请求ID
|
# 生成请求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}")
|
logger.info(f"内容审核完成,请求ID: {request_id}, 选题索引: {topic_index}, 审核结果: {judge_success}")
|
||||||
return request_id, topic_index, judged_data, 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}")
|
logger.info(f"开始运行完整流水线,日期: {dates}, 数量: {num_topics}, 内嵌审核: {auto_judge}")
|
||||||
|
|
||||||
# 生成请求ID
|
# 生成请求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: 生成选题
|
# 步骤1: 生成选题
|
||||||
_, topics = await self.generate_topics(dates, num_topics, styles, audiences, scenic_spots, products)
|
_, topics = await self.generate_topics(dates, num_topics, styles, audiences, scenic_spots, products)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user