From 8f4a7c502d84b8c16cf7c59cc5fc26840f8664c3 Mon Sep 17 00:00:00 2001 From: jinye_huang Date: Mon, 14 Jul 2025 13:59:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86request=5Fid?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=92=8Couput=E2=80=94=E2=80=94handle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/__pycache__/dependencies.cpython-312.pyc | Bin 2363 -> 3304 bytes api/dependencies.py | 37 ++++++++++++------ api/models/__pycache__/poster.cpython-312.pyc | Bin 5057 -> 5075 bytes api/models/__pycache__/tweet.cpython-312.pyc | Bin 9789 -> 9825 bytes api/models/poster.py | 4 +- api/models/tweet.py | 8 ++-- .../__pycache__/prompt.cpython-312.pyc | Bin 8865 -> 8874 bytes api/routers/prompt.py | 2 +- .../__pycache__/poster.cpython-312.pyc | Bin 5368 -> 5368 bytes .../__pycache__/tweet.cpython-312.pyc | Bin 15650 -> 15648 bytes api/services/poster.py | 4 +- api/services/tweet.py | 10 ++--- 12 files changed, 39 insertions(+), 26 deletions(-) diff --git a/api/__pycache__/dependencies.cpython-312.pyc b/api/__pycache__/dependencies.cpython-312.pyc index 3a619957c3c646bed8d8505e90523f1b37a978cb..d295e1905b963bc146cc3ab7d6c5cebd480e8b8b 100644 GIT binary patch literal 3304 zcmc&$U2GKB6~41O-W~7ku8n`Nu?=+`8CX;7@FOUcL@^-=4z{6zO35beXzCfegZ8g? zW|A6VX`4bINhy{Z*(5ee5JYui3Pe=|B?-zyUaC~>Sc^oN8bwt-Yx5KfH!pqaId^t; z{ljCWUTN-p_ug}V&N<&X_kJCTgb0+@apULT2!#9#8(#4jz`|bvjFKM{g~W-%DZT`k z^u>J~Vt>M)^vC@S^9erALtj~5pE!?4K;ipvA#U{vC2*X0^M*Jh;`JZrCJU#KgM-1CVAjHjbNamV z`?s9mj5sGJoKLc@Y3+ck;}Ota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dmJZbfj>Gu*xJ zf9`*ArT^oX{ja{R$;sRO?=pT>pDfIX*q5Pz3WzfQ+o1eSW8p*%RJc6t0Nm(HLWHnr z(@Agf7*xtGIzomHz2G(-TIV;7+B!5-n}~IcNzlYN)>wDN_1hG7J=R-x$i$jZm;r^w zN>@TljLCd$m%=DxD`IXZoNKuq2fIPK2FZIDFt&X^U^a6DKg2j0dfKHJ=jlPwN%=|T zb-3l3cQ)p|fJ7bVBz;9#p{=kh#IAx6dT7W<{l}2d0e;>*^2VLRa~t`dak+eZbaqFY md#H}0%I+Z*P4&*~N#fWwRaEM=0ha(zT~7HuQ$dR;neBgD$|H3E 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 639d214048f2b538aef721859902f4e9c719e792..dfee064013e0e00527faa72e7821e835d6fc1c43 100644 GIT binary patch delta 341 zcmX@8ep#LOG%qg~0}!OlDalx~kvE84u}Z!mzqll|NY}`~$i%?h&{WsZ$k@cxOgGUm z$tc-4#bk0hdpo23=Kt(TOl*2Utws8i`}yP;4JWs8c`@ouzQ`ras6V+uNP@|p~+fg24b6o2n!Hl1tM&~rrAwi#}{J?mRAOes(=WD zN;VLe1w^od2$05GtZ6y E0M*1zcmMzZ delta 375 zcmcbteo&qFG%qg~0}vc;$#u-W#LxAVoAg5?p$B2==0RI-2wR+z?H ztZ6y#!e*1x`1={nz;^Qs*fWaV5-CZoD8cYvuHZCASCF-$?m%rZKwPW@ zBw83A@QB`!Gns5I6abX(5K>}u0U7Hud4rG-qs!#KLf)ESKY}d-OUZ%cO$$UsC=Ik?i_2n6jp1U# zO(S!Wgm~0&BBqIn2M@%PAtolq#2E48j291H(f5Bv;2^lk`(@s|+1WS$nSIswz0b5^ zG8O0XXJAF2-g<7jVQ^&}dU`eyYxFhwnww601C3svzd3NKaojuMi}@#;vkwiMjJ0KN z7Izk~Ag!6k)N?$6)&`shx`1||gX&9N%2}8#9AS^F^okFb0;|N=Y1=&d~|eBNQpZ4A|-?u0waJ7gz2Z# zCP!dK=(94%BI0_9qfutO0Iw0~p$$uwgwN4tNj)hQ_ZEWip8kqrf#_47kqmS6@+H1-v#u1*$muwc0!~Tdi!0j$0RVqO)OoYxT-;#KcLm zt+O}^p|7?rISS_xy{okIs2`joWKbttGmhl^YD1^f%*nlqkG@-?c6oS+{>5qTC3g# z!EWe4I&es=_yRf1duZNpopn%QPU&)6!Fy&+se{9@L@1W{vpDXd_M+#uKCv^EofruF z7PZ`k^UeT}c|ZQzbWVf=V#R9&rZpdSn%>!)=&NNf%@}JPo7q}y*#Yze3eZDujAq4; z&`;ltapsqmO%(fC{&A@JKqsx4%2*%0w$+leWHRW1cmUY&gO}s60zj{eIegQ}7O1Vj zrl;+A3;^c=)Xy&nz*fY4!dsPWs+C2^qt0fW6(!UeZ1httR>DXMQ;B(%g;6qnH-D+@ zg@}g7iUiV2_deZSN#DHj`Ob8D=308`kvR2Nsa1>i>1km*1uYg=$}}_<42S-9A7}le zKpziD*JI-SoLF03cMI2hE!#StF*}Nbqcmw9W>IOjQr4ZN^$L}iDoZGCAFn!rly2Z8 z-~&zp((coUnyJ9CTj2;Z1DK$G$NhXM^kh`r>?p!fswscVMjuu*W=*BcBax9%G!omU zq%%KiTorztD0j;I_o@Uksp;DSvdsKPuXXZ9y@Jpic-$m-^8ZxQ~&?~ 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 81afe5249010db7e27ba8824a0ae61039ce695eb..5d84379f1be952d0617c35c201ec397594809072 100644 GIT binary patch delta 330 zcmZ4Jy2_RJG%qg~0}$lSDak0_$jc_ARHcxdpI4HaSE6fVU}R!oZfL4&Xk=_+YNnfL zm}Hb}oMN(BM@WH@(P6W%FgG(}#N80F0AePBh$Ilf z0U`{5geGTE1c;pqB5XiJI*7;w5uPBz3q-hqh~&xVq*NITHh+;)U^WN?ij;}~iRV-1 zKkJzAw5|2&x}8s#Y<#h0sg*(z#A2YVCKtre{K+nII*JKE5v-2jf;wXIL^(sook(W(K@|JjVeqKpxUP-)>fswI+xuI#ip^>qPso7>z zAq7T8^UYzx+{}!Tlas_O8B;gU5u3rx77a9@C}y&iRHREHh?xW;l0gIqh%f*Wnw&+E zAa)vvumKSnAR-Gyc!CHo5a9+QQYK%QQe`aM{8vhWnK68_oSdu>!~&qCCKp6=!DL@K z9Yvg$aX~GcJWI}yF=g@@IW5N0&EMs&Fft}hKBHjDoeHw25JZ$t=24VoY}l-&*uV$? DvH(5K 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 135d452c5aeae3b33ef3253d697278490c5a5b73..fc910e47822cdfb11be1ba7e5e8962fd1943f583 100644 GIT binary patch delta 328 zcmeyN`9qWYG%qg~0}#l~DcQ(9i9@=Iy&%81B(+HQ7q4ohYOZREuBwNsuWIluM%~Ff zI9{`<0@YT@PPP@4-F%EQlZnx2vjGnui;g)^xX1!TSb_*E5Md1@{Hj<>QY%Vyixfa2 ziXcL1a+ZJrn=**0GI@c3hKD+csR1H1L4+2F&;}Bk97W0suwzNGJdR delta 310 zcmeyN`9qWYG%qg~0}$x8=5OSl#36l)y&%81B(*617q4ohYOZQZysC$)uWIlu#`wuQ zI9{`<0o7K?PPP@4-F%EQlZnxIvjGnui;e|QxX2PjSb+#@5Mcu({BE(9q*j!~7b${7 zlt6^?xACqJ>I$Z_+0Aq7T$SC9~UPJViN vW?p)c_T+QID>R)zYQYW?0I}RbgcOL701~%2Y;yBcN^?@}iUKCj7l{M_C74J3 diff --git a/api/services/__pycache__/tweet.cpython-312.pyc b/api/services/__pycache__/tweet.cpython-312.pyc index 0bcf40783041eb4e6a9000edfad001353f63671c..6934a9367a764eaed79c903b8faec7fe011fa188 100644 GIT binary patch delta 1037 zcmaKrO-~b16oxyroz~U@O%P}mT1W=kNMUGAkPk(%si2`+@FR^{g6*UXlumOy7-(X2 zX`+cScS{WV1B^HejEyE5cm4r$7bY%5e}ZA}`dC$kZtZY?WUpyX{ zg+K1iZPz=Gzar{dNzF#~JW^U(m2wejTAG#SnGi9)dA>FYQDdj|o+!rn4KicII`TsM zo-?Zzb)~3B$N<6-V35A;2L15#A6tsL^E#}Fn@o4<05UwD>>`oi3 Yhd>X2ZDh`gRV}wxP|lJk^!cm4r$7bY%5e}Zx4eWwkg3uf`;J?Ea8yyx6AtIN-qU7tN3 zmyJJ(o9nK3o&XaKy`0aezdh1LsVHStX-YaN%`!nXzk0qj2#Wc-<%TE@@Hfb`sdVIo zcIM3JB||G2D%pp04CtpXedDb%WSoOJGJ5$o8KsT>CwmSc>jXxCgTN4w0ERiDt)vTD zp&$G{VS+M$w>yc%VIWP#z#(DGoD78QVl8bh20u24(>Og&m2iCJ1eYmt28k`E5S|6j zaYWtZJoKhX5KbGDni zd#*d&M{zO*9HWnWyWQ3Y>Wx%;i-kL@Vu%UU5{+Uh^W*XE}zdur|T`lvuAs1*OyflP`= zxUi_5HN<2l7({)wKO$sgp0k#0d-r4Ky#rl#;SP-rriCGT zZ!p*ilgx2V*9!y&Yui|WS~lMfzPEc{3A+C68!zMEe8F&fOXS 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)