From 31a5c73225627b2369385106403302a3e29a34e6 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 4 Sep 2026 18:26:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(inference):=20=E6=A8=A1=E6=9D=BF=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E5=85=9C=E5=BA=95=E4=BF=AE=E6=AD=A3=E2=80=94=E2=80=94?= =?UTF-8?q?=E5=8E=9F=E7=94=9Fjinja2.Environment=E6=97=A0renders=E6=96=B9?= =?UTF-8?q?=E6=B3=95(=E7=8B=AC=E7=AB=8B=E8=84=9A=E6=9C=AC=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=B4=A9=E6=BA=83);=E8=AF=8A=E6=96=AD=E6=94=B9print(?= =?UTF-8?q?=E6=A8=A1=E5=9D=97logger=E6=9C=AA=E6=8E=A5=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=AE=A1=E9=81=93)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pipeline_llm/inference.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/pipeline_llm/inference.py b/pipeline_llm/inference.py index 0fea1e8..2ad37d5 100644 --- a/pipeline_llm/inference.py +++ b/pipeline_llm/inference.py @@ -192,14 +192,18 @@ async def _render_tmpl(tmplstr, ns): te = getattr(ServerEnv(), 'tmpl_engine', None) except Exception: te = None - if te is None or not callable(getattr(te, 'renders', None)): - try: - from jinja2 import Environment - te = Environment(enable_async=True) - except Exception: - raise GovernError('模板引擎不可用:jinja2 未安装或未初始化,无法组包上游请求') - _tmpl_engine_cache = te - return await te.renders(tmplstr, ns) + if te is not None and callable(getattr(te, 'renders', None)): + _tmpl_engine_cache = te + return await te.renders(tmplstr, ns) + # 兜底:原生 jinja2.Environment 没有 renders 方法,用 from_string+render_async + try: + from jinja2 import Environment + env = Environment(enable_async=True) + _tmpl_engine_cache = None # 原生引擎不缓存(避免掩盖后续真正的引擎注入) + t = env.from_string(tmplstr) + return await t.render_async(ns) + except Exception as e: + raise GovernError('模板渲染失败(组包不可用):%s' % str(e)[:200]) async def _render_headers(tmpl, ns): @@ -274,10 +278,11 @@ async def _call_upstream_chat(ctx, payload): headers = await _render_headers(profile.get('headers', ''), ns) body = await _build_upstream_body(ctx, payload) timeout = int(_fnum(ctx.get('timeout')) or _TOTAL_TIMEOUT) - # 可诊断性:只记长度不记值(真 key 不落日志) - logger.info("inference: 组包 url=%s auth头长度=%d body字段=%s", - url.split('?')[0], len(headers.get('Authorization', '') or ''), - sorted(body.keys())) + # 可诊断性:只记长度不记值(真 key 不落日志)。print 确保进应用日志 + # (模块 logging.getLogger 的 handler 未接应用日志管道,2026-09-04 实测) + print("[inference] 组包 url=%s auth头长度=%d body字段=%s" % ( + url.split('?')[0], len(headers.get('Authorization', '') or ''), + sorted(body.keys()))) last = None for attempt in range(_MAX_ATTEMPTS):