| import asyncio |
| import os |
| import sys |
| from dotenv import load_dotenv |
|
|
| load_dotenv() |
| sys.path.append(os.path.join(os.getcwd(), 'python', 'src')) |
|
|
| from server.utils import get_supabase_client |
| from server.services.embeddings.contextual_embedding_service import generate_contextual_embedding |
| from server.services.embeddings.embedding_service import create_embedding |
|
|
| async def deep_rag_optimize(): |
| client = get_supabase_client() |
| print("π Starting Deep RAG Optimization (Surgical Mode)...") |
| |
| |
| res = client.table('archon_crawled_pages').select('id, content, source_id, metadata').filter('metadata->>contextual_embedding', 'is', 'null').execute() |
| todo_chunks = res.data |
| |
| if not todo_chunks: |
| print("β¨ All chunks are already optimized!") |
| return |
|
|
| print(f"π¦ Found {len(todo_chunks)} chunks to optimize.") |
| total_optimized = 0 |
| |
| |
| source_groups = {} |
| for chunk in todo_chunks: |
| sid = chunk['source_id'] |
| if sid not in source_groups: |
| source_groups[sid] = [] |
| source_groups[sid].append(chunk) |
|
|
| for sid, chunks in source_groups.items(): |
| print(f"\nπ Source: {sid}") |
| |
| all_chunks_res = client.table('archon_crawled_pages').select('content').eq('source_id', sid).order('chunk_number').execute() |
| full_doc = "\n".join([c['content'] for c in all_chunks_res.data]) |
| |
| for chunk in chunks: |
| print(f" β‘ Optimizing chunk {chunk['id']}...") |
| new_content, success = await generate_contextual_embedding(full_doc, chunk['content']) |
| |
| if success: |
| new_embedding = await create_embedding(new_content) |
| metadata = chunk.get('metadata') or {} |
| metadata['contextual_embedding'] = True |
| metadata['original_content_before_contextual'] = chunk['content'] |
| |
| client.table('archon_crawled_pages').update({ |
| 'content': new_content, |
| 'embedding': new_embedding, |
| 'metadata': metadata |
| }).eq('id', chunk['id']).execute() |
| total_optimized += 1 |
| print(f" β
Success.") |
| else: |
| print(f" β Failed to generate contextual embedding.") |
| |
| |
| await asyncio.sleep(2) |
| |
| print(f"\n⨠Deep RAG Optimization complete! Total chunks optimized: {total_optimized}") |
|
|
| if __name__ == "__main__": |
| asyncio.run(deep_rag_optimize()) |
|
|