{
"event": "PostToolUse",
"tool_name": "Read",
"tool_response": {
"type": "text",
"file": {
"filePath": "\/var\/www\/scripts\/pipeline\/backfill_knowledge_semantics.py",
"content": "def analyze_entity(entity: dict, model: str = \"mistral\") -> dict | None:\n \"\"\"Analyze a single entity with Ollama.\"\"\"\n try:\n context = entity.get(\"context\") or \"\"\n if len(context) > 1500:\n context = context[:1500]\n\n prompt = PROMPT_TEMPLATE.format(\n name=entity.get(\"name\", \"\"),\n entity_type=entity.get(\"type\", \"unknown\"),\n context=context,\n )\n\n response = ollama.generate(\n model=model,\n prompt=prompt,\n options={\"num_predict\": 300},\n )\n\n response_text = response[\"response\"].strip()\n\n # Extract JSON - handle nested objects\n start = response_text.find(\"{\")\n if start < 0:\n return None\n\n # Find matching closing brace\n depth = 0\n end = start\n for i, char in enumerate(response_text[start:], start):\n if char == \"{\":\n depth += 1\n elif char == \"}\":\n depth -= 1\n if depth == 0:\n end = i + 1\n break\n\n if end > start:\n json_str = response_text[start:end]\n data = json.loads(json_str)\n data = validate_and_fix(data)\n data[\"model_used\"] = model\n return data\n\n except Exception as e:\n db.log(\"WARNING\", f\"Backfill: Knowledge semantic analysis failed for entity {entity['id']}: {e}\")\n\n return None\n",
"numLines": 50,
"startLine": 161,
"totalLines": 323
}
}
}