Integrate Ollama into role detection with fallback to pattern matching

- Updated RoleDetector to use OllamaService for AI-based speaker detection
- Implemented _detect_with_ollama() method with availability check
- Added fallback logic: tries Ollama first, then pattern matching
- Implemented role merging and deduplication across both methods
- Handles Ollama unavailable gracefully with logging
- Logs detection method used (AI vs pattern)
- Updated main.py endpoint to await async detect_roles
- Added comprehensive integration tests

Requirements: 14.1, 14.4, 14.5
This commit is contained in:
2025-12-09 10:49:37 +00:00
parent 73dc5695ca
commit f56f8086e5
3 changed files with 248 additions and 8 deletions

View File

@@ -62,7 +62,7 @@ app = FastAPI(
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "http://localhost:3000"],
allow_origins=["http://localhost:5173", "http://localhost:5174", "http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
@@ -111,6 +111,8 @@ async def detect_roles(request: RoleDetectionRequest):
"""
Analyzes text and returns detected speaker roles.
Uses AI-based detection (Ollama) first, then falls back to pattern matching.
Supports two pattern types:
- Colon pattern: "RoleName:" at line start
- Bracket pattern: "[RoleName]" preceding dialogue
@@ -120,7 +122,7 @@ async def detect_roles(request: RoleDetectionRequest):
logger.info(f"Role detection requested for text of length {len(request.text)}")
try:
roles = role_detector.detect_roles(request.text)
roles = await role_detector.detect_roles(request.text)
logger.info(f"Detected {len(roles)} roles: {[r['label'] for r in roles]}")
return RolesResponse(roles=roles)
except Exception as e: