File size: 7,460 Bytes
f7892e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

"""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'<div class="status-error">❌ Unknown enhancement type: {enhancement_type}</div>'
        
        # Get relevant context
        context = self.get_relevant_context(script, enhancement_type)
        
        # For now, return a placeholder with context info
        enhanced_script = f"""[CONTEXT-AWARE {enhancement_type.upper()} ENHANCEMENT]

RELEVANT CONTEXT FOUND:
{context[:300]}{'...' if len(context) > 300 else ''}

ENHANCED SCRIPT:
{script}

(Note: Full LLM integration will be added when API keys are configured)
"""
        
        status_message = f'<div class="status-success">βœ… Enhanced with {enhancement_type} style using relevant context from knowledge base</div>'
        return enhanced_script, status_message
    
    def analyze_character_consistency(self, script: str) -> str:
        """Analyze script for character consistency."""
        # Search for character-related content
        results = rag_service.search(script, k=3, content_type="character")
        
        if not results:
            return "No character information found in knowledge base."
        
        analysis = "CHARACTER CONSISTENCY ANALYSIS:\n\n"
        for result in results:
            char_name = result['metadata'].get('title', 'Unknown Character')
            analysis += f"β€’ {char_name}: Found in knowledge base\n"
            analysis += f"  Context: {result['content'][:100]}...\n\n"
        
        return analysis
    
    def suggest_story_elements(self, script: str) -> str:
        """Suggest relevant story elements for the script."""
        # Search across all content types
        story_results = rag_service.search(script, k=2, content_type="story")
        world_results = rag_service.search(script, k=2, content_type="world_element")
        
        suggestions = "STORY ELEMENT SUGGESTIONS:\n\n"
        
        if story_results:
            suggestions += "Related Stories:\n"
            for result in story_results:
                title = result['metadata'].get('title', 'Unknown')
                suggestions += f"β€’ {title}\n"
        
        if world_results:
            suggestions += "\nRelevant World Elements:\n"
            for result in world_results:
                title = result['metadata'].get('title', 'Unknown')
                elem_type = result['metadata'].get('content_type', 'element')
                suggestions += f"β€’ {title} ({elem_type})\n"
        
        if not story_results and not world_results:
            suggestions += "No relevant story elements found."
        
        return suggestions


# Global enhancer instance
context_enhancer = ContextAwareEnhancer()