- server/: FastAPI 협상 엔진 (5라운드, Q-Table/UCB, 페르소나 계측, :53117) - frontend/: 데모 콘솔 (React+Tailwind → nginx, /api 프록시) - eval/·testdata/: 지표 8종 측정 하네스 + 재현 가능 테스트셋 - results/: 측정 증빙 (지표별 result+provenance, raw 원본 동결+SHA256) ① 86.0 · ② 76.0 · ③ 5.99% · ⑥ p95 3.61ms · ⑦ 611만/년 · ⑧ 91/100 · ⑨ 99.71 · ⑩ 97.99 - docker-compose.yml + run.sh: backend+frontend 일괄 기동 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0112XUWF4bc3gGrEt8vsYCzy
76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
"""FastAPI 앱 조립 — 미들웨어(응답시간 계측) + 라우터 등록.
|
|
|
|
import 시점에 app 및 협상 엔진이 초기화된다.
|
|
"""
|
|
|
|
import json
|
|
import logging
|
|
import time
|
|
from datetime import datetime, timezone, timedelta
|
|
|
|
from fastapi import FastAPI, Request
|
|
from starlette.middleware.cors import CORSMiddleware
|
|
|
|
from common.logger import setup_logging
|
|
from common.paths import resolve_data_path
|
|
from config.config_loader import app_config
|
|
|
|
setup_logging()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
app = FastAPI(
|
|
title=f"{app_config.server.name} Negotiation Service",
|
|
description="협상 에이전트 경량 서버 — 성능지표 측정용",
|
|
version="0.1.0",
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 응답시간 계측 (⑥ 답변속도) — 요청 수신 → 응답 완료 ms
|
|
_rt_log_path = None
|
|
if app_config.instrument.response_time_log:
|
|
_rt_log_path = resolve_data_path(app_config.instrument.response_time_log)
|
|
_rt_log_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
@app.middleware("http")
|
|
async def response_time_middleware(request: Request, call_next):
|
|
t0 = time.perf_counter()
|
|
response = await call_next(request)
|
|
elapsed_ms = (time.perf_counter() - t0) * 1000
|
|
response.headers["X-Response-Time-Ms"] = f"{elapsed_ms:.2f}"
|
|
|
|
if _rt_log_path is not None and request.url.path.endswith("/chat"):
|
|
try:
|
|
with open(_rt_log_path, "a", encoding="utf-8") as f:
|
|
f.write(json.dumps({
|
|
"ts": datetime.now(timezone(timedelta(hours=9))).isoformat(),
|
|
"path": request.url.path,
|
|
"status": response.status_code,
|
|
"ms": round(elapsed_ms, 2),
|
|
}) + "\n")
|
|
except OSError:
|
|
pass
|
|
|
|
return response
|
|
|
|
|
|
# 엔진 초기화 (라우터 import보다 먼저 — 엔드포인트가 app.state.engine을 사용)
|
|
from services.negotiation.engine import ChatEngine # noqa: E402
|
|
|
|
app.state.config = app_config
|
|
app.state.engine = ChatEngine(app_config)
|
|
logger.info(f"[startup] engine ready — cards={app.state.engine.card_script_repo.count()}, "
|
|
f"profiling={'on' if app.state.engine.profiler else 'off'}")
|
|
|
|
from router.v1.nego import admin, chat # noqa: E402
|
|
|
|
app.include_router(admin.router)
|
|
app.include_router(chat.router)
|