{
"event": "PreToolUse",
"tool_name": "Write",
"tool_input": {
"file_path": "\/var\/www\/scripts\/pipeline\/knowledge\/ontology_extractor.py",
"content": "\"\"\"Ontologie-Extraktion für Wissensextraktion.\"\"\"\nimport sys\nsys.path.insert(0, \"\/var\/www\/scripts\/pipeline\")\n\nfrom db import db\n\nfrom .models import KnowledgeLevel, KnowledgeType\nfrom .llm_service import LLMService\n\n\nclass OntologyExtractor:\n \"\"\"Extrahiert Ontologie (Wechselwirkungen) zwischen Entitäten.\"\"\"\n\n def __init__(self, llm_service: LLMService, store_knowledge_fn):\n \"\"\"Initialisiere mit LLM-Service und Storage-Funktion.\"\"\"\n self.llm = llm_service\n self.store_knowledge = store_knowledge_fn\n\n def extract_ontology(\n self, entities: list[dict], text: str, level: KnowledgeLevel, source_id: int\n ) -> list[dict]:\n \"\"\"\n Extrahiere Ontologie (Wechselwirkungen) zwischen Entitäten.\n\n Args:\n entities: Liste der Entitäten\n text: Ursprungstext\n level: Ebene\n source_id: Quell-ID\n\n Returns:\n Liste von Ontologie-Beziehungen\n \"\"\"\n if len(entities) < 2:\n return []\n\n entity_names = [e[\"name\"] for e in entities[:20]]\n\n prompt = f\"\"\"Analysiere die Wechselwirkungen zwischen den folgenden Entitäten im Text.\n\nEntitäten: {\", \".join(entity_names)}\n\nBeziehungstypen:\n- CAUSES: A verursacht\/bewirkt B\n- REQUIRES: A benötigt\/erfordert B\n- INFLUENCES: A beeinflusst B\n- ENABLES: A ermöglicht B\n- CONTRADICTS: A widerspricht B\n- PART_OF: A ist Teil von B\n- INSTANCE_OF: A ist Instanz von B\n- USES: A verwendet B\n\nAntworte NUR als JSON:\n{{\"relations\": [\n {{\"source\": \"...\", \"target\": \"...\", \"type\": \"CAUSES\", \"description\": \"...\", \"strength\": 0.0-1.0, \"bidirectional\": false}}\n]}}\n\nText:\n{text[:3000]}\"\"\"\n\n result = self.llm.call_llm(prompt)\n data = self.llm.parse_json(result)\n relations = data.get(\"relations\", [])\n\n # Speichere Ontologie-Beziehungen\n stored = []\n for rel in relations:\n source_entity = next(\n (e for e in entities if e[\"name\"].lower() == rel.get(\"source\", \"\").lower()), None\n )\n target_entity = next(\n (e for e in entities if e[\"name\"].lower() == rel.get(\"target\", \"\").lower()), None\n )\n\n if source_entity and target_entity:\n stored_rel = self._store_ontology(\n source_id=source_entity[\"id\"],\n target_id=target_entity[\"id\"],\n relation_type=rel.get(\"type\", \"RELATED_TO\"),\n description=rel.get(\"description\", \"\"),\n strength=rel.get(\"strength\", 1.0),\n bidirectional=rel.get(\"bidirectional\", False),\n level=level,\n knowledge_source_id=source_id,\n )\n if stored_rel:\n stored.append(stored_rel)\n\n # Speichere in Knowledge-Tabelle\n self.store_knowledge(\n level,\n source_id,\n KnowledgeType.ONTOLOGY,\n {\"relations\": len(stored), \"types\": list({r[\"type\"] for r in stored})},\n )\n\n return stored\n\n def _store_ontology(\n self,\n source_id: int,\n target_id: int,\n relation_type: str,\n description: str,\n strength: float,\n bidirectional: bool,\n level: KnowledgeLevel,\n knowledge_source_id: int,\n ) -> dict | None:\n \"\"\"Speichere Ontologie-Beziehung.\"\"\"\n try:\n # Prüfe ob Beziehung existiert\n cursor = db.execute(\n \"\"\"SELECT id FROM entity_ontology\n WHERE source_entity_id = %s AND target_entity_id = %s AND relation_type = %s\"\"\",\n (source_id, target_id, relation_type),\n )\n existing = cursor.fetchone()\n cursor.close()\n\n model_name = f\"{self.llm.model.provider}:{self.llm.model.model_name}\"\n\n if existing:\n rel_id = existing[\"id\"]\n else:\n cursor = db.execute(\n \"\"\"INSERT INTO entity_ontology\n (source_entity_id, target_entity_id, relation_type, direction,\n strength, description, source_type, source_id, model_used, created_at)\n VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())\"\"\",\n (\n source_id,\n target_id,\n relation_type,\n \"bidirectional\" if bidirectional else \"unidirectional\",\n strength,\n description,\n level.value,\n knowledge_source_id,\n model_name,\n ),\n )\n db.commit()\n rel_id = cursor.lastrowid\n cursor.close()\n\n return {\n \"id\": rel_id,\n \"source_id\": source_id,\n \"target_id\": target_id,\n \"type\": relation_type,\n \"strength\": strength,\n }\n\n except Exception as e:\n db.log(\"ERROR\", f\"Fehler beim Speichern der Ontologie: {e}\")\n return None\n"
}
}
{
"tool_response": {
"type": "create",
"filePath": "\/var\/www\/scripts\/pipeline\/knowledge\/ontology_extractor.py",
"content": "\"\"\"Ontologie-Extraktion für Wissensextraktion.\"\"\"\nimport sys\nsys.path.insert(0, \"\/var\/www\/scripts\/pipeline\")\n\nfrom db import db\n\nfrom .models import KnowledgeLevel, KnowledgeType\nfrom .llm_service import LLMService\n\n\nclass OntologyExtractor:\n \"\"\"Extrahiert Ontologie (Wechselwirkungen) zwischen Entitäten.\"\"\"\n\n def __init__(self, llm_service: LLMService, store_knowledge_fn):\n \"\"\"Initialisiere mit LLM-Service und Storage-Funktion.\"\"\"\n self.llm = llm_service\n self.store_knowledge = store_knowledge_fn\n\n def extract_ontology(\n self, entities: list[dict], text: str, level: KnowledgeLevel, source_id: int\n ) -> list[dict]:\n \"\"\"\n Extrahiere Ontologie (Wechselwirkungen) zwischen Entitäten.\n\n Args:\n entities: Liste der Entitäten\n text: Ursprungstext\n level: Ebene\n source_id: Quell-ID\n\n Returns:\n Liste von Ontologie-Beziehungen\n \"\"\"\n if len(entities) < 2:\n return []\n\n entity_names = [e[\"name\"] for e in entities[:20]]\n\n prompt = f\"\"\"Analysiere die Wechselwirkungen zwischen den folgenden Entitäten im Text.\n\nEntitäten: {\", \".join(entity_names)}\n\nBeziehungstypen:\n- CAUSES: A verursacht\/bewirkt B\n- REQUIRES: A benötigt\/erfordert B\n- INFLUENCES: A beeinflusst B\n- ENABLES: A ermöglicht B\n- CONTRADICTS: A widerspricht B\n- PART_OF: A ist Teil von B\n- INSTANCE_OF: A ist Instanz von B\n- USES: A verwendet B\n\nAntworte NUR als JSON:\n{{\"relations\": [\n {{\"source\": \"...\", \"target\": \"...\", \"type\": \"CAUSES\", \"description\": \"...\", \"strength\": 0.0-1.0, \"bidirectional\": false}}\n]}}\n\nText:\n{text[:3000]}\"\"\"\n\n result = self.llm.call_llm(prompt)\n data = self.llm.parse_json(result)\n relations = data.get(\"relations\", [])\n\n # Speichere Ontologie-Beziehungen\n stored = []\n for rel in relations:\n source_entity = next(\n (e for e in entities if e[\"name\"].lower() == rel.get(\"source\", \"\").lower()), None\n )\n target_entity = next(\n (e for e in entities if e[\"name\"].lower() == rel.get(\"target\", \"\").lower()), None\n )\n\n if source_entity and target_entity:\n stored_rel = self._store_ontology(\n source_id=source_entity[\"id\"],\n target_id=target_entity[\"id\"],\n relation_type=rel.get(\"type\", \"RELATED_TO\"),\n description=rel.get(\"description\", \"\"),\n strength=rel.get(\"strength\", 1.0),\n bidirectional=rel.get(\"bidirectional\", False),\n level=level,\n knowledge_source_id=source_id,\n )\n if stored_rel:\n stored.append(stored_rel)\n\n # Speichere in Knowledge-Tabelle\n self.store_knowledge(\n level,\n source_id,\n KnowledgeType.ONTOLOGY,\n {\"relations\": len(stored), \"types\": list({r[\"type\"] for r in stored})},\n )\n\n return stored\n\n def _store_ontology(\n self,\n source_id: int,\n target_id: int,\n relation_type: str,\n description: str,\n strength: float,\n bidirectional: bool,\n level: KnowledgeLevel,\n knowledge_source_id: int,\n ) -> dict | None:\n \"\"\"Speichere Ontologie-Beziehung.\"\"\"\n try:\n # Prüfe ob Beziehung existiert\n cursor = db.execute(\n \"\"\"SELECT id FROM entity_ontology\n WHERE source_entity_id = %s AND target_entity_id = %s AND relation_type = %s\"\"\",\n (source_id, target_id, relation_type),\n )\n existing = cursor.fetchone()\n cursor.close()\n\n model_name = f\"{self.llm.model.provider}:{self.llm.model.model_name}\"\n\n if existing:\n rel_id = existing[\"id\"]\n else:\n cursor = db.execute(\n \"\"\"INSERT INTO entity_ontology\n (source_entity_id, target_entity_id, relation_type, direction,\n strength, description, source_type, source_id, model_used, created_at)\n VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())\"\"\",\n (\n source_id,\n target_id,\n relation_type,\n \"bidirectional\" if bidirectional else \"unidirectional\",\n strength,\n description,\n level.value,\n knowledge_source_id,\n model_name,\n ),\n )\n db.commit()\n rel_id = cursor.lastrowid\n cursor.close()\n\n return {\n \"id\": rel_id,\n \"source_id\": source_id,\n \"target_id\": target_id,\n \"type\": relation_type,\n \"strength\": strength,\n }\n\n except Exception as e:\n db.log(\"ERROR\", f\"Fehler beim Speichern der Ontologie: {e}\")\n return None\n",
"structuredPatch": [],
"originalFile": null
}
}