feat(chat): 新增对话流性能监控功能及相关日志输出
This commit is contained in:
1 parent
e827c5dc38
commit
9d7dad6a4b
19 files changed
+1603
-271
No files matched your search
@@ -35,6 +35,7 @@ from flask import current_app
|
||||
|
||||
from app.config import Config
|
||||
from app.models import ChatLog, db
|
||||
from app.services.chat_perf import ChatPerfTracker, chat_perf_enabled
|
||||
from app.services.integration_api import IntegrationApiService
|
||||
|
||||
|
||||
@@ -561,6 +562,7 @@ class DifyService:
|
||||
)
|
||||
|
||||
start_time = time.time()
|
||||
perf = ChatPerfTracker(request_id=request_id, agent_id=getattr(agent, "id", None))
|
||||
first_token_at: float | None = None
|
||||
message_count = 0
|
||||
event_count = 0
|
||||
@@ -681,6 +683,7 @@ class DifyService:
|
||||
|
||||
event = data.get('event')
|
||||
event_count += 1
|
||||
perf.on_event(event, data)
|
||||
# Keep stream event logs behind an explicit switch to prevent
|
||||
# flooding when global logger level is DEBUG.
|
||||
if DifyService.VERBOSE_STREAM_LOG and current_app:
|
||||
@@ -722,6 +725,9 @@ class DifyService:
|
||||
|
||||
if event in ('message_end', 'workflow_finished'):
|
||||
persist_stream_chat(data, event)
|
||||
if chat_perf_enabled():
|
||||
perf_summary = perf.log_summary(current_app.logger if current_app else None)
|
||||
yield f"data: {json.dumps({'event': 'chat_perf', 'perf': perf_summary}, ensure_ascii=False)}\n\n"
|
||||
|
||||
yield f"data: {json.dumps(data, ensure_ascii=False)}\n\n"
|
||||
if event in ('message_end', 'workflow_finished'):
|
||||
|
||||
Reference in new issue
Block a user