import os from pathlib import Path from typing import Dict, List, Optional class KnowledgeBaseManager: def __init__(self, knowledge_base_path: str = "knowledge_base"): self.kb_path = Path(knowledge_base_path) self.documents = {} self.kb_path.mkdir(exist_ok=True) def scan_documents(self) -> Dict: documents = {} categories = { 'faqs': 'Frequently Asked Questions', 'policies': 'Company Policies', 'product_manuals': 'Product Manuals', # 'development' intentionally omitted } for category, desc in categories.items(): cat_path = self.kb_path / category if cat_path.exists(): docs = [] for f in cat_path.glob('*.md'): docs.append(self._analyze_document(f, category)) documents[category] = {'description': desc, 'documents': docs} self.documents = documents return documents def _analyze_document(self, file_path: Path, category: str) -> Dict: with open(file_path, 'r', encoding='utf-8') as f: content = f.read() title = next((line.strip('# ').strip() for line in content.split('\n') if line.startswith('# ')), file_path.stem) return { 'filename': file_path.name, 'title': title, 'category': category, 'path': str(file_path.relative_to(self.kb_path)), 'size': file_path.stat().st_size, 'word_count': len(content.split()), 'char_count': len(content) } def load_document_content(self, document_path: str) -> Optional[str]: full = self.kb_path / document_path if full.exists(): with open(full, 'r', encoding='utf-8') as f: return f.read() return None def search_documents(self, query: str, category: Optional[str] = None) -> List[Dict]: results = [] ql = query.lower() for cat, info in self.documents.items(): if category and cat != category: continue for doc in info['documents']: content = self.load_document_content(doc['path']) if not content: continue if ql in content.lower() or ql in doc['title'].lower(): score = content.lower().count(ql) results.append({**doc, 'relevance_score': score}) results.sort(key=lambda x: x['relevance_score'], reverse=True) return results