aee0f09db8
- Datafabrik: Dockerfile fix, agentorkestrering fungerar - Vision: Identify-modell, FAISS, OCR alla testade - API: Alla 7 integrationstester passerade - Upplösare: Entitetsupplösning verifierad
104 lines
2.9 KiB
Python
104 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Knowledge Graph Agent
|
|
Bygger och uppdaterar kunskapsgrafen kontinuerligt
|
|
"""
|
|
import sqlite3
|
|
import json
|
|
from datetime import datetime
|
|
|
|
DB_PATH = "/home/bernt/.openclaw/workspace/rivp-pilot-1/rivp.db"
|
|
|
|
def build_knowledge_graph():
|
|
conn = sqlite3.connect(DB_PATH)
|
|
c = conn.cursor()
|
|
|
|
# Hämta alla vägar och deras relationer
|
|
c.execute('''
|
|
SELECT r.id, r.name, r.county, r.type, r.length_km,
|
|
COUNT(o.id) as obs_count,
|
|
AVG(o.confidence) as avg_confidence
|
|
FROM roads r
|
|
LEFT JOIN observations o ON r.id = o.road_id
|
|
GROUP BY r.id
|
|
''')
|
|
|
|
nodes = []
|
|
for row in c.fetchall():
|
|
nodes.append({
|
|
"id": f"road-{row[0]}",
|
|
"type": "road",
|
|
"name": row[1],
|
|
"county": row[2],
|
|
"road_type": row[3],
|
|
"length_km": row[4],
|
|
"observation_count": row[5],
|
|
"avg_confidence": round(row[6] or 0, 2)
|
|
})
|
|
|
|
# Skapa relationer mellan vägar i samma län
|
|
edges = []
|
|
counties = {}
|
|
for node in nodes:
|
|
county = node["county"]
|
|
if county not in counties:
|
|
counties[county] = []
|
|
counties[county].append(node["id"])
|
|
|
|
for county, road_ids in counties.items():
|
|
for i in range(len(road_ids)):
|
|
for j in range(i+1, len(road_ids)):
|
|
edges.append({
|
|
"source": road_ids[i],
|
|
"target": road_ids[j],
|
|
"type": "same_county",
|
|
"weight": 0.5
|
|
})
|
|
|
|
# Hämta observationer som noder
|
|
c.execute('''
|
|
SELECT o.id, o.observation_type, o.severity, o.confidence,
|
|
r.id as road_id, r.name as road_name
|
|
FROM observations o
|
|
JOIN roads r ON o.road_id = r.id
|
|
LIMIT 100
|
|
''')
|
|
|
|
for row in c.fetchall():
|
|
nodes.append({
|
|
"id": f"obs-{row[0]}",
|
|
"type": "observation",
|
|
"observation_type": row[1],
|
|
"severity": row[2],
|
|
"confidence": round(row[3], 2)
|
|
})
|
|
edges.append({
|
|
"source": f"obs-{row[0]}",
|
|
"target": f"road-{row[4]}",
|
|
"type": "observed_on",
|
|
"weight": row[3]
|
|
})
|
|
|
|
conn.close()
|
|
|
|
graph = {
|
|
"nodes": nodes,
|
|
"edges": edges,
|
|
"metadata": {
|
|
"generated": datetime.now().isoformat(),
|
|
"node_count": len(nodes),
|
|
"edge_count": len(edges)
|
|
}
|
|
}
|
|
|
|
with open('/home/bernt/.openclaw/workspace/life-agents/knowledge_graph.json', 'w') as f:
|
|
json.dump(graph, f, indent=2)
|
|
|
|
return graph
|
|
|
|
if __name__ == "__main__":
|
|
print(f"[{datetime.now().isoformat()}] Knowledge Graph Agent")
|
|
graph = build_knowledge_graph()
|
|
print(f"Built graph with {graph['metadata']['node_count']} nodes and {graph['metadata']['edge_count']} edges")
|
|
print("Knowledge graph updated.")
|