hgzero/ai-python/app/api/v1/suggestions.py
Minseo-Jo 9bf3597cec AI 서비스 Python 마이그레이션 및 프론트엔드 연동 문서 추가
주요 변경사항:
- AI 서비스 Java → Python (FastAPI) 완전 마이그레이션
- 포트 변경: 8083 → 8086
- SSE 스트리밍 기능 구현 및 테스트 완료
- Claude API 연동 (claude-3-5-sonnet-20241022)
- Redis 슬라이딩 윈도우 방식 텍스트 축적
- Azure Event Hub 연동 준비 (STT 텍스트 수신)

프론트엔드 연동 지원:
- API 연동 가이드 업데이트 (Python 버전 반영)
- Mock 데이터 개발 가이드 신규 작성
- STT 개발 완료 전까지 Mock 데이터로 UI 개발 가능

기술 스택:
- Python 3.13
- FastAPI 0.104.1
- Anthropic Claude API 0.42.0
- Redis (asyncio) 5.0.1
- Azure Event Hub 5.11.4
- Pydantic 2.10.5

테스트 결과:
-  서비스 시작 정상
-  헬스 체크 성공
-  SSE 스트리밍 동작 확인
-  Redis 연결 정상

다음 단계:
- STT (Azure Speech) 서비스 연동 개발
- Event Hub를 통한 실시간 텍스트 수신
- E2E 통합 테스트 (STT → AI → Frontend)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-27 11:52:30 +09:00

94 lines
3.1 KiB
Python

"""AI 제안사항 SSE 엔드포인트"""
from fastapi import APIRouter
from sse_starlette.sse import EventSourceResponse
import logging
import asyncio
from typing import AsyncGenerator
from app.models import RealtimeSuggestionsResponse
from app.services.claude_service import ClaudeService
from app.services.redis_service import RedisService
from app.config import get_settings
logger = logging.getLogger(__name__)
router = APIRouter()
settings = get_settings()
# 서비스 인스턴스
claude_service = ClaudeService()
@router.get("/meetings/{meeting_id}/stream")
async def stream_ai_suggestions(meeting_id: str):
"""
실시간 AI 제안사항 SSE 스트리밍
Args:
meeting_id: 회의 ID
Returns:
Server-Sent Events 스트림
"""
logger.info(f"SSE 스트림 시작 - meetingId: {meeting_id}")
async def event_generator() -> AsyncGenerator:
"""SSE 이벤트 생성기"""
redis_service = RedisService()
try:
# Redis 연결
await redis_service.connect()
previous_count = 0
while True:
# 현재 세그먼트 개수 확인
current_count = await redis_service.get_segment_count(meeting_id)
# 임계값 이상이고, 이전보다 증가했으면 분석
if (current_count >= settings.min_segments_for_analysis
and current_count > previous_count):
# 누적된 텍스트 조회
accumulated_text = await redis_service.get_accumulated_text(meeting_id)
if accumulated_text:
# Claude API로 분석
suggestions = await claude_service.analyze_suggestions(accumulated_text)
if suggestions.suggestions:
# SSE 이벤트 전송
yield {
"event": "ai-suggestion",
"id": str(current_count),
"data": suggestions.json()
}
logger.info(
f"AI 제안사항 발행 - meetingId: {meeting_id}, "
f"개수: {len(suggestions.suggestions)}"
)
previous_count = current_count
# 5초마다 체크
await asyncio.sleep(5)
except asyncio.CancelledError:
logger.info(f"SSE 스트림 종료 - meetingId: {meeting_id}")
# 회의 종료 시 데이터 정리는 선택사항 (나중에 조회 필요할 수도)
# await redis_service.cleanup_meeting_data(meeting_id)
except Exception as e:
logger.error(f"SSE 스트림 오류 - meetingId: {meeting_id}", exc_info=e)
finally:
await redis_service.disconnect()
return EventSourceResponse(event_generator())
@router.get("/test")
async def test_endpoint():
"""테스트 엔드포인트"""
return {"message": "AI Suggestions API is working", "port": settings.port}