Module 6 · Tools and agents
Lesson 6.3
Lookup tools and search tools
A lookup tool returns one exact record, and a search tool returns the best matches with scores.
The problem
Why it matters
Without it
Every question goes through one search. A question with an exact id can return a similar record instead of the right one.
With it
Exact questions use a lookup that returns one record or a clear not-found. Open questions use a search with ranked matches.
How it works
A lookup takes one exact key
A lookup takes one key, such as order id A-1042, and returns one record. The Map stores each id with its status. A missing key returns undefined, which the code turns into a clear message.
Key Map returns A-1042 shipped A-9999 undefined, no record Two lookups in the orders map. const orders = new Map([["A-1042", "shipped"]]);console.log(orders.get("A-1042"));console.log(orders.get("A-9999"));Output
shipped undefined
A search counts shared words
A search splits the query into words and counts how many appear in each article. The query refund order number shares three words with the refund request article. Scores set the order of the results.
Scores for the query "refund order number". const query = "refund order number".split(" ");const article = "Refund requests need the order number.";const text = article.toLowerCase();const hits = query.filter((word) => text.includes(word));const label = "Shared: " + hits.join(", ");console.log(label + " (" + hits.length + ")");Output
Shared: refund, order, number (3)
Results come back without a cutoff
The search returns the best few results even when every score is zero. A rideshare query shares no words with any article, yet one result still comes back, with score 0. The model must judge whether that result answers the question.
Query Top result Score refund order number Refund requests need the order number. 3 rideshare Refunds take 5 to 7 days. 0 Two searches, each with its top result. Choose by the question
Use a lookup when the question names one record. Use a search when the question is about a topic or a symptom. A search for an exact id can return a similar record instead.
Use a lookup
- The question names one id
- Returns one record or not found
Use a search
- The question names a topic or symptom
- Returns ranked matches with scores
Which tool fits which question.
Common mistakes
What goes wrong
- Searching for an exact id. A search can return a similar record instead of the one asked for.
- Expecting the search to have a score cutoff. Without one, the model must judge weak results.
- Treating shared words as meaning. Use word counting only as a first step.
Use it when and skip it when
Is it the right tool?
Use it when
- The question names one record, such as an order id.
- The question is open, such as a topic or a symptom.
Skip it when
- The records are few, and a plain list is easy to read.
Words to know
Terms in this lesson
- Lookup
- A tool that returns one record for one exact key.
- Search
- A tool that ranks records by how well they match a query.
- Score
- A number that says how well one record matches the query.
- Key
- The exact value that a lookup uses, such as an order id.