"""LangChain tools and chains for context-aware AI enhancement.""" from typing import List, Dict, Any, Optional from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from langchain.schema import BaseOutputParser from rag_services import rag_service class ContextAwareEnhancer: """Handles context-aware script enhancement using RAG.""" def __init__(self): self.enhancement_prompts = { "dramatic": """ You are enhancing a script with dramatic flair. Use the following context about characters and story elements to make the enhancement more consistent and engaging. CONTEXT: {context} ORIGINAL SCRIPT: {script} Please enhance this script with dramatic elements while maintaining consistency with the established characters and world. Focus on: - Heightened emotional stakes - Compelling character motivations - Dramatic tension and conflict - Rich, evocative language ENHANCED SCRIPT: """, "romantic": """ You are enhancing a script with romantic elements. Use the following context about characters and relationships to create authentic romantic moments. CONTEXT: {context} ORIGINAL SCRIPT: {script} Please enhance this script with romantic elements while staying true to the established characters and their relationships. Focus on: - Emotional intimacy and connection - Character chemistry and dynamics - Tender, heartfelt dialogue - Romantic atmosphere and mood ENHANCED SCRIPT: """, "professional": """ You are enhancing a script for professional presentation. Use the following context to ensure accuracy and consistency. CONTEXT: {context} ORIGINAL SCRIPT: {script} Please enhance this script for a professional context while maintaining consistency with established facts and characters. Focus on: - Clear, authoritative language - Proper structure and flow - Professional tone and delivery - Accurate information and details ENHANCED SCRIPT: """, "casual": """ You are making a script more casual and conversational. Use the following context about characters to match their established personalities. CONTEXT: {context} ORIGINAL SCRIPT: {script} Please enhance this script with a casual, conversational tone while keeping character voices consistent. Focus on: - Natural, everyday language - Relaxed, friendly tone - Character-appropriate dialogue - Conversational flow and rhythm ENHANCED SCRIPT: """, "character_consistent": """ You are enhancing a script to be more consistent with established characters. Use the character information below to guide your enhancement. CHARACTER CONTEXT: {context} ORIGINAL SCRIPT: {script} Please enhance this script to be more consistent with the established characters. Ensure: - Dialogue matches character personalities and speech patterns - Actions align with character motivations - Character relationships are honored - Character development feels authentic ENHANCED SCRIPT: """, "plot_coherent": """ You are enhancing a script to improve plot coherence. Use the story context below to ensure consistency. STORY CONTEXT: {context} ORIGINAL SCRIPT: {script} Please enhance this script to improve plot coherence and consistency. Focus on: - Logical story progression - Consistent world-building elements - Proper setup and payoff - Clear cause and effect relationships ENHANCED SCRIPT: """ } def get_relevant_context(self, script: str, enhancement_type: str, max_context_length: int = 1000) -> str: """Get relevant context for script enhancement.""" if not script.strip(): return "No relevant context found." # Search for relevant content results = rag_service.search(script, k=5) if not results: return "No relevant context found." # Build context string context_parts = [] current_length = 0 for result in results: metadata = result['metadata'] content = result['content'] # Create context entry context_entry = f"\n--- {metadata.get('content_type', 'Content').title()}: {metadata.get('title', 'Unknown')} ---\n{content}\n" if current_length + len(context_entry) > max_context_length: break context_parts.append(context_entry) current_length += len(context_entry) return "\n".join(context_parts) if context_parts else "No relevant context found." def enhance_script_with_context(self, script: str, enhancement_type: str) -> tuple[str, str]: """Enhance script using relevant context from the knowledge base.""" if enhancement_type not in self.enhancement_prompts: return script, f'