| |
| """ |
| Re-process PDFs to store embeddings in Qdrant |
| Run this if Qdrant was empty when PDFs were first processed |
| """ |
|
|
| import sys |
| from pathlib import Path |
|
|
| |
| project_root = Path(__file__).parent |
| sys.path.insert(0, str(project_root)) |
|
|
| print("π Re-processing PDFs to store embeddings in Qdrant...") |
| print("=" * 70) |
| print("\nβ οΈ This will:") |
| print(" - Process all PDFs in data/pdfs/") |
| print(" - Generate embeddings using OpenAI API") |
| print(" - Store embeddings in Qdrant") |
| print(" - May take a few minutes and use API credits") |
| print("\n" + "=" * 70) |
|
|
| |
| try: |
| from qdrant_client import QdrantClient |
| client = QdrantClient(host='localhost', port=6333, timeout=5) |
| _ = client.get_collections() |
| print("β
Qdrant is running!") |
| except Exception as e: |
| print(f"β Qdrant is not running!") |
| print(f" Error: {e}") |
| print("\nπ‘ Start Qdrant first:") |
| print(" ./start_qdrant.sh") |
| print(" or") |
| print(" docker run -d --name qdrant -p 6333:6333 qdrant/qdrant") |
| sys.exit(1) |
|
|
| |
| try: |
| from src.config.credentials import CredentialsManager |
| creds = CredentialsManager() |
| api_key = creds.get_api_key("openai") |
| if not api_key: |
| print("β OPENAI_API_KEY not found in .env file!") |
| sys.exit(1) |
| print("β
OpenAI API key found") |
| except Exception as e: |
| print(f"β Error checking OpenAI key: {e}") |
| sys.exit(1) |
|
|
| |
| print("\n" + "=" * 70) |
| print("π Processing PDFs...") |
| print("=" * 70) |
|
|
| try: |
| from src.cdms.document_loader import DocumentLoader |
| |
| loader = DocumentLoader() |
| result = loader.load_all_pdfs(force_reprocess=True) |
| |
| if result.get("success"): |
| print("\n" + "=" * 70) |
| print("β
SUCCESS!") |
| print("=" * 70) |
| print(f" Files processed: {result['successful']}/{result['total_files']}") |
| print(f" Total chunks: {result['total_chunks']}") |
| print(f" Embeddings generated: {result['total_embeddings']}") |
| |
| if result['total_embeddings'] > 0: |
| print("\nπ PDFs are now searchable in Qdrant!") |
| print("\nπ‘ Test it:") |
| print(" python src/tools/rag_tool.py") |
| print(" or") |
| print(" streamlit run src/streamlit_app_conversational.py") |
| else: |
| print("\nβ οΈ No embeddings were generated!") |
| print(" Check if OpenAI API key is valid") |
| else: |
| print(f"\nβ Error: {result.get('error')}") |
| sys.exit(1) |
| |
| except Exception as e: |
| print(f"\nβ Error: {e}") |
| import traceback |
| traceback.print_exc() |
| sys.exit(1) |
|
|
|
|