Protokoll #27219

ID27219
Zeitstempel2025-12-28 02:45:24.261119
Clientroot
IP145.224.72.140
Modellclaude-sonnet-4-20250514
Statuscompleted
Tokens3,556 (Input: 328, Output: 3,228)
Dauer115 ms
Request-Zeit2025-12-28 02:45:24.261119
Response-Zeit2025-12-28 02:45:24.376026

Request

{
    "event": "PreToolUse",
    "tool_name": "Edit",
    "tool_input": {
        "file_path": "\/var\/www\/scripts\/pipeline\/pipeline.py",
        "old_string": "def run_full_pipeline(run_id=None, pipeline_id=None):\n    \"\"\"Run complete pipeline: scan → process → embed.\"\"\"\n    progress = PipelineProgress(run_id) if run_id else None\n\n    print(\"=\" * 50)\n    print(\"KI-System Pipeline - Full Run\")\n    if run_id:\n        print(f\"Run ID: {run_id}, Pipeline ID: {pipeline_id}\")\n    print(\"=\" * 50)",
        "new_string": "def run_full_pipeline(run_id=None, pipeline_id=None):\n    \"\"\"Run complete pipeline: scan → process → embed.\n\n    Pipeline selection:\n    - pipeline_id=5: Scientific Pipeline (semantics BEFORE embedding)\n    - pipeline_id=1-4 or None: Legacy Pipeline (semantics AFTER embedding)\n    \"\"\"\n    progress = PipelineProgress(run_id) if run_id else None\n\n    # Determine which processing function to use\n    use_v5 = pipeline_id == 5\n    pipeline_name = \"Wissenschaftliche Pipeline v1\" if use_v5 else \"Standard Pipeline\"\n\n    print(\"=\" * 50)\n    print(f\"KI-System Pipeline - {pipeline_name}\")\n    if run_id:\n        print(f\"Run ID: {run_id}, Pipeline ID: {pipeline_id}\")\n    if use_v5:\n        print(\"Mode: Semantik VOR Embedding (wissenschaftlich korrekt)\")\n    print(\"=\" * 50)"
    }
}

Response

