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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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python3
"""

Script to create embeddings and populate the vector store with Manim documentation.

"""

import os
import sys
import time
import json
import logging
from pathlib import Path
from typing import List, Dict, Any

from src.rag.vector_store import EnhancedRAGVectorStore as RAGVectorStore
from src.config.config import Config

# Setup logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

def download_manim_docs():
    """Download Manim documentation if not available locally."""
    print("πŸ“₯ Checking for Manim documentation...")
    
    manim_docs_path = Config.MANIM_DOCS_PATH
    os.makedirs(manim_docs_path, exist_ok=True)
    
    # Check if we have any documentation files
    doc_files = []
    for ext in ['.py', '.md', '.rst', '.txt']:
        doc_files.extend(list(Path(manim_docs_path).rglob(f'*{ext}')))
    
    if len(doc_files) == 0:
        print("πŸ“ No documentation files found. Creating sample documentation...")
        create_sample_manim_docs(manim_docs_path)
    else:
        print(f"βœ… Found {len(doc_files)} documentation files")

def create_sample_manim_docs(docs_path: str):
    """Create sample Manim documentation for testing."""
    
    # Create core documentation
    core_docs = {
        "circle.py": '''

"""Circle mobject in Manim.



The Circle class is one of the basic geometric shapes in Manim.

It can be used to create circular objects that can be animated.



Example:

    circle = Circle(radius=1, color=BLUE)

    self.add(circle)

    self.play(Create(circle))



Attributes:

    radius (float): The radius of the circle

    color (str): The color of the circle

    stroke_width (float): The width of the circle's outline

"""



from manim import *



class Circle(Arc):

    \"\"\"A circle mobject.

    

    Parameters

    ----------

    radius : float

        The radius of the circle.

    color : str

        The color of the circle.

    \"\"\"

    

    def __init__(self, radius=1, **kwargs):

        super().__init__(start_angle=0, angle=TAU, radius=radius, **kwargs)

        

    def animate_creation(self):

        \"\"\"Animate the creation of the circle.\"\"\"

        return Create(self)

        

    def move_to_position(self, position):

        \"\"\"Move circle to a specific position.\"\"\"

        return self.move_to(position)

''',
        
        "text.py": '''

"""Text mobjects in Manim.



Text and Tex classes for displaying text and mathematical expressions.



Example:

    text = Text("Hello World", font_size=48)

    equation = MathTex(r"E = mc^2")

    

    self.play(Write(text))

    self.play(Transform(text, equation))

"""



from manim import *



class Text(SVGMobject):

    \"\"\"Text mobject for displaying regular text.

    

    Parameters

    ----------

    text : str

        The text to display

    font_size : int

        Size of the font

    color : str

        Color of the text

    \"\"\"

    

    def __init__(self, text, font_size=48, **kwargs):

        self.text = text

        self.font_size = font_size

        super().__init__(**kwargs)

        

class MathTex(SVGMobject):

    \"\"\"Mathematical text using LaTeX.

    

    Parameters

    ----------

    tex_string : str

        LaTeX string for mathematical expression

    \"\"\"

    

    def __init__(self, tex_string, **kwargs):

        self.tex_string = tex_string

        super().__init__(**kwargs)

''',

        "animation.py": '''

"""Animation classes in Manim.



Core animation classes for creating smooth transitions and movements.



Example:

    circle = Circle()

    

    # Basic animations

    self.play(Create(circle))

    self.play(FadeIn(circle))

    self.play(Transform(circle, square))

"""



from manim import *



class Create(Animation):

    \"\"\"Animation that creates a mobject by drawing it.

    

    Parameters

    ----------

    mobject : Mobject

        The mobject to create

    run_time : float

        Duration of the animation

    \"\"\"

    

    def __init__(self, mobject, run_time=1, **kwargs):

        super().__init__(mobject, run_time=run_time, **kwargs)



class Transform(Animation):

    \"\"\"Transform one mobject into another.

    

    Parameters

    ----------

    mobject : Mobject

        Source mobject

    target_mobject : Mobject

        Target mobject to transform into

    \"\"\"

    

    def __init__(self, mobject, target_mobject, **kwargs):

        self.target_mobject = target_mobject

        super().__init__(mobject, **kwargs)



class FadeIn(Animation):

    \"\"\"Fade in animation.\"\"\"

    pass



class FadeOut(Animation):

    \"\"\"Fade out animation.\"\"\"

    pass

''',

        "scene.py": '''

"""Scene class - the foundation of Manim animations.



Every Manim animation is built using Scene classes.



Example:

    class MyScene(Scene):

        def construct(self):

            circle = Circle()

            self.play(Create(circle))

            self.wait(1)

"""



from manim import *



class Scene:

    \"\"\"Base class for all Manim scenes.

    

    The Scene class provides the foundation for creating animations.

    Override the construct() method to define your animation.

    \"\"\"

    

    def __init__(self):

        self.mobjects = []

        

    def construct(self):

        \"\"\"Override this method to create your animation.\"\"\"

        pass

        

    def add(self, *mobjects):

        \"\"\"Add mobjects to the scene.\"\"\"

        for mobject in mobjects:

            self.mobjects.append(mobject)

            

    def play(self, *animations, run_time=1):

        \"\"\"Play animations.

        

        Parameters

        ----------

        animations : Animation

            Animations to play

        run_time : float

            Duration to run animations

        \"\"\"

        for animation in animations:

            animation.run_time = run_time

            

    def wait(self, duration=1):

        \"\"\"Wait for specified duration.\"\"\"

        time.sleep(duration)

''',

        "plotting.py": '''

"""Plotting utilities in Manim.



Classes for creating mathematical plots and graphs.



Example:

    axes = Axes(x_range=[-3, 3], y_range=[-2, 2])

    graph = axes.plot(lambda x: x**2, color=BLUE)

    

    self.play(Create(axes))

    self.play(Create(graph))

"""



from manim import *



class Axes(VGroup):

    \"\"\"Coordinate axes for plotting.

    

    Parameters

    ----------

    x_range : list

        Range for x-axis [min, max, step]

    y_range : list  

        Range for y-axis [min, max, step]

    \"\"\"

    

    def __init__(self, x_range=[-1, 1, 1], y_range=[-1, 1, 1], **kwargs):

        self.x_range = x_range

        self.y_range = y_range

        super().__init__(**kwargs)

        

    def plot(self, function, color=WHITE, **kwargs):

        \"\"\"Plot a mathematical function.

        

        Parameters

        ----------

        function : callable

            Function to plot

        color : str

            Color of the plot

        \"\"\"

        return ParametricFunction(function, color=color, **kwargs)

        

    def get_graph(self, function, **kwargs):

        \"\"\"Get graph of function.\"\"\"

        return self.plot(function, **kwargs)



class NumberPlane(Axes):

    \"\"\"A coordinate plane with grid lines.\"\"\"

    

    def __init__(self, **kwargs):

        super().__init__(**kwargs)

'''
    }
    
    # Create the core documentation files
    for filename, content in core_docs.items():
        file_path = os.path.join(docs_path, filename)
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(content)
    
    # Create a README file
    readme_content = '''# Manim Documentation



This directory contains documentation for Manim (Mathematical Animation Engine).



## Core Classes



- **Circle**: Create circular shapes and animations

- **Text/MathTex**: Display text and mathematical expressions  

- **Scene**: Base class for all animations

- **Animation**: Classes for creating smooth transitions

- **Axes/NumberPlane**: Plotting and coordinate systems



## Basic Usage



```python

from manim import *



class MyScene(Scene):

    def construct(self):

        # Create objects

        circle = Circle(radius=1, color=BLUE)

        text = Text("Hello Manim!")

        

        # Animate

        self.play(Create(circle))

        self.play(Write(text))

        self.wait(1)

```



For more information, visit: https://docs.manim.community/

'''
    
    readme_path = os.path.join(docs_path, "README.md")
    with open(readme_path, 'w', encoding='utf-8') as f:
        f.write(readme_content)
    
    print(f"βœ… Created {len(core_docs) + 1} sample documentation files")

