spatial: replace pgvector semantic search with targeted tag+name search
The embedding approach was overkill: it embedded only 'name — kind' (short strings), spent a 4B model + ~2.5GB VRAM + 4.6GB of vectors + a one-time 450k-row job to do what indexed SQL does directly. New default — no embedding model, no extra VRAM: - The LLM agent is the semantic layer: it maps the user's concept to OSM kinds + local-language name keywords. It is already in VRAM for chat, so this costs nothing. - /kinds: returns the tag vocabulary that actually exists (GROUP BY kind with counts) so the agent grounds its choices in real data. - /search: indexed retrieval — kind IN/ILIKE (poi_kind), name FTS (to_tsvector) + trigram (pg_trgm) for fuzzy/substring, optional ST_DWithin radius. Ranked by trigram similarity then distance. - schema.sql: real trigram GIN index (poi_name_trgm_ops); renamed the misnamed FTS index to poi_name_fts. - Agent tools: poi_semantic -> poi_kinds + poi_search (both pin results on the map). pgvector demoted to an opt-in path (embed/ + /semantic) — still works if poi_vec is built, but no longer the default. Dropped the half-built poi_vec and stopped the background embed run.
This commit is contained in:
+35
-9
@@ -418,17 +418,42 @@ const TOOLS = {
|
||||
|
||||
// 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) => {
|
||||
// browse the OSM tag vocabulary that actually exists in this extract —
|
||||
// grounds the agent's concept→tag choices in real data instead of guesses.
|
||||
poi_kinds: 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));
|
||||
let qs = 'limit=' + Math.min(50, Math.max(1, +a.limit || 30));
|
||||
if (a.q) qs += '&q=' + encodeURIComponent(a.q);
|
||||
if (a.extract) qs += '&extract=' + encodeURIComponent(a.extract);
|
||||
const rj = await fetch('/spatial/semantic?' + qs);
|
||||
const rj = await fetch('/spatial/kinds?' + qs);
|
||||
const j = await rj.json();
|
||||
if (j.error) return { error: j.error };
|
||||
return { count: (j.kinds || []).length, kinds: j.kinds || [] };
|
||||
},
|
||||
|
||||
// Targeted POI search: the agent maps the user's concept to OSM kinds +
|
||||
// name keywords (local-language variants); Postgres retrieves with indexed
|
||||
// kind/FTS/trigram filters — no embedding model. kinds: "historic=fort",
|
||||
// a family "tourism", or a bare value; terms: ["wine","vino","cava"].
|
||||
poi_search: async (a) => {
|
||||
if (!spatialOn) return { error: 'the spatial database is offline — use search_places instead' };
|
||||
const kinds = (Array.isArray(a.kinds) ? a.kinds : [a.kinds]).filter(Boolean);
|
||||
const terms = (Array.isArray(a.terms) ? a.terms : [a.terms]).filter(Boolean);
|
||||
if (!kinds.length && !terms.length) return { error: 'pass kinds and/or terms' };
|
||||
let qs = '';
|
||||
if (kinds.length) qs += 'kinds=' + encodeURIComponent(kinds.join('|')) + '&';
|
||||
if (terms.length) qs += 'terms=' + encodeURIComponent(terms.join('|')) + '&';
|
||||
if (a.lat != null && a.lng != null) {
|
||||
qs += 'lat=' + a.lat + '&lng=' + a.lng;
|
||||
if (a.r) qs += '&r=' + Math.min(50000, Math.max(50, +a.r));
|
||||
}
|
||||
qs += 'limit=' + Math.min(30, Math.max(1, +a.limit || 10));
|
||||
if (a.extract) qs += '&extract=' + encodeURIComponent(a.extract);
|
||||
const rj = await fetch('/spatial/search?' + 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 })) };
|
||||
({ name: x.name, kind: x.kind, lat: x.lat, lng: x.lng, score: x.score, dist_m: Math.round(x.dist_m), opening_hours: x.opening_hours || null, fee: x.fee || null, website: x.website || null })) };
|
||||
},
|
||||
|
||||
route_between: async (a) => {
|
||||
@@ -657,7 +682,8 @@ function llmSystemPrompt() {
|
||||
' 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 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' +
|
||||
' poi_kinds(q?, limit?) — list the OSM POI tags that actually exist in the local extract, with counts (q filters, e.g. q="wine"). Call this FIRST when you are not sure which tag fits the user\'s idea.\n' +
|
||||
' poi_search(kinds?, terms?, lat?, lng?, r?, limit?) — targeted POI search over the local OSM extract. kinds = OSM tags (e.g. "historic=fort", or a family like "tourism"); terms = name keywords (give local-language variants, e.g. ["wine","vino","cava"]). Translate the user\'s concept into tags + keywords — YOU are the semantic layer; Postgres does the exact retrieval. Optional lat/lng/r restricts to a radius (default 10 km). 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' +
|
||||
@@ -716,7 +742,7 @@ async function askLLM(v) {
|
||||
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) {
|
||||
if ((call.name === 'poi_near' || call.name === 'poi_search') && 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) });
|
||||
@@ -1069,7 +1095,7 @@ function focusPoint(latlng, zoom = 16) {
|
||||
}
|
||||
|
||||
// ---------------- spatial tool pins ----------------
|
||||
// poi_near / poi_semantic results land here as pins (one layer group, so a
|
||||
// poi_near / poi_search 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) {
|
||||
|
||||
+1
-1
@@ -413,7 +413,7 @@ 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) */
|
||||
/* spatial-tool result pins (poi_near / poi_search) */
|
||||
.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); }
|
||||
|
||||
Reference in New Issue
Block a user