#21MockLLM keyword routingMedium

MockLLM keyword routing

Background

MockLLM is a deterministic stand-in for a real model: it scans a query for keyword sets and returns a scripted response, or a fallback when nothing matches. Each key is a space-separated set of words that all must appear (case-insensitively) in the query.

for key, response in responses.items():
    if all(word in query.lower() for word in key.split()):
        return response
return fallback

Problem statement

Implement mock_route(query, responses, fallback="[unverifiable answer]") reproducing this routing.

Input

  • query — the user's query string.
  • responses — dict mapping "word1 word2 ..." keys to response strings.
  • fallback — returned when no key fully matches.

Output

Returns the response for the first key all of whose words appear in query (case-insensitive), else fallback.

Examples

Example 1 — all keywords present

Input:  query = "What is reinforcement learning?",
        responses = {"reinforcement learning": "RL is ..."}
Output: "RL is ..."

Example 2 — a keyword missing → fallback

Input:  query = "Tell me about AlphaCode results",
        responses = {"alphacode humaneval": "..."}
Output: "[unverifiable answer]"

Explanation: "humaneval" isn't in the query.

Constraints

  • Match is case-insensitive; all words of a key must be present.
  • Return the first matching key's response (dict insertion order).
  • No match → fallback.

Notes

  • The fallback is the lesson's whole point: the bare model answers something even when it has no grounded response — plausible, not verified.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: all keywords present
  • Sample: missing keyword falls back
  • Reference: first matching key wins