Spaces:
Sleeping
Sleeping
File size: 3,469 Bytes
09da05c | 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 | """Script to chunk and index fact data markdown files into a RAG system."""
import argparse
from pathlib import Path
import sys
from loguru import logger
from adsp.data_pipeline.embedding_utils import get_configured_embedding_model_name
from adsp.data_pipeline.fact_data_pipeline.extract_raw.config import FactDataExtractionConfig
from adsp.data_pipeline.fact_data_pipeline.rag import (
documents_to_context_prompt,
run_fact_data_indexing_pipeline,
)
def main():
"""Chunk and index fact data markdown files."""
parser = argparse.ArgumentParser(
description="Chunk and index fact data markdown files for RAG retrieval."
)
parser.add_argument(
"--markdown-dir",
type=str,
default=None,
help="Directory containing markdown files (default: from config)",
)
parser.add_argument(
"--embedding-model",
type=str,
default=get_configured_embedding_model_name(),
help="Embedding model name for the configured backend",
)
parser.add_argument(
"--chunk-size",
type=int,
default=1200,
help="Maximum chunk size in characters (default: 1200)",
)
parser.add_argument(
"--chunk-overlap",
type=int,
default=50,
help="Overlap between chunks in characters (default: 50)",
)
parser.add_argument(
"--pattern",
type=str,
default="*.md",
help="Glob pattern for markdown files (default: *.md, recursive)",
)
parser.add_argument(
"--top-k",
type=int,
default=10,
help="Number of results to return for test query (default: 10)",
)
parser.add_argument(
"--skip-test-query",
action="store_true",
help="Skip running test queries after indexing (queries run by default)"
)
args = parser.parse_args()
# Determine markdown directory
if args.markdown_dir:
markdown_dir = Path(args.markdown_dir)
else:
config = FactDataExtractionConfig()
markdown_dir = config.fact_data_output_dir / "pages"
if not markdown_dir.exists():
logger.error(f"Markdown directory not found: {markdown_dir}")
return 1
# Run the indexing pipeline
try:
rag = run_fact_data_indexing_pipeline(
markdown_dir=markdown_dir,
embedding_model_name=args.embedding_model,
chunk_size=args.chunk_size,
chunk_overlap=args.chunk_overlap,
pattern=args.pattern,
)
except Exception as e:
logger.error(f"Pipeline failed: {e}")
import traceback
traceback.print_exc()
return 1
if not args.skip_test_query:
queries = [
"What are the demographics of Curious Connoisseurs?",
"What coffee brands do they prefer?",
"Tell me about their lifestyle and interests",
]
for query in queries:
print(f"\nQuery: {query}")
print("=" * 60)
# Search for relevant chunks
results = rag.search(query, k=10)
# Convert to context prompt format
context = documents_to_context_prompt(results)
print(f"\nRetrieved {len(results)} relevant chunks:\n")
print(context)
print("\n" + "=" * 60)
return 0
if __name__ == "__main__":
sys.exit(main())
|