{
    "tool_response": {
        "filePath": "\/var\/www\/scripts\/pipeline\/pipeline.py",
        "oldString": "def run_full_pipeline(run_id=None, pipeline_id=None):\n    \"\"\"Run complete pipeline: scan → process → embed.\"\"\"\n    progress = PipelineProgress(run_id) if run_id else None\n\n    print(\"=\" * 50)\n    print(\"KI-System Pipeline - Full Run\")\n    if run_id:\n        print(f\"Run ID: {run_id}, Pipeline ID: {pipeline_id}\")\n    print(\"=\" * 50)",
        "newString": "def run_full_pipeline(run_id=None, pipeline_id=None):\n    \"\"\"Run complete pipeline: scan → process → embed.\n\n    Pipeline selection:\n    - pipeline_id=5: Scientific Pipeline (semantics BEFORE embedding)\n    - pipeline_id=1-4 or None: Legacy Pipeline (semantics AFTER embedding)\n    \"\"\"\n    progress = PipelineProgress(run_id) if run_id else None\n\n    # Determine which processing function to use\n    use_v5 = pipeline_id == 5\n    pipeline_name = \"Wissenschaftliche Pipeline v1\" if use_v5 else \"Standard Pipeline\"\n\n    print(\"=\" * 50)\n    print(f\"KI-System Pipeline - {pipeline_name}\")\n    if run_id:\n        print(f\"Run ID: {run_id}, Pipeline ID: {pipeline_id}\")\n    if use_v5:\n        print(\"Mode: Semantik VOR Embedding (wissenschaftlich korrekt)\")\n    print(\"=\" * 50)",
        "originalFile": "#!\/usr\/bin\/env python3\n\"\"\"\nKI-System Document Pipeline\nMain orchestration script for document processing.\n\nUsage:\n    python pipeline.py scan             # Scan for new documents\n    python pipeline.py process          # Process queued documents\n    python pipeline.py embed            # Embed pending chunks\n    python pipeline.py semantic <id>    # Run semantic analysis on document\n    python pipeline.py semantic-queue   # Process semantic queue\n    python pipeline.py enrich-entities  # Enrich entity descriptions via Ollama\n    python pipeline.py enrich-entities 50  # Limit to 50 entities\n    python pipeline.py all              # Full pipeline run\n    python pipeline.py all --pipeline-id=1 --run-id=5  # With tracking\n    python pipeline.py file <path>      # Process single file\n    python pipeline.py status           # Show pipeline status\n\"\"\"\n\nimport argparse\nimport os\nimport time\nfrom pathlib import Path\n\nfrom config import (\n    MAX_RETRIES,\n    RETRY_BACKOFF_BASE,\n    SEMANTIC_AUTO_QUEUE,\n    SEMANTIC_SYNC,\n    SEMANTIC_USE_ANTHROPIC,\n)\nfrom constants import DEFAULT_LIMIT\nfrom db import PipelineProgress, db\nfrom detect import queue_files, scan_directory\nfrom step_embed import EmbeddingStep\nfrom step_entity_enrich import EntityEnrichStep\nfrom step_extract import ExtractionStep\nfrom step_load import LoadStep\nfrom step_semantic import SemanticStep\nfrom step_semantic_extended import (\n    DuplicateCheckStep,\n    KnowledgeSemanticAnalyzeStep,\n    KnowledgeSemanticStoreStep,\n    TextSemanticAnalyzeStep,\n    TextSemanticStoreStep,\n)\nfrom step_transform import TransformationStep\n\n\ndef process_file(file_path, progress=None):\n    \"\"\"Process a single file through the pipeline.\"\"\"\n    file_name = Path(file_path).name\n\n    if progress:\n        progress.update_document(file_name)\n\n    # Initialize pipeline steps\n    extract_step = ExtractionStep(db, progress)\n    load_step = LoadStep(db, progress)\n    transform_step = TransformationStep(db, progress)\n    embed_step = EmbeddingStep(db, progress)\n\n    # Check if cancelled before starting\n    if progress and progress.is_cancelled():\n        return \"cancelled\", 0, 0\n\n    # Step 1: Extract\n    extract_result = extract_step.execute(file_path)\n    if not extract_result[\"success\"]:\n        if extract_result.get(\"error\") == \"cancelled\":\n            return \"cancelled\", 0, 0\n        return False, 0, 0\n\n    extraction = extract_result[\"extraction\"]\n    file_info = extract_result[\"file_info\"]\n    total_pages = extract_result.get(\"total_pages\", 0)\n\n    # Check if cancelled after extraction\n    if progress and progress.is_cancelled():\n        return \"cancelled\", 0, 0\n\n    # Step 2: Load document\n    doc_id = load_step.create_document(file_info)\n\n    # Step 3: Store pages (PDFs and multi-page documents)\n    page_map = load_step.store_pages(doc_id, extraction)\n\n    # Step 4: Vision analysis (PDFs only)\n    if file_info[\"type\"] == \".pdf\":\n        transform_step.execute_vision(doc_id, file_path, file_info[\"type\"])\n\n        # Check if cancelled after vision\n        if progress and progress.is_cancelled():\n            return \"cancelled\", 0, 0\n\n    # Step 5: Chunking\n    chunks = transform_step.execute_chunking(extraction, total_pages)\n\n    # Step 6: Store chunks with page references\n    chunks = load_step.store_chunks(doc_id, chunks, page_map)\n\n    # Check if cancelled after chunking\n    if progress and progress.is_cancelled():\n        return \"cancelled\", len(chunks), 0\n\n    # Step 7: Enrichment (PDFs only)\n    if file_info[\"type\"] == \".pdf\":\n        transform_step.execute_enrichment(doc_id, file_info[\"type\"])\n\n        # Check if cancelled after enrichment\n        if progress and progress.is_cancelled():\n            return \"cancelled\", len(chunks), 0\n\n    # Step 8: Embeddings (Layer 3 - Document becomes searchable)\n    embedded = embed_step.execute(chunks, doc_id, file_name, file_path)\n\n    # Document is now searchable - update status to \"embedded\"\n    load_step.update_document_status(doc_id, \"embedded\")\n\n    if progress:\n        progress.add_log(f\"Layer 3 fertig: {file_name} ist jetzt suchbar\")\n\n    # Check if cancelled after embedding\n    if progress and progress.is_cancelled():\n        return \"cancelled\", len(chunks), embedded\n\n    # Step 9: Semantic analysis (Layer 4 - Optional\/Async)\n    semantic_step = SemanticStep(db, progress)\n    full_text = extract_step.get_full_text_from_extraction(extraction)\n\n    if SEMANTIC_SYNC:\n        # Run semantic analysis synchronously\n        try:\n            semantic_step.execute(doc_id, full_text, use_anthropic=SEMANTIC_USE_ANTHROPIC)\n            # Update to done only after semantic completes\n            load_step.update_document_status(doc_id, \"done\")\n        except Exception as e:\n            # Semantic failed but document is still searchable\n            db.log(\"WARNING\", f\"Semantic analysis failed for {file_name}: {e}\")\n            if progress:\n                progress.add_log(f\"Semantik-Fehler (Dokument bleibt suchbar): {str(e)[:50]}\")\n    elif SEMANTIC_AUTO_QUEUE:\n        # Queue for async processing\n        semantic_step.queue(doc_id, priority=5)\n        load_step.update_document_status(doc_id, \"done\")\n        if progress:\n            progress.add_log(f\"Semantik in Queue: {file_name}\")\n    else:\n        # No semantic analysis\n        load_step.update_document_status(doc_id, \"done\")\n\n    if progress:\n        progress.add_log(f\"Fertig: {file_name}\")\n\n    return True, len(chunks), embedded\n\n\ndef process_file_v5(file_path, progress=None):\n    \"\"\"Process a single file through Pipeline #5 (Scientific Pipeline).\n\n    Key difference from process_file():\n    - Semantic analysis happens BEFORE embedding (scientifically correct)\n    - Uses extended semantic steps for text and knowledge semantics\n    \"\"\"\n    file_name = Path(file_path).name\n\n    if progress:\n        progress.update_document(file_name)\n\n    # Initialize pipeline steps\n    extract_step = ExtractionStep(db, progress)\n    load_step = LoadStep(db, progress)\n    transform_step = TransformationStep(db, progress)\n    embed_step = EmbeddingStep(db, progress)\n    text_semantic_analyze = TextSemanticAnalyzeStep(db, progress)\n    text_semantic_store = TextSemanticStoreStep(db, progress)\n    knowledge_semantic_analyze = KnowledgeSemanticAnalyzeStep(db, progress)\n    knowledge_semantic_store = KnowledgeSemanticStoreStep(db, progress)\n    duplicate_check = DuplicateCheckStep(db, progress)\n\n    # Check if cancelled before starting\n    if progress and progress.is_cancelled():\n        return \"cancelled\", 0, 0\n\n    # Phase 1: Existenz - Extract\n    extract_result = extract_step.execute(file_path)\n    if not extract_result[\"success\"]:\n        if extract_result.get(\"error\") == \"cancelled\":\n            return \"cancelled\", 0, 0\n        return False, 0, 0\n\n    extraction = extract_result[\"extraction\"]\n    file_info = extract_result[\"file_info\"]\n    total_pages = extract_result.get(\"total_pages\", 0)\n    content_hash = file_info.get(\"hash\", \"\")\n\n    # Check if cancelled after extraction\n    if progress and progress.is_cancelled():\n        return \"cancelled\", 0, 0\n\n    # Phase 1: Existenz - Load document\n    doc_id = load_step.create_document(file_info)\n\n    # Phase 1: Existenz - Duplicate check\n    dup_result = duplicate_check.execute(doc_id, content_hash)\n    if dup_result[\"status\"] == \"abort\":\n        load_step.update_document_status(doc_id, \"duplicate\")\n        if progress:\n            progress.add_log(f\"Duplikat: {file_name} = Doc #{dup_result['duplicate_id']}\")\n        return True, 0, 0  # Not an error, just skip\n\n    # Phase 2: Text - Store pages\n    page_map = load_step.store_pages(doc_id, extraction)\n\n    # Phase 2: Text - Vision analysis (PDFs only)\n    if file_info[\"type\"] == \".pdf\":\n        transform_step.execute_vision(doc_id, file_path, file_info[\"type\"])\n        if progress and progress.is_cancelled():\n            return \"cancelled\", 0, 0\n\n    # Phase 3: Struktur - Chunking\n    chunks = transform_step.execute_chunking(extraction, total_pages)\n\n    # Phase 3: Struktur - Store chunks with page references\n    chunks = load_step.store_chunks(doc_id, chunks, page_map)\n\n    if progress and progress.is_cancelled():\n        return \"cancelled\", len(chunks), 0\n\n    # Phase 3: Struktur - Enrichment (PDFs only)\n    if file_info[\"type\"] == \".pdf\":\n        transform_step.execute_enrichment(doc_id, file_info[\"type\"])\n        if progress and progress.is_cancelled():\n            return \"cancelled\", len(chunks), 0\n\n    # Phase 4: Textsemantik - Analyze chunks\n    if progress:\n        progress.add_log(\"Phase 4: Textsemantik...\")\n\n    # Prepare chunks for analysis\n    chunk_data = [{\"id\": c[\"id\"], \"content\": c[\"content\"]} for c in chunks]\n    analyzed_chunks = text_semantic_analyze.execute(chunk_data, {\"model\": \"mistral\"})\n\n    # Store text semantics\n    text_semantic_store.execute(analyzed_chunks, {})\n\n    if progress and progress.is_cancelled():\n        return \"cancelled\", len(chunks), 0\n\n    # Phase 5-6: Entity + Wissenssemantik\n    if progress:\n        progress.add_log(\"Phase 5-6: Entity-Extraktion + Wissenssemantik...\")\n\n    # Run standard semantic analysis (entities, relations, taxonomy)\n    semantic_step = SemanticStep(db, progress)\n    full_text = extract_step.get_full_text_from_extraction(extraction)\n\n    try:\n        semantic_step.execute(doc_id, full_text, use_anthropic=SEMANTIC_USE_ANTHROPIC)\n    except Exception as e:\n        db.log(\"WARNING\", f\"Semantic analysis failed for {file_name}: {e}\")\n        if progress:\n            progress.add_log(f\"Semantik-Warnung: {str(e)[:50]}\")\n\n    # Load entities for knowledge semantics\n    cursor = db.execute(\n        \"\"\"SELECT e.id, e.name, e.type, c.content as context\n           FROM entities e\n           JOIN chunk_entities ce ON e.id = ce.entity_id\n           JOIN chunks c ON ce.chunk_id = c.id\n           WHERE c.document_id = %s\n           GROUP BY e.id\"\"\",\n        (doc_id,),\n    )\n    entities = cursor.fetchall()\n    cursor.close()\n\n    if entities:\n        # Convert... [TRUNCATED-61ec1ca2625e82b5]",
        "structuredPatch": [
            {
                "oldStart": 351,
                "oldLines": 13,
                "newStart": 351,
                "newLines": 24,
                "lines": [
                    " ",
                    " ",
                    " def run_full_pipeline(run_id=None, pipeline_id=None):",
                    "-    \"\"\"Run complete pipeline: scan → process → embed.\"\"\"",
                    "+    \"\"\"Run complete pipeline: scan → process → embed.",
                    "+",
                    "+    Pipeline selection:",
                    "+    - pipeline_id=5: Scientific Pipeline (semantics BEFORE embedding)",
                    "+    - pipeline_id=1-4 or None: Legacy Pipeline (semantics AFTER embedding)",
                    "+    \"\"\"",
                    "     progress = PipelineProgress(run_id) if run_id else None",
                    " ",
                    "+    # Determine which processing function to use",
                    "+    use_v5 = pipeline_id == 5",
                    "+    pipeline_name = \"Wissenschaftliche Pipeline v1\" if use_v5 else \"Standard Pipeline\"",
                    "+",
                    "     print(\"=\" * 50)",
                    "-    print(\"KI-System Pipeline - Full Run\")",
                    "+    print(f\"KI-System Pipeline - {pipeline_name}\")",
                    "     if run_id:",
                    "         print(f\"Run ID: {run_id}, Pipeline ID: {pipeline_id}\")",
                    "+    if use_v5:",
                    "+        print(\"Mode: Semantik VOR Embedding (wissenschaftlich korrekt)\")",
                    "     print(\"=\" * 50)",
                    " ",
                    "     try:"
                ]
            }
        ],
        "userModified": false,
        "replaceAll": false
    }
}
← Vorheriger Zur Liste Nächster →