Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
Update Welcome notebook: Fix opening issue and add comprehensive module reference tables
KaifAhmad18b9f6dbd0926
diff --git a/cookbook/introduction/01_Welcome_to_Semantica.ipynb b/cookbook/introduction/01_Welcome_to_Semantica.ipynbindex 8f481a95..b148995a 100644--- a/cookbook/introduction/01_Welcome_to_Semantica.ipynb+++ b/cookbook/introduction/01_Welcome_to_Semantica.ipynb@@ -1,667 +1,516 @@{- "cells": [- {- "cell_type": "markdown",- "metadata": {},- "source": [- "[](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/01_Welcome_to_Semantica.ipynb)\n",- "\n",- "# Welcome to Semantica\n",- "\n",- "## Overview\n",- "\n",- "This notebook introduces you to the **Semantica framework** - a comprehensive knowledge graph and semantic processing framework for building production-ready semantic AI applications.\n",- "\n",- "**Documentation**: [Getting Started](https://semantica.readthedocs.io/getting-started/) • [Concepts](https://semantica.readthedocs.io/concepts/) • [API Reference](https://semantica.readthedocs.io/reference/)\n",- "\n",- "### What You'll Learn\n",- "\n",- "- What Semantica is and why it's useful\n",- "- How to install and configure the framework\n",- "- Understanding the framework architecture\n",- "- Key concepts and terminology\n",- "- Next steps for getting started\n",- "\n",- "## What is Semantica?\n",- "\n",- "**Semantica** is a production-ready framework for:\n",- "\n",- "- **Building Knowledge Graphs**: Transform unstructured data into structured knowledge graphs\n",- "- **Semantic Processing**: Extract entities, relationships, and meaning from text\n",- "- **GraphRAG**: Graph-based retrieval augmented generation\n",- "- **Temporal Analysis**: Time-aware knowledge graphs\n",- "- **Text Processing**: Handle text and structured data\n",- "- **Enterprise Features**: Quality assurance, conflict resolution, ontology generation\n",- "\n",- "### Use Cases\n",- "\n",- "- Threat intelligence and cybersecurity\n",- "- Healthcare and medical research\n",- "- Financial analysis and fraud detection\n",- "- Supply chain optimization\n",- "- Research and knowledge management\n",- "- Multi-agent AI systems\n",- "\n",- "\n",- "## Installation & Setup\n",- "\n",- "### Prerequisites\n",- "\n",- "Before installing Semantica, ensure you have:\n",- "- Python 3.8 or higher\n",- "- pip package manager\n",- "- (Optional) Virtual environment for isolation\n",- "\n",- "### Installation Methods\n",- "\n",- "```bash\n",- "# Method 1: Install from PyPI (Recommended)\n",- "pip install semantica\n",- "\n",- "# Or install with all optional dependencies:\n",- "pip install semantica[all]\n",- "\n",- "# Method 2: Install from source (development version)\n",- "git clone https://github.com/Hawksight-AI/semantica.git\n",- "cd semantica\n",- "pip install -e .\n",- "\n",- "# Or with all optional dependencies:\n",- "pip install -e \".[all]\"\n",- "\n",- "# Verify installation\n",- "import semantica\n",- "print(semantica.__version__)\n",- "```\n",- "\n",- "### Configuration\n",- "\n",- "```bash\n",- "# Set up environment variables for API keys and configuration\n",- "# export SEMANTICA_API_KEY=your_openai_key\n",- "# export SEMANTICA_EMBEDDING_PROVIDER=openai\n",- "# export SEMANTICA_MODEL_NAME=gpt-4\n",- "\n",- "# Or use a config file (config.yaml):\n",- "# api_keys:\n",- "# openai: your_key_here\n",- "# anthropic: your_key_here\n",- "# embedding:\n",- "# provider: openai\n",- "# model: text-embedding-3-large\n",- "# dimensions: 3072\n",- "# knowledge_graph:\n",- "# backend: networkx # or neo4j, arangodb\n",- "# temporal: true\n",- "```\n",- "\n",- "---\n",- "\n",- "## Framework Architecture Overview\n",- "\n",- "Semantica is organized into modular components, each handling a specific aspect of semantic processing:\n",- "\n",- "### 1. INGEST MODULE - Data Ingestion\n",- "**Purpose**: Ingest data from various sources\n",- "**Components**:\n",- "- `FileIngestor`: Read files (PDF, DOCX, HTML, JSON, CSV, etc.)\n",- "- `WebIngestor`: Scrape and ingest web pages\n",- "- `FeedIngestor`: Process RSS/Atom feeds\n",- "- `StreamIngestor`: Real-time data streaming (Kafka, RabbitMQ, Kinesis, Pulsar)\n",- "- `DBIngestor`: Database queries and ingestion (PostgreSQL, MySQL, SQLite, Oracle, SQL Server)\n",- "- `EmailIngestor`: Process email messages (IMAP, POP3)\n",- "- `RepoIngestor`: Git repository analysis\n",- "- `MCPIngestor`: Model Context Protocol server integration\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.ingest import FileIngestor, WebIngestor, FeedIngestor, StreamIngestor, DBIngestor, EmailIngestor, RepoIngestor, MCPIngestor\n",- "file_ingestor = FileIngestor()\n",- "web_ingestor = WebIngestor()\n",- "documents = file_ingestor.ingest(\"data/\")\n",- "web_docs = web_ingestor.ingest(\"https://example.com\")\n",- "```\n",- "\n",- "### 2. PARSE MODULE - Document Parsing\n",- "**Purpose**: Parse and extract content from various formats\n",- "**Components**:\n",- "- `DocumentParser`: Main parser orchestrator\n",- "- `PDFParser`: Extract text, tables, images from PDFs\n",- "- `DOCXParser`: Parse Word documents\n",- "- `HTMLParser`: Extract content from HTML\n",- "- `JSONParser`: Parse structured JSON data\n",- "- `ExcelParser`: Process spreadsheets\n",- "- `ImageParser`: OCR and image analysis\n",- "- `CodeParser`: Parse source code files\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.parse import DocumentParser\n",- "parser = DocumentParser()\n",- "parsed_docs = parser.parse(documents)\n",- "```\n",- "\n",- "### 3. NORMALIZE MODULE - Text Normalization\n",- "**Purpose**: Clean and normalize text for processing\n",- "**Components**:\n",- "- `TextNormalizer`: Main normalization orchestrator\n",- "- `TextCleaner`: Remove noise, fix encoding\n",- "- `DataCleaner`: Clean structured data\n",- "- `EntityNormalizer`: Normalize entity names\n",- "- `DateNormalizer`: Standardize date formats\n",- "- `NumberNormalizer`: Normalize numeric values\n",- "- `LanguageDetector`: Detect document language\n",- "- `EncodingHandler`: Handle character encoding\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.normalize import TextNormalizer\n",- "normalizer = TextNormalizer()\n",- "normalized = normalizer.normalize(parsed_docs)\n",- "```\n",- "\n",- "### 4. SEMANTIC_EXTRACT MODULE - Entity & Relationship Extraction\n",- "**Purpose**: Extract entities, relationships, and semantic information\n",- "**Components**:\n",- "- `NERExtractor`: Named Entity Recognition\n",- "- `RelationExtractor`: Extract relationships between entities\n",- "- `SemanticAnalyzer`: Deep semantic analysis\n",- "- `SemanticNetworkExtractor`: Extract semantic networks\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.semantic_extract import NERExtractor, RelationExtractor\n",- "extractor = NERExtractor()\n",- "entities = extractor.extract(normalized_docs)\n",- "relation_extractor = RelationExtractor()\n",- "relationships = relation_extractor.extract(normalized_docs, entities)\n",- "```\n",- "\n",- "### 5. KG MODULE - Knowledge Graph Construction\n",- "**Purpose**: Build and manage knowledge graphs\n",- "**Components**:\n",- "- `GraphBuilder`: Construct knowledge graphs from entities/relationships\n",- "- `GraphAnalyzer`: Analyze graph structure and properties\n",- "- `GraphValidator`: Validate graph quality and consistency\n",- "- `EntityResolver`: Resolve entity conflicts and duplicates\n",- "- `ConflictDetector`: Detect conflicting information\n",- "- `CentralityCalculator`: Calculate node importance metrics\n",- "- `CommunityDetector`: Detect communities in graphs\n",- "- `ConnectivityAnalyzer`: Analyze graph connectivity\n",- "- `TemporalQuery`: Query temporal knowledge graphs\n",- "- `Deduplicator`: Remove duplicate entities/relationships\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.kg import GraphBuilder, GraphAnalyzer\n",- "builder = GraphBuilder()\n",- "kg = builder.build(entities, relationships)\n",- "analyzer = GraphAnalyzer()\n",- "metrics = analyzer.analyze(kg)\n",- "```\n",- "\n",- "### 6. EMBEDDINGS MODULE - Embedding Generation\n",- "**Purpose**: Generate vector embeddings for text data\n",- "**Components**:\n",- "- `EmbeddingGenerator`: Main embedding orchestrator\n",- "- `TextEmbedder`: Generate text embeddings\n",- "- `VectorEmbeddingManager`: Manage embeddings for vector databases\n",- "- `GraphEmbeddingManager`: Manage embeddings for graph databases\n",- "- `ProviderAdapters`: Support for OpenAI, BGE, FastEmbed, Llama, etc.\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.embeddings import EmbeddingGenerator\n",- "generator = EmbeddingGenerator()\n",- "embeddings = generator.generate_embeddings(documents, data_type=\"text\")\n",- "```\n",- "\n",- "### 7. VECTOR_STORE MODULE - Vector Database Operations\n",- "**Purpose**: Store and search vector embeddings\n",- "**Components**:\n",- "- `VectorStore`: Main vector store interface\n",- "- `FAISSAdapter`: FAISS integration\n",- "- `HybridSearch`: Combine vector and keyword search\n",- "- `VectorRetriever`: Retrieve relevant vectors\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.vector_store import VectorStore, HybridSearch\n",- "vector_store = VectorStore()\n",- "vector_store.store(embeddings, documents, metadata)\n",- "hybrid_search = HybridSearch(vector_store)\n",- "results = hybrid_search.search(query, top_k=10)\n",- "```\n",- "\n",- "### 8. GRAPH_STORE MODULE - Persistent Graph Database Operations\n",- "**Purpose**: Store and query property graphs in Neo4j or FalkorDB\n",- "**Components**:\n",- "- `GraphStore`: Main graph store interface\n",- "- `Neo4jAdapter`: Neo4j integration (enterprise features)\n",- "- `FalkorDBAdapter`: FalkorDB integration (Redis-based, ultra-fast)\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.graph_store import GraphStore\n",- "store = GraphStore(backend=\"neo4j\", uri=\"bolt://localhost:7687\", user=\"neo4j\", password=\"password\")\n",- "store.connect()\n",- "node1 = store.create_node(\n",- " labels=[\"Person\"],\n",- " properties={\"name\": \"John\", \"age\": 30}\n",- ")\n",- "node2 = store.create_node(\n",- " labels=[\"Person\"],\n",- " properties={\"name\": \"Jane\", \"age\": 28}\n",- ")\n",- "store.create_relationship(\n",- " start_node_id=node1[\"id\"],\n",- " end_node_id=node2[\"id\"],\n",- " rel_type=\"KNOWS\",\n",- " properties={\"since\": 2020}\n",- ")\n",- "results = store.execute_query(\"MATCH (p:Person) RETURN p.name\")\n",- "store.close()\n",- "```\n",- "\n",- "### 9. REASONING MODULE - Inference and Reasoning\n",- "**Purpose**: Perform logical inference and reasoning\n",- "**Components**:\n",- "- `InferenceEngine`: Main inference orchestrator\n",- "- `RuleManager`: Manage inference rules\n",- "- `DeductiveReasoner`: Deductive reasoning\n",- "- `AbductiveReasoner`: Abductive reasoning\n",- "- `ExplanationGenerator`: Generate explanations for inferences\n",- "- `RETEEngine`: RETE algorithm for rule matching\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.reasoning import InferenceEngine, RuleManager\n",- "inference_engine = InferenceEngine()\n",- "rule_manager = RuleManager()\n",- "new_facts = inference_engine.forward_chain()\n",- "```\n",- "\n",- "### 10. ONTOLOGY MODULE - Ontology Generation\n",- "**Purpose**: Generate and manage ontologies\n",- "**Components**:\n",- "- `OntologyGenerator`: Generate ontologies from knowledge graphs\n",- "- `OntologyValidator`: Validate ontology structure\n",- "- `OWLGenerator`: Generate OWL format ontologies\n",- "- `PropertyGenerator`: Generate ontology properties\n",- "- `ClassInferrer`: Infer ontology classes\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.ontology import OntologyGenerator\n",- "generator = OntologyGenerator()\n",- "ontology = generator.generate_from_graph(kg)\n",- "```\n",- "\n",- "### 11. EXPORT MODULE - Data Export\n",- "**Purpose**: Export data in various formats\n",- "**Components**:\n",- "- `JSONExporter`: Export to JSON\n",- "- `RDFExporter`: Export to RDF/XML\n",- "- `CSVExporter`: Export to CSV\n",- "- `GraphExporter`: Export to graph formats (GraphML, GEXF)\n",- "- `OWLExporter`: Export to OWL\n",- "- `VectorExporter`: Export vectors\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.export import JSONExporter, RDFExporter\n",- "json_exporter = JSONExporter()\n",- "json_exporter.export(kg, \"output.json\")\n",- "```\n",- "\n",- "### 12. VISUALIZATION MODULE - Graph Visualization\n",- "**Purpose**: Visualize knowledge graphs and analytics\n",- "**Components**:\n",- "- `KGVisualizer`: Visualize knowledge graphs\n",- "- `EmbeddingVisualizer`: Visualize embeddings (t-SNE, PCA, UMAP)\n",- "- `QualityVisualizer`: Visualize quality metrics\n",- "- `AnalyticsVisualizer`: Visualize graph analytics\n",- "- `TemporalVisualizer`: Visualize temporal data\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.visualization import KGVisualizer\n",- "visualizer = KGVisualizer()\n",- "visualizer.visualize(kg)\n",- "```\n",- "\n",- "### 13. PIPELINE MODULE - Pipeline Orchestration\n",- "**Purpose**: Build and execute processing pipelines\n",- "**Components**:\n",- "- `PipelineBuilder`: Build complex pipelines\n",- "- `ExecutionEngine`: Execute pipelines\n",- "- `FailureHandler`: Handle pipeline failures\n",- "- `ParallelismManager`: Enable parallel processing\n",- "- `ResourceScheduler`: Schedule resources\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.pipeline import PipelineBuilder\n",- "builder = PipelineBuilder()\n",- "pipeline = builder.add_step(\"ingest\", FileIngestor()) \\\\\n",- " .add_step(\"parse\", DocumentParser()) \\\\\n",- " .build()\n",- "```\n",- "\n",- "### 14. CORE MODULE - Framework Orchestration\n",- "**Purpose**: Framework initialization, configuration, lifecycle management, and plugin system\n",- "**Components**:\n",- "- `Semantica`: Main framework class coordinating all components\n",- "- `ConfigManager`: Configuration loading, validation, and management\n",- "- `Config`: Configuration data class with validation\n",- "- `LifecycleManager`: System lifecycle management with hooks and health monitoring\n",- "- `PluginRegistry`: Dynamic plugin discovery and loading\n",- "- `MethodRegistry`: Registry for custom orchestration methods\n",- "\n",- "**Example**:\n",- "```python\n",- "from semantica.core import Semantica, ConfigManager\n",- "\n",- "# Load configuration\n",- "config_manager = ConfigManager()\n",- "config = config_manager.load_from_file(\"config.yaml\")\n",- "\n",- "# Initialize framework\n",- "framework = Semantica(config=config)\n",- "framework.initialize()\n",- "\n",- "# Build knowledge base\n",- "result = framework.build_knowledge_base(\n",- " sources=[\"doc1.pdf\", \"doc2.docx\"],\n",- " embeddings=True,\n",- " graph=True\n",- ")\n",- "\n",- "# Shutdown gracefully\n",- "framework.shutdown()\n",- "```\n",- "\n",- "---\n",- "\n",- "## Key Concepts Explained\n",- "\n",- "Understanding these concepts is crucial for working with Semantica:\n",- "\n",- "### 1. KNOWLEDGE GRAPHS\n",- "**Definition**: A knowledge graph is a structured representation of entities (nodes) and their relationships (edges) with properties and attributes.\n",- "\n",- "**Structure**:\n",- "- **Nodes**: Represent entities (people, places, concepts, events)\n",- "- **Edges**: Represent relationships (works_for, located_in, causes)\n",- "- **Properties**: Attributes of entities and relationships\n",- "- **Metadata**: Additional information (sources, timestamps, confidence)\n",- "\n",- "**Example**:\n",- "- Entity: \"John Doe\" (Person)\n",- "- Relationship: \"works_for\" -> \"Acme Corp\" (Organization)\n",- "- Properties: `{start_date: \"2020-01-01\", role: \"Engineer\"}`\n",- "\n",- "**Benefits**:\n",- "- Structured representation of unstructured data\n",- "- Enables complex queries and reasoning\n",- "- Supports temporal tracking\n",- "- Facilitates knowledge discovery\n",- "\n",- "### 2. ENTITY EXTRACTION (NER - Named Entity Recognition)\n",- "**Definition**: The process of identifying and classifying named entities in text into predefined categories.\n",- "\n",- "**Entity Types**:\n",- "- **Person**: Names of people\n",- "- **Organization**: Companies, institutions\n",- "- **Location**: Places, geographic entities\n",- "- **Date/Time**: Temporal expressions\n",- "- **Money**: Monetary values\n",- "- **Product**: Products and services\n",- "- **Event**: Events and occurrences\n",- "- **Custom**: Domain-specific entities\n",- "\n",- "**Example**:\n",- "Text: \"Apple Inc. was founded by Steve Jobs in Cupertino, California.\"\n",- "Entities:\n",- "- \"Apple Inc.\" -> Organization\n",- "- \"Steve Jobs\" -> Person\n",- "- \"Cupertino, California\" -> Location\n",- "\n",- "**Methods**:\n",- "- Rule-based: Pattern matching\n",- "- Machine Learning: Trained models (spaCy, transformers)\n",- "- LLM-based: Using large language models\n",- "\n",- "### 3. RELATIONSHIP EXTRACTION\n",- "**Definition**: Identifying and extracting relationships between entities in text.\n",- "\n",- "**Relationship Types**:\n",- "- **Semantic**: \"works_for\", \"located_in\", \"causes\"\n",- "- **Temporal**: \"before\", \"after\", \"during\"\n",- "- **Causal**: \"causes\", \"results_in\", \"prevents\"\n",- "- **Hierarchical**: \"part_of\", \"subclass_of\", \"instance_of\"\n",- "\n",- "**Example**:\n",- "Text: \"John works for Acme Corp in New York.\"\n",- "Relationships:\n",- "- (John, works_for, Acme Corp)\n",- "- (Acme Corp, located_in, New York)\n",- "\n",- "**Methods**:\n",- "- Pattern matching\n",- "- Dependency parsing\n",- "- Machine learning models\n",- "- LLM-based extraction\n",- "\n",- "### 4. EMBEDDINGS\n",- "**Definition**: Dense vector representations of text, images, or other data that capture semantic meaning in a continuous vector space.\n",- "\n",- "**Properties**:\n",- "- Similar entities have similar embeddings (close in vector space)\n",- "- Enable semantic search and similarity calculations\n",- "- Fixed or variable dimensions (typically 128-4096)\n",- "\n",- "**Example**:\n",- "Text: \"machine learning\"\n",- "Embedding: `[0.123, -0.456, 0.789, ..., 0.234]` (vector of 1536 dimensions)\n",- "\n",- "**Use Cases**:\n",- "- Semantic search\n",- "- Clustering and classification\n",- "- Recommendation systems\n",- "- Anomaly detection\n",- "\n",- "### 5. TEMPORAL GRAPHS\n",- "**Definition**: Knowledge graphs that track changes over time, allowing queries about the state of the graph at specific time points.\n",- "\n",- "**Features**:\n",- "- Timestamps on entities and relationships\n",- "- Version history\n",- "- Time-point queries\n",- "- Temporal pattern detection\n",- "\n",- "**Example**:\n",- "- Entity: \"Company X\"\n",- "- Relationship: (Company X, has_CEO, Person Y)\n",- "- Temporal: `valid_from=\"2020-01-01\", valid_to=\"2023-12-31\"`\n",- "\n",- "**Use Cases**:\n",- "- Tracking organizational changes\n",- "- Monitoring system evolution\n",- "- Analyzing trends over time\n",- "- Historical analysis\n",- "\n",- "### 6. GraphRAG (Graph-based Retrieval Augmented Generation)\n",- "**Definition**: An advanced RAG approach that combines vector search with knowledge graph traversal to provide more accurate and contextually relevant information to LLMs.\n",- "\n",- "**Components**:\n",- "- **Vector Store**: For semantic similarity search\n",- "- **Knowledge Graph**: For structured relationship traversal\n",- "- **Hybrid Search**: Combines both approaches\n",- "- **LLM Integration**: Uses retrieved context for generation\n",- "\n",- "**Advantages over Traditional RAG**:\n",- "- Better handling of complex queries\n",- "- Relationship-aware retrieval\n",- "- Reduced hallucinations\n",- "- More accurate answers\n",- "\n",- "**Example Workflow**:\n",- "1. Query: \"Who worked with John at Acme Corp?\"\n",- "2. Vector search finds relevant documents\n",- "3. Knowledge graph traversal finds relationships\n",- "4. Combined context sent to LLM\n",- "5. LLM generates accurate answer using both sources\n",- "\n",- "### 7. ONTOLOGY\n",- "**Definition**: A formal specification of concepts, relationships, and constraints in a domain, typically expressed in OWL (Web Ontology Language).\n",- "\n",- "**Components**:\n",- "- **Classes**: Categories of entities\n",- "- **Properties**: Relationships and attributes\n",- "- **Individuals**: Specific instances\n",- "- **Axioms**: Rules and constraints\n",- "\n",- "**Example**:\n",- "- Class: Person\n",- "- SubClass: Employee, Customer\n",- "- Property: worksFor (domain: Person, range: Organization)\n",- "\n",- "**Use Cases**:\n",- "- Standardize domain knowledge\n",- "- Enable reasoning\n",- "- Facilitate data integration\n",- "- Support semantic web\n",- "\n",- "### 8. QUALITY ASSURANCE\n",- "**Definition**: Processes and metrics to ensure knowledge graph quality, including completeness, consistency, and accuracy.\n",- "\n",- "**Metrics**:\n",- "- **Completeness**: Percentage of entities with required properties\n",- "- **Consistency**: Absence of contradictions\n",- "- **Accuracy**: Correctness of extracted information\n",- "- **Coverage**: Breadth of domain coverage\n",- "\n",- "**Methods**:\n",- "- Validation rules\n",- "- Automated quality checks\n",- "- Conflict detection\n",- "- Source verification\n",- "---\n",- "\n",- "## Next Steps\n",- "\n",- "Now that you understand the basics, here are recommended next steps:\n",- "\n",- "1. **Your First Knowledge Graph** (`01_Your_First_Knowledge_Graph.ipynb`)\n",- " - Build your first knowledge graph from a document\n",- " - Learn the basic workflow\n",- "\n",- "2. **Configuration Basics** (`02_Configuration_Basics.ipynb`)\n",- " - Set up configuration files\n",- " - Configure API keys and providers\n",- "\n",- "3. **Core Workflows** (`01_core_workflows/`)\n",- " - Learn common patterns and workflows\n",- " - Start with \"From Unstructured to Structured\"\n",- "\n",- "4. **Use Cases** (`03_use_cases/`)\n",- " - Explore domain-specific applications\n",- " - Find examples relevant to your domain\n",- "\n",- "---\n",- "\n",- "## Best Practices\n",- "\n",- "### 1. START SMALL\n",- "- Begin with simple documents\n",- "- Validate each step before moving forward\n",- "- Build incrementally\n",- "\n",- "### 2. CONFIGURE PROPERLY\n",- "- Use environment variables for sensitive data\n",- "- Set up proper logging\n",- "- Configure appropriate model sizes\n",- "\n",- "### 3. VALIDATE DATA\n",- "- Always validate extracted entities\n",- "- Check relationship quality\n",- "- Use quality assurance tools\n",- "\n",- "### 4. HANDLE ERRORS\n",- "- Implement error handling\n",- "- Use retry mechanisms\n",- "- Log errors for debugging\n",- "\n",- "### 5. OPTIMIZE PERFORMANCE\n",- "- Use batch processing for large datasets\n",- "- Enable parallel processing where possible\n",- "- Cache embeddings and results\n",- "\n",- "### 6. DOCUMENT YOUR WORKFLOWS\n",- "- Document data sources\n",- "- Track processing steps\n",- "- Maintain metadata\n",- "\n",- "---\n",- "\n",- "## Troubleshooting\n",- "\n",- "Common issues and solutions:\n",- "\n",- "### Issue 1: Import Errors\n",- "**Solution**:\n",- "- Ensure Semantica is properly installed\n",- "- Check Python version (3.8+)\n",- "- Verify virtual environment is activated\n",- "- Install missing dependencies: `pip install -r requirements.txt`\n",- "\n",- "### Issue 2: API Key Errors\n",- "**Solution**:\n",- "- Set environment variables: `export SEMANTICA_API_KEY=your_key`\n",- "- Check config file for correct key format\n",- "- Verify API key is valid and has sufficient credits\n",- "\n",- "### Issue 3: Memory Issues\n",- "**Solution**:\n",- "- Process documents in batches\n",- "- Use smaller embedding models\n",- "- Enable garbage collection\n",- "- Consider using streaming for large datasets\n",- "\n",- "### Issue 4: Low Quality Extractions\n",- "**Solution**:\n",- "- Preprocess and normalize text\n",- "- Use domain-specific models\n",- "- Adjust extraction parameters\n",- "- Validate and clean extracted entities\n",- "\n",- "### Issue 5: Slow Processing\n",- "**Solution**:\n",- "- Enable parallel processing\n",- "- Use GPU acceleration if available\n",- "- Cache intermediate results\n",- "- Optimize batch sizes\n",- "\n"- ]- },- {- "cell_type": "code",- "execution_count": null,- "metadata": {},- "outputs": [],- "source": [- "%pip install -U \"semantica[all]\"\n",- "import semantica\n",- "print(semantica.__version__)\n"- ]- }- ],- "metadata": {- "language_info": {- "name": "python"- }+ "cells": [+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "[](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/01_Welcome_to_Semantica.ipynb)\n",+ "\n",+ "# Welcome to Semantica\n",+ "\n",+ "**Open Source Framework for Semantic Layer & Knowledge Engineering**\n",+ "\n",+ "Semantica is a Python framework for transforming raw, messy, multi-source data into **semantic layers** and **knowledge graphs** that are ready to power GraphRAG, AI agents, multi-agent systems, and analytical applications.\n",+ "\n",+ "This notebook is an executable introduction. It combines:\n",+ "\n",+ "- High-level explanation of what Semantica is and why it exists\n",+ "- A structured tour of the architecture and key modules\n",+ "- Small, runnable code snippets that show the end-to-end flow\n",+ "\n",+ "**You should use this notebook to understand the big picture, not to learn every API in depth.**"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## What Is Semantica?\n",+ "\n",+ "Semantica is a **semantic intelligence and knowledge engineering framework**. It helps you:\n",+ "\n",+ "- Build **knowledge graphs** from unstructured and semi-structured data\n",+ "- Create a unified **semantic layer** on top of diverse data sources\n",+ "- Power **GraphRAG**, AI agents, and multi-agent systems with structured knowledge\n",+ "- Incorporate **temporal and quality-aware reasoning** into your applications\n",+ "\n",+ "### Core Capabilities\n",+ "\n",+ "- **Universal ingestion**: Files, web, feeds, databases, repositories, streams\n",+ "- **Rich parsing**: PDFs, Office documents, HTML, JSON, CSV, images, code\n",+ "- **Normalization**: Cleaning, language detection, entity normalization, date/number standardization\n",+ "- **Semantic extraction**: Named entities, relationships, events, semantic networks\n",+ "- **Knowledge graph construction**: Property graphs from entities and relations\n",+ "- **Embeddings and vector search**: Text and graph embeddings, hybrid retrieval\n",+ "- **Reasoning and ontology**: Rule-based inference, ontology generation and validation\n",+ "- **Visualization and analytics**: Graph visualizations and quality metrics"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Who Is Semantica For?\n",+ "\n",+ "- **AI/ML engineers** building GraphRAG systems, agents, and tools that need long-term memory\n",+ "- **Data engineers** orchestrating semantic enrichment pipelines over large, heterogeneous datasets\n",+ "- **Knowledge engineers and ontologists** designing and maintaining formal knowledge structures\n",+ "- **Researchers and analysts** creating domain knowledge graphs from documents and data feeds\n",+ "- **Product and platform teams** embedding semantic intelligence into applications and services"+ ]},- "nbformat": 4,- "nbformat_minor": 2+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Architecture Overview\n",+ "\n",+ "Semantica is organized as three conceptual layers and multiple concrete modules.\n",+ "\n",+ "### Layers\n",+ "\n",+ "- **Input Layer**\n",+ " - Connects to files, web pages, APIs, databases, email, feeds, repositories, and streams\n",+ " - Normalizes these different sources into a unified internal representation\n",+ "\n",+ "- **Semantic Layer**\n",+ " - Performs parsing, cleaning, semantic extraction, graph construction, embeddings, and reasoning\n",+ " - This is where **unstructured data becomes structured knowledge**\n",+ "\n",+ "- **Output Layer**\n",+ " - Exposes knowledge graphs, embeddings, ontologies, and analytics\n",+ " - Integrates with vector stores, graph databases, and downstream applications"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Key Modules at a Glance\n",+ "\n",+ "Below is a conceptual overview of the core modules. Later cells show a small end-to-end example that stitches several of them together.\n",+ "\n",+ "- **Ingest**\n",+ " - Components: `FileIngestor`, `WebIngestor`, `FeedIngestor`, `StreamIngestor`, `DBIngestor`, `EmailIngestor`, `RepoIngestor`, `MCPIngestor`\n",+ " - Responsibility: Bring data from many sources into the pipeline\n",+ "\n",+ "- **Parse**\n",+ " - Components: `DocumentParser`, format-specific parsers like `PDFParser`, `HTMLParser`, `JSONParser`, `ExcelParser`\n",+ " - Responsibility: Turn raw content into structured document objects\n",+ "\n",+ "- **Normalize**\n",+ " - Components: `TextNormalizer`, `TextCleaner`, `EntityNormalizer`, `DateNormalizer`, `NumberNormalizer`\n",+ " - Responsibility: Clean text, standardize entities and values, prepare for extraction\n",+ "\n",+ "- **Semantic extract**\n",+ " - Components: `NERExtractor`, `RelationExtractor`, `SemanticAnalyzer`, `SemanticNetworkExtractor`\n",+ " - Responsibility: Identify entities and relationships that will become nodes and edges in the graph\n",+ "\n",+ "- **Knowledge graph (KG)**\n",+ " - Components: `GraphBuilder`, `GraphAnalyzer`, `GraphValidator`, `EntityResolver`, `ConflictDetector`\n",+ " - Responsibility: Build, analyze, and validate the knowledge graph\n",+ "\n",+ "- **Embeddings and vector store**\n",+ " - Components: `EmbeddingGenerator`, `VectorStore`, `HybridSearch`\n",+ " - Responsibility: Generate vector representations and enable semantic search\n",+ "\n",+ "- **Graph store**\n",+ " - Components: `GraphStore`, adapters for backends like Neo4j or FalkorDB\n",+ " - Responsibility: Persist and query graphs in external databases\n",+ "\n",+ "- **Reasoning and ontology**\n",+ " - Components: `InferenceEngine`, `RuleManager`, `OntologyGenerator`, `OntologyValidator`\n",+ " - Responsibility: Apply rules, infer new facts, and maintain formal ontologies\n",+ "\n",+ "- **Visualization**\n",+ " - Components: `KGVisualizer`, `EmbeddingVisualizer`, `QualityVisualizer`, `AnalyticsVisualizer`\n",+ " - Responsibility: Inspect, debug, and present graphs, embeddings, and quality metrics"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Core Concepts (High-Level)\n",+ "\n",+ "- **Knowledge graph**\n",+ " - Nodes represent entities such as people, organizations, locations, events, or concepts\n",+ " - Edges represent relationships such as `works_for`, `located_in`, `founded_by`\n",+ " - Properties capture attributes and metadata such as timestamps, sources, and confidence\n",+ "\n",+ "- **Entities and relationships**\n",+ " - Entities are extracted from text and data using NER\n",+ " - Relationships connect entities and are extracted using pattern-based, model-based, or LLM-based methods\n",+ "\n",+ "- **Embeddings**\n",+ " - Numerical vectors that encode semantic meaning of text or graph structures\n",+ " - Used for semantic search, clustering, and similarity-based retrieval\n",+ "\n",+ "- **GraphRAG**\n",+ " - Combines vector search with graph traversal\n",+ " - Uses both embeddings and graph structure to retrieve rich, context-aware information\n",+ "\n",+ "- **Ontology**\n",+ " - A formal model of classes, relationships, and constraints in a domain\n",+ " - Used to standardize meaning, enable reasoning, and integrate heterogeneous data\n",+ "\n",+ "- **Quality and governance**\n",+ " - Quality metrics (completeness, consistency, accuracy, coverage)\n",+ " - Conflict detection and resolution at the knowledge graph level"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Installation\n",+ "\n",+ "You can install Semantica from PyPI. In this notebook, we use a pip cell so it can run in local Jupyter or Colab.\n",+ "\n",+ "Equivalent shell commands:\n",+ "\n",+ "```bash\n",+ "pip install semantica\n",+ "pip install semantica[all]\n",+ "```"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "%pip install -U \"semantica[all]\"\n",+ "import semantica\n",+ "semantica.__version__"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Basic Configuration\n",+ "\n",+ "Semantica uses configuration for API keys, embedding providers, and knowledge graph options. The example below mirrors a typical configuration while staying simple enough for a notebook."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "import os\n",+ "from pathlib import Path\n",+ "\n",+ "os.environ[\"SEMANTICA_API_KEY\"] = \"your_openai_key\"\n",+ "os.environ[\"SEMANTICA_EMBEDDING_PROVIDER\"] = \"openai\"\n",+ "os.environ[\"SEMANTICA_MODEL_NAME\"] = \"gpt-4\"\n",+ "\n",+ "config_text = \"\"\"api_keys:\n",+ " openai: your_key_here\n",+ " anthropic: your_key_here\n",+ "embedding:\n",+ " provider: openai\n",+ " model: text-embedding-3-large\n",+ " dimensions: 3072\n",+ "knowledge_graph:\n",+ " backend: networkx\n",+ " temporal: true\n",+ "\"\"\"\n",+ "Path(\"config.yaml\").write_text(config_text, encoding=\"utf-8\")\n",+ "Path(\"config.yaml\").read_text(encoding=\"utf-8\")"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Quick Start: Build a Tiny Knowledge Base\n",+ "\n",+ "The simplest way to use Semantica is the high-level `build` helper. It ingests data, runs a default pipeline, and returns a dictionary that includes the knowledge graph, embeddings, metadata, and statistics."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "from semantica import build\n",+ "from pathlib import Path\n",+ "\n",+ "docs_dir = Path(\"welcome_docs\")\n",+ "docs_dir.mkdir(exist_ok=True)\n",+ "text_path = docs_dir / \"apple.txt\"\n",+ "text_content = (\n",+ " \"Apple Inc. was founded by Steve Jobs, Steve Wozniak and Ronald Wayne in\"\n",+ " \" Cupertino, California.\"\n",+ ")\n",+ "text_path.write_text(text_content, encoding=\"utf-8\")\n",+ "\n",+ "result = build(str(docs_dir), embeddings=False, graph=True)\n",+ "sorted(result.keys())"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Minimal End-to-End Pipeline\n",+ "\n",+ "The next example shows how to explicitly use several modules in sequence. This mirrors the architecture discussed earlier:\n",+ "\n",+ "1. Ingest a directory of documents\n",+ "2. Parse them into structured documents\n",+ "3. Normalize text\n",+ "4. Extract entities and relationships\n",+ "5. Build and analyze a knowledge graph\n",+ "6. Create embeddings and store them in a vector store\n",+ "7. Run a hybrid semantic search query"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "from semantica.ingest import FileIngestor\n",+ "from semantica.parse import DocumentParser\n",+ "from semantica.normalize import TextNormalizer\n",+ "from semantica.semantic_extract import NERExtractor, RelationExtractor\n",+ "from semantica.kg import GraphBuilder, GraphAnalyzer\n",+ "from semantica.embeddings import EmbeddingGenerator\n",+ "from semantica.vector_store import VectorStore, HybridSearch\n",+ "\n",+ "ingestor = FileIngestor()\n",+ "documents = ingestor.ingest(str(docs_dir))\n",+ "\n",+ "parser = DocumentParser()\n",+ "parsed_docs = parser.parse(documents)\n",+ "\n",+ "normalizer = TextNormalizer()\n",+ "normalized_docs = normalizer.normalize(parsed_docs)\n",+ "\n",+ "ner = NERExtractor()\n",+ "entities = ner.extract(normalized_docs)\n",+ "rel_extractor = RelationExtractor()\n",+ "relationships = rel_extractor.extract(normalized_docs, entities)\n",+ "\n",+ "builder = GraphBuilder()\n",+ "kg = builder.build(entities, relationships)\n",+ "analyzer = GraphAnalyzer()\n",+ "metrics = analyzer.analyze(kg)\n",+ "\n",+ "emb_generator = EmbeddingGenerator()\n",+ "embeddings = emb_generator.generate_embeddings(documents, data_type=\"text\")\n",+ "\n",+ "vec_store = VectorStore()\n",+ "vec_store.store(embeddings, documents, metadata={})\n",+ "hybrid = HybridSearch(vec_store)\n",+ "search_results = hybrid.search(\"Apple founders\", top_k=3)\n",+ "len(search_results)"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Visualization\n",+ "\n",+ "Semantica includes a powerful visualization module. Here we create an interactive network graph from the knowledge graph built above."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "from semantica.visualization import KGVisualizer\n",+ "\n",+ "# Create a visualizer instance\n",+ "viz = KGVisualizer(layout=\"force\", color_scheme=\"vibrant\")\n",+ "\n",+ "# Generate an interactive network visualization\n",+ "# This returns a Plotly figure object that renders in the notebook\n",+ "fig = viz.visualize_network(kg, output=\"interactive\")\n",+ "fig.show()"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Ontology Generation\n",+ "\n",+ "You can also automatically generate an ontology (a formal model of your domain) from the extracted entities and relationships."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "from semantica.ontology import OntologyGenerator\n",+ "\n",+ "generator = OntologyGenerator(base_uri=\"https://example.org/ontology/\")\n",+ "\n",+ "# Generate ontology from the extracted data\n",+ "ontology = generator.generate_ontology({\n",+ " \"entities\": entities,\n",+ " \"relationships\": relationships\n",+ "})\n",+ "\n",+ "# View inferred classes\n",+ "[cls[\"name\"] for cls in ontology.get(\"classes\", [])[:5]]"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Advanced: Data Splitting and Chunking\n",+ "\n",+ "For RAG applications, splitting documents into smaller chunks is essential. Semantica provides a `split` module for this purpose."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "from semantica.split import Splitter\n",+ "\n",+ "splitter = Splitter(chunk_size=100, chunk_overlap=20)\n",+ "chunks = splitter.split_documents(documents)\n",+ "\n",+ "print(f\"Original documents: {len(documents)}\")\n",+ "print(f\"Generated chunks: {len(chunks)}\")"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Advanced: Reasoning and Inference\n",+ "\n",+ "The `reasoning` module allows you to derive new facts from existing knowledge using logic rules."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "from semantica.reasoning import InferenceEngine\n",+ "\n",+ "# Simple rule: If X founded Y, then X works_for Y\n",+ "rule = \"\"\"\n",+ "IF (?x founded ?y) THEN (?x works_for ?y)\n",+ "\"\"\"\n",+ "\n",+ "engine = InferenceEngine()\n",+ "engine.add_rule(rule)\n",+ "inferred_facts = engine.infer(kg)\n",+ "\n",+ "print(f\"Inferred {len(inferred_facts)} new facts\")"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Advanced: Export and Persistence\n",+ "\n",+ "You can save your knowledge graph to disk or export it to standard formats like CSV, JSON, or RDF."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "from semantica.export import GraphExporter\n",+ "\n",+ "exporter = GraphExporter()\n",+ "exporter.export(kg, format=\"json\", output_path=\"knowledge_graph.json\")\n",+ "\n",+ "print(\"Graph exported to knowledge_graph.json\")"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Other Specialized Modules\n",+ "\n",+ "Semantica includes many other specialized modules for advanced use cases:\n",+ "\n",+ "- **Context**: Manages agent memory, conversation history, and context graphs for AI agents.\n",+ "- **Conflicts**: Detects and resolves conflicting information from different sources (e.g., different birth dates for the same person).\n",+ "- **Deduplication**: Identifies and merges duplicate entities (e.g., \"Steve Jobs\" vs \"Stephen Jobs\").\n",+ "- **Pipeline**: Orchestrates complex, multi-step workflows with retries and error handling.\n",+ "- **Triplet Store**: Adapters for enterprise RDF stores like BlazeGraph, Jena, or Virtuoso.\n",+ "- **Graph Store**: Adapters for Property Graph databases like Neo4j or FalkorDB."+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Using the Core `Semantica` Class\n",+ "\n",+ "For more complex systems, you can work directly with the `Semantica` core class and a configuration object. This gives you access to lifecycle management, plugin registration, and orchestration helpers."+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "metadata": {},+ "outputs": [],+ "source": [+ "from semantica.core import Semantica, ConfigManager\n",+ "\n",+ "config_manager = ConfigManager()\n",+ "config = config_manager.load_from_file(\"config.yaml\")\n",+ "\n",+ "framework = Semantica(config=config)\n",+ "framework.initialize()\n",+ "\n",+ "kb_result = framework.build_knowledge_base(\n",+ " sources=[str(docs_dir)],\n",+ " embeddings=True,\n",+ " graph=True,\n",+ ")\n",+ "\n",+ "framework.shutdown()\n",+ "sorted(kb_result.keys())"+ ]+ },+ {+ "cell_type": "markdown",+ "metadata": {},+ "source": [+ "## Where to Go Next\n",+ "\n",+ "- Run the notebooks under `cookbook/introduction` for focused module overviews\n",+ "- Explore `cookbook/use_cases` for domain-specific end-to-end workflows\n",+ "- Read the **Core Concepts** documentation for deeper theory and best practices"+ ]+ }+ ],+ "metadata": {+ "language_info": {+ "name": "python"+ }+ },+ "nbformat": 4,+ "nbformat_minor": 2}