diff --git a/ai-python/.env.example b/ai-python/.env.example new file mode 100644 index 0000000..937a478 --- /dev/null +++ b/ai-python/.env.example @@ -0,0 +1,26 @@ +# 서버 설정 +PORT=8086 +HOST=0.0.0.0 + +# Claude API +CLAUDE_API_KEY=your-api-key-here +CLAUDE_MODEL=claude-3-5-sonnet-20241022 +CLAUDE_MAX_TOKENS=2000 +CLAUDE_TEMPERATURE=0.3 + +# Redis +REDIS_HOST=20.249.177.114 +REDIS_PORT=6379 +REDIS_PASSWORD=Hi5Jessica! +REDIS_DB=4 + +# Azure Event Hub +EVENTHUB_CONNECTION_STRING=Endpoint=sb://hgzero-eventhub-ns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=VUqZ9vFgu35E3c6RiUzoOGVUP8IZpFvlV+AEhC6sUpo= +EVENTHUB_NAME=hgzero-eventhub-name +EVENTHUB_CONSUMER_GROUP=ai-transcript-group + +# CORS +CORS_ORIGINS=["http://localhost:*","http://127.0.0.1:*"] + +# 로깅 +LOG_LEVEL=INFO diff --git a/ai-python/.gitignore b/ai-python/.gitignore new file mode 100644 index 0000000..2ba20f4 --- /dev/null +++ b/ai-python/.gitignore @@ -0,0 +1,37 @@ +# Python +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +venv/ +ENV/ +env/ +.venv + +# Environment +.env + +# IDE +.vscode/ +.idea/ +*.swp +*.swo + +# OS +.DS_Store +Thumbs.db + +# Logs +*.log +logs/ + +# Testing +.pytest_cache/ +.coverage +htmlcov/ + +# Distribution +build/ +dist/ +*.egg-info/ diff --git a/ai-python/README.md b/ai-python/README.md new file mode 100644 index 0000000..87fd8ad --- /dev/null +++ b/ai-python/README.md @@ -0,0 +1,167 @@ +# AI Service (Python) + +실시간 AI 제안사항 서비스 - FastAPI 기반 + +## 📋 개요 + +STT 서비스에서 실시간으로 변환된 텍스트를 받아 Claude API로 분석하여 회의 제안사항을 생성하고, SSE(Server-Sent Events)로 프론트엔드에 스트리밍합니다. + +## 🏗️ 아키텍처 + +``` +Frontend (회의록 작성 화면) + ↓ (SSE 연결) +AI Service (Python) + ↓ (Redis 조회) +Redis (실시간 텍스트 축적) + ↑ (Event Hub) +STT Service (음성 → 텍스트) +``` + +## 🚀 실행 방법 + +### 1. 환경 설정 + +```bash +# .env 파일 생성 +cp .env.example .env + +# .env에서 아래 값 설정 +CLAUDE_API_KEY=sk-ant-... # 실제 Claude API 키 +``` + +### 2. 의존성 설치 + +```bash +# 가상환경 생성 (권장) +python3 -m venv venv +source venv/bin/activate # Mac/Linux +# venv\Scripts\activate # Windows + +# 패키지 설치 +pip install -r requirements.txt +``` + +### 3. 서비스 시작 + +```bash +# 방법 1: 스크립트 실행 +./start.sh + +# 방법 2: 직접 실행 +python3 main.py +``` + +### 4. 서비스 확인 + +```bash +# 헬스 체크 +curl http://localhost:8086/health + +# SSE 스트림 테스트 +curl -N http://localhost:8086/api/v1/ai/suggestions/meetings/test-meeting/stream +``` + +## 📡 API 엔드포인트 + +### SSE 스트리밍 + +``` +GET /api/v1/ai/suggestions/meetings/{meeting_id}/stream +``` + +**응답 형식 (SSE)**: +```json +event: ai-suggestion +data: { + "suggestions": [ + { + "id": "uuid", + "content": "신제품의 타겟 고객층을 20-30대로 설정...", + "timestamp": "00:05:23", + "confidence": 0.92 + } + ] +} +``` + +## 🔧 개발 환경 + +- **Python**: 3.9+ +- **Framework**: FastAPI +- **AI**: Anthropic Claude API +- **Cache**: Redis +- **Event**: Azure Event Hub + +## 📂 프로젝트 구조 + +``` +ai-python/ +├── main.py # FastAPI 진입점 +├── requirements.txt # 의존성 +├── .env.example # 환경 변수 예시 +├── start.sh # 시작 스크립트 +└── app/ + ├── config.py # 환경 설정 + ├── models/ + │ └── response.py # 응답 모델 + ├── services/ + │ ├── claude_service.py # Claude API 서비스 + │ ├── redis_service.py # Redis 서비스 + │ └── eventhub_service.py # Event Hub 리스너 + └── api/ + └── v1/ + └── suggestions.py # SSE 엔드포인트 +``` + +## ⚙️ 환경 변수 + +| 변수 | 설명 | 기본값 | +|------|------|--------| +| `CLAUDE_API_KEY` | Claude API 키 | (필수) | +| `CLAUDE_MODEL` | Claude 모델 | claude-3-5-sonnet-20241022 | +| `REDIS_HOST` | Redis 호스트 | 20.249.177.114 | +| `REDIS_PORT` | Redis 포트 | 6379 | +| `EVENTHUB_CONNECTION_STRING` | Event Hub 연결 문자열 | (선택) | +| `PORT` | 서비스 포트 | 8086 | + +## 🔍 동작 원리 + +1. **STT → Event Hub**: STT 서비스가 음성을 텍스트로 변환하여 Event Hub에 발행 +2. **Event Hub → Redis**: AI 서비스가 Event Hub에서 텍스트를 받아 Redis에 축적 (슬라이딩 윈도우: 최근 5분) +3. **Redis → Claude API**: 임계값(10개 세그먼트) 이상이면 Claude API로 분석 +4. **Claude API → Frontend**: 분석 결과를 SSE로 프론트엔드에 스트리밍 + +## 🧪 테스트 + +```bash +# Event Hub 없이 SSE만 테스트 (Mock 데이터) +curl -N http://localhost:8086/api/v1/ai/suggestions/meetings/test-meeting/stream + +# 5초마다 샘플 제안사항이 발행됩니다 +``` + +## 📝 개발 가이드 + +### Claude API 키 발급 +1. https://console.anthropic.com/ 접속 +2. API Keys 메뉴에서 새 키 생성 +3. `.env` 파일에 설정 + +### Redis 연결 확인 +```bash +redis-cli -h 20.249.177.114 -p 6379 -a Hi5Jessica! ping +# 응답: PONG +``` + +### Event Hub 설정 (선택) +- Event Hub가 없어도 SSE 스트리밍은 동작합니다 +- STT 연동 시 필요 + +## 🚧 TODO + +- [ ] Event Hub 연동 테스트 +- [ ] 프론트엔드 연동 테스트 +- [ ] 에러 핸들링 강화 +- [ ] 로깅 개선 +- [ ] 성능 모니터링 diff --git a/ai-python/app/__init__.py b/ai-python/app/__init__.py new file mode 100644 index 0000000..37cfe08 --- /dev/null +++ b/ai-python/app/__init__.py @@ -0,0 +1,2 @@ +"""AI Service - Python FastAPI""" +__version__ = "1.0.0" diff --git a/ai-python/app/api/__init__.py b/ai-python/app/api/__init__.py new file mode 100644 index 0000000..05eed47 --- /dev/null +++ b/ai-python/app/api/__init__.py @@ -0,0 +1 @@ +"""API 레이어""" diff --git a/ai-python/app/api/v1/__init__.py b/ai-python/app/api/v1/__init__.py new file mode 100644 index 0000000..88d91ee --- /dev/null +++ b/ai-python/app/api/v1/__init__.py @@ -0,0 +1 @@ +"""API v1""" diff --git a/ai-python/app/api/v1/suggestions.py b/ai-python/app/api/v1/suggestions.py new file mode 100644 index 0000000..4debf16 --- /dev/null +++ b/ai-python/app/api/v1/suggestions.py @@ -0,0 +1,93 @@ +"""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} diff --git a/ai-python/app/config.py b/ai-python/app/config.py new file mode 100644 index 0000000..bb17551 --- /dev/null +++ b/ai-python/app/config.py @@ -0,0 +1,55 @@ +"""환경 설정""" +from pydantic_settings import BaseSettings +from functools import lru_cache +from typing import List + + +class Settings(BaseSettings): + """환경 설정 클래스""" + + # 서버 설정 + app_name: str = "AI Service (Python)" + host: str = "0.0.0.0" + port: int = 8086 # STT(8084)와 충돌 방지 + + # Claude API + claude_api_key: str = "" + claude_model: str = "claude-3-5-sonnet-20241022" + claude_max_tokens: int = 250000 + claude_temperature: float = 0.7 + + # Redis + redis_host: str = "20.249.177.114" + redis_port: int = 6379 + redis_password: str = "" + redis_db: int = 4 + + # Azure Event Hub + eventhub_connection_string: str = "" + eventhub_name: str = "hgzero-eventhub-name" + eventhub_consumer_group: str = "ai-transcript-group" + + # CORS + cors_origins: List[str] = [ + "http://localhost:*", + "http://127.0.0.1:*", + "http://localhost:8080", + "http://localhost:3000" + ] + + # 로깅 + log_level: str = "INFO" + + # 분석 임계값 + min_segments_for_analysis: int = 10 + text_retention_seconds: int = 300 # 5분 + + class Config: + env_file = ".env" + case_sensitive = False + + +@lru_cache() +def get_settings() -> Settings: + """싱글톤 설정 인스턴스""" + return Settings() diff --git a/ai-python/app/models/__init__.py b/ai-python/app/models/__init__.py new file mode 100644 index 0000000..e1b9eae --- /dev/null +++ b/ai-python/app/models/__init__.py @@ -0,0 +1,4 @@ +"""데이터 모델""" +from .response import SimpleSuggestion, RealtimeSuggestionsResponse + +__all__ = ["SimpleSuggestion", "RealtimeSuggestionsResponse"] diff --git a/ai-python/app/models/response.py b/ai-python/app/models/response.py new file mode 100644 index 0000000..f10540e --- /dev/null +++ b/ai-python/app/models/response.py @@ -0,0 +1,45 @@ +"""응답 모델""" +from pydantic import BaseModel, Field +from typing import List + + +class SimpleSuggestion(BaseModel): + """간소화된 AI 제안사항""" + + id: str = Field(..., description="제안 ID") + content: str = Field(..., description="제안 내용 (1-2문장)") + timestamp: str = Field(..., description="타임스탬프 (HH:MM:SS)") + confidence: float = Field(..., ge=0.0, le=1.0, description="신뢰도 (0-1)") + + class Config: + json_schema_extra = { + "example": { + "id": "sugg-001", + "content": "신제품의 타겟 고객층을 20-30대로 설정하고, 모바일 우선 전략을 취하기로 논의 중입니다.", + "timestamp": "00:05:23", + "confidence": 0.92 + } + } + + +class RealtimeSuggestionsResponse(BaseModel): + """실시간 AI 제안사항 응답""" + + suggestions: List[SimpleSuggestion] = Field( + default_factory=list, + description="AI 제안사항 목록" + ) + + class Config: + json_schema_extra = { + "example": { + "suggestions": [ + { + "id": "sugg-001", + "content": "신제품의 타겟 고객층을 20-30대로 설정하고...", + "timestamp": "00:05:23", + "confidence": 0.92 + } + ] + } + } diff --git a/ai-python/app/services/__init__.py b/ai-python/app/services/__init__.py new file mode 100644 index 0000000..1b90ec2 --- /dev/null +++ b/ai-python/app/services/__init__.py @@ -0,0 +1 @@ +"""서비스 레이어""" diff --git a/ai-python/app/services/claude_service.py b/ai-python/app/services/claude_service.py new file mode 100644 index 0000000..f62b878 --- /dev/null +++ b/ai-python/app/services/claude_service.py @@ -0,0 +1,147 @@ +"""Claude API 서비스""" +import anthropic +import json +import logging +from typing import List +from datetime import datetime +import uuid +from app.config import get_settings +from app.models import SimpleSuggestion, RealtimeSuggestionsResponse + +logger = logging.getLogger(__name__) +settings = get_settings() + + +class ClaudeService: + """Claude API 클라이언트""" + + def __init__(self): + self.client = None + if settings.claude_api_key: + self.client = anthropic.Anthropic(api_key=settings.claude_api_key) + + async def analyze_suggestions(self, transcript_text: str) -> RealtimeSuggestionsResponse: + """ + 회의 텍스트를 분석하여 AI 제안사항 생성 + + Args: + transcript_text: 누적된 회의 텍스트 + + Returns: + RealtimeSuggestionsResponse + """ + if not self.client: + logger.warning("Claude API 키가 설정되지 않음 - Mock 데이터 반환") + return self._generate_mock_suggestions() + + logger.info(f"Claude API 호출 - 텍스트 길이: {len(transcript_text)}") + + system_prompt = """당신은 회의록 작성 전문 AI 어시스턴트입니다. + +실시간 회의 텍스트를 분석하여 **중요한 제안사항만** 추출하세요. + +**추출 기준**: +- 회의 안건과 직접 관련된 내용 +- 논의가 필요한 주제 +- 결정된 사항 +- 액션 아이템 + +**제외할 내용**: +- 잡담, 농담, 인사말 +- 회의와 무관한 대화 +- 단순 확인이나 질의응답 + +**응답 형식**: JSON만 반환 (다른 설명 없이) +{ + "suggestions": [ + { + "content": "구체적인 제안 내용 (1-2문장으로 명확하게)", + "confidence": 0.9 + } + ] +} + +**주의**: +- 각 제안은 독립적이고 명확해야 함 +- 회의 맥락에서 실제 중요한 내용만 포함 +- confidence는 0-1 사이 값 (확신 정도)""" + + try: + response = self.client.messages.create( + model=settings.claude_model, + max_tokens=settings.claude_max_tokens, + temperature=settings.claude_temperature, + system=system_prompt, + messages=[ + { + "role": "user", + "content": f"다음 회의 내용을 분석해주세요:\n\n{transcript_text}" + } + ] + ) + + # 응답 파싱 + content_text = response.content[0].text + suggestions_data = self._parse_claude_response(content_text) + + logger.info(f"Claude API 응답 성공 - 제안사항: {len(suggestions_data.get('suggestions', []))}개") + + return RealtimeSuggestionsResponse( + suggestions=[ + SimpleSuggestion( + id=str(uuid.uuid4()), + content=s["content"], + timestamp=self._get_current_timestamp(), + confidence=s.get("confidence", 0.8) + ) + for s in suggestions_data.get("suggestions", []) + ] + ) + + except Exception as e: + logger.error(f"Claude API 호출 실패: {e}") + return RealtimeSuggestionsResponse(suggestions=[]) + + def _parse_claude_response(self, text: str) -> dict: + """Claude 응답에서 JSON 추출 및 파싱""" + # ```json ... ``` 제거 + if "```json" in text: + start = text.find("```json") + 7 + end = text.rfind("```") + text = text[start:end].strip() + elif "```" in text: + start = text.find("```") + 3 + end = text.rfind("```") + text = text[start:end].strip() + + try: + return json.loads(text) + except json.JSONDecodeError as e: + logger.error(f"JSON 파싱 실패: {e}, 원문: {text[:200]}") + return {"suggestions": []} + + def _get_current_timestamp(self) -> str: + """현재 타임스탬프 (HH:MM:SS)""" + return datetime.now().strftime("%H:%M:%S") + + def _generate_mock_suggestions(self) -> RealtimeSuggestionsResponse: + """Mock 제안사항 생성 (테스트용)""" + mock_suggestions = [ + "신제품의 타겟 고객층을 20-30대로 설정하고, 모바일 우선 전략을 취하기로 논의 중입니다.", + "개발 일정: 1차 프로토타입은 11월 15일까지 완성, 2차 베타는 12월 1일 론칭", + "마케팅 예산 배분에 대해 SNS 광고 60%, 인플루언서 마케팅 40%로 의견이 나왔으나 추가 검토 필요" + ] + + import random + content = random.choice(mock_suggestions) + + return RealtimeSuggestionsResponse( + suggestions=[ + SimpleSuggestion( + id=str(uuid.uuid4()), + content=content, + timestamp=self._get_current_timestamp(), + confidence=0.85 + ) + ] + ) diff --git a/ai-python/app/services/eventhub_service.py b/ai-python/app/services/eventhub_service.py new file mode 100644 index 0000000..c6109e6 --- /dev/null +++ b/ai-python/app/services/eventhub_service.py @@ -0,0 +1,114 @@ +"""Azure Event Hub 서비스 - STT 텍스트 수신""" +import asyncio +import logging +import json +from azure.eventhub.aio import EventHubConsumerClient +from azure.eventhub.extensions.checkpointstoreblobaio import BlobCheckpointStore + +from app.config import get_settings +from app.services.redis_service import RedisService + +logger = logging.getLogger(__name__) +settings = get_settings() + + +class EventHubService: + """Event Hub 리스너 - STT 텍스트 실시간 수신""" + + def __init__(self): + self.client = None + self.redis_service = RedisService() + + async def start(self): + """Event Hub 리스닝 시작""" + if not settings.eventhub_connection_string: + logger.warning("Event Hub 연결 문자열이 설정되지 않음 - Event Hub 리스너 비활성화") + return + + logger.info("Event Hub 리스너 시작") + + try: + # Redis 연결 + await self.redis_service.connect() + + # Event Hub 클라이언트 생성 + self.client = EventHubConsumerClient.from_connection_string( + conn_str=settings.eventhub_connection_string, + consumer_group=settings.eventhub_consumer_group, + eventhub_name=settings.eventhub_name, + ) + + # 이벤트 수신 시작 + async with self.client: + await self.client.receive( + on_event=self.on_event, + on_error=self.on_error, + starting_position="-1", # 최신 이벤트부터 + ) + + except Exception as e: + logger.error(f"Event Hub 리스너 오류: {e}") + finally: + await self.redis_service.disconnect() + + async def on_event(self, partition_context, event): + """ + 이벤트 수신 핸들러 + + 이벤트 형식 (STT Service에서 발행): + { + "eventType": "TranscriptSegmentReady", + "meetingId": "meeting-123", + "text": "변환된 텍스트", + "timestamp": 1234567890000 + } + """ + try: + # 이벤트 데이터 파싱 + event_data = json.loads(event.body_as_str()) + + event_type = event_data.get("eventType") + meeting_id = event_data.get("meetingId") + text = event_data.get("text") + timestamp = event_data.get("timestamp") + + if event_type == "TranscriptSegmentReady" and meeting_id and text: + logger.info( + f"STT 텍스트 수신 - meetingId: {meeting_id}, " + f"텍스트 길이: {len(text)}" + ) + + # Redis에 텍스트 축적 (슬라이딩 윈도우) + await self.redis_service.add_transcript_segment( + meeting_id=meeting_id, + text=text, + timestamp=timestamp + ) + + logger.debug(f"Redis 저장 완료 - meetingId: {meeting_id}") + + # 체크포인트 업데이트 + await partition_context.update_checkpoint(event) + + except Exception as e: + logger.error(f"이벤트 처리 오류: {e}", exc_info=True) + + async def on_error(self, partition_context, error): + """에러 핸들러""" + logger.error( + f"Event Hub 에러 - Partition: {partition_context.partition_id}, " + f"Error: {error}" + ) + + async def stop(self): + """Event Hub 리스너 종료""" + if self.client: + await self.client.close() + logger.info("Event Hub 리스너 종료") + + +# 백그라운드 태스크로 실행할 함수 +async def start_eventhub_listener(): + """Event Hub 리스너 백그라운드 실행""" + service = EventHubService() + await service.start() diff --git a/ai-python/app/services/redis_service.py b/ai-python/app/services/redis_service.py new file mode 100644 index 0000000..018d6c5 --- /dev/null +++ b/ai-python/app/services/redis_service.py @@ -0,0 +1,117 @@ +"""Redis 서비스 - 실시간 텍스트 축적""" +import redis.asyncio as redis +import logging +from typing import List +from app.config import get_settings + +logger = logging.getLogger(__name__) +settings = get_settings() + + +class RedisService: + """Redis 서비스 (슬라이딩 윈도우 방식)""" + + def __init__(self): + self.redis_client = None + + async def connect(self): + """Redis 연결""" + try: + self.redis_client = await redis.Redis( + host=settings.redis_host, + port=settings.redis_port, + password=settings.redis_password, + db=settings.redis_db, + decode_responses=True + ) + await self.redis_client.ping() + logger.info("Redis 연결 성공") + except Exception as e: + logger.error(f"Redis 연결 실패: {e}") + raise + + async def disconnect(self): + """Redis 연결 종료""" + if self.redis_client: + await self.redis_client.close() + logger.info("Redis 연결 종료") + + async def add_transcript_segment( + self, + meeting_id: str, + text: str, + timestamp: int + ): + """ + 실시간 텍스트 세그먼트 추가 (슬라이딩 윈도우) + + Args: + meeting_id: 회의 ID + text: 텍스트 세그먼트 + timestamp: 타임스탬프 (밀리초) + """ + key = f"meeting:{meeting_id}:transcript" + value = f"{timestamp}:{text}" + + # Sorted Set에 추가 (타임스탬프를 스코어로) + await self.redis_client.zadd(key, {value: timestamp}) + + # 설정된 시간 이전 데이터 제거 (기본 5분) + retention_ms = settings.text_retention_seconds * 1000 + cutoff_time = timestamp - retention_ms + await self.redis_client.zremrangebyscore(key, 0, cutoff_time) + + logger.debug(f"텍스트 세그먼트 추가 - meetingId: {meeting_id}") + + async def get_accumulated_text(self, meeting_id: str) -> str: + """ + 누적된 텍스트 조회 (최근 5분) + + Args: + meeting_id: 회의 ID + + Returns: + 누적된 텍스트 (시간순) + """ + key = f"meeting:{meeting_id}:transcript" + + # 최신순으로 모든 세그먼트 조회 + segments = await self.redis_client.zrevrange(key, 0, -1) + + if not segments: + return "" + + # 타임스탬프 제거하고 텍스트만 추출 + texts = [] + for seg in segments: + parts = seg.split(":", 1) + if len(parts) == 2: + texts.append(parts[1]) + + # 시간순으로 정렬 (역순으로 조회했으므로 다시 뒤집기) + return "\n".join(reversed(texts)) + + async def get_segment_count(self, meeting_id: str) -> int: + """ + 누적된 세그먼트 개수 + + Args: + meeting_id: 회의 ID + + Returns: + 세그먼트 개수 + """ + key = f"meeting:{meeting_id}:transcript" + count = await self.redis_client.zcard(key) + return count if count else 0 + + async def cleanup_meeting_data(self, meeting_id: str): + """ + 회의 종료 시 데이터 정리 + + Args: + meeting_id: 회의 ID + """ + key = f"meeting:{meeting_id}:transcript" + await self.redis_client.delete(key) + logger.info(f"회의 데이터 정리 완료 - meetingId: {meeting_id}") diff --git a/ai-python/main.py b/ai-python/main.py new file mode 100644 index 0000000..98d1617 --- /dev/null +++ b/ai-python/main.py @@ -0,0 +1,93 @@ +"""AI Service - FastAPI 애플리케이션""" +import logging +import uvicorn +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from contextlib import asynccontextmanager + +from app.config import get_settings +from app.api.v1 import suggestions + +# 로깅 설정 +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' +) +logger = logging.getLogger(__name__) + +settings = get_settings() + + +@asynccontextmanager +async def lifespan(app: FastAPI): + """애플리케이션 생명주기 관리""" + logger.info("=" * 60) + logger.info(f"AI Service (Python) 시작 - Port: {settings.port}") + logger.info(f"Claude Model: {settings.claude_model}") + logger.info(f"Redis: {settings.redis_host}:{settings.redis_port}") + logger.info("=" * 60) + + # TODO: Event Hub 리스너 시작 (별도 백그라운드 태스크) + # asyncio.create_task(start_eventhub_listener()) + + yield + + logger.info("AI Service 종료") + + +# FastAPI 애플리케이션 +app = FastAPI( + title=settings.app_name, + version="1.0.0", + description="실시간 AI 제안사항 서비스 (Python)", + lifespan=lifespan +) + +# CORS 설정 +app.add_middleware( + CORSMiddleware, + allow_origins=settings.cors_origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +# 라우터 등록 +app.include_router( + suggestions.router, + prefix="/api/v1/ai/suggestions", + tags=["AI Suggestions"] +) + + +@app.get("/") +async def root(): + """루트 엔드포인트""" + return { + "service": settings.app_name, + "version": "1.0.0", + "status": "running", + "endpoints": { + "test": "/api/v1/ai/suggestions/test", + "stream": "/api/v1/ai/suggestions/meetings/{meeting_id}/stream" + } + } + + +@app.get("/health") +async def health_check(): + """헬스 체크""" + return { + "status": "healthy", + "service": settings.app_name + } + + +if __name__ == "__main__": + uvicorn.run( + "main:app", + host=settings.host, + port=settings.port, + reload=True, # 개발 모드 + log_level=settings.log_level.lower() + ) diff --git a/ai-python/requirements.txt b/ai-python/requirements.txt new file mode 100644 index 0000000..30069d4 --- /dev/null +++ b/ai-python/requirements.txt @@ -0,0 +1,21 @@ +# FastAPI 및 서버 +fastapi==0.104.1 +uvicorn[standard]==0.24.0 +sse-starlette==1.8.2 + +# AI/ML +anthropic==0.42.0 + +# 데이터베이스 및 캐시 +redis==5.0.1 + +# Azure 서비스 +azure-eventhub==5.11.4 + +# 데이터 모델 및 검증 +pydantic==2.10.5 +pydantic-settings==2.7.1 + +# 유틸리티 +python-dotenv==1.0.0 +python-json-logger==2.0.7 diff --git a/ai-python/start.sh b/ai-python/start.sh new file mode 100755 index 0000000..b0ac5af --- /dev/null +++ b/ai-python/start.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# AI Service (Python) 시작 스크립트 + +echo "======================================" +echo "AI Service (Python) 시작" +echo "======================================" + +# 가상환경 활성화 (선택사항) +# source venv/bin/activate + +# 의존성 설치 확인 +if [ ! -d "venv" ]; then + echo "가상환경이 없습니다. 생성 중..." + python3 -m venv venv + source venv/bin/activate + pip install -r requirements.txt +else + source venv/bin/activate +fi + +# .env 파일 확인 +if [ ! -f ".env" ]; then + echo ".env 파일이 없습니다. .env.example을 복사합니다." + cp .env.example .env + echo "⚠️ .env 파일에 실제 API 키를 설정해주세요." +fi + +# FastAPI 서버 시작 +echo "======================================" +echo "FastAPI 서버 시작 중..." +echo "Port: 8086" +echo "======================================" + +python3 main.py diff --git a/develop/dev/dev-ai-frontend-integration.md b/develop/dev/dev-ai-frontend-integration.md new file mode 100644 index 0000000..c154764 --- /dev/null +++ b/develop/dev/dev-ai-frontend-integration.md @@ -0,0 +1,482 @@ +# AI 서비스 프론트엔드 통합 가이드 + +## 개요 + +AI 서비스의 실시간 제안사항 API를 프론트엔드에서 사용하기 위한 통합 가이드입니다. + +**⚠️ 중요**: AI 서비스가 **Python (FastAPI)**로 마이그레이션 되었습니다. +- **기존 포트**: 8083 (Java Spring Boot) → **새 포트**: 8086 (Python FastAPI) +- **엔드포인트 경로**: `/api/suggestions/...` → `/api/v1/ai/suggestions/...` + +--- + +## 1. API 정보 + +### 엔드포인트 +``` +GET /api/v1/ai/suggestions/meetings/{meetingId}/stream +``` + +**변경 사항**: +- ✅ **새 경로** (Python): `/api/v1/ai/suggestions/meetings/{meetingId}/stream` +- ❌ **구 경로** (Java): `/api/suggestions/meetings/{meetingId}/stream` + +### 메서드 +- **HTTP Method**: GET +- **Content-Type**: text/event-stream (SSE) +- **인증**: 개발 환경에서는 불필요 (운영 환경에서는 JWT 필요) + +### 파라미터 +| 파라미터 | 타입 | 필수 | 설명 | +|---------|------|------|------| +| meetingId | string (UUID) | 필수 | 회의 고유 ID | + +### 예시 +``` +# Python (새 버전) +http://localhost:8086/api/v1/ai/suggestions/meetings/550e8400-e29b-41d4-a716-446655440000/stream + +# Java (구 버전 - 사용 중단 예정) +http://localhost:8083/api/suggestions/meetings/550e8400-e29b-41d4-a716-446655440000/stream +``` + +--- + +## 2. 응답 데이터 구조 + +### SSE 이벤트 형식 +``` +event: ai-suggestion +id: 123456789 +data: {"suggestions":[...]} +``` + +### 데이터 스키마 (JSON) +```typescript +interface RealtimeSuggestionsDto { + suggestions: SimpleSuggestionDto[]; +} + +interface SimpleSuggestionDto { + id: string; // 제안 고유 ID (예: "suggestion-1") + content: string; // 제안 내용 (예: "신제품의 타겟 고객층...") + timestamp: string; // 시간 정보 (HH:MM:SS 형식, 예: "00:05:23") + confidence: number; // 신뢰도 점수 (0.0 ~ 1.0) +} +``` + +### 샘플 응답 +```json +{ + "suggestions": [ + { + "id": "suggestion-1", + "content": "신제품의 타겟 고객층을 20-30대로 설정하고, 모바일 우선 전략을 취하기로 논의 중입니다.", + "timestamp": "00:05:23", + "confidence": 0.92 + } + ] +} +``` + +--- + +## 3. 프론트엔드 구현 방법 + +### 3.1 EventSource로 연결 + +```javascript +// 회의 ID (실제로는 회의 생성 API에서 받아야 함) +const meetingId = '550e8400-e29b-41d4-a716-446655440000'; + +// SSE 연결 (Python 버전) +const apiUrl = `http://localhost:8086/api/v1/ai/suggestions/meetings/${meetingId}/stream`; +const eventSource = new EventSource(apiUrl); + +// 연결 성공 +eventSource.onopen = function(event) { + console.log('SSE 연결 성공'); +}; + +// ai-suggestion 이벤트 수신 +eventSource.addEventListener('ai-suggestion', function(event) { + const data = JSON.parse(event.data); + const suggestions = data.suggestions; + + suggestions.forEach(suggestion => { + console.log('제안:', suggestion.content); + addSuggestionToUI(suggestion); + }); +}); + +// 에러 처리 +eventSource.onerror = function(error) { + console.error('SSE 연결 오류:', error); + eventSource.close(); +}; +``` + +### 3.2 UI에 제안사항 추가 + +```javascript +function addSuggestionToUI(suggestion) { + const container = document.getElementById('aiSuggestionList'); + + // 중복 방지 + if (document.getElementById(`suggestion-${suggestion.id}`)) { + return; + } + + // HTML 생성 + const html = ` +
{suggestion.content}
+ +