Spaces:
Build error
Build error
File size: 20,492 Bytes
5467f26 44db119 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 5f971cd 5467f26 5f971cd 5467f26 5f971cd 5467f26 5f971cd 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 edc3818 5467f26 |
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 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 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
#!/usr/bin/env python3
"""
D&D RAG System Initialization Script
Loads all D&D content into ChromaDB using existing notebook parsing code.
This is a pragmatic wrapper that uses proven parsing logic.
Usage:
python initialize_rag.py [--clear] [--only spells,monsters,classes,races]
Examples:
python initialize_rag.py # Load all content
python initialize_rag.py --clear # Clear and reload all
python initialize_rag.py --only spells # Load only spells
"""
import argparse
import sys
from pathlib import Path
from typing import List, Dict, Any
import re
# Add project to path
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))
# Import our core infrastructure
from dnd_rag_system.core.chroma_manager import ChromaDBManager
from dnd_rag_system.core.base_chunker import Chunk
from dnd_rag_system.config import settings
from dnd_rag_system.parsers.spell_parser import SpellParser, SpellChunker
# =============================================================================
# SPELL LOADER (using proper SpellParser)
# =============================================================================
def load_spells(db_manager: ChromaDBManager, clear: bool = False):
"""Load spells from spells.txt and all_spells.txt into ChromaDB."""
print("\n" + "="*70)
print("π LOADING SPELLS")
print("="*70)
if clear:
db_manager.clear_collection(settings.COLLECTION_NAMES['spells'])
# Use the proper SpellParser
parser = SpellParser()
parsed_spells = parser.parse()
print(f"β Parsed {len(parsed_spells)} spells")
# Use SpellChunker to create optimized chunks
chunker = SpellChunker()
all_chunks = []
for parsed_spell in parsed_spells:
chunks = chunker.create_chunks(parsed_spell)
all_chunks.extend(chunks)
print(f"β Created {len(all_chunks)} spell chunks (multiple chunks per spell)")
# Add to ChromaDB
if all_chunks:
db_manager.add_chunks(settings.COLLECTION_NAMES['spells'], all_chunks)
print(f"β
Loaded {len(all_chunks)} spell chunks into ChromaDB")
return len(all_chunks)
# =============================================================================
# MONSTER LOADER (adapted from monster_to_rag.ipynb)
# =============================================================================
def load_monsters(db_manager: ChromaDBManager, clear: bool = False):
"""Load monsters from extracted_monsters.txt into ChromaDB."""
print("\n" + "="*70)
print("πΉ LOADING MONSTERS")
print("="*70)
if clear:
db_manager.clear_collection(settings.COLLECTION_NAMES['monsters'])
# Read extracted monsters
print(f"π Reading {settings.EXTRACTED_MONSTERS_TXT}")
if not settings.EXTRACTED_MONSTERS_TXT.exists():
print("β οΈ Monster file not found, skipping")
return 0
with open(settings.EXTRACTED_MONSTERS_TXT, 'r', encoding='utf-8') as f:
monsters_content = f.read()
# Simple monster parsing
monster_blocks = _split_monster_blocks(monsters_content)
print(f"β Found {len(monster_blocks)} monster blocks")
# Create chunks
chunks = []
for i, block in enumerate(monster_blocks):
try:
monster_chunk = _parse_monster_to_chunk(block)
if monster_chunk:
chunks.append(monster_chunk)
if (i + 1) % 50 == 0:
print(f" Processed {i + 1}/{len(monster_blocks)} monsters...")
except Exception as e:
print(f" Warning: Failed to parse monster {i+1}: {e}")
continue
print(f"β Created {len(chunks)} monster chunks")
# Add to ChromaDB
if chunks:
db_manager.add_chunks(settings.COLLECTION_NAMES['monsters'], chunks)
print(f"β
Loaded {len(chunks)} monsters into ChromaDB")
return len(chunks)
def _split_monster_blocks(content: str) -> List[str]:
"""Split monster text into individual blocks."""
# Pattern: MONSTER NAME (often all caps or title case)
blocks = content.split('\n\n')
valid_blocks = [b.strip() for b in blocks if len(b.strip()) > 200]
return valid_blocks
def _parse_monster_to_chunk(block: str) -> Chunk:
"""Parse a monster block into a Chunk object with weighted name."""
lines = [l.strip() for l in block.split('\n') if l.strip()]
if not lines:
return None
# Extract name (usually first line)
name = lines[0].strip()
# Clean up common formatting issues in monster names
name = re.sub(r'\s+', ' ', name) # Normalize whitespace
name = name.strip()
# Try to extract CR
cr = "Unknown"
cr_match = re.search(r'Challenge(?:\s+Rating)?[:\s]+([^\s\(]+)', block, re.IGNORECASE)
if cr_match:
cr = cr_match.group(1).strip()
# Extract monster type if present (e.g., "Large dragon", "Medium humanoid")
monster_type = ""
type_match = re.search(r'(Tiny|Small|Medium|Large|Huge|Gargantuan)\s+(aberration|beast|celestial|construct|dragon|elemental|fey|fiend|giant|humanoid|monstrosity|ooze|plant|undead)', block, re.IGNORECASE)
if type_match:
monster_type = f"{type_match.group(1)} {type_match.group(2)}"
# IMPROVEMENT: Add monster name weighting for better retrieval
# Repeat name multiple times at the start for better matching
weighted_content = f"MONSTER: {name}\n{name}\n\n"
# Add formatted header with key info
weighted_content += f"**{name}**"
if monster_type:
weighted_content += f" - {monster_type}"
if cr != "Unknown":
weighted_content += f" (CR {cr})"
weighted_content += "\n\n"
# Add the full monster stat block
weighted_content += block
metadata = {
'name': name,
'challenge_rating': cr,
'monster_type': monster_type,
'content_type': 'monster'
}
tags = {'monster', f'cr_{cr.replace("/", "_")}'}
if monster_type:
# Add type tag (e.g., 'dragon', 'humanoid')
type_only = monster_type.split()[-1] if monster_type else ''
if type_only:
tags.add(f'type_{type_only.lower()}')
return Chunk(
content=weighted_content,
chunk_type='monster_stats',
metadata=metadata,
tags=tags
)
# =============================================================================
# CLASS LOADER (adapted from classes_to_rag.ipynb)
# =============================================================================
def load_classes(db_manager: ChromaDBManager, clear: bool = False):
"""Load classes from extracted_classes.txt into ChromaDB."""
print("\n" + "="*70)
print("βοΈ LOADING CLASSES")
print("="*70)
if clear:
db_manager.clear_collection(settings.COLLECTION_NAMES['classes'])
# Read extracted classes
print(f"π Reading {settings.EXTRACTED_CLASSES_TXT}")
if not settings.EXTRACTED_CLASSES_TXT.exists():
print("β οΈ Classes file not found, skipping")
return 0
with open(settings.EXTRACTED_CLASSES_TXT, 'r', encoding='utf-8') as f:
classes_content = f.read()
# Simple class parsing - split by known class names
class_blocks = _split_class_blocks(classes_content)
print(f"β Found {len(class_blocks)} class blocks")
# Create chunks
chunks = []
for class_name, content in class_blocks.items():
try:
class_chunk = _parse_class_to_chunk(class_name, content)
if class_chunk:
chunks.append(class_chunk)
except Exception as e:
print(f" Warning: Failed to parse class {class_name}: {e}")
continue
print(f"β Created {len(chunks)} class chunks")
# Add to ChromaDB
if chunks:
db_manager.add_chunks(settings.COLLECTION_NAMES['classes'], chunks)
print(f"β
Loaded {len(chunks)} classes into ChromaDB")
return len(chunks)
def _split_class_blocks(content: str) -> Dict[str, str]:
"""Split content by class names at start of line (section headers)."""
class_blocks = {}
for i, class_name in enumerate(settings.DND_CLASSES):
# FIXED: Look for class name at the beginning of a line (^)
# This finds the detailed section header, not mentions in the table
pattern = rf'^{class_name.upper()}$'
matches = list(re.finditer(pattern, content, re.MULTILINE))
if matches:
start = matches[0].start()
# Find end (next class section or end of text)
end = len(content)
# Look for ANY other class name on its own line after this one
for next_class in settings.DND_CLASSES:
if next_class == class_name:
continue
next_pattern = rf'^{next_class.upper()}$'
next_match = re.search(next_pattern, content[start+10:], re.MULTILINE)
if next_match:
candidate_end = start + 10 + next_match.start()
end = min(end, candidate_end)
class_content = content[start:end].strip()
if len(class_content) > 500: # Substantial content
class_blocks[class_name] = class_content
return class_blocks
def _parse_class_to_chunk(class_name: str, content: str) -> Chunk:
"""Parse a class block into a Chunk object with weighted name."""
metadata = {
'name': class_name,
'content_type': 'class'
}
tags = {'class', f'class_{class_name.lower()}'}
# IMPROVEMENT: Add class name weighting for better retrieval
formatted_content = f"CLASS: {class_name}\n{class_name}\n\n"
formatted_content += f"**{class_name}** - D&D Class\n\n"
formatted_content += content[:2000] # Limit size
return Chunk(
content=formatted_content,
chunk_type='class_features',
metadata=metadata,
tags=tags
)
# =============================================================================
# RACE LOADER (adapted from races_to_rag.ipynb)
# =============================================================================
def load_races(db_manager: ChromaDBManager, clear: bool = False):
"""Load races from Player's Handbook PDF into ChromaDB."""
print("\n" + "="*70)
print("π§ LOADING RACES")
print("="*70)
if clear:
db_manager.clear_collection(settings.COLLECTION_NAMES['races'])
# Check if PDF exists
if not settings.PLAYERS_HANDBOOK_PDF.exists():
print(f"β οΈ Player's Handbook PDF not found: {settings.PLAYERS_HANDBOOK_PDF}")
print(" Skipping race loading")
return 0
try:
import pdfplumber
except ImportError:
print("β οΈ pdfplumber not installed. Install with: pip install pdfplumber")
return 0
print(f"π Extracting race text from PDF (pages 18-46)...")
# Extract text from PDF
race_text = _extract_race_text_from_pdf(settings.PLAYERS_HANDBOOK_PDF)
if not race_text:
print("β Failed to extract race text from PDF")
return 0
print(f"β Extracted {len(race_text)} characters")
# Parse race sections
race_sections = _parse_race_sections(race_text)
print(f"β Found {len(race_sections)} races")
# Create chunks
chunks = []
for race_data in race_sections:
race_name = race_data['name']
race_content = race_data['content']
print(f" Processing: {race_name}")
# Create chunks for this race
race_chunks = _create_race_chunks(race_name, race_content)
chunks.extend(race_chunks)
print(f"β Created {len(chunks)} race chunks")
# Add to ChromaDB
if chunks:
db_manager.add_chunks(settings.COLLECTION_NAMES['races'], chunks)
print(f"β
Loaded {len(chunks)} race chunks into ChromaDB")
return len(chunks)
def _extract_race_text_from_pdf(pdf_path: Path, start_page: int = 18, end_page: int = 46) -> str:
"""Extract race text from Player's Handbook PDF."""
import pdfplumber
extracted_text = ""
try:
with pdfplumber.open(pdf_path) as pdf:
# PDF pages are 0-indexed
for page_num in range(start_page - 1, min(end_page, len(pdf.pages))):
if page_num < len(pdf.pages):
page = pdf.pages[page_num]
page_text = page.extract_text()
if page_text:
extracted_text += page_text + "\n"
# Clean up the text
extracted_text = re.sub(r'\s+', ' ', extracted_text)
extracted_text = re.sub(r'--- PAGE \d+ ---', '', extracted_text)
return extracted_text.strip()
except Exception as e:
print(f"β Error extracting PDF: {e}")
return ""
def _parse_race_sections(text: str) -> List[Dict]:
"""Parse text into individual race sections."""
race_names = ['DRAGONBORN', 'DWARF', 'ELF', 'GNOME', 'HALF-ELF',
'HALFLING', 'HALF-ORC', 'HUMAN', 'TIEFLING']
race_sections = []
for race_name in race_names:
# Find race section
pattern = rf'\b{race_name}\b'
matches = list(re.finditer(pattern, text, re.IGNORECASE))
for match in matches:
start_pos = match.start()
# Check if this looks like a race header
context_after = text[start_pos:start_pos + 500]
# Look for indicators this is a section header
if any(indicator in context_after for indicator in
['Ability Score Increase', 'Age.', 'Size.', 'Speed.']):
# Find end of section (next race or end of text)
end_pos = len(text)
for other_race in race_names:
if other_race != race_name:
next_match = re.search(rf'\b{other_race}\b', text[start_pos + 100:])
if next_match:
candidate_end = start_pos + 100 + next_match.start()
if any(indicator in text[candidate_end:candidate_end + 200]
for indicator in ['Ability Score Increase', 'Age.', 'Size.']):
end_pos = min(end_pos, candidate_end)
race_content = text[start_pos:end_pos].strip()
if len(race_content) > 200:
race_sections.append({
'name': race_name.title(),
'content': race_content
})
break # Take first good match
return race_sections
def _create_race_chunks(race_name: str, race_content: str) -> List[Chunk]:
"""Create chunks from race content."""
chunks = []
# Extract basic metadata
metadata = _extract_race_metadata(race_name, race_content)
# 1. Main description chunk (first part before traits)
trait_start = re.search(r'(Ability Score Increase|Age\.|Size\.)', race_content, re.IGNORECASE)
if trait_start:
description = race_content[:trait_start.start()].strip()
else:
description = race_content[:1000]
if description:
desc_content = f"RACE: {race_name}\n{race_name}\n\n**{race_name}** - D&D Race\n\n{description[:1500]}"
chunks.append(Chunk(
content=desc_content,
chunk_type='race_description',
metadata=metadata,
tags={'race', f'race_{race_name.lower()}', 'description'}
))
# 2. Traits chunk
traits_content = f"RACE: {race_name}\n**{race_name} Racial Traits:**\n\n"
if metadata.get('ability_increases'):
increases = [f"{k.title()} +{v}" for k, v in metadata['ability_increases'].items()]
traits_content += f"**Ability Score Increases:** {', '.join(increases)}\n\n"
if metadata.get('size'):
traits_content += f"**Size:** {metadata['size']}\n"
if metadata.get('speed'):
traits_content += f"**Speed:** {metadata['speed']}\n"
if metadata.get('darkvision'):
traits_content += f"**Darkvision:** {metadata['darkvision']} feet\n"
if metadata.get('languages'):
traits_content += f"**Languages:** {', '.join(metadata['languages'])}\n"
traits_content += f"\n{race_content[trait_start.start():trait_start.start() + 1000] if trait_start else ''}"
chunks.append(Chunk(
content=traits_content,
chunk_type='race_traits',
metadata=metadata,
tags={'race', f'race_{race_name.lower()}', 'traits', 'mechanics'}
))
return chunks
def _extract_race_metadata(race_name: str, content: str) -> Dict[str, Any]:
"""Extract metadata from race content."""
metadata = {
'name': race_name,
'content_type': 'race',
'ability_increases': {},
'size': '',
'speed': '',
'darkvision': 0,
'languages': []
}
# Ability increases
ability_pattern = r'Your (\w+) score increases by (\d+)'
for ability, increase in re.findall(ability_pattern, content, re.IGNORECASE):
metadata['ability_increases'][ability.lower()] = int(increase)
# Size
size_match = re.search(r'Size\.\s*([^.]{0,200}?)\.', content, re.IGNORECASE | re.DOTALL)
if size_match:
size_text = size_match.group(1).strip()
if 'Medium' in size_text:
metadata['size'] = 'Medium'
elif 'Small' in size_text:
metadata['size'] = 'Small'
# Speed
speed_match = re.search(r'Speed\.\s*([^.]{0,200}?)\.', content, re.IGNORECASE | re.DOTALL)
if speed_match:
speed_text = speed_match.group(1).strip()
metadata['speed'] = speed_text[:50]
# Darkvision
darkvision_match = re.search(r'darkvision.*?(\d+)\s*feet', content, re.IGNORECASE)
if darkvision_match:
metadata['darkvision'] = int(darkvision_match.group(1))
# Languages
lang_match = re.search(r'Languages\.\s*([^.]{0,200}?)\.', content, re.IGNORECASE | re.DOTALL)
if lang_match:
lang_text = lang_match.group(1)
for lang in ['Common', 'Elvish', 'Dwarvish', 'Draconic', 'Giant', 'Gnomish', 'Goblin', 'Halfling', 'Orc']:
if lang in lang_text:
metadata['languages'].append(lang)
return metadata
# =============================================================================
# MAIN INITIALIZATION
# =============================================================================
def main():
"""Main initialization function."""
parser = argparse.ArgumentParser(description='Initialize D&D RAG System')
parser.add_argument('--clear', action='store_true', help='Clear existing data')
parser.add_argument('--only', type=str, help='Load only specific collections (comma-separated)')
args = parser.parse_args()
print("\n" + "="*70)
print("π² D&D RAG SYSTEM INITIALIZATION")
print("="*70)
# Initialize ChromaDB
print("\nπ§ Initializing ChromaDB...")
db_manager = ChromaDBManager()
# Determine what to load
load_all = args.only is None
to_load = args.only.split(',') if args.only else ['spells', 'monsters', 'classes', 'races']
# Load each collection
results = {}
if load_all or 'spells' in to_load:
results['spells'] = load_spells(db_manager, args.clear)
if load_all or 'monsters' in to_load:
results['monsters'] = load_monsters(db_manager, args.clear)
if load_all or 'classes' in to_load:
results['classes'] = load_classes(db_manager, args.clear)
if load_all or 'races' in to_load:
results['races'] = load_races(db_manager, args.clear)
# Summary
print("\n" + "="*70)
print("π INITIALIZATION SUMMARY")
print("="*70)
total_chunks = sum(results.values())
for content_type, count in results.items():
print(f" {content_type.capitalize()}: {count} chunks")
print(f"\nβ
Total: {total_chunks} chunks loaded into ChromaDB")
# Show collection stats
print("\nπ Collection Statistics:")
stats = db_manager.get_all_stats()
for collection_name, col_stats in stats['collections'].items():
print(f" {collection_name}: {col_stats.get('total_documents', 0)} documents")
print("\nπ Initialization complete!")
print(f" Database: {db_manager.persist_dir}")
print("\nπ‘ Next steps:")
print(" - Test searches: python test_rag_search.py")
print(" - Run GM dialogue: python run_gm_dialogue.py")
if __name__ == '__main__':
main()
|