spatial: pgvector semantic POI index + UI map pins
- db/Dockerfile: postgis:16-3.4 + postgresql-16-pgvector (works around the EOL bullseye release files: disable Check-Valid-Until, add pgdg repo) - 20-vector.sql: CREATE EXTENSION vector on first boot - embed/main.go (embedpoi): build poi_vec from poi — zembed embeddings of 'name — kind' (2560-d), resumable, HNSW cosine index at the end - main.go: /semantic endpoint (embeds q with the local model, cosine <=> over poi_vec, returns lat/lng/score); add lat/lng to /near /nearest /corridor so the UI can pin results; bind the vector as a text literal + ::vector cast (lib/pq has no []float32 codec) - mock/app.js: poi_semantic tool + poi_near now returns coords; agent loop drops poi_near/poi_semantic results as map pins (showSpatialPins) - mock/styles.css: .sp-dot / .sp-card pin + popup styles - README: document the pgvector stack + embedpoi
This commit is contained in:
+48
-3
@@ -401,7 +401,7 @@ const TOOLS = {
|
||||
|
||||
// POI lookup against the local PostGIS extract (native ST_DWithin / KNN /
|
||||
// corridor queries — real OSM data, not the curated demo pools).
|
||||
poi_near: async (a) => {
|
||||
poi_near: async (a) => { // lat/lng are also returned — the UI drops pins
|
||||
if (!spatialOn) return { error: 'the spatial database is offline — use search_places instead' };
|
||||
const lat = +a.lat, lng = +a.lng, r = Math.min(50000, Math.max(100, +a.radius_m || 500));
|
||||
if (!isFinite(lat) || !isFinite(lng)) return { error: 'lat/lng must be numbers (lat, lng in degrees)' };
|
||||
@@ -413,7 +413,22 @@ const TOOLS = {
|
||||
const j = await rj.json();
|
||||
if (j.error) return { error: j.error };
|
||||
return { count: (j.results || []).length, results: (j.results || []).map(x =>
|
||||
({ name: x.name, kind: x.kind, dist_m: Math.round(x.dist_m), opening_hours: x.opening_hours || null, fee: x.fee || null, website: x.website || null })) };
|
||||
({ name: x.name, kind: x.kind, lat: x.lat, lng: x.lng, dist_m: Math.round(x.dist_m), opening_hours: x.opening_hours || null, fee: x.fee || null, website: x.website || null })) };
|
||||
},
|
||||
|
||||
// semantic POI search over the pgvector index (zembed embeddings, cosine).
|
||||
// "museums of ancient Etruscan art near the city center" style queries.
|
||||
poi_semantic: async (a) => {
|
||||
if (!spatialOn) return { error: 'the spatial database is offline — use search_places instead' };
|
||||
const q = String(a.query || '').trim();
|
||||
if (!q) return { error: 'query is required' };
|
||||
let qs = 'q=' + encodeURIComponent(q) + '&limit=' + Math.min(30, Math.max(1, +a.limit || 10));
|
||||
if (a.extract) qs += '&extract=' + encodeURIComponent(a.extract);
|
||||
const rj = await fetch('/spatial/semantic?' + qs);
|
||||
const j = await rj.json();
|
||||
if (j.error) return { error: j.error };
|
||||
return { count: (j.results || []).length, results: (j.results || []).map(x =>
|
||||
({ name: x.name, kind: x.kind, lat: x.lat, lng: x.lng, score: x.score, opening_hours: x.opening_hours || null, fee: x.fee || null, website: x.website || null })) };
|
||||
},
|
||||
|
||||
route_between: async (a) => {
|
||||
@@ -641,7 +656,8 @@ function llmSystemPrompt() {
|
||||
' search_places(category, region?) — candidate places. category ∈ {' + cats.join(', ') + '} ; region ∈ {' + regions.join(', ') + '} or omit. Returns id, name, price_eur, dur_min, tags, pitch, walk_from_hotel_min.\n' +
|
||||
' place_facts(id) — full facts for one place (id or name): price, duration, tags, pitch, walk from hotel, summary.\n' +
|
||||
' web_search(query) — LIVE web search. Use for anything that can change since the data snapshot: opening hours, entry fees, seasonal events, current prices, whether a place still exists. Returns results with source URLs — always cite the URL(s) you relied on in your answer.\n' +
|
||||
' poi_near(lat, lng, radius_m?, kind?, name?) — real POIs from the local OSM/PostGIS extract (e.g. kind "amenity=restaurant"). Use when the curated pools are empty or the user asks "what is around X".\n' +
|
||||
' poi_near(lat, lng, radius_m?, kind?, name?) — real POIs within a radius of a point from the local OSM/PostGIS extract (e.g. kind "amenity=restaurant"). Use when the curated pools are empty or the user asks "what is around X". Results appear as map pins.\n' +
|
||||
' poi_semantic(query, limit?) — fuzzy/semantic POI search over the local OSM extract (embedding cosine, e.g. "medieval fortresses with panoramic views"). Better than kind filters when the OSM tag for the idea is unclear. Results appear as map pins.\n' +
|
||||
' route_between(from, to) — real walking minutes + km between two places ("hotel" or a place id/name).\n' +
|
||||
' review_plan(dayId?) — deterministic checks: day overrun vs the waking window, meals at implausible hours, duplicates. dayId optional.\n' +
|
||||
' add_stop(dayId, slot, candidateId, state?) — put a candidate on a day. slot ∈ lunch,dinner,visit. If the slot is taken the current stop is demoted to a backup (a swap). state ∈ planned,idea,backup (default planned). candidateId comes from search_places.\n' +
|
||||
@@ -699,6 +715,10 @@ async function askLLM(v) {
|
||||
let res;
|
||||
if (TOOLS[call.name]) res = await Promise.resolve(TOOLS[call.name](call.args || {}));
|
||||
else res = { error: 'unknown tool. available: ' + Object.keys(TOOLS).join(', ') };
|
||||
// surface spatial lookups as map pins, not just JSON in the transcript
|
||||
if ((call.name === 'poi_near' || call.name === 'poi_semantic') && res && Array.isArray(res.results) && res.results.length) {
|
||||
showSpatialPins(res.results, call.name);
|
||||
}
|
||||
messages.push({ role: 'user', content: '[tool result for ' + call.name + ']\n' + JSON.stringify(res) });
|
||||
continue;
|
||||
}
|
||||
@@ -1047,6 +1067,31 @@ function focusPoint(latlng, zoom = 16) {
|
||||
const ring = L.circleMarker(latlng, { radius: 16, color: '#b5533c', weight: 3, fill: false, opacity: .9 }).addTo(map);
|
||||
setTimeout(() => map.removeLayer(ring), 1500);
|
||||
}
|
||||
|
||||
// ---------------- spatial tool pins ----------------
|
||||
// poi_near / poi_semantic results land here as pins (one layer group, so a
|
||||
// new search replaces the old set). Clicking a pin pops its facts card.
|
||||
let pinLayer = null;
|
||||
function showSpatialPins(points, title) {
|
||||
if (!map) return;
|
||||
if (!pinLayer) { pinLayer = L.layerGroup().addTo(map); }
|
||||
pinLayer.clearLayers();
|
||||
const pts = (points || []).filter(p => isFinite(p.lat) && isFinite(p.lng)).slice(0, 50);
|
||||
if (!pts.length) return;
|
||||
pts.forEach(p => {
|
||||
const mk = L.marker([p.lat, p.lng], { icon: L.divIcon({ className: 'sp-pin', html: '<div class="sp-dot"></div>', iconSize: [12, 12], iconAnchor: [6, 6] }) });
|
||||
const f = [];
|
||||
if (isFinite(p.dist_m)) f.push('📍 ' + (p.dist_m >= 1000 ? (p.dist_m / 1000).toFixed(1) + ' km' : p.dist_m + ' m') + ' away');
|
||||
if (isFinite(p.score)) f.push('match ' + (100 * p.score).toFixed(0) + '%');
|
||||
if (p.opening_hours) f.push('🕑 ' + p.opening_hours);
|
||||
if (p.fee) f.push('💶 fee: ' + p.fee);
|
||||
if (p.website) f.push('🔗 ' + p.website);
|
||||
mk.bindPopup('<div class="sp-card"><b>' + esc(p.name) + '</b><span class="sp-kind">' + esc(p.kind || '') + '</span>' +
|
||||
f.map(x => '<div>' + esc(x) + '</div>').join('') + '</div>');
|
||||
pinLayer.addLayer(mk);
|
||||
});
|
||||
map.fitBounds(L.latLngBounds(pts.map(p => [p.lat, p.lng])).pad(0.3), { maxZoom: 16 });
|
||||
}
|
||||
const flyTo = s => focusPoint(s.at);
|
||||
function tempShow(s) {
|
||||
if (stopById(s.id)) return;
|
||||
|
||||
@@ -413,6 +413,13 @@ button { font: inherit; }
|
||||
.hotel-pin { font-size: 22px; transform: translate(-50%,-90%); transition: transform .15s; }
|
||||
.hotel-pin.hotel-alt { filter: grayscale(1); opacity: .75; }
|
||||
|
||||
/* spatial-tool result pins (poi_near / poi_semantic) */
|
||||
.sp-dot { width: 12px; height: 12px; border-radius: 50%; background: #1d7a68;
|
||||
border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45); transition: transform .15s; }
|
||||
.sp-pin:hover .sp-dot { transform: scale(1.35); }
|
||||
.sp-card { font-size: 12px; line-height: 1.5; min-width: 160px; max-width: 260px; }
|
||||
.sp-card .sp-kind { display: block; color: #6b7280; font-size: 11px; margin: 2px 0 4px; }
|
||||
|
||||
/* map markers: current-day numbered vs other-day dots */
|
||||
.mk { background: #b5533c; color: #fff; width: 26px; height: 26px; border-radius: 50%;
|
||||
display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px;
|
||||
|
||||
Reference in New Issue
Block a user