def create_vector_store_with_progress():
    """Create and populate the vector store with progress tracking."""
    
    print("πŸš€ Initializing Enhanced RAG Vector Store...")
    print(f"πŸ“ ChromaDB Path: {Config.CHROMA_DB_PATH}")
    print(f"πŸ“ Manim Docs Path: {Config.MANIM_DOCS_PATH}")
    print(f"🧠 Embedding Model: {Config.EMBEDDING_MODEL}")
    print(f"πŸ“Š Expected Embedding Dimensions: 384")
    
    # Ensure ChromaDB directory exists
    os.makedirs(Config.CHROMA_DB_PATH, exist_ok=True)
    
    start_time = time.time()
    
    try:
        # Create vector store - this will automatically process documents and create embeddings
        vector_store = RAGVectorStore(
            chroma_db_path=Config.CHROMA_DB_PATH,
            manim_docs_path=Config.MANIM_DOCS_PATH,
            embedding_model=Config.EMBEDDING_MODEL,
            session_id="embedding_creation",
            use_langfuse=False
        )
        
        print("βœ… Vector store initialized successfully!")
        
        # Check document count
        if hasattr(vector_store, 'core_vector_store') and vector_store.core_vector_store:
            try:
                collection = vector_store.core_vector_store._collection
                doc_count = collection.count()
                print(f"πŸ“Š Total documents in vector store: {doc_count}")
                
                if doc_count > 0:
                    # Test the embedding dimensions
                    embedding_function = vector_store._get_embedding_function()
                    test_embedding = embedding_function.embed_query("test query")
                    print(f"🧠 Embedding dimensions: {len(test_embedding)}")
                    
                    # Get sample documents
                    sample_docs = collection.peek(limit=3)
                    print(f"πŸ“‹ Sample document IDs: {sample_docs.get('ids', [])[:3]}")
                    
                    # Test search functionality
                    print("\nπŸ” Testing search functionality...")
                    results = vector_store.core_vector_store.similarity_search_with_relevance_scores(
                        query="How to create a circle?",
                        k=3,
                        score_threshold=0.0
                    )
                    
                    print(f"🎯 Search results: {len(results)} documents found")
                    for i, (doc, score) in enumerate(results):
                        print(f"   [{i+1}] Score: {score:.4f} | Source: {doc.metadata.get('source', 'unknown')}")
                        print(f"       Content: {doc.page_content[:100]}...")
                else:
                    print("⚠️ Warning: No documents found in vector store")
                    
            except Exception as e:
                print(f"❌ Error checking document count: {e}")
        
        elapsed_time = time.time() - start_time
        print(f"\n⏱️ Vector store creation completed in {elapsed_time:.2f} seconds")
        
        return vector_store
        
    except Exception as e:
        print(f"❌ Error creating vector store: {e}")
        import traceback
        traceback.print_exc()
        return None

def verify_embeddings():
    """Verify that embeddings were created successfully."""
    
    print("\nπŸ”¬ Verifying embeddings...")
    
    try:
        # Load the vector store
        vector_store = RAGVectorStore(
            chroma_db_path=Config.CHROMA_DB_PATH,
            manim_docs_path=Config.MANIM_DOCS_PATH,
            embedding_model=Config.EMBEDDING_MODEL,
            session_id="verification",
            use_langfuse=False
        )
        
        # Test queries
        test_queries = [
            "How to create a circle in Manim?",
            "Text animation examples",
            "Mathematical plotting with axes",
            "Scene construction basics"
        ]
        
        print("🎯 Testing search with various queries:")
        
        for query in test_queries:
            results = vector_store.core_vector_store.similarity_search_with_relevance_scores(
                query=query,
                k=2,
                score_threshold=0.0
            )
            
            print(f"\nQuery: '{query}'")
            print(f"Results: {len(results)} documents")
            
            for i, (doc, score) in enumerate(results):
                print(f"  [{i+1}] Score: {score:.4f}")
                print(f"      Source: {doc.metadata.get('source', 'unknown')}")
                print(f"      Content: {doc.page_content[:80]}...")
        
        print("\nβœ… Embedding verification completed successfully!")
        return True
        
    except Exception as e:
        print(f"❌ Embedding verification failed: {e}")
        return False

def main():
    """Main function to create embeddings."""
    
    print("🎬 MANIM RAG EMBEDDING CREATION")
    print("="*50)
    
    # Step 1: Download/create documentation
    download_manim_docs()
    
    # Step 2: Create vector store and embeddings
    vector_store = create_vector_store_with_progress()
    
    if vector_store is None:
        print("❌ Failed to create vector store. Exiting.")
        return False
    
    # Step 3: Verify embeddings
    success = verify_embeddings()
    
    if success:
        print("\nπŸŽ‰ SUCCESS! Embeddings created and verified.")
        print(f"πŸ“ ChromaDB location: {Config.CHROMA_DB_PATH}")
        print("πŸ” You can now run the RAG system tests.")
        print("\nNext steps:")
        print("  1. Run: python test_rag_system.py")
        print("  2. Run: python debug_retrieval.py")
    else:
        print("\n❌ FAILED! Something went wrong during verification.")
    
    return success

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)