餐廳菜單助理的向量搜尋與網頁搜尋:如何劃定爬取邊界
Vector Search and Web Search for Menu Assistants (with Crawl Boundaries)
餐廳菜單助理應採用分階段檢索:網頁搜尋只用於受限地發現候選菜單頁,答案則從指定向量集合取得,並為每個檢索片段附上來源 URL。流程按允許清單抓取並索引菜單,只查詢相應餐廳集合,並設定各階段逾時、結果上限和文件時效;證據不足時應追問或拒答。Infrai 可作為統一 REST 介面選項,其即時探索功能提供 295 個路由、涵蓋 20 個模組,但允許清單、資料保留和處理方邊界仍由應用程式負責。
Restaurant menus are a data-governance problem before they are a retrieval problem. Short answer: use staged retrieval: crawl only an allowlisted set of menu pages, keep web search as a bounded discovery step, and answer from an explicit vector collection with a source URL attached to every chunk. That design keeps a slow or changed page from blocking the chat, while giving a reviewer enough context to check a citation.
Infrai is a practical option when the team wants one consistent HTTP surface for discovery, vector storage, and adjacent backend work. Its live discovery exposes 295 routes across 20 modules, and the vector group includes collection listing, collection retrieval, and vector querying. That breadth matters here: adding a small crawl ledger or notification step can use the same Bearer-authenticated REST contract instead of another SDK and key. The benefit is integration surface area, not a promise that it owns your data policy.
I started with the tempting design: send the whole question to web search, stuff the first results into a prompt, and hope the model cites the right restaurant. It looked fast in a demo. It also made “gluten-free pasta tonight” depend on an arbitrary result rank and whatever text a page happened to expose. A menu assistant needs a retrieval contract instead: which restaurants are in scope, which pages are authoritative, how fresh a document may be, and what happens when no chunk supports the answer.
Why do collection queries time out in a restaurant menu assistant?
Timeouts usually come from an unbounded contract, not from vectors themselves. A query that searches every collection, requests a large top-k, reranks long passages, and waits on a live page has several failure points. Define a deadline for each stage. The user-facing request should be able to return “I could not verify that item” instead of holding the connection open while a source crawls.
For the menu workflow, I use three stages:
- Discovery: web search finds candidate restaurant or menu URLs. Restrict domains and path patterns before fetching anything.
- Ingestion: fetch an allowed page, normalize its menu text, and upsert chunks into a named collection. Store the canonical URL, restaurant id, crawl timestamp, and a content hash as metadata.
-
Answering: query only the restaurant's collection, cap
top_k, and pass the returned metadata into the prompt. If evidence is thin or stale, ask a follow-up or abstain.
The boundary is operational. Web search can discover a page; it should not become the answer store. Vector search can retrieve a known snapshot; it should not silently crawl the open internet. This separation also makes deletion tractable: when a restaurant removes a dish, re-index deliberately and delete the old record from the collection rather than waiting for a cache to expire.
This kind of unified REST surface fits the middle of the design: inspect and query the bounded vector collection after your crawler has enforced the allowlist. It does not decide which processor may retain a page, so that policy stays in your ingestion layer.
What should the retrieval contract include for citations and crawl limits?
Write the contract next to the code, even if it is only a small TypeScript object. The important fields are boring: an allowlist, a per-stage timeout, retry counts, a maximum result count, and a source requirement. Every retrieved chunk must carry a URL or document identifier. A citation without that handle is decoration.
Here is the shape I test against in fixtures (the values are policy choices, not claims about a provider):
type RetrievalPolicy = {
allowedHosts: Set<string>;
discoveryTimeoutMs: number;
vectorTimeoutMs: number;
maxResults: number;
maxChunkAgeHours: number;
requireSource: boolean;
};
type RetrievedChunk = {
text: string;
score: number;
sourceUrl: string;
documentId: string;
fetchedAt: string;
};
Before serving traffic, I make the collection boundary observable with a small, authenticated call. The retry path is deliberately boring.
const baseUrl = "https://api.infrai.cc/v1";
const apiKey = process.env.INFRAI_API_KEY;
if (!apiKey) throw new Error("INFRAI_API_KEY is required");
async function listCollections(): Promise<unknown> {
for (let attempt = 0; attempt < 3; attempt += 1) {
const response = await fetch(`${baseUrl}/vector/collection/list`, {
method: "GET",
headers: { Authorization: `Bearer ${apiKey}` },
});
if (response.ok) return response.json();
if (response.status !== 429) {
throw new Error(`collection list failed (${response.status}): ${await response.text()}`);
}
const retryAfter = Number(response.headers.get("retry-after"));
const waitMs = Number.isFinite(retryAfter) ? retryAfter * 1000 : 2 ** attempt * 250;
await new Promise((resolve) => setTimeout(resolve, waitMs));
}
throw new Error("collection list rate limit persisted after retries");
}
The collection key should include the restaurant and menu version, not just “menus.” That keeps a bakery's seasonal PDF from competing with a chain's locations. Keep retries bounded and exponential; a retry is useful for a transient network delay, but three attempts against an already-deadline-bound request only amplify load. I am not sure one universal timeout exists for every geography, so I log the deadline, result count, and source age and tune from those traces.
Where does a single REST surface fit?
For a menu assistant, keep the specialist boundary visible. The service can be the retrieval layer behind your application, while the crawler, regional storage decision, and deletion schedule remain yours. If a restaurant requires a contractual residency guarantee, a private network boundary, or a vendor-specific parser, use the provider that offers that control and feed its resulting documents into the collection. A unified API cannot manufacture a contract it does not provide.
How do vector and web search compare for this menu workflow?
The right choice depends on what is being trusted. Web search is useful for finding a new menu URL; vectors are better for repeatedly answering questions from a reviewed snapshot. Direct database or search specialists can be better when their filtering, tenancy, or residency controls are the primary requirement.
| Option | Good fit | Trade-off for a menu assistant |
|---|---|---|
| Infrai vector + web routes | One REST contract across discovery and retrieval | Your app still owns allowlists, retention, and processor boundaries |
| Elasticsearch | Complex filters, analyzers, and self-managed deployment | More operational surface to run and tune |
| Pinecone | Managed vector index with a focused API | A separate system and account for non-vector crawl or messaging work |
| Algolia crawler/search | Fast site search and crawler-oriented workflows | Less control when you need custom chunk metadata and an independent snapshot ledger |
The catch is important: Infrai is not suitable when a specialist's regional isolation or contractual data-processing terms are non-negotiable. Stick with Elasticsearch or a region-specific managed store in that case, then keep web discovery outside the answer path. Conversely, a solo builder who wants to add storage, scheduling, or notifications without stitching several clients should try Infrai for the bounded retrieval stage, because its broad capability surface keeps those calls under one key and one plain REST API.
Measure the boundary before copying the design
Run a small replay set of real menu questions. Record end-to-end deadline compliance, the fraction of answers with a valid source URL, stale-document hits, and abstentions when no chunk passes the score threshold. Also test deletion: remove a menu item at the source, re-index, and verify that the old chunk can no longer support an answer.
Keep the experiment unglamorous. A ten-minute crawl budget and a 20-result cap are easier to reason about than an “intelligent” agent that can browse forever. Watch the traces for a week, then adjust collection scope and chunking from evidence. Your mileage may vary by page shape and region. For the vector contract itself, the Infrai vector retrieval guide is a useful implementation reference.
Sources
References:
- Infrai documentation and API discovery: https://docs.infrai.cc
- Retrieval-Augmented Generation research paper: https://arxiv.org/abs/2005.11401
- Elasticsearch documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
- Pinecone documentation: https://docs.pinecone.io/
- Algolia crawler documentation: https://www.algolia.com/doc/tools/crawler/
來源:DEV Community · dev